#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ ht_insert(void *, unsigned long, char *);
int /*<<< orphan*/ ht_remove(void *, char *);
int /*<<< orphan*/ initialize();
int /*<<< orphan*/ mark_corruption(void *, unsigned long);
int /*<<< orphan*/ mhandle_free(void *, char *);
void *mhandle_malloc(unsigned long, char *);
int /*<<< orphan*/ outofmem(char *);
void *realloc(void *, unsigned long);
void *mhandle_realloc(void *ptr, unsigned long size, char *at) {
  if (!ptr)
    return mhandle_malloc(size, at);
  if (!size) {
    mhandle_free(ptr, at);
    return NULL;
  }
  initialize();
  ptr -= 2;
  ht_remove(ptr, at);
  ptr = realloc(ptr, size + (2 * 2));
  if (!ptr)
    outofmem(at);
  mark_corruption(ptr, size);
  ht_insert(ptr, size, at);
  return ptr + 2;
}


x86 assembly (clang, -O0):
<mhandle_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b5 <mhandle_realloc+0x35>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <mhandle_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    124d <mhandle_realloc+0xcd>
cmpq   $0x0,-0x18(%rbp)
jne    11da <mhandle_realloc+0x5a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <mhandle_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    124d <mhandle_realloc+0xcd>
mov    $0x0,%al
call   1060 <initialize@plt>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <ht_remove@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1223 <mhandle_realloc+0xa3>
mov    -0x20(%rbp),%rdi
call   1090 <outofmem@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <mark_corruption@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <ht_insert@plt>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mhandle_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
test   %rdi,%rdi
je     11e9 <mhandle_realloc+0x69>
mov    %rdi,%rbx
test   %r15,%r15
je     11f9 <mhandle_realloc+0x79>
xor    %eax,%eax
call   1060 <initialize@plt>
add    $0xfffffffffffffffe,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <ht_remove@plt>
lea    0x4(%r15),%rsi
mov    %rbx,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ca <mhandle_realloc+0x4a>
mov    %r14,%rdi
call   1090 <outofmem@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <mark_corruption@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <ht_insert@plt>
add    $0x2,%rbx
jmp    1206 <mhandle_realloc+0x86>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <mhandle_malloc@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <mhandle_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mhandle_realloc(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = load i64, i64* %6
%12 = load i8*, i8** %7
%13 = call i8* @mhandle_malloc(i64 %11, i8* %12)
store i8* %13, i8** %4
br label %47
14: ; preds = %3
%15 = load i64, i64* %6
%16 = icmp ne i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = load i8*, i8** %7
%20 = call i32 @mhandle_free(i8* %18, i8* %19)
store i8* null, i8** %4
br label %47
21: ; preds = %14
%22 = call i32 (...) @initialize()
%23 = load i8*, i8** %5
%24 = getelementptr i8, i8* %23, i64 -2
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = load i8*, i8** %7
%27 = call i32 @ht_remove(i8* %25, i8* %26)
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = add i64 %29, 4
%31 = call i8* @realloc(i8* %28, i64 %30)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %37, label %34
34: ; preds = %21
%35 = load i8*, i8** %7
%36 = call i32 @outofmem(i8* %35)
br label %37
37: ; preds = %34, %21
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = call i32 @mark_corruption(i8* %38, i64 %39)
%41 = load i8*, i8** %5
%42 = load i64, i64* %6
%43 = load i8*, i8** %7
%44 = call i32 @ht_insert(i8* %41, i64 %42, i8* %43)
%45 = load i8*, i8** %5
%46 = getelementptr i8, i8* %45, i64 2
store i8* %46, i8** %4
br label %47
47: ; preds = %37, %17, %10
%48 = load i8*, i8** %4
ret i8* %48
}

LLVM IR (-O2):
define i8* @mhandle_realloc(i8* %0, i64 %1, i8* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i8* @mhandle_malloc(i64 %1, i8* %2)
br label %24
7: ; preds = %3
%8 = icmp eq i64 %1, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @mhandle_free(i8* %0, i8* %2)
br label %24
11: ; preds = %7
%12 = tail call i32 (...) @initialize()
%13 = getelementptr i8, i8* %0, i64 -2
%14 = tail call i32 @ht_remove(i8* %13, i8* %2)
%15 = add i64 %1, 4
%16 = tail call i8* @realloc(i8* %13, i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @outofmem(i8* %2)
br label %20
20: ; preds = %18, %11
%21 = tail call i32 @mark_corruption(i8* %16, i64 %1)
%22 = tail call i32 @ht_insert(i8* %16, i64 %1, i8* %2)
%23 = getelementptr i8, i8* %16, i64 2
br label %24
24: ; preds = %20, %9, %5
%25 = phi i8* [ %23, %20 ], [ null, %9 ], [ %6, %5 ]
ret i8* %25
}