#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
void *myMalloc(long long);
scalar_t__ realloc(void *, long long);
int /*<<< orphan*/ stderr;
void *myRealloc(void *p, long long n) {
  if (n == 0) {
    free(p);
    return NULL;
  }
  if (p == NULL)
    return myMalloc(n);
  p = (void *)realloc(p, n);
  if (p == NULL) {
    fprintf(stderr, "Error: realloc failed\n");
    exit(1);
  }
  return p;
}


x86 assembly (clang, -O0):
<myRealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <myRealloc+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e1 <myRealloc+0x91>
cmpq   $0x0,-0x10(%rbp)
jne    119e <myRealloc+0x4e>
mov    -0x18(%rbp),%rdi
call   1040 <myMalloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11e1 <myRealloc+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d9 <myRealloc+0x89>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1050 <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):
<myRealloc>:
push   %rax
test   %rsi,%rsi
je     1167 <myRealloc+0x17>
test   %rdi,%rdi
je     1170 <myRealloc+0x20>
call   1060 <realloc@plt>
test   %rax,%rax
je     1179 <myRealloc+0x29>
pop    %rcx
ret
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    %rsi,%rdi
pop    %rax
jmp    1040 <myMalloc@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Error: realloc failed\0A\00"
define i8* @myRealloc(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 i64, i64* %5
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %4
%10 = call i32 @free(i8* %9)
store i8* null, i8** %3
br label %30
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i64, i64* %5
%16 = call i8* @myMalloc(i64 %15)
store i8* %16, i8** %3
br label %30
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 %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %30
30: ; preds = %28, %14, %8
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Error: realloc failed\0A\00"
define i8* @myRealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @free(i8* %0)
br label %19
6: ; preds = %2
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i8* @myMalloc(i64 %1)
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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%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* [ null, %4 ], [ %9, %8 ], [ %18, %17 ]
ret i8* %20
}