#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 */
scalar_t__ address_out_of_range(void *);
void *mem_alloc(size_t);
int /*<<< orphan*/ mem_copy(void *, void *, size_t);
int /*<<< orphan*/ mem_free(void *);
void *mem_realloc(void *addr, size_t size) {
  if (address_out_of_range(addr)) {
    return NULL;
  }
  void *new_addr = mem_alloc(size);
  if (new_addr) {
    mem_copy(new_addr, addr, size);
    mem_free(addr);
    return new_addr;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<mem_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <address_out_of_range@plt>
cmp    $0x0,%rax
je     1170 <mem_realloc+0x30>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <mem_realloc+0x77>
mov    -0x18(%rbp),%rdi
call   1060 <mem_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11af <mem_realloc+0x6f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <mem_copy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <mem_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <mem_realloc+0x77>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1050 <address_out_of_range@plt>
test   %rax,%rax
je     1159 <mem_realloc+0x19>
xor    %ebx,%ebx
jmp    117f <mem_realloc+0x3f>
mov    %r15,%rdi
call   1060 <mem_alloc@plt>
test   %rax,%rax
je     1155 <mem_realloc+0x15>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <mem_copy@plt>
mov    %r14,%rdi
call   1030 <mem_free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mem_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = call i64 @address_out_of_range(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %25
11: ; preds = %2
%12 = load i64, i64* %5
%13 = call i8* @mem_alloc(i64 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i8*, i8** %6
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = call i32 @mem_copy(i8* %17, i8* %18, i64 %19)
%21 = load i8*, i8** %4
%22 = call i32 @mem_free(i8* %21)
%23 = load i8*, i8** %6
store i8* %23, i8** %3
br label %25
24: ; preds = %11
store i8* null, i8** %3
br label %25
25: ; preds = %24, %16, %10
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
define i8* @mem_realloc(i8* %0, i64 %1) {
%3 = tail call i64 @address_out_of_range(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = tail call i8* @mem_alloc(i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @mem_copy(i8* %6, i8* %0, i64 %1)
%10 = tail call i32 @mem_free(i8* %0)
br label %11
11: ; preds = %8, %5, %2
%12 = phi i8* [ null, %2 ], [ %6, %8 ], [ null, %5 ]
ret i8* %12
}