#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *Realloc(void *p, size_t n) {
  if (p == NULL)
    return Malloc(n);
  if (n == 0) {
    Free(p);
    return NULL;
  }
  p = (void *)realloc(p, n);
  if (p == NULL) {
    fprintf(stderr, "Realloc, Could not allocate %zu 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    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117d <Realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1030 <Malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11e5 <Realloc+0x95>
cmpq   $0x0,-0x18(%rbp)
jne    119e <Realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1060 <Free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <Realloc+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11dd <Realloc+0x8d>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe32(%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    %rsi,%rbx
test   %rdi,%rdi
je     116d <Realloc+0x1d>
test   %rbx,%rbx
je     1176 <Realloc+0x26>
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     117f <Realloc+0x2f>
pop    %rbx
ret
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <Malloc@plt>
call   1060 <Free@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Realloc, Could not allocate %zu bytes\0A\00"
define i8* @Realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%6 = load i8*, i8** %4
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i64, i64* %5
%10 = call i8* @Malloc(i64 %9)
store i8* %10, i8** %3
br label %31
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp eq i64 %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 i64, i64* %5
%20 = call i64 @realloc(i8* %18, i64 %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 i64, i64* %5
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %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 = [39 x i8] c"Realloc, Could not allocate %zu bytes\0A\00"
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 %19
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 %19
10: ; preds = %6
%11 = tail call i64 @realloc(i8* %0, i64 %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 ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %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
}