PnpRebalance

__int64 __fastcall PnpRebalance(__int64 a1, VOID *a2, unsigned int a3, char a4){
  __int64 v5; 
  VOID *v6; 
  unsigned __int16 *v9; 
  VOID **PoolWithTag; 
  VOID **v11; 
  int BestConfiguration; 
  int v13; 
  __int128 *v14; 
  __int64 v15; 
  __int64 v16; 
  unsigned int v17; 
  unsigned __int64 v18; 
  _OWORD *v19; 
  unsigned int v22; 
  __int64 i; 
  int v24; 
  size_t Size[2]; 
  VOID *v26; 
  _LIST_ENTRY ArbiterList; 
  __int128 v28[7]; 

  v5 = a3;
  v6 = *(VOID **)(a1 + 712);
  v26 = a2;
  ArbiterList = 0i64;
  if( v6 )
    memset(v6, 0i64, 0x58u);
  memset(v28, 0i64, sizeof(v28));
  v9 = (unsigned __int16 *)(a1 + 40);
  *((_QWORD *)&v28[2] + 1) = *(&PnpReplaceEvent + 831);
  *((_QWORD *)&v28[1] + 1) = *(&PnpReplaceEvent + 831);
  *((_QWORD *)&v28[3] + 1) = *(_QWORD *)&KUSER_SHARED_DATA.InterruptTime.LowPart;
  LOBYTE(v28[3]) = a4;
  PnpDiagnosticTraceObject(&KMPnPEvt_Rebalance_Start, (unsigned __int16 *)(a1 + 40));
  PoolWithTag = ExAllocatePoolWithTag(1ui64, (unsigned __int64)*(&PnpReplaceEvent + 1500) << 6, 812674640i64);
  v11 = PoolWithTag;
  if( !PoolWithTag )
  {
    LODWORD(v28[1]) = 2;
    BestConfiguration = -1073741670;
    goto LABEL_17;
  }
  if( (_DWORD)v5 )
    memmove(PoolWithTag, a2, (_DWORD)v5 << 6);
  v13 = 0;
  v14 = &v28[1];
  v22 = 0;
  BestConfiguration = 0;
  v15 = 0i64;
  *(_QWORD *)Size = v5 << 6;
  v16 = (__int64)&v11[8 * v5];
  for( i = v16; ; v16 = i )
  {
    ++DWORD1(v28[0]);
    v24 = v13;
    PnpFindRebalanceCandidates(v15, a1, v16, &v22, a4, (__int64)v28);
    v13 = v22;
    if( !(_DWORD)v5 && !a4 )
    {
      v17 = 0;
      if( !v22 )
      {
LABEL_13:
        BestConfiguration = -1073741823;
        if( *(_DWORD *)(a1 + 300) == 777 )
          *((_DWORD *)&v28[1] + v15) = 4;
        else
          *((_DWORD *)&v28[1] + v15) = 3;
LABEL_15:
        PnpCancelStopDeviceSubtree(*(&PnpReplaceEvent + 831));
        v18 = i;
        goto LABEL_16;
      }
      while( v11[8 * (unsigned __int64)v17] != *(VOID **)(a1 + 32) )
      {
        if( ++v17 >= v22 )
          goto LABEL_13;
      }
    }
    if( v24 != v22 )
      break;
    if( !*(_DWORD *)v14 )
      *(_DWORD *)v14 = 5;
    if( BestConfiguration != -1073739512 )
      BestConfiguration = -1073741823;
LABEL_28:
    v15 = (unsigned int)(v15 + 1);
    v14 = (__int128 *)((char *)v14 + 4);
    if( (unsigned int)v15 >= 2 )
      goto LABEL_15;
  }
  BestConfiguration = PnpFindBestConfiguration((INT64)v11, (unsigned int)v5 + v22, &ArbiterList);
  if( BestConfiguration < 0 )
  {
    if( !*(_DWORD *)v14 )
      *(_DWORD *)v14 = 6;
    goto LABEL_28;
  }
  PnpStopDeviceSubtree(*(&PnpReplaceEvent + 831));
  IopCommitConfiguration(&ArbiterList);
  v18 = i;
  if( (_DWORD)v5 )
    PnpBuildCmResourceLists((unsigned __int64)v11, i, 0);
  PnpBuildCmResourceLists(v18, (unsigned __int64)&v11[8 * (unsigned __int64)(unsigned int)(v5 + v13)], 1);
  if( (_DWORD)v5 )
    memmove(v26, v11, Size[0]);
LABEL_16:
  PnpFreeResourceRequirementsForAssignTable(v18, (unsigned __int64)&v11[8 * (unsigned __int64)(unsigned int)(v5 + v13)]);
  ExFreePoolWithTag(v11, 0x30706E50u);
  v9 = (unsigned __int16 *)(a1 + 40);
LABEL_17:
  PnpTraceRebalanceResult();
  v19 = *(_OWORD **)(a1 + 712);
  if( BestConfiguration >= 0 )
  {
    if( v19 )
    {
      ExFreePoolWithTag(v19, 0x62655250u);
      *(_QWORD *)(a1 + 712) = 0i64;
    }
  }
  else if( v19 )
  {
    *v19 = v28[0];
    v19[1] = v28[1];
    v19[2] = v28[2];
    v19[3] = v28[3];
    v19[4] = v28[4];
    *((_QWORD *)v19 + 10) = *(_QWORD *)&v28[5];
  }
  PnpDiagnosticTraceObjectWithStatus(&KMPnPEvt_Rebalance_Stop, v9, BestConfiguration);
  return(unsigned int)BestConfiguration;
}

Referenced by:

PnpAllocateResources
PnpProcessRebalance
PnpReallocateResources