#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*/ Free(void *);
void *Malloc(size_t);
int /*<<< orphan*/ fatal_error(size_t);
void *realloc(void *, size_t);
void *Realloc(void *ptr, size_t size) {
  if (ptr == NULL)
    return Malloc(size);
  else if (size == 0) {
    Free(ptr);
    return NULL;
  } else {
    void *new_ptr;
    new_ptr = realloc(ptr, size);
    if (new_ptr == NULL)
      fatal_error(size);
    return new_ptr;
  }
}


x86 assembly (clang, -O0):
<Realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <Realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1030 <Malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11bb <Realloc+0x7b>
cmpq   $0x0,-0x18(%rbp)
jne    118e <Realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1060 <Free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bb <Realloc+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b3 <Realloc+0x73>
mov    -0x18(%rbp),%rdi
call   1040 <fatal_error@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Realloc>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     115d <Realloc+0x1d>
test   %rbx,%rbx
je     1166 <Realloc+0x26>
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     116d <Realloc+0x2d>
pop    %rbx
ret
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <Malloc@plt>
call   1060 <Free@plt>
jmp    1175 <Realloc+0x35>
mov    %rbx,%rdi
call   1040 <fatal_error@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @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* @Malloc(i64 %1)
br label %15
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @Free(i8* %0)
br label %15
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i64 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @fatal_error(i64 %1)
br label %15
15: ; preds = %10, %13, %8, %4
%16 = phi i8* [ %5, %4 ], [ null, %8 ], [ null, %13 ], [ %11, %10 ]
ret i8* %16
}