RtlInterlockedSetClearRunEx

INT64 __fastcall RtlInterlockedSetClearRunEx(INT64 a1, UINT64 a2, UINT64 a3){
  UINT64 v3; 
  volatile signed __int32 *v4; 
  UINT64 v5; 
  signed __int32 v6; 
  int v7; 
  signed __int32 v8; 
  signed __int32 v10; 
  int v11; 
  signed __int32 v12; 
  signed __int32 v13; 
  int v14; 
  signed __int32 v15; 
  unsigned __int64 v16; 
  volatile signed __int32 *v17; 
  unsigned __int64 v18; 
  unsigned __int64 v19; 
  v3 = a2 & 0x1F;
  v4 = (volatile signed __int32 *)((*(_QWORD *)(a1 + 8) + (a2 >> 3)) & 0xFFFFFFFFFFFFFFFCui64);
  v5 = a3;
  if( v3 + a3 <= 0x20 )
  {
    v6 = *v4;
    if( a3 == 32 )
      v7 = -1;
    else
      v7 = ((1 << a3) - 1) << v3;
    if( (v6 & v7) == 0 )
    {
      while( 1 )
      {
        v8 = v6;
        v6 = _InterlockedCompareExchange(v4, v7 | v6, v6);
        if( v8 == v6 )
          break;
        if( (v6 & v7) != 0 )
          return 0i64;
      }
      return 1i64;
    }
    return 0i64;
  }
  if( (a2 & 0x1F) != 0 )
  {
    v10 = *v4;
    v11 = ((1 << (32 - (a2 & 0x1F))) - 1) << v3;
    if( (*v4 & v11) != 0 )
      return 0i64;
    while( 1 )
    {
      v12 = v10;
      v10 = _InterlockedCompareExchange(v4, v11 | v10, v10);
      if( v12 == v10 )
        break;
      if( (v10 & v11) != 0 )
        return 0i64;
    }
    a3 -= 32 - (unsigned int)(a2 & 0x1F);
    ++v4;
  }
  if( a3 >= 0x20 )
  {
    while( !_InterlockedCompareExchange(v4, -1, 0) )
    {
      ++v4;
      a3 -= 32i64;
      if( a3 < 0x20 )
        goto LABEL_12;
    }
    if( v5 == a3 )
      return 0i64;
    v16 = v5 - a3;
    v17 = (volatile signed __int32 *)(*(_QWORD *)(a1 + 8) + 4 * (a2 >> 5));
    if( v3 + v16 <= 0x20 )
    {
      if( v16 != 32 )
        goto LABEL_28;
      goto LABEL_29;
    }
    if( (a2 & 0x1F) != 0 )
    {
      _InterlockedAnd(v17, ~(((1 << (32 - (a2 & 0x1F))) - 1) << v3));
      v16 -= 32 - (unsigned int)(a2 & 0x1F);
      ++v17;
    }
    if( v16 >= 0x20 )
    {
      v18 = v16 >> 5;
      v16 += -32i64 * (v16 >> 5);
      do
      {
        *v17++ = 0;
        --v18;
      }
      while( v18 );
    }
    if( !v16 )
      return 0i64;
LABEL_48:
    _InterlockedAnd(v17, -1 << v16);
    return 0i64;
  }
LABEL_12:
  if( !a3 )
    return 1i64;
  v13 = *v4;
  v14 = (1 << a3) - 1;
  if( (*v4 & v14) == 0 )
  {
    do
    {
      v15 = v13;
      v13 = _InterlockedCompareExchange(v4, v14 | v13, v13);
      if( v15 == v13 )
        return 1i64;
    }
    while( (v13 & v14) == 0 );
  }
  v16 = v5 - a3;
  v17 = (volatile signed __int32 *)(*(_QWORD *)(a1 + 8) + 4 * (a2 >> 5));
  if( v3 + v16 > 0x20 )
  {
    if( (a2 & 0x1F) != 0 )
    {
      _InterlockedAnd(v17, ~(((1 << (32 - (a2 & 0x1F))) - 1) << v3));
      v16 -= 32 - (unsigned int)(a2 & 0x1F);
      ++v17;
    }
    if( v16 >= 0x20 )
    {
      v19 = v16 >> 5;
      v16 += -32i64 * (v16 >> 5);
      do
      {
        *v17++ = 0;
        --v19;
      }
      while( v19 );
    }
    if( !v16 )
      return 0i64;
    goto LABEL_48;
  }
  if( v16 == 32 )
  {
LABEL_29:
    *v17 = 0;
    return 0i64;
  }
LABEL_28:
  _InterlockedAnd(v17, ~(((1 << v16) - 1) << v3));
  return 0i64;
}

Referenced by:

MiAllocateSlabEntry
MiAttemptCoalesce
MiReservePtes