MiObtainMdlCharges

INT64 __fastcall MiObtainMdlCharges(INT64 a1){
  UINT64 v1; 
  int v2; 
  unsigned __int64 v3; 
  int v5; 
  __int64 v6; 
  _MI_PARTITION *v8; 
  unsigned __int64 v9; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v11; 
  bool v12; 
  signed __int32 v13; 
  __int64 v14; 
  unsigned __int64 v15; 
  unsigned __int64 v16; 
  UINT64 v17; 
  unsigned __int64 v18; 
  UINT64 v19; 
  int v20; 
  __int64 v21; 
  __int64 v22; 
  v1 = *(_QWORD *)(a1 + 40);
  v2 = *(_DWORD *)(a1 + 8);
  v3 = v1 >> 4;
  v5 = 0;
  if( !(v1 >> 4) )
    v3 = 1i64;
  if( (v2 & 0x40) != 0 )
  {
    v3 = *(_QWORD *)(a1 + 32);
LABEL_13:
    v5 = 1;
    goto LABEL_5;
  }
  if( (v2 & 0x20) != 0 && *(_QWORD *)(a1 + 32) )
  {
    v3 = *(_QWORD *)(a1 + 32);
    goto LABEL_13;
  }
  while( 1 )
  {
LABEL_5:
    if( (MiAcquireNonPagedResources(*(_MI_PARTITION **)a1, v1) & 0x80000000) != 0i64 )
      goto LABEL_27;
    LODWORD(v6) = MiAllocatePool((struct _SLIST_ENTRY *)0x40);
    if( v6 )
      break;
    v8 = *(_MI_PARTITION **)a1;
    MiReturnCommit(*(_MI_PARTITION **)a1, v1);
    v9 = v1;
    if( v8 == (_MI_PARTITION *)&MiSystemPartition )
    {
      CurrentPrcb = KeGetCurrentPrcb();
      v11 = *((int *)CurrentPrcb + 8391);
      if( (_DWORD)v11 != -1 )
      {
        if( v1 + v11 <= 0x100 )
        {
          do
          {
            if( v1 >= 0x80000 )
              break;
            v13 = _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v1 + v11, v11);
            v12 = (_DWORD)v11 == v13;
            LODWORD(v11) = v13;
            if( v12 )
              goto LABEL_27;
          }
          while( v13 != -1 && v1 + v13 <= 0x100 );
        }
        if( (int)v11 > 192
          && (_DWORD)v11 != -1
          && (_DWORD)v11 == _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 192, v11) )
        {
          v9 = v1 + (int)v11 - 192;
        }
      }
    }
    if( v9 )
      _InterlockedExchangeAdd64((volatile signed __int64 *)v8 + 896, v9);
LABEL_27:
    if( (*(_DWORD *)(a1 + 8) & 4) != 0 )
      return 3221225626i64;
    v14 = *(_QWORD *)(*(_QWORD *)a1 + 7168i64);
    if( v14 <= 0 )
      return 3221225626i64;
    v15 = *(_QWORD *)(a1 + 48);
    if( v14 <= v15 )
      return 3221225626i64;
    v16 = *(_QWORD *)(*(_QWORD *)a1 + 7464i64);
    v17 = v14 - v15;
    v18 = *(_QWORD *)(*(_QWORD *)a1 + 7592i64);
    if( v16 < v18 )
    {
      v19 = v18 - v16;
      if( v19 < v17 )
        v17 = v19;
    }
    if( v1 <= v17 )
    {
      if( v1 <= v3 )
      {
        if( v5 == 1 || v3 == 1 )
          return 3221225626i64;
        while( v1 < v3 )
          v3 >>= 1;
        if( !v3 )
          v3 = 1i64;
      }
      v1 -= v3;
    }
    else
    {
      v1 = v17;
    }
    v20 = *(_DWORD *)(a1 + 8);
    if( (v20 & 0x40) != 0 )
    {
      v21 = *(_QWORD *)(a1 + 32) - 1i64;
    }
    else
    {
      if( (v20 & 0x20) == 0 )
        goto LABEL_50;
      v22 = *(_QWORD *)(a1 + 32);
      if( !v22 )
        goto LABEL_50;
      v21 = v22 - 1;
    }
    v1 &= ~v21;
LABEL_50:
    if( !v1 )
      return 3221225626i64;
  }
  *(_QWORD *)v6 = 0i64;
  *(_WORD *)(v6 + 10) = 0;
  *(_WORD *)(v6 + 8) = 8 * (v1 + 6);
  *(_DWORD *)(v6 + 40) = (_DWORD)v1 << 12;
  *(_QWORD *)(v6 + 32) = 0i64;
  *(_DWORD *)(v6 + 44) = 0;
  *(_QWORD *)(v6 + 24) = 0i64;
  *(_QWORD *)(a1 + 40) = v1;
  *(_QWORD *)(a1 + 64) = v6;
  if( *(ULONG_PTR **)a1 == &MiSystemPartition )
    _InterlockedExchangeAdd64(&qword_140C4ECF8, v1);
  return 0i64;
}

Referenced by:

MiAllocatePagesForMdl