#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(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ realloc(void *, int);
int /*<<< orphan*/ stderr;
void *Realloc(void *p, int n) {
  if (p == NULL)
    return Malloc(n);
  if (n == 0) {
    Free(p);
    return NULL;
  }
  p = (void *)realloc(p, n);
  if (p == NULL) {
    fprintf(stderr, "Could not allocate %i bytes\n", n);
    exit(1);
  }
  return p;
}


x86 assembly (clang, -O0):
<Realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117b <Realloc+0x2b>
mov    -0x14(%rbp),%edi
call   1030 <Malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11e0 <Realloc+0x90>
cmpl   $0x0,-0x14(%rbp)
jne    119b <Realloc+0x4b>
mov    -0x10(%rbp),%rdi
call   1060 <Free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <Realloc+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <Realloc+0x88>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Realloc>:
push   %rbx
mov    %esi,%ebx
test   %rdi,%rdi
je     116a <Realloc+0x1a>
test   %ebx,%ebx
je     1172 <Realloc+0x22>
mov    %ebx,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     117b <Realloc+0x2b>
pop    %rbx
ret
mov    %ebx,%edi
pop    %rbx
jmp    1030 <Malloc@plt>
call   1060 <Free@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Could not allocate %i bytes\0A\00"
define i8* @Realloc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%6 = load i8*, i8** %4
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* %5
%10 = call i8* @Malloc(i32 %9)
store i8* %10, i8** %3
br label %31
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %4
%16 = call i32 @Free(i8* %15)
store i8* null, i8** %3
br label %31
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i64 @realloc(i8* %18, i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* %5
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = load i8*, i8** %4
store i8* %30, i8** %3
br label %31
31: ; preds = %29, %14, %8
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Could not allocate %i bytes\0A\00"
define i8* @Realloc(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @Malloc(i32 %1)
br label %19
6: ; preds = %2
%7 = icmp eq i32 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @Free(i8* %0)
br label %19
10: ; preds = %6
%11 = tail call i64 @realloc(i8* %0, i32 %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %1)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = inttoptr i64 %11 to i8*
br label %19
19: ; preds = %17, %8, %4
%20 = phi i8* [ %5, %4 ], [ null, %8 ], [ %18, %17 ]
ret i8* %20
}