PspReturnQuota

UINT64 __fastcall PspReturnQuota(CHAR *a1, INT64 a2, UINT64 a3, UINT64 a4){
  unsigned int v4; 
  __int64 *v5; 
  __int64 v6; 
  ULONG_PTR v9; 
  char v10; 
  volatile signed __int64 *v11; 
  unsigned __int64 v12; 
  unsigned __int64 v13; 
  ULONG_PTR BugCheckParameter4; 
  unsigned __int64 v15; 
  signed __int64 v16; 
  bool v17; 
  UINT64 result; 
  unsigned __int64 *v19; 
  unsigned __int64 v20; 
  INT64 v21; 
  v4 = a3;
  v5 = (__int64 *)a1;
  v6 = (__int64)(int)a3 << 7;
  v9 = (int)a3;
  v10 = PspResourceFlags[8 * (int)a3];
  v11 = (volatile signed __int64 *)&a1[v6];
  _m_prefetchw(&a1[v6]);
  v12 = *(_QWORD *)&a1[v6];
  v13 = *(_QWORD *)&a1[v6 + 64];
  if( *(_QWORD *)&a1[v6 + 80] )
  {
    if( v13 > v12 )
    {
      v19 = (unsigned __int64 *)&qword_140C1DF68[7 * (int)a3];
      v20 = *v19;
      if( v13 - v12 > *v19 )
      {
        if( v20 > a4 )
          v20 = a4;
        if( v13 == _InterlockedCompareExchange64(v11 + 8, v13 - v20, v13)
          && _InterlockedExchangeAdd64(v11 + 9, v20) + v20 > *v19 )
        {
          v21 = _InterlockedExchange64(v11 + 9, 0i64);
          if( v21 )
            PspReturnResourceQuota(v4, (INT64)&a1[v6], v21, 0i64);
        }
      }
    }
  }
  BugCheckParameter4 = a4;
  while( 1 )
  {
    do
    {
      if( BugCheckParameter4 >= v12 )
      {
        v15 = v12;
        v16 = 0i64;
      }
      else
      {
        v15 = BugCheckParameter4;
        v16 = v12 - BugCheckParameter4;
      }
      result = _InterlockedCompareExchange64(v11, v16, v12);
      v17 = v12 == result;
      v12 = result;
    }
    while( !v17 );
    BugCheckParameter4 -= v15;
    if( !BugCheckParameter4 )
      break;
    if( v5 == PspSystemQuotaBlock )
      KeBugCheckEx(0x21u, a2, v9, a4, BugCheckParameter4);
    v5 = PspSystemQuotaBlock;
    v11 = (__int64 *)((char *)PspSystemQuotaBlock + v6);
    _m_prefetchw((char *)PspSystemQuotaBlock + v6);
    v12 = *(__int64 *)((char *)PspSystemQuotaBlock + v6);
  }
  if( a2 )
  {
    if( (v10 & 4) != 0 )
      _InterlockedExchangeAdd64((volatile signed __int64 *)(a2 + 8 * v9 + 1136), -(__int64)a4);
  }
  return result;
}

Referenced by:

ExFreeHeapPool
ObpFreeObject
ObpIncrementHandleCountEx
PsChargeSharedPoolQuota
PsReturnPoolQuota
PsReturnProcessNonPagedPoolQuota
PsReturnProcessPageFileQuota
PsReturnProcessPagedPoolQuota
PsReturnProcessQuota
PsReturnSharedPoolQuota