#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*/ MT_add_entry(unsigned long, size_t, char *, int,
                                char const *, char const *);
int /*<<< orphan*/ MT_find_entry(unsigned long);
int /*<<< orphan*/ MT_init_redzone(void *, size_t);
int /*<<< orphan*/ MT_modify_entry(unsigned long, unsigned long, size_t, char *,
                                   int, char const *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           char const *, unsigned long);
int /*<<< orphan*/ perror(char *);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *MT_Realloc(void *ptr, size_t size, int line, const char *func_name,
                 const char *file_name) {
  void *newptr;
  size += 4;
  if (!ptr) {
    if ((newptr = (void *)realloc(ptr, size)) == NULL) {
      perror("realloc failed to resize memory!");
      exit(1);
    }
    MT_add_entry((unsigned long int)newptr, size, "RAL", line, func_name,
                 file_name);
  } else if (!MT_find_entry((unsigned long int)ptr)) {
    fprintf(stderr,
            "MemTracker: %s:%u func:%s()  realloc called but 0x%lX not "
            "allocated!\n",
            file_name, line, func_name, (unsigned long int)ptr);
    return ptr;
  } else {
    if ((newptr = (void *)realloc(ptr, size)) == NULL) {
      perror("realloc failed to resize memory!");
      exit(1);
    }
    MT_modify_entry((unsigned long int)ptr, (unsigned long int)newptr, size,
                    "RAL", line, func_name, file_name);
  }
  MT_init_redzone(newptr, size);
  return newptr;
}


x86 assembly (clang, -O0):
<MT_Realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1207 <MT_Realloc+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e3 <MT_Realloc+0x63>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
lea    0xe24(%rip),%rdx
call   1080 <MT_add_entry@plt>
jmp    12a8 <MT_Realloc+0x128>
mov    -0x10(%rbp),%rdi
call   10a0 <MT_find_entry@plt>
cmp    $0x0,%eax
jne    124a <MT_Realloc+0xca>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xded(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bd <MT_Realloc+0x13d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    127b <MT_Realloc+0xfb>
lea    0xd94(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%r8d
mov    -0x28(%rbp),%r9
mov    -0x30(%rbp),%rax
lea    0xd87(%rip),%rcx
mov    %rax,(%rsp)
call   1040 <MT_modify_entry@plt>
jmp    12a8 <MT_Realloc+0x128>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <MT_init_redzone@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MT_Realloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,%r15
mov    %edx,%r12d
mov    %rsi,%r14
add    $0x4,%r14
test   %rdi,%rdi
je     11e7 <MT_Realloc+0x67>
mov    %rdi,%rbx
call   10a0 <MT_find_entry@plt>
test   %eax,%eax
je     1221 <MT_Realloc+0xa1>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     1254 <MT_Realloc+0xd4>
mov    %rax,%rbp
mov    %r13,(%rsp)
lea    0xe53(%rip),%rcx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r12d,%r8d
mov    %r15,%r9
call   1040 <MT_modify_entry@plt>
mov    %rbp,%rbx
jmp    1214 <MT_Realloc+0x94>
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     1254 <MT_Realloc+0xd4>
mov    %rax,%rbx
lea    0xe21(%rip),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%ecx
mov    %r15,%r8
mov    %r13,%r9
call   1080 <MT_add_entry@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <MT_init_redzone@plt>
jmp    1242 <MT_Realloc+0xc2>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    %r13,%rdx
mov    %r12d,%ecx
mov    %r15,%r8
mov    %rbx,%r9
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"realloc failed to resize memory!\00"
@.str.1 = [4 x i8] c"RAL\00"
@stderr = global i32 0
@.str.2 = [70 x i8] c"MemTracker: %s:%u func:%s() realloc called but 0x%lX not allocated!\0A\00"
define i8* @MT_Realloc(i8* %0, i64 %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%13 = load i64, i64* %8
%14 = add i64 %13, 4
store i64 %14, i64* %8
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %34, label %17
17: ; preds = %5
%18 = load i8*, i8** %7
%19 = load i64, i64* %8
%20 = call i64 @realloc(i8* %18, i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %12
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i8*, i8** %12
%28 = ptrtoint i8* %27 to i64
%29 = load i64, i64* %8
%30 = load i32, i32* %9
%31 = load i8*, i8** %10
%32 = load i8*, i8** %11
%33 = call i32 @MT_add_entry(i64 %28, i64 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %31, i8* %32)
br label %68
34: ; preds = %5
%35 = load i8*, i8** %7
%36 = ptrtoint i8* %35 to i64
%37 = call i32 @MT_find_entry(i64 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %34
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %11
%42 = load i32, i32* %9
%43 = load i8*, i8** %10
%44 = load i8*, i8** %7
%45 = ptrtoint i8* %44 to i64
%46 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %42, i8* %43, i64 %45)
%47 = load i8*, i8** %7
store i8* %47, i8** %6
br label %73
48: ; preds = %34
%49 = load i8*, i8** %7
%50 = load i64, i64* %8
%51 = call i64 @realloc(i8* %49, i64 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %12
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
%58 = load i8*, i8** %7
%59 = ptrtoint i8* %58 to i64
%60 = load i8*, i8** %12
%61 = ptrtoint i8* %60 to i64
%62 = load i64, i64* %8
%63 = load i32, i32* %9
%64 = load i8*, i8** %10
%65 = load i8*, i8** %11
%66 = call i32 @MT_modify_entry(i64 %59, i64 %61, i64 %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %63, i8* %64, i8* %65)
br label %67
67: ; preds = %57
br label %68
68: ; preds = %67, %26
%69 = load i8*, i8** %12
%70 = load i64, i64* %8
%71 = call i32 @MT_init_redzone(i8* %69, i64 %70)
%72 = load i8*, i8** %12
store i8* %72, i8** %6
br label %73
73: ; preds = %68, %39
%74 = load i8*, i8** %6
ret i8* %74
}

LLVM IR (-O2):
@.str = [33 x i8] c"realloc failed to resize memory!\00"
@.str.1 = [4 x i8] c"RAL\00"
@stderr = global i32 0
@.str.2 = [70 x i8] c"MemTracker: %s:%u func:%s() realloc called but 0x%lX not allocated!\0A\00"
define i8* @MT_Realloc(i8* %0, i64 %1, i32 %2, i8* %3, i8* %4) {
%6 = add i64 %1, 4
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = tail call i64 @realloc(i8* null, i64 %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = tail call i32 @MT_add_entry(i64 %9, i64 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %2, i8* %3, i8* %4)
br label %31
16: ; preds = %5
%17 = ptrtoint i8* %0 to i64
%18 = tail call i32 @MT_find_entry(i64 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %2, i8* %3, i64 %17)
br label %35
23: ; preds = %16
%24 = tail call i64 @realloc(i8* %0, i64 %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = tail call i32 @MT_modify_entry(i64 %17, i64 %24, i64 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %2, i8* %3, i8* %4)
br label %31
31: ; preds = %29, %14
%32 = phi i64 [ %24, %29 ], [ %9, %14 ]
%33 = inttoptr i64 %32 to i8*
%34 = tail call i32 @MT_init_redzone(i8* %33, i64 %6)
br label %35
35: ; preds = %31, %20
%36 = phi i8* [ %33, %31 ], [ %0, %20 ]
ret i8* %36
}