RtlpAllocateHeapInternal

VOID *__stdcall RtlpAllocateHeapInternal(VOID *HeapHandle, UINT64 Size, UINT64 Flags){
  __int16 v3; 
  UINT64 v4; 
  NTSTATUS v6; 
  VOID *v7; 
  VOID *v8; 
  int v10; 
  unsigned __int64 v11; 
  int v12; 
  int v13; 
  int v14; 
  UINT64 v15; 
  __int64 v16; 
  VOID *Heap; 
  UINT64 v18; 
  VOID *v19; 
  UINT64 *v20; 
  UINT64 *Interceptor; 
  UINT64 Interceptora; 
  UINT16 *Interceptorb; 
  VOID *a6; 
  VOID *a6a; 
  INT64 v26; 
  INT64 v27; 
  UINT64 v28; 
  UINT64 TracingSource; 

  LOWORD(TracingSource) = v3;
  v4 = Size;
  if( *((_DWORD *)HeapHandle + 4) == -857879331 )
  {
    if( Size > 0x20000 )
      return 0i64;
    v6 = RtlpHpConvertFlagsToSegmentFlags((unsigned int)Flags);
    LODWORD(v7) = RtlpHpVsContextAllocate(
                    (INT64)HeapHandle + 64,
                    (unsigned int)v4,
                    (unsigned int)v4,
                    *((_DWORD *)HeapHandle + 5) | (unsigned int)v6);
    return v7;
  }
  v10 = *((_DWORD *)HeapHandle + 29) | Flags;
  v11 = 0i64;
  LODWORD(TracingSource) = 0;
  v28 = 0i64;
  if( Size > 0x7FFFFFFFFFFFFFFFi64
    || !RtlpHpCheckAllocationSizeLimit((VOID *)Size, HeapHandle, (INT64)HeapHandle + 376) )
  {
    goto LABEL_24;
  }
  v12 = *((_DWORD *)HeapHandle + 36);
  v13 = -1073741823;
  if( v12 )
  {
    if( (v10 & 0x3C000102) != 0 )
    {
      v12 = 0;
    }
    else
    {
      if( (_WORD)v12 == 1 )
        v14 = ((__int64(__fastcall *)(VOID *, _QWORD, __int64, UINT64 *))RtlpInterceptorRoutines.QuadPart)(
                HeapHandle,
                0i64,
                1i64,
                &v28);
      else
        v14 = -1073741823;
      if( v14 < 0 )
        goto LABEL_24;
      v15 = (v28 + 15) & 0xFFFFFFFFFFFFFFF0ui64;
      v4 += v15 + 16;
      v28 = v15 + 16;
    }
  }
  v16 = 1i64;
  if( v4 )
    v16 = v4;
  v11 = (v16 + 23) & 0xFFFFFFFFFFFFFFF0ui64;
  Heap = RtlpAllocateHeap((_HEAP *)HeapHandle, v10 | 2u, v4, v11, Interceptor, (UINT64)&TracingSource);
  v8 = Heap;
  if( !Heap )
    goto LABEL_25;
  if( v12 )
  {
    v19 = Heap;
    LODWORD(a6) = v12;
    LOWORD(Interceptora) = v28;
    v4 -= v28;
    v8 = RtlpSetupExtendedBlock(HeapHandle, v10, Heap, v18, Interceptora, a6);
    if( (_WORD)v12 == 1 )
      v13 = ((__int64(__fastcall *)(VOID *, VOID *, __int64, VOID *))RtlpInterceptorRoutines.QuadPart)(
              HeapHandle,
              v8,
              2i64,
              v19);
    if( v13 < 0 )
    {
      RtlFreeHeap(HeapHandle, 0i64, v8, v20, Interceptorb, a6a, v26, v27);
LABEL_24:
      v8 = 0i64;
LABEL_25:
      if( (v10 & 4) != 0 )
      {
        if( v11 )
          v4 = v11;
        RtlpAllocateHeapRaiseException(v4);
      }
    }
  }
  return v8;
}

Referenced by:

RtlAllocateHeap
RtlpHpAllocWithExceptionProtection