#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 *);
size_t get_region_size(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *__bound_realloc(void *ptr, size_t size, const void *caller) {
  void *ptr1;
  size_t 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 == ((size_t)(-1)))
      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    1207 <__bound_realloc+0xb7>
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    1207 <__bound_realloc+0xb7>
mov    -0x10(%rbp),%rdi
call   1070 <get_region_size@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11e1 <__bound_realloc+0x91>
lea    0xe24(%rip),%rdi
call   1050 <bound_error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     11b3 <__bound_realloc+0x63>
mov    %rsi,%rdi
mov    %r14,%rsi
call   1030 <__bound_malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     11c1 <__bound_realloc+0x71>
test   %r15,%r15
je     11c1 <__bound_realloc+0x71>
mov    %rbx,%rdi
call   1070 <get_region_size@plt>
mov    %rax,%r12
cmp    $0xffffffffffffffff,%rax
jne    1198 <__bound_realloc+0x48>
lea    0xe6d(%rip),%rdi
call   1050 <bound_error@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <__bound_free@plt>
jmp    11c1 <__bound_realloc+0x71>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <__bound_free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 i64
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 i64 @get_region_size(i8* %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = icmp eq i64 %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 i64, i64* %9
%38 = call i32 @memcpy(i8* %35, i8* %36, i64 %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 i64 @get_region_size(i8* %0)
%14 = icmp eq i64 %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, i64 %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
}