PnpQueueQueryAndRemoveEvent

NTSTATUS __stdcall PnpQueueQueryAndRemoveEvent(
        UNICODE_STRING *DeviceInstance,
        PNP_VETO_TYPE *VetoType,
        PWCHAR VetoName,
        UINT64 *VetoNameLength,
        UINT64 Flags,
        UINT64 KernelModeCaller){
  int v7; 
  volatile __int32 *v9; 
  PNP_VETO_TYPE *v10; 
  unsigned __int16 *v11; 
  _QWORD *v12; 
  PVOID v13; 
  __int64 v14; 
  char *PoolWithTag; 
  int v16; 
  int v17; 
  int v18; 
  NTSTATUS v19; 
  PNP_VETO_TYPE *v20; 
  unsigned int v21; 
  int v23; 
  PWCHAR v24; 
  PLARGE_INTEGER Timeout; 
  char v26; 
  NTSTATUS Status; 
  int Status_4; 
  PVOID P; 
  PVOID Object; 
  struct _KEVENT Event; 
  v7 = Flags & 8;
  Status = 0;
  memset(&Event, 0, sizeof(Event));
  v9 = 0i64;
  P = 0i64;
  v10 = 0i64;
  v26 = 0;
  v11 = 0i64;
  *VetoType = PNP_VetoTypeUnknown;
  if( (Flags & 8) != 0 )
    PnpDiagnosticTraceObject(&KMPnPEvt_DeviceEject_Start, DeviceInstance);
  v12 = PnpDeviceObjectFromDeviceInstanceWithTag((INT64)DeviceInstance, 0x43706E50ui64);
  Object = v12;
  if( !v12 )
  {
    v20 = VetoType;
    v19 = -1073741810;
    goto LABEL_28;
  }
  v13 = *(PVOID *)(v12[39] + 40i64);
  if( !v13 )
  {
    v19 = -1073741810;
LABEL_36:
    v20 = VetoType;
    goto LABEL_25;
  }
  if( v13 == IopRootDeviceNode )
  {
    v19 = -1073741790;
    goto LABEL_36;
  }
  v14 = *(unsigned int *)VetoNameLength;
  if( (_DWORD)v14 )
  {
    PoolWithTag = (char *)ExAllocatePoolWithTag(PagedPool, v14 + 20, 0x20207050ui64);
    v10 = (PNP_VETO_TYPE *)PoolWithTag;
    if( !PoolWithTag )
    {
      v19 = -1073741670;
LABEL_39:
      v20 = VetoType;
      goto LABEL_25;
    }
    v11 = (unsigned __int16 *)(PoolWithTag + 4);
    v26 = 1;
    *((_WORD *)PoolWithTag + 2) = 0;
    *(_QWORD *)(PoolWithTag + 12) = PoolWithTag + 20;
    v16 = 0;
    *((_WORD *)PoolWithTag + 3) = *(_WORD *)VetoNameLength;
  }
  else
  {
    v16 = 1;
  }
  if( v7 && (_DWORD)KernelModeCaller )
    v16 = 1;
  Status_4 = (Flags & 0xB) != 0;
  if( (Flags & 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(&Event, NotificationEvent, 0);
  v19 = PnpSetTargetDeviceRemove(
          Object,
          0,
          Status_4,
          v18,
          v16,
          v17,
          0,
          (__int64)&Event,
          0i64,
          0i64,
          (__int64)&Status,
          (__int64)v10,
          (__int64)v11,
          (__int64)&P);
  if( v19 < 0 )
    goto LABEL_49;
  v19 = KeWaitForSingleObject(&Event, Executive, 0, 1u, 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(&Event, Executive, 0, 0, 0i64);
LABEL_16:
    v20 = VetoType;
    if( v19 >= 0 )
      v19 = Status;
    if( v10 )
      *VetoType = *v10;
    if( v11 )
    {
      v21 = *v11;
      if( (_WORD)v21 )
      {
        if( v21 >= *(_DWORD *)VetoNameLength )
        {
          LOWORD(v21) = *(_WORD *)VetoNameLength - 2;
          *v11 = v21;
        }
        memmove((UINT8 *)VetoName, *((UINT8 **)v11 + 1), (unsigned __int16)v21);
        VetoName[(unsigned __int64)*v11 >> 1] = 0;
        LOWORD(v21) = *v11;
      }
      *(_DWORD *)VetoNameLength = (unsigned __int16)v21;
    }
    if( *VetoType == PNP_VetoDevice )
    {
      if( VetoName )
      {
        v23 = 0;
        v24 = VetoName;
        if( *VetoName )
        {
          while( *v24 != 92 || ++v23 != 3 )
          {
            if( !*++v24 )
              goto LABEL_60;
          }
          *v24 = 0;
LABEL_60:
          if( v23 == 3 )
            *(_DWORD *)VetoNameLength = (unsigned __int16)((_WORD)v24 - (_WORD)VetoName);
        }
      }
    }
    goto LABEL_24;
  }
  v19 = -1073741536;
  v26 = 0;
  *((_BYTE *)P + 72) = 1;
  _InterlockedExchange(v9 + 17, 0);
LABEL_49:
  v20 = VetoType;
LABEL_24:
  v9 = (volatile __int32 *)P;
LABEL_25:
  ObfDereferenceObjectWithTag(Object, 0x43706E50ui64);
  if( v9 && _InterlockedExchangeAdd(v9 + 16, 0xFFFFFFFF) == 1 )
    ExFreePoolWithTag(P, 0x4B706E50u);
LABEL_28:
  if( v7 )
  {
    LODWORD(Timeout) = *v20 | ((_DWORD)KernelModeCaller != 0 ? 0x80000000 : 0);
    PnpDiagnosticTraceDeviceOperation(
      (EVENT_DESCRIPTOR *)&KMPnPEvt_DeviceEject_Stop,
      DeviceInstance,
      (unsigned int)v19,
      (UNICODE_STRING *)v11,
      (UINT64)Timeout);
  }
  if( v26 )
    ExFreePoolWithTag(v10, 0);
  return v19;
}

Referenced by:

PiCMQueryRemove
PiControlQueryAndRemoveDevice
PnpRequestDeviceEjectExWorker