#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_malloc_gmp(size_t);
void *trace_realloc_gmp(void *userptr, size_t oldsize, size_t newsize) {
  if (userptr == NULL)
    return trace_malloc_gmp(newsize);
  void *newptr = trace_malloc(newsize, "GMP");
  if (oldsize < newsize)
    memcpy(newptr, userptr, oldsize);
  else
    memcpy(newptr, userptr, newsize);
  trace_free(userptr, "GMP");
  return newptr;
}


x86 assembly (clang, -O0):
<trace_realloc_gmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <trace_realloc_gmp+0x31>
mov    -0x20(%rbp),%rdi
call   1050 <trace_malloc_gmp@plt>
mov    %rax,-0x8(%rbp)
jmp    11d2 <trace_realloc_gmp+0x92>
mov    -0x20(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <trace_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a9 <trace_realloc_gmp+0x69>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
jmp    11ba <trace_realloc_gmp+0x7a>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1030 <trace_free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_realloc_gmp>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
test   %rdi,%rdi
je     1199 <trace_realloc_gmp+0x59>
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xea2(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <trace_malloc@plt>
mov    %rax,%r13
cmp    %rbx,%r15
cmovb  %r15,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <trace_free@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <trace_malloc_gmp@plt>

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

LLVM IR (-O2):
@.str = [4 x i8] c"GMP\00"
define i8* @trace_realloc_gmp(i8* %0, i64 %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i8* @trace_malloc_gmp(i64 %2)
br label %13
7: ; preds = %3
%8 = tail call i8* @trace_malloc(i64 %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp ult i64 %1, %2
%10 = select i1 %9, i64 %1, i64 %2
%11 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %10)
%12 = tail call i32 @trace_free(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %7, %5
%14 = phi i8* [ %6, %5 ], [ %8, %7 ]
ret i8* %14
}