RtlFindClearBitsEx

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

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

Referenced by:

MiFindEmptyAddressRange
MiSelectRelocationStartHint