#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*/ __bound_free(void *, void const *);
void *__bound_malloc(size_t, void const *);
int /*<<< orphan*/ bound_error(char *);
int get_region_size(void *);
int /*<<< orphan*/ memcpy(void *, void *, int);
void *__bound_realloc(void *ptr, size_t size, const void *caller) {
  void *ptr1;
  int old_size;
  if (size == 0) {
    __bound_free(ptr, caller);
    return NULL;
  } else {
    ptr1 = __bound_malloc(size, caller);
    if (ptr == NULL || ptr1 == NULL)
      return ptr1;
    old_size = get_region_size(ptr);
    if (old_size == 0xffffffff)
      bound_error("realloc'ing invalid pointer");
    memcpy(ptr1, ptr, old_size);
    __bound_free(ptr, caller);
    return ptr1;
  }
}


x86 assembly (clang, -O0):
<__bound_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1189 <__bound_realloc+0x39>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <__bound_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1204 <__bound_realloc+0xb4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <__bound_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b0 <__bound_realloc+0x60>
cmpq   $0x0,-0x28(%rbp)
jne    11bd <__bound_realloc+0x6d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1204 <__bound_realloc+0xb4>
mov    -0x10(%rbp),%rdi
call   1070 <get_region_size@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11df <__bound_realloc+0x8f>
lea    0xe26(%rip),%rdi
call   1050 <bound_error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <__bound_free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__bound_realloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     11af <__bound_realloc+0x5f>
mov    %rsi,%rdi
mov    %r14,%rsi
call   1030 <__bound_malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     11bd <__bound_realloc+0x6d>
test   %r15,%r15
je     11bd <__bound_realloc+0x6d>
mov    %rbx,%rdi
call   1070 <get_region_size@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1195 <__bound_realloc+0x45>
lea    0xe70(%rip),%rdi
call   1050 <bound_error@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <__bound_free@plt>
jmp    11bd <__bound_realloc+0x6d>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <__bound_free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"realloc'ing invalid pointer\00"
define i8* @__bound_realloc(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i8*, i8** %7
%15 = call i32 @__bound_free(i8* %13, i8* %14)
store i8* null, i8** %4
br label %43
16: ; preds = %3
%17 = load i64, i64* %6
%18 = load i8*, i8** %7
%19 = call i8* @__bound_malloc(i64 %17, i8* %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = load i8*, i8** %8
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22, %16
%26 = load i8*, i8** %8
store i8* %26, i8** %4
br label %43
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = call i32 @get_region_size(i8* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @bound_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i8*, i8** %8
%36 = load i8*, i8** %5
%37 = load i32, i32* %9
%38 = call i32 @memcpy(i8* %35, i8* %36, i32 %37)
%39 = load i8*, i8** %5
%40 = load i8*, i8** %7
%41 = call i32 @__bound_free(i8* %39, i8* %40)
%42 = load i8*, i8** %8
store i8* %42, i8** %4
br label %43
43: ; preds = %34, %25, %12
%44 = load i8*, i8** %4
ret i8* %44
}

LLVM IR (-O2):
@.str = [28 x i8] c"realloc'ing invalid pointer\00"
define i8* @__bound_realloc(i8* %0, i64 %1, i8* %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @__bound_free(i8* %0, i8* %2)
br label %20
7: ; preds = %3
%8 = tail call i8* @__bound_malloc(i64 %1, i8* %2)
%9 = icmp eq i8* %0, null
%10 = icmp eq i8* %8, null
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = tail call i32 @get_region_size(i8* %0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @bound_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 @memcpy(i8* %8, i8* %0, i32 %13)
%19 = tail call i32 @__bound_free(i8* %0, i8* %2)
br label %20
20: ; preds = %7, %17, %5
%21 = phi i8* [ null, %5 ], [ %8, %17 ], [ %8, %7 ]
ret i8* %21
}