RtlpHpSegSegmentAllocate

INT64 __fastcall RtlpHpSegSegmentAllocate(INT64 a1, INT64 a2, INT64 a3){
  unsigned int v3; 
  unsigned int v5; 
  INT64 v6; 
  union _RTL_RUN_ONCE *HeapManager; 
  unsigned int v8; 
  unsigned __int64 v9; 
  _BOOL8 v10; 
  UINT64 v11; 
  INT64 v12; 
  INT64 v13; 
  __int128 v15[3]; 
  UINT64 v16; 
  v3 = a2 + 2;
  v5 = 0;
  v6 = RtlpHpSegMgrAllocate(a1, (unsigned int)(a2 + 2), a3);
  if( !v6 )
    return 0i64;
  v15[0] = *(_OWORD *)(a1 + 40);
  HeapManager = RtlpHpEnvGetHeapManager(v15);
  v9 = v8;
  v10 = v8 != 0x100000;
  v11 = 2 * ((v6 - HeapManager[1].Value) >> 20);
  v16 = v11 + 2 * ((unsigned __int64)v8 >> 20);
  if( v11 >= v16 )
  {
LABEL_6:
    v5 = 1;
    _InterlockedExchangeAdd64((volatile signed __int64 *)(*(__int16 *)(a1 + 22) + a1), v9 >> 12);
    _InterlockedExchangeAdd64((volatile signed __int64 *)(*(__int16 *)(a1 + 22) + a1 + 8), v3);
    v13 = v6;
    v6 = 0i64;
  }
  else
  {
    v12 = (INT64)&HeapManager[2];
    while( (int)RtlCSparseBitmapBitmaskWrite(v12, v11, (INT64)HeapManager, v10 + 1) >= 0 )
    {
      v11 += 2i64;
      if( v11 >= v16 )
        goto LABEL_6;
    }
    v13 = 0i64;
  }
  if( v6 )
    RtlpHpSegSegmentFree(a1, v6, v3, v5);
  return v13;
}

Referenced by:

RtlpHpSegContextReserve
RtlpHpSegPageRangeAllocate