PnpQueueQueryAndRemoveEvent

__int64 __fastcall PnpQueueQueryAndRemoveEvent(unsigned __int16 *a1, char *a2, _WORD *a3, _WORD *a4, char a5, int a6){
  int v7; 
  volatile __int32 *v9; 
  VOID **v10; 
  unsigned __int16 *v11; 
  _QWORD *v12; 
  __int64 v13; 
  __int64 v14; 
  VOID **PoolWithTag; 
  int v16; 
  int v17; 
  int v18; 
  int v19; 
  char *v20; 
  unsigned int v21; 
  int v23; 
  _WORD *v24; 
  char v25; 
  int v26; 
  int v27; 
  PVOID P; 
  PVOID v29; 
  __int128 Object; 
  __int64 v31; 

  v7 = a5 & 8;
  v31 = 0i64;
  v26 = 0;
  Object = 0i64;
  v9 = 0i64;
  P = 0i64;
  v10 = 0i64;
  v25 = 0;
  v11 = 0i64;
  *(_DWORD *)a2 = 0;
  if( (a5 & 8) != 0 )
    PnpDiagnosticTraceObject(&KMPnPEvt_DeviceEject_Start, a1);
  v12 = PnpDeviceObjectFromDeviceInstanceWithTag((__int64)a1, 0x43706E50u);
  v29 = v12;
  if( !v12 )
  {
    v20 = a2;
    v19 = -1073741810;
    goto LABEL_28;
  }
  v13 = *(_QWORD *)(v12[39] + 40i64);
  if( !v13 )
  {
    v19 = -1073741810;
LABEL_36:
    v20 = a2;
    goto LABEL_25;
  }
  if( v13 == *(&PnpReplaceEvent + 831) )
  {
    v19 = -1073741790;
    goto LABEL_36;
  }
  v14 = *(unsigned int *)a4;
  if( (_DWORD)v14 )
  {
    PoolWithTag = ExAllocatePoolWithTag(1ui64, v14 + 20, 538996816i64);
    v10 = PoolWithTag;
    if( !PoolWithTag )
    {
      v19 = -1073741670;
LABEL_39:
      v20 = a2;
      goto LABEL_25;
    }
    v11 = (unsigned __int16 *)PoolWithTag + 2;
    v25 = 1;
    *((_WORD *)PoolWithTag + 2) = 0;
    *(VOID **)((char *)PoolWithTag + 12) = (char *)PoolWithTag + 20;
    v16 = 0;
    *((_WORD *)PoolWithTag + 3) = *a4;
  }
  else
  {
    v16 = 1;
  }
  if( v7 && a6 )
    v16 = 1;
  v27 = (a5 & 0xB) != 0;
  if( (a5 & 2) != 0 )
  {
    v17 = 22;
    goto LABEL_13;
  }
  if( !v7 )
  {
    v17 = 21;
LABEL_13:
    v18 = 0;
    goto LABEL_14;
  }
  v17 = 47;
  v18 = 1;
LABEL_14:
  KeInitializeEvent((INT64)&Object, 0, 0);
  v19 = PnpSetTargetDeviceRemove(
          v29,
          0,
          v27,
          v18,
          v16,
          v17,
          0,
          (__int64)&Object,
          0i64,
          0i64,
          (__int64)&v26,
          (__int64)v10,
          (__int64)v11,
          (__int64)&P);
  if( v19 < 0 )
    goto LABEL_49;
  v19 = KeWaitForSingleObject((UINT64)&Object, 0, 0, 1, 0i64);
  if( v19 != 257 )
    goto LABEL_16;
  v9 = (volatile __int32 *)P;
  if( (unsigned int)PnpRemoveEventFromQueue(P) )
  {
    v19 = -1073741536;
    goto LABEL_39;
  }
  if( _InterlockedExchange(v9 + 17, 1) )
  {
    v19 = KeWaitForSingleObject((UINT64)&Object, 0, 0, 0, 0i64);
LABEL_16:
    v20 = a2;
    if( v19 >= 0 )
      v19 = v26;
    if( v10 )
      *(_DWORD *)a2 = *(_DWORD *)v10;
    if( v11 )
    {
      v21 = *v11;
      if( (_WORD)v21 )
      {
        if( v21 >= *(_DWORD *)a4 )
        {
          LOWORD(v21) = *a4 - 2;
          *v11 = v21;
        }
        memmove(a3, *((const VOID **)v11 + 1), (unsigned __int16)v21);
        a3[(unsigned __int64)*v11 >> 1] = 0;
        LOWORD(v21) = *v11;
      }
      *(_DWORD *)a4 = (unsigned __int16)v21;
    }
    if( *(_DWORD *)a2 == 6 )
    {
      if( a3 )
      {
        v23 = 0;
        v24 = a3;
        if( *a3 )
        {
          while( *v24 != 92 || ++v23 != 3 )
          {
            if( !*++v24 )
              goto LABEL_60;
          }
          *v24 = 0;
LABEL_60:
          if( v23 == 3 )
            *(_DWORD *)a4 = (unsigned __int16)((_WORD)v24 - (_WORD)a3);
        }
      }
    }
    goto LABEL_24;
  }
  v19 = -1073741536;
  v25 = 0;
  *((_BYTE *)P + 72) = 1;
  _InterlockedExchange(v9 + 17, 0);
LABEL_49:
  v20 = a2;
LABEL_24:
  v9 = (volatile __int32 *)P;
LABEL_25:
  ObfDereferenceObjectWithTag(v29, 0x43706E50ui64);
  if( v9 && _InterlockedExchangeAdd(v9 + 16, 0xFFFFFFFF) == 1 )
    ExFreePoolWithTag(P, 0x4B706E50u);
LABEL_28:
  if( v7 )
    PnpDiagnosticTraceDeviceOperation(&KMPnPEvt_DeviceEject_Stop, a1, v19, (__int64)v11, *v20);
  if( v25 )
    ExFreePoolWithTag(v10, 0);
  return(unsigned int)v19;
}

Referenced by:

PiCMQueryRemove
PiControlQueryAndRemoveDevice
PnpRequestDeviceEjectExWorker