RtlFindClearBitsAndSet

VOID *__stdcall RtlFindClearBitsAndSet(
        _RTL_AVL_TABLE *Table,
        VOID *Buffer,
        UINT64 BufferSize,
        UINT8 *NewElement,
        VOID *NodeOrParent,
        _TABLE_SEARCH_RESULT SearchResult){
  unsigned int Parent; 
  int v7; 
  unsigned int v8; 
  _RTL_BITMAP *v9; 
  int v10; 
  int v11; 
  _RTL_BALANCED_LINKS *LeftChild; 
  int v13; 
  unsigned int v14; 
  unsigned int v15; 
  char *v16; 
  unsigned int v17; 
  __int64 v18; 
  unsigned __int64 v19; 
  unsigned __int64 *v20; 
  __int64 v21; 
  unsigned __int64 v22; 
  unsigned int v23; 
  bool v24; 
  unsigned int v25; 
  unsigned int v27; 
  bool v28; 
  __int64 v29; 
  int v30; 
  unsigned int v31; 
  __int64 v32; 
  unsigned __int64 v33; 
  int v34; 
  char v35; 
  unsigned __int64 *v36; 
  __int64 v37; 
  unsigned int v38; 
  __int64 v39; 
  __int64 v40; 
  int v41; 
  unsigned __int64 *v42; 
  __int64 v43; 
  unsigned int v44; 
  unsigned __int64 v45; 
  __int64 v46; 
  _RTL_BALANCED_LINKS *v47; 
  int v49; 

  v49 = BufferSize;
  Parent = (unsigned int)Table->BalancedRoot.Parent;
  v7 = BufferSize;
  v8 = (unsigned int)Buffer;
  v9 = (_RTL_BITMAP *)Table;
  v10 = (unsigned int)BufferSize < LODWORD(Table->BalancedRoot.Parent) ? BufferSize : 0;
  v11 = LODWORD(Table->BalancedRoot.Parent) - 1;
  if( !(_DWORD)Buffer )
  {
    v25 = v10 & 0xFFFFFFF8;
    goto LABEL_15;
  }
  LeftChild = Table->BalancedRoot.LeftChild;
  v47 = LeftChild;
  v13 = ((unsigned __int8)LeftChild & 4) != 0i64 ? 0x20 : 0;
  while( 1 )
  {
    v14 = v13 + v11;
    v15 = v13 + v10;
    v16 = (char *)LeftChild - (((unsigned __int8)v47 & 4) != 0i64 ? 4 : 0);
    if( v11 - v10 + 1 >= v8 )
    {
      v17 = v14 - v8 + 1;
      v18 = (1i64 << (v15 & 0x3F)) - 1;
      v19 = (unsigned __int64)&v16[8 * ((unsigned __int64)v17 >> 6)];
      v20 = (unsigned __int64 *)&v16[8 * ((unsigned __int64)v15 >> 6)];
      v21 = *v20 | v18;
      if( v8 > 0x7F )
      {
        v33 = v19 + 8;
        if( (v17 & 0x3F) == 0 )
          v33 = (unsigned __int64)&v16[8 * ((unsigned __int64)(v14 - v8 + 1) >> 6)];
        if( !v21 )
        {
          v34 = 0;
          goto LABEL_43;
        }
        if( *++v20 )
          goto LABEL_47;
        v28 = !_BitScanReverse64((unsigned __int64 *)&v40, v21);
        if( !v28 )
        {
          v34 = 63 - v40;
          goto LABEL_43;
        }
LABEL_81:
        v34 = 64;
LABEL_43:
        while( 1 )
        {
          v23 = ((unsigned int)(((char *)v20 - v16) >> 3) << 6) - v34;
          if( v23 > v17 )
            goto LABEL_18;
          v35 = v8 - v34;
          v36 = &v20[(unsigned __int64)(v8 - v34) >> 6];
          while( ++v20 != v36 )
          {
            if( *v20 )
              goto LABEL_47;
          }
          v38 = v35 & 0x3F;
          if( !v38 )
            goto LABEL_12;
          v28 = !_BitScanForward64((unsigned __int64 *)&v39, *v20);
          if( v28 )
            LODWORD(v39) = 64;
          if( (unsigned int)v39 >= v38 )
            goto LABEL_12;
          do
          {
LABEL_47:
            if( (unsigned __int64)v20 > v33 )
              goto LABEL_18;
            ++v20;
          }
          while( *v20 );
          v28 = !_BitScanReverse64((unsigned __int64 *)&v37, *(v20 - 1));
          if( v28 )
            goto LABEL_81;
          v34 = 63 - v37;
        }
      }
      if( v8 < 0x40 )
      {
        if( v8 > 1 )
        {
          v41 = 0;
          v42 = (unsigned __int64 *)&v16[8 * ((unsigned __int64)v14 >> 6)];
          while( v21 != -1 )
          {
LABEL_64:
            v28 = !_BitScanForward64((unsigned __int64 *)&v43, v21);
            if( v28 )
              LODWORD(v43) = 64;
            if( v41 + (int)v43 >= v8 )
            {
              LODWORD(v45) = -v41;
LABEL_71:
              v23 = ((unsigned int)(((char *)v20 - v16) >> 3) << 6) + v45;
              v24 = v23 <= v17;
              goto LABEL_11;
            }
            v44 = v8;
            v45 = ~v21;
            while( 1 )
            {
              v45 &= v45 >> (v44 >> 1);
              if( !v45 )
                break;
              v44 -= v44 >> 1;
              if( v44 <= 1 )
              {
                _BitScanForward64(&v45, v45);
                goto LABEL_71;
              }
            }
            if( v20 == v42 )
              goto LABEL_18;
            v28 = !_BitScanReverse64((unsigned __int64 *)&v46, v21);
            if( v28 )
              v41 = 64;
            else
              v41 = 63 - v46;
            v21 = *++v20;
          }
          while( (unsigned __int64)++v20 <= v19 )
          {
            v21 = *v20;
            if( *v20 != -1i64 )
            {
              v41 = 0;
              goto LABEL_64;
            }
          }
        }
        else
        {
          while( v21 == -1 )
          {
            if( (unsigned __int64)++v20 > v19 )
              goto LABEL_18;
            v21 = *v20;
          }
          _BitScanForward64(&v22, ~v21);
          v23 = v22 + ((unsigned int)(((char *)v20 - v16) >> 3) << 6);
          v24 = v23 <= v17;
LABEL_11:
          if( v24 )
          {
LABEL_12:
            if( v23 != -1 )
              break;
            goto LABEL_19;
          }
        }
LABEL_18:
        v23 = -1;
LABEL_19:
        v7 = v49;
        goto LABEL_20;
      }
      while( 1 )
      {
        while( 1 )
        {
          while( v21 < 0 )
          {
            if( (unsigned __int64)++v20 > v19 )
              goto LABEL_18;
            v21 = *v20;
          }
          v28 = !_BitScanReverse64((unsigned __int64 *)&v29, v21);
          if( v28 )
            v30 = 64;
          else
            v30 = 63 - v29;
          v23 = (((unsigned int)(((char *)v20 - v16) >> 3) + 1) << 6) - v30;
          if( v23 > v17 )
            goto LABEL_18;
          v31 = v8 - v30;
          if( v8 == v30 )
            goto LABEL_12;
          v21 = *++v20;
          if( v31 >= 0x40 )
            break;
LABEL_35:
          v28 = !_BitScanForward64((unsigned __int64 *)&v32, v21);
          if( v28 )
            LODWORD(v32) = 64;
          if( (unsigned int)v32 >= v31 )
            goto LABEL_12;
        }
        if( !*v20 )
        {
          v31 -= 64;
          if( !v31 )
            goto LABEL_12;
          v21 = *++v20;
          goto LABEL_35;
        }
      }
    }
    v23 = -1;
LABEL_20:
    if( !v10 )
      break;
    LeftChild = v47;
    v27 = v8 + v7;
    if( v8 + v7 > Parent )
      v27 = Parent;
    v11 = v27 - 1;
    v10 = 0;
  }
  v9 = (_RTL_BITMAP *)Table;
  v25 = v23 - v13;
  if( v23 == -1 )
    v25 = -1;
LABEL_15:
  if( v25 != -1 )
    RtlSetBits(v9, v25, v8);
  return(VOID *)v25;
}

Referenced by:

HalpDmaAllocateContiguousPagesFromContiguousPoolV2
HalpDmaAllocateContiguousPagesFromContiguousPoolV3
HalpDmaAllocateScatterPagesFromContiguousPoolAtHighLevelV2
HalpDmaAllocateScatterPagesFromContiguousPoolAtHighLevelV3
HalpDmaAllocateScatterPagesFromContiguousPoolV2
HalpDmaAllocateScatterPagesFromContiguousPoolV3
HalpIommuAcquireNewDomain
HalpIrtAllocateIndex
MiAllocatePartitionId
MiAssignSessionRanges
MiAssignSoftwareWsleRegion
MiAssignSystemVa
MiGetUltraHugeAlreadyActive
MiReserveDriverPtes
MiReserveFaultPte
MiSessionCreateInternal
RtlApplyHotPatch
RtlHotPatchSynchronizationRequired
SepAddLuidToIndexEntry
SepGetLowBoxNumberEntry
TtmiCreateTerminal