#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 */
void *_memory_alloc(size_t);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ assert_ptr_exists(void *);
int /*<<< orphan*/ change_ptr(void *, void *);
void *realloc(void *, size_t);
void *_memory_realloc(void *ptr, size_t size) {
  if (ptr == NULL) {
    return _memory_alloc((size));
  }
  assert_ptr_exists(ptr);
  void *new_ptr = realloc(ptr, size);
  assert(new_ptr != NULL);
  if (new_ptr != ptr) {
    change_ptr(ptr, new_ptr);
  }
  return new_ptr;
}


x86 assembly (clang, -O0):
<_memory_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117d <_memory_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1070 <_memory_alloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11cc <_memory_realloc+0x7c>
mov    -0x10(%rbp),%rdi
call   1040 <assert_ptr_exists@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11c4 <_memory_realloc+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <change_ptr@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_memory_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     119e <_memory_realloc+0x4e>
mov    %rdi,%rbx
call   1040 <assert_ptr_exists@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
cmp    %rbx,%r14
je     1193 <_memory_realloc+0x43>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <change_ptr@plt>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <_memory_alloc@plt>

LLVM IR (-O0):
define i8* @_memory_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 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = call i8* @_memory_alloc(i64 %10)
store i8* %11, i8** %3
br label %31
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @assert_ptr_exists(i8* %13)
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = call i8* @realloc(i8* %15, i64 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = icmp ne i8* %22, %23
br i1 %24, label %25, label %29
25: ; preds = %12
%26 = load i8*, i8** %4
%27 = load i8*, i8** %6
%28 = call i32 @change_ptr(i8* %26, i8* %27)
br label %29
29: ; preds = %25, %12
%30 = load i8*, i8** %6
store i8* %30, i8** %3
br label %31
31: ; preds = %29, %9
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
define i8* @_memory_realloc(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @_memory_alloc(i64 %1)
br label %15
6: ; preds = %2
%7 = tail call i32 @assert_ptr_exists(i8* %0)
%8 = tail call i8* @realloc(i8* %0, i64 %1)
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp eq i8* %8, %0
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = tail call i32 @change_ptr(i8* %0, i8* %8)
br label %15
15: ; preds = %6, %13, %4
%16 = phi i8* [ %5, %4 ], [ %8, %13 ], [ %0, %6 ]
ret i8* %16
}