#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*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ trace_free(void *, char *);
void *trace_malloc(size_t, char *);
void *trace_realloc(void *userptr, size_t newsize, char *where) {
  if (userptr == NULL)
    return trace_malloc(newsize, where);
  size_t *p = (size_t *)userptr;
  size_t oldsize = p[-1];
  void *newptr = trace_malloc(newsize, where);
  memcpy(newptr, userptr, oldsize);
  trace_free(userptr, where);
  return newptr;
}


x86 assembly (clang, -O0):
<trace_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1165 <trace_realloc+0x35>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <trace_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11b0 <trace_realloc+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <trace_malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <trace_free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdi,%rdi
je     117d <trace_realloc+0x4d>
mov    %rdi,%rbx
mov    -0x8(%rdi),%r15
mov    %rsi,%rdi
mov    %r14,%rsi
call   1050 <trace_malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <trace_free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsi,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <trace_malloc@plt>

LLVM IR (-O0):
define i8* @trace_realloc(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i64, i64* %6
%15 = load i8*, i8** %7
%16 = call i8* @trace_malloc(i64 %14, i8* %15)
store i8* %16, i8** %4
br label %34
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = bitcast i8* %18 to i64*
store i64* %19, i64** %8
%20 = load i64*, i64** %8
%21 = getelementptr inbounds i64, i64* %20, i64 -1
%22 = load i64, i64* %21
store i64 %22, i64* %9
%23 = load i64, i64* %6
%24 = load i8*, i8** %7
%25 = call i8* @trace_malloc(i64 %23, i8* %24)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = load i8*, i8** %5
%28 = load i64, i64* %9
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %5
%31 = load i8*, i8** %7
%32 = call i32 @trace_free(i8* %30, i8* %31)
%33 = load i8*, i8** %10
store i8* %33, i8** %4
br label %34
34: ; preds = %17, %13
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
define i8* @trace_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* @trace_malloc(i64 %1, i8* %2)
br label %14
7: ; preds = %3
%8 = getelementptr inbounds i8, i8* %0, i64 -8
%9 = bitcast i8* %8 to i64*
%10 = load i64, i64* %9
%11 = tail call i8* @trace_malloc(i64 %1, i8* %2)
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %10)
%13 = tail call i32 @trace_free(i8* %0, i8* %2)
br label %14
14: ; preds = %7, %5
%15 = phi i8* [ %6, %5 ], [ %11, %7 ]
ret i8* %15
}