RtlFindClearBitsAndSetEx

UINT64 __stdcall RtlFindClearBitsAndSetEx(_RTL_BITMAP_EX *BitMapHeader, UINT64 NumberToFind, UINT64 HintIndex){
  unsigned __int64 SizeOfBitMap; 
  unsigned __int64 *Buffer; 
  UINT64 v8; 
  unsigned __int64 v9; 
  UINT64 v10; 
  unsigned __int64 *v11; 
  unsigned __int64 *v12; 
  __int64 v13; 
  unsigned __int64 v14; 
  unsigned __int64 v15; 
  unsigned __int64 v16; 
  __int64 v18; 
  unsigned __int64 *v19; 
  bool v20; 
  __int64 v21; 
  unsigned int v22; 
  unsigned __int64 v23; 
  UINT64 v24; 
  __int64 v25; 
  unsigned __int64 *v26; 
  __int64 v27; 
  unsigned int v28; 
  unsigned int v29; 
  __int64 v30; 
  __int64 v31; 
  unsigned __int64 *v32; 
  unsigned __int64 v33; 
  __int64 v34; 
  unsigned int v35; 
  UINT64 v36; 

  SizeOfBitMap = BitMapHeader->SizeOfBitMap;
  Buffer = BitMapHeader->Buffer;
  v8 = HintIndex & -(__int64)(HintIndex < BitMapHeader->SizeOfBitMap);
  v9 = BitMapHeader->SizeOfBitMap - 1;
  if( NumberToFind )
  {
    while( 1 )
    {
      if( v9 - v8 + 1 < NumberToFind )
      {
LABEL_26:
        v15 = -1i64;
        goto LABEL_27;
      }
      v10 = v9 - NumberToFind + 1;
      v11 = &Buffer[v10 >> 6];
      v12 = &Buffer[v8 >> 6];
      v13 = *v12 | ((1i64 << (v8 & 0x3F)) - 1);
      if( NumberToFind > 0x7F )
      {
        v26 = v11 + 1;
        if( (v10 & 0x3F) == 0 )
          v26 = &Buffer[v10 >> 6];
        if( v13 )
        {
          if( *++v12 )
            goto LABEL_49;
          v20 = !_BitScanReverse64((unsigned __int64 *)&v27, v13);
          if( v20 )
            v28 = 64;
          else
            v28 = 63 - v27;
        }
        else
        {
          v28 = 0;
        }
        while( 1 )
        {
          v15 = ((v12 - Buffer) << 6) - v28;
          if( v15 > v10 )
            goto LABEL_26;
          v32 = &v12[(NumberToFind - v28) >> 6];
          while( ++v12 != v32 )
          {
            if( *v12 )
              goto LABEL_49;
          }
          v29 = ((_BYTE)NumberToFind - (_BYTE)v28) & 0x3F;
          if( (((_BYTE)NumberToFind - (_BYTE)v28) & 0x3F) == 0 )
            goto LABEL_11;
          v20 = !_BitScanForward64((unsigned __int64 *)&v30, *v12);
          if( v20 )
            LODWORD(v30) = 64;
          if( (unsigned int)v30 >= v29 )
            goto LABEL_11;
          do
          {
LABEL_49:
            if( v12 > v26 )
              goto LABEL_26;
            ++v12;
          }
          while( *v12 );
          v20 = !_BitScanReverse64((unsigned __int64 *)&v31, *(v12 - 1));
          if( v20 )
            v28 = 64;
          else
            v28 = 63 - v31;
        }
      }
      if( NumberToFind >= 0x40 )
        break;
      if( NumberToFind > 1 )
      {
        v18 = 0i64;
        v19 = &Buffer[v9 >> 6];
        while( v13 != -1 )
        {
LABEL_18:
          v20 = !_BitScanForward64((unsigned __int64 *)&v21, v13);
          if( v20 )
            LODWORD(v21) = 64;
          if( (unsigned int)(v18 + v21) >= NumberToFind )
          {
            v23 = -v18;
LABEL_25:
            v15 = ((v12 - Buffer) << 6) + v23;
            goto LABEL_10;
          }
          v22 = NumberToFind;
          v23 = ~v13;
          while( 1 )
          {
            v23 &= v23 >> (v22 >> 1);
            if( !v23 )
              break;
            v22 -= v22 >> 1;
            if( v22 <= 1 )
            {
              _BitScanForward64(&v23, v23);
              goto LABEL_25;
            }
          }
          if( v12 == v19 )
            goto LABEL_26;
          v20 = !_BitScanReverse64((unsigned __int64 *)&v25, v13);
          if( v20 )
            v18 = 64i64;
          else
            v18 = (unsigned int)(63 - v25);
          v13 = *++v12;
        }
        while( 1 )
        {
          if( ++v12 > v11 )
            goto LABEL_26;
          v13 = *v12;
          if( *v12 != -1i64 )
          {
            v18 = 0i64;
            goto LABEL_18;
          }
        }
      }
      while( v13 == -1 )
      {
        if( ++v12 > v11 )
          goto LABEL_26;
        v13 = *v12;
      }
      _BitScanForward64(&v14, ~v13);
      v15 = v14 + ((v12 - Buffer) << 6);
LABEL_10:
      if( v15 > v10 )
        goto LABEL_26;
LABEL_11:
      if( v15 != -1i64 )
      {
LABEL_12:
        v16 = v15;
        goto LABEL_13;
      }
LABEL_27:
      if( !v8 )
        goto LABEL_12;
      v24 = NumberToFind + HintIndex;
      if( NumberToFind + HintIndex > SizeOfBitMap )
        v24 = SizeOfBitMap;
      v9 = v24 - 1;
      v8 = 0i64;
    }
    while( 1 )
    {
      while( 1 )
      {
        while( v13 < 0 )
        {
          if( ++v12 > v11 )
            goto LABEL_26;
          v13 = *v12;
        }
        v20 = !_BitScanReverse64((unsigned __int64 *)&v34, v13);
        if( v20 )
          v35 = 64;
        else
          v35 = 63 - v34;
        v15 = ((v12 - Buffer + 1) << 6) - v35;
        if( v15 > v10 )
          goto LABEL_26;
        v36 = NumberToFind - v35;
        if( NumberToFind == v35 )
          goto LABEL_11;
        v13 = *++v12;
        if( v36 >= 0x40 )
          break;
LABEL_58:
        v20 = !_BitScanForward64(&v33, v13);
        if( v20 )
          v33 = 64i64;
        if( v33 >= v36 )
          goto LABEL_11;
      }
      if( !*v12 )
      {
        v36 -= 64i64;
        if( !v36 )
          goto LABEL_11;
        v13 = *++v12;
        goto LABEL_58;
      }
    }
  }
  v16 = v8 & 0xFFFFFFFFFFFFFFF8ui64;
LABEL_13:
  if( v16 != -1i64 )
    RtlSetBitsEx(BitMapHeader, v16, NumberToFind);
  return v16;
}

Referenced by:

ExpSaPageGroupAllocateMemory
MiAllocateFromSubAllocatedRegion
MiObtainRelocationBits
MiObtainSessionVa
RtlpHpFixedVsAllocate