RtlFindSetBitsAndClearEx

UINT64 __fastcall RtlFindSetBitsAndClearEx(UINT64 *a1, UINT64 a2, UINT64 a3){
  UINT64 v3; 
  UINT64 v5; 
  UINT64 v8; 
  UINT64 v9; 
  UINT64 v10; 
  UINT64 v11; 
  _QWORD *v12; 
  __int64 v13; 
  unsigned __int64 v14; 
  unsigned __int64 v15; 
  unsigned __int64 v16; 
  __int64 v18; 
  _QWORD *v19; 
  bool v20; 
  __int64 v21; 
  unsigned int v22; 
  unsigned __int64 v23; 
  __int64 v24; 
  __int64 v25; 
  unsigned int v26; 
  UINT64 v27; 
  unsigned __int64 v28; 
  UINT64 v29; 
  __int64 v30; 
  unsigned int v31; 
  __int64 v32; 
  _QWORD *v33; 
  unsigned int v34; 
  __int64 v35; 
  UINT64 v36; 
  int v37; 
  int v38; 
  v3 = *a1;
  v5 = a1[1];
  v8 = a3 & -(__int64)(a3 < *a1);
  v9 = *a1 - 1;
  if( a2 )
  {
    while( 1 )
    {
      if( v9 - v8 + 1 < a2 )
      {
LABEL_16:
        v15 = -1i64;
        goto LABEL_75;
      }
      v10 = v9 - a2 + 1;
      v11 = v5 + 8 * (v10 >> 6);
      v12 = (_QWORD *)(v5 + 8 * (v8 >> 6));
      v13 = ~*v12 | ((1i64 << (v8 & 0x3F)) - 1);
      if( a2 > 0x7F )
      {
        v29 = v11 + 8;
        if( (v10 & 0x3F) == 0 )
          v29 = v5 + 8 * (v10 >> 6);
        if( v13 )
        {
          v30 = ~*++v12;
          if( *v12 != -1i64 )
            goto LABEL_56;
          v37 &= v30;
          v20 = !_BitScanReverse64((unsigned __int64 *)&v30, v13);
          if( v20 )
            v31 = 64;
          else
            v31 = 63 - v30;
        }
        else
        {
          v31 = 0;
        }
        while( 1 )
        {
          v15 = ((__int64)((__int64)v12 - v5) >> 3 << 6) - v31;
          if( v15 > v10 )
            goto LABEL_16;
          v33 = &v12[(a2 - v31) >> 6];
          while( ++v12 != v33 )
          {
            if( *v12 != -1i64 )
              goto LABEL_56;
          }
          v34 = ((_BYTE)a2 - (_BYTE)v31) & 0x3F;
          if( (((_BYTE)a2 - (_BYTE)v31) & 0x3F) == 0 )
            goto LABEL_11;
          v20 = !_BitScanForward64((unsigned __int64 *)&v35, ~*v12);
          if( v20 )
            LODWORD(v35) = 64;
          if( (unsigned int)v35 >= v34 )
            goto LABEL_11;
          do
          {
LABEL_56:
            if( (unsigned __int64)v12 > v29 )
              goto LABEL_16;
            ++v12;
          }
          while( *v12 != -1i64 );
          v38 &= ~*(_DWORD *)v12;
          v20 = !_BitScanReverse64((unsigned __int64 *)&v32, ~*(v12 - 1));
          if( v20 )
            v31 = 64;
          else
            v31 = 63 - v32;
        }
      }
      if( a2 >= 0x40 )
        break;
      if( a2 > 1 )
      {
        v18 = 0i64;
        v19 = (_QWORD *)(v5 + 8 * (v9 >> 6));
        while( v13 != -1 )
        {
LABEL_19:
          v20 = !_BitScanForward64((unsigned __int64 *)&v21, v13);
          if( v20 )
            LODWORD(v21) = 64;
          if( (unsigned int)(v18 + v21) >= a2 )
          {
            v23 = -v18;
LABEL_26:
            v15 = ((__int64)((__int64)v12 - v5) >> 3 << 6) + v23;
            goto LABEL_10;
          }
          v22 = a2;
          v23 = ~v13;
          while( 1 )
          {
            v23 &= v23 >> (v22 >> 1);
            if( !v23 )
              break;
            v22 -= v22 >> 1;
            if( v22 <= 1 )
            {
              _BitScanForward64(&v23, v23);
              goto LABEL_26;
            }
          }
          if( v12 == v19 )
            goto LABEL_16;
          v20 = !_BitScanReverse64((unsigned __int64 *)&v24, v13);
          if( v20 )
            v18 = 64i64;
          else
            v18 = (unsigned int)(63 - v24);
          v13 = ~*++v12;
        }
        while( 1 )
        {
          if( (unsigned __int64)++v12 > v11 )
            goto LABEL_16;
          v13 = ~*v12;
          if( *v12 )
          {
            v18 = 0i64;
            goto LABEL_19;
          }
        }
      }
      while( v13 == -1 )
      {
        if( (unsigned __int64)++v12 > v11 )
          goto LABEL_16;
        v13 = ~*v12;
      }
      _BitScanForward64(&v14, ~v13);
      v15 = v14 + ((__int64)((__int64)v12 - v5) >> 3 << 6);
LABEL_10:
      if( v15 > v10 )
        goto LABEL_16;
LABEL_11:
      if( v15 != -1i64 )
      {
LABEL_12:
        v16 = v15;
        goto LABEL_13;
      }
LABEL_75:
      if( !v8 )
        goto LABEL_12;
      v36 = a2 + a3;
      if( a2 + a3 > v3 )
        v36 = v3;
      v9 = v36 - 1;
      v8 = 0i64;
    }
    while( 1 )
    {
      while( 1 )
      {
        while( v13 < 0 )
        {
          if( (unsigned __int64)++v12 > v11 )
            goto LABEL_16;
          v13 = ~*v12;
        }
        v20 = !_BitScanReverse64((unsigned __int64 *)&v25, v13);
        if( v20 )
          v26 = 64;
        else
          v26 = 63 - v25;
        v15 = ((((__int64)((__int64)v12 - v5) >> 3) + 1) << 6) - v26;
        if( v15 > v10 )
          goto LABEL_16;
        v27 = a2 - v26;
        if( a2 == v26 )
          goto LABEL_11;
        v13 = ~*++v12;
        if( v27 >= 0x40 )
          break;
LABEL_45:
        v20 = !_BitScanForward64(&v28, v13);
        if( v20 )
          v28 = 64i64;
        if( v28 >= v27 )
          goto LABEL_11;
      }
      if( *v12 == -1i64 )
      {
        v27 -= 64i64;
        if( !v27 )
          goto LABEL_11;
        v13 = ~*++v12;
        goto LABEL_45;
      }
    }
  }
  v16 = v8 & 0xFFFFFFFFFFFFFFF8ui64;
LABEL_13:
  if( v16 != -1i64 )
    RtlClearBitsEx((INT64)a1, v16, a2);
  return v16;
}

Referenced by:

MiGetUltraMapping