MiReturnResident

UINT64 __fastcall MiReturnResident(INT64 a1, UINT64 a2){
  UINT64 result; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v4; 
  bool v5; 
  result = (UINT64)&MiSystemPartition;
  if( (ULONG_PTR *)a1 == &MiSystemPartition )
  {
    CurrentPrcb = KeGetCurrentPrcb();
    v4 = *((int *)CurrentPrcb + 8391);
    if( (_DWORD)v4 != -1 )
    {
      for( ; a2 + v4 <= 0x100; result = a2 + (int)result )
      {
        if( a2 >= 0x80000 )
          break;
        result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v4 + a2, v4);
        v5 = (_DWORD)v4 == (_DWORD)result;
        v4 = (int)result;
        if( v5 )
          return result;
        if( (_DWORD)result == -1 )
          break;
      }
      if( (int)v4 > 192 && (_DWORD)v4 != -1 )
      {
        result = (unsigned int)_InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 192, v4);
        if( (_DWORD)v4 == (_DWORD)result )
        {
          result = (unsigned int)(v4 - 192);
          a2 += (int)result;
        }
      }
    }
  }
  if( a2 )
    _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 7168), a2);
  return result;
}

Referenced by:

MiChargeLargeProtoSubsection
MiCreatePagingFileMap
MiDereferenceSessionFinal
MiFreeInitializationCode
MiFreePartitionPhysicalPages
MiInsertVadCharges
MiMarkBootGuardPage
MiReleaseVadEventBlocks
MiRemoveVadCharges
MiReturnSystemImageCommitment
MiUnloadSystemImage
MmCleanProcessAddressSpace
MmCreateProcessAddressSpace
MmDeleteProcessAddressSpace
MmReturnChargesToLockPagedPool