RtlpHpSegSegmentAllocate

NTSTATUS __stdcall RtlpHpSegSegmentAllocate(INT64 a1, INT64 a2, INT64 a3){
  unsigned int v3; 
  unsigned int v5; 
  INT64 v6; 
  INT64 v7; 
  _RTL_RUN_ONCE *HeapManager; 
  unsigned int v9; 
  unsigned __int64 v10; 
  _BOOL8 v11; 
  UINT64 v12; 
  INT64 v13; 
  NTSTATUS v14; 
  __int128 v16[3]; 
  UINT64 v17; 

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

Referenced by:

RtlpHpSegContextReserve
RtlpHpSegPageRangeAllocate