MiChargePartitionResidentAvailable

INT64 __fastcall MiChargePartitionResidentAvailable(INT64 a1, UINT64 a2, INT64 a3){
  signed __int64 v5; 
  unsigned __int64 v6; 
  __int64 v7; 
  signed __int64 v8; 
  _BOOL8 v9; 
  struct _KPRCB *CurrentPrcb; 
  unsigned int v11; 
  unsigned __int64 v12; 
  struct _KPRCB *v13; 
  __int64 v14; 
  bool v15; 
  signed __int32 v16; 
  _KPRCB **v18; 
  __int64 v19; 
  unsigned __int32 v20; 
  unsigned __int64 v21; 
  if( a2 + a3 >= a2 || a3 == 0xFFFFFFFFi64 )
  {
    v5 = *(_QWORD *)(a1 + 7168);
    v6 = 0i64;
    if( v5 < 1024 && (ULONG_PTR *)a1 == &MiSystemPartition )
    {
      if( (_DWORD)KeNumberProcessors_0 )
      {
        v18 = &KiProcessorBlock;
        v19 = (unsigned int)KeNumberProcessors_0;
        do
        {
          if( *((_DWORD *)*v18 + 8391) != -1 )
          {
            v20 = _InterlockedExchange((volatile __int32 *)*v18 + 8391, -1);
            if( v20 != -1 )
              v6 += v20;
          }
          ++v18;
          --v19;
        }
        while( v19 );
      }
      if( v6 >= a2 )
      {
        v21 = v6 - a2;
        if( v21 )
          _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 7168), v21);
        return 3i64;
      }
      a2 -= v6;
    }
    if( v5 <= 0 )
    {
LABEL_46:
      if( a3 != 0xFFFFFFFFi64 )
      {
LABEL_47:
        ++dword_140C4E534;
        if( v6 )
          _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 7168), v6);
        return 0i64;
      }
      _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 7168), -(__int64)a2);
      return 3i64;
    }
    while( 1 )
    {
      if( a2 + a3 > v5 && a3 != 0xFFFFFFFFi64 )
        goto LABEL_47;
      v7 = _InterlockedCompareExchange64((volatile signed __int64 *)(a1 + 7168), v5 - a2, v5);
      if( v5 == v7 )
        break;
      v5 = v7;
      if( v7 <= 0 )
        goto LABEL_46;
    }
    v8 = v5 - a2;
    v9 = v8 < 1024;
    if( (ULONG_PTR *)a1 == &MiSystemPartition )
    {
      CurrentPrcb = KeGetCurrentPrcb();
      v11 = *((_DWORD *)CurrentPrcb + 8391);
      if( v11 == -1 )
      {
        if( v8 >= 2048 )
        {
          _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 0, -1);
          v11 = 0;
          goto LABEL_9;
        }
      }
      else if( v11 < 0x40 )
      {
LABEL_9:
        if( v8 > 1024 )
        {
          v12 = 128 - v11;
          if( v8 == _InterlockedCompareExchange64((volatile signed __int64 *)(a1 + 7168), v8 - v12, v8) )
          {
            v13 = KeGetCurrentPrcb();
            v14 = *((int *)v13 + 8391);
            if( (_DWORD)v14 != -1 )
            {
              if( v12 + v14 <= 0x100 )
              {
                do
                {
                  if( v12 >= 0x80000 )
                    break;
                  v16 = _InterlockedCompareExchange((volatile signed __int32 *)v13 + 8391, v12 + v14, v14);
                  v15 = (_DWORD)v14 == v16;
                  LODWORD(v14) = v16;
                  if( v15 )
                    return v9 + 2;
                }
                while( v16 != -1 && v12 + v16 <= 0x100 );
              }
              if( (int)v14 > 192
                && (_DWORD)v14 != -1
                && (_DWORD)v14 == _InterlockedCompareExchange((volatile signed __int32 *)v13 + 8391, 192, v14) )
              {
                v12 += (int)v14 - 192;
              }
            }
            if( v12 )
              _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 7168), v12);
          }
        }
      }
    }
    return v9 + 2;
  }
  ++dword_140C4E530;
  return 0i64;
}

Referenced by:

MiChargeForLockedPage
MiChargeResident
MiFlushSectionInternal
MiLockPageTablePage
MiMakePageAvoidRead
MiObtainFaultCharges
MiProbeLockFrame
MiResolveProtoPteFault
MmCheckCachedPageStates