RtlpFreeHeapInternal

UINT64 __stdcall RtlpFreeHeapInternal(PVOID HeapHandle, PVOID BaseAddress, UINT64 Flags){
  char *v3; 
  unsigned int v4; 
  int v5; 
  unsigned int v8; 
  _HEAP_ENTRY *v10; 
  PVOID v11; 
  INT64 v12; 
  unsigned int SubSegmentCode; 
  int v14; 
  unsigned __int16 Size; 
  unsigned int v16; 
  unsigned __int16 v17; 
  __int64 v18; 
  int v19; 
  int v20; 
  UINT64 *v21; 
  VOID *v22; 
  int v23; 
  int v24; 
  int v25; 
  UINT64 v26; 
  v26 = (UINT64)v3;
  v4 = 0;
  v5 = Flags;
  if( *((_DWORD *)HeapHandle + 4) == -857879331 )
  {
    LODWORD(v26) = 0;
    v8 = RtlpHpConvertFlagsToSegmentFlags((unsigned int)Flags);
    return(unsigned int)RtlpHpVsContextFree(
                           (INT64)HeapHandle + 64,
                           0i64,
                           (UINT64)BaseAddress,
                           *((_DWORD *)HeapHandle + 5) | v8,
                           &v26);
  }
  if( (*((_DWORD *)HeapHandle + 30) & 1) != 0 )
  {
    v10 = (_HEAP_ENTRY *)RtlpProbeUserBufferSafe((INT64)HeapHandle, (UINT64)BaseAddress);
  }
  else
  {
    if( ((unsigned __int8)BaseAddress & 0xF) != 0 )
    {
      v11 = BaseAddress;
      v12 = 9i64;
    }
    else
    {
      v10 = (_HEAP_ENTRY *)((char *)BaseAddress - 16);
      _m_prefetchw((char *)BaseAddress - 16);
      if( *((char *)BaseAddress - 1) == 5 )
        v10 -= v10->UnpackedEntry.SegmentOffset;
      if( (v10->UnpackedEntry.UnusedBytes & 0x3F) != 0 )
        goto LABEL_13;
      v11 = v10;
      v12 = 8i64;
    }
    RtlpLogHeapFailure(v12, (INT64)HeapHandle, (UINT64)v11, 0i64, 0i64, 0i64);
    v10 = 0i64;
  }
LABEL_13:
  if( !v10 )
    return v4;
  if( *((char *)BaseAddress - 1) != 5 )
    return(unsigned __int8)RtlpFreeHeap((_HEAP *)HeapHandle, v5 | 2u, v10, v3, v21, v22);
  if( !*((_DWORD *)HeapHandle + 31) )
    goto LABEL_21;
  v23 = v10->UnpackedEntry.SubSegmentCode ^ *((_DWORD *)HeapHandle + 34);
  if( HIBYTE(v23) != ((unsigned __int8)v23 ^ (unsigned __int8)(BYTE1(v23) ^ BYTE2(v23))) )
  {
LABEL_34:
    RtlpLogHeapFailure(3i64, (INT64)HeapHandle, (UINT64)v10, (INT64)BaseAddress, 0i64, 0i64);
    return v4;
  }
  if( *((_DWORD *)HeapHandle + 31) )
  {
    SubSegmentCode = v10->UnpackedEntry.SubSegmentCode;
    v14 = *((_DWORD *)HeapHandle + 31);
    LOWORD(v24) = SubSegmentCode;
    if( (v14 & SubSegmentCode) != 0 )
      v24 = *((_DWORD *)HeapHandle + 34) ^ SubSegmentCode;
    Size = v24;
  }
  else
  {
LABEL_21:
    Size = v10->UnpackedEntry.Size;
    v14 = 0;
  }
  if( v10->UnpackedEntry.UnusedBytes == 4 )
  {
    if( v14 )
    {
      v16 = v10->UnpackedEntry.SubSegmentCode;
      LOWORD(v25) = v16;
      if( (v16 & *((_DWORD *)HeapHandle + 31)) != 0 )
        v25 = *((_DWORD *)HeapHandle + 34) ^ v16;
      v17 = v25;
    }
    else
    {
      v17 = v10->UnpackedEntry.Size;
    }
    v18 = (__int64)v10[-1].UnpackedEntry.PreviousBlockPrivateData + Size - v17;
  }
  else
  {
    v18 = 16i64 * Size;
  }
  if( (char *)v10 + v18 < BaseAddress )
    goto LABEL_34;
  v19 = *((_DWORD *)BaseAddress - 2);
  if( (v5 & 0x3C000102) != 0 )
    return(unsigned __int8)RtlpFreeHeap((_HEAP *)HeapHandle, v5 | 2u, v10, v3, v21, v22);
  v3 = *((char *)BaseAddress - 1) == 5 ? (char *)BaseAddress - 16 * (unsigned __int8)*((char *)BaseAddress - 2) : 0i64;
  v20 = v19 && (_WORD)v19 == 1 ? ((__int64(__fastcall *)(PVOID, PVOID, __int64, char *))RtlpInterceptorRoutines)(
                                   HeapHandle,
                                   BaseAddress,
                                   3i64,
                                   v3) : -1073741823;
  if( v20 >= 0 )
    return(unsigned __int8)RtlpFreeHeap((_HEAP *)HeapHandle, v5 | 2u, v10, v3, v21, v22);
  return v4;
}

Referenced by:

RtlFreeHeap
RtlpHpFreeWithExceptionProtection