#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*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *xrealloc(void *p, size_t n) {
  if (!n && p) {
    free(p);
    return NULL;
  }
  p = realloc(p, n);
  if (!p && n != 0) {
    fprintf(stderr, "realloc failed, memory exhausted\n");
    abort();
  }
  return p;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117c <xrealloc+0x3c>
cmpq   $0x0,-0x10(%rbp)
je     117c <xrealloc+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <xrealloc+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bf <xrealloc+0x7f>
cmpq   $0x0,-0x18(%rbp)
je     11bf <xrealloc+0x7f>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     1157 <xrealloc+0x17>
test   %rbx,%rbx
jne    1157 <xrealloc+0x17>
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rbx,%rbx
je     1169 <xrealloc+0x29>
test   %rax,%rax
je     116b <xrealloc+0x2b>
pop    %rbx
ret
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1040 <abort@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"realloc failed, memory exhausted\0A\00"
define noalias i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp ne i8* %0, null
%5 = and i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @free(i8* %0)
br label %17
8: ; preds = %2
%9 = tail call i8* @realloc(i8* %0, i64 %1)
%10 = icmp eq i8* %9, null
%11 = icmp ne i64 %1, 0
%12 = and i1 %11, %10
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %8, %6
%18 = phi i8* [ null, %6 ], [ %9, %8 ]
ret i8* %18
}