IopIncrementDeviceObjectRefCount

VOID __fastcall IopIncrementDeviceObjectRefCount(_DEVICE_OBJECT *DeviceObject, UINT8 LockRequired){
  unsigned __int8 CurrentIrql; 
  volatile __int64 **v4; 
  int v5; 
  _KSPIN_LOCK_QUEUE *v6; 
  _KSPIN_LOCK_QUEUE *Next; 
  _DRIVER_OBJECT *DriverObject; 

  if( LockRequired )
  {
    CurrentIrql = KeGetCurrentIrql();
    __writecr8(2ui64);
    v4 = (volatile __int64 **)((char *)KeGetPcr()->NtTib.ArbitraryUserPointer + 160);
    if( _InterlockedExchange64(v4[1], (__int64)v4) )
      KxWaitForLockOwnerShip((INT64)v4);
    v5 = ++DeviceObject->ReferenceCount;
    v6 = (_KSPIN_LOCK_QUEUE *)((char *)KeGetPcr()->NtTib.ArbitraryUserPointer + 160);
    _m_prefetchw(v6);
    Next = v6->Next;
    if( !v6->Next )
    {
      if( v6 == (_KSPIN_LOCK_QUEUE *)_InterlockedCompareExchange64(
                                        (volatile signed __int64 *)v6->Lock,
                                        0i64,
                                        (signed __int64)v6) )
      {
LABEL_6:
        __writecr8(CurrentIrql);
        goto LABEL_7;
      }
      Next = KxWaitForLockChainValid(v6);
    }
    v6->Next = 0i64;
    _InterlockedXor64((volatile signed __int64 *)&Next->Lock, 1ui64);
    goto LABEL_6;
  }
  v5 = ++DeviceObject->ReferenceCount;
LABEL_7:
  if( v5 <= 0 )
  {
    DriverObject = DeviceObject->DriverObject;
    if( DriverObject )
    {
      IoAddTriageDumpDataBlock(DriverObject, (_BYTE *)0x150);
      IoAddTriageDumpDataBlock(
        DeviceObject->DriverObject->DriverName.Buffer,
        (_BYTE *)DeviceObject->DriverObject->DriverName.Length);
    }
    KeBugCheckEx(0x18u, 0i64, DeviceObject, (PVOID)6, (PVOID)DeviceObject->ReferenceCount);
  }
}

Referenced by:

IoCreateStreamFileObjectEx2
IoRegisterFileSystem
IopCompleteUnloadOrDelete
IopDeleteFile
IopMountVolume
IopShutdownBaseFileSystems
PnpMarkDeviceForRemove