RtlpHpLfhSubsegmentCreate

INT64 __fastcall RtlpHpLfhSubsegmentCreate(INT64 a1, INT64 a2, UINT64 a3){
  __int64 v4; 
  unsigned int v5; 
  unsigned int v7; 
  BOOL v8; 
  int v9; 
  unsigned int v10; 
  unsigned int v11; 
  unsigned int v12; 
  int v13; 
  unsigned int v14; 
  int v15; 
  unsigned int v16; 
  unsigned int v17; 
  unsigned int v18; 
  unsigned int v19; 
  unsigned int v20; 
  UINT8 v21; 
  INT64 v22; 
  unsigned int v23; 
  INT64 v24; 
  unsigned int v26; 
  unsigned int v27; 
  unsigned int v28; 
  unsigned int v29; 
  int v30; 
  int v31; 
  unsigned int v32; 
  int v33; 
  unsigned int v34; 
  v4 = *(unsigned __int8 *)(a2 + 1);
  v5 = a3;
  v30 = 0;
  v33 = 0;
  v7 = (unsigned __int16)RtlpBucketBlockSizes[v4];
  v32 = v7;
  v8 = (RtlpHpLfhPerfFlags & 1) != 0
    && RtlpHpLfhBucketSubsegmentStatsUpdate((volatile INT64 *)(a1 + 64), (unsigned int)v4);
  v9 = RtlpHpLfhBucketComputeNewSubsegmentBlockCount(a2, v8);
  v10 = 7;
  v11 = v7 * v9;
  v12 = 18;
  v13 = (8 * (((unsigned __int64)(unsigned int)(2 * v9) + 63) >> 6) + 63) & 0xFFFFFFF0;
  v14 = v13 + 2 * ((v7 * v9 + v13 + 4095) >> 12);
  v15 = v14 + v11;
  if( v14 + v11 >= 0xF0000 )
    v15 = 983040;
  _BitScanReverse(&v16, v15 - 1);
  v17 = 7;
  v18 = v16 + 1;
  if( v18 > 7 )
    v17 = v18;
  v19 = 18;
  if( v17 < 0x12 )
    v19 = v17;
  if( v19 <= 0xC )
    LOBYTE(v19) = 12;
  v20 = 1 << v19;
  if( (RtlpHpLfhPerfFlags & 8) != 0 && v14 <= v11 >> 6 )
  {
    if( v11 >= 0xF0000 )
      v11 = 983040;
    _BitScanReverse(&v28, v11 - 1);
    v29 = v28 + 1;
    if( v29 > 7 )
      v10 = v29;
    v34 = v10;
    if( v10 < 0x12 )
      v12 = v10;
    if( v12 <= 0xC )
      LOBYTE(v12) = 12;
    if( v20 > 1 << v12 )
      v20 = 1 << v12;
  }
  if( (v5 & 1) != 0 )
    v21 = -1;
  else
    v21 = RtlpHpAcquireLockShared((INT64 *)(a1 + 72), *(unsigned __int8 *)(a1 + 57));
  v22 = ((__int64(__fastcall *)(_QWORD, _QWORD, _QWORD, int *, int *))(a1 ^ RtlpHpHeapGlobals ^ *(_QWORD *)(a1 + 8)))(
          *(_QWORD *)a1,
          v20,
          v5,
          &v33,
          &v30);
  if( v22 )
  {
    if( v8 && (RtlpHpLfhPerfFlags & 2) != 0 || (v33 & 1) != 0 )
    {
      v23 = v20;
    }
    else
    {
      v26 = 2 * v32;
      v31 = 0;
      if( ((2 * v32 - 1) & (2 * v32)) != 0 )
      {
        _BitScanReverse(&v27, v26);
        v31 = v27 + 1;
        v26 = 1 << (v27 + 1);
      }
      if( v26 <= 0x1000 )
        v26 = 4096;
      v23 = v20;
      if( v26 < v20 )
        v23 = v26;
    }
    if( ((int(__fastcall *)(_QWORD, INT64, _QWORD))(a1 ^ RtlpHpHeapGlobals ^ *(_QWORD *)(a1 + 24)))(
           *(_QWORD *)a1,
           v22,
           v23) < 0 )
    {
      v24 = 0i64;
    }
    else
    {
      RtlpHpLfhSubsegmentInitialize(v22, v20, v23, v32, a1);
      _InterlockedIncrement64((volatile signed __int64 *)(a2 + 64));
      _InterlockedExchangeAdd64((volatile signed __int64 *)(a2 + 56), *(unsigned __int16 *)(v22 + 34));
      v24 = v22;
      v22 = 0i64;
    }
    if( v22 )
      ((void(__fastcall *)(_QWORD, INT64, _QWORD, _QWORD))(a1 ^ RtlpHpHeapGlobals ^ *(_QWORD *)(a1 + 16)))(
        *(_QWORD *)a1,
        v22,
        v20,
        v5);
  }
  else
  {
    v24 = 0i64;
  }
  if( (v5 & 1) == 0 )
  {
    if( *(_BYTE *)(a1 + 57) )
    {
      ExReleaseSpinLockSharedFromDpcLevel((PEX_SPIN_LOCK)(a1 + 72));
      __writecr8(v21);
    }
    else
    {
      if( _InterlockedCompareExchange64((volatile signed __int64 *)(a1 + 72), 0i64, 17i64) != 17 )
        ExfReleasePushLockShared((INT64 *)(a1 + 72));
      KeAbPostRelease((PVOID)(a1 + 72));
      KiLeaveGuardedRegionUnsafe((__int64)KeGetCurrentThread());
    }
  }
  return v24;
}

Referenced by:

RtlpHpLfhSlotAllocate