#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*/ cleanup();
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *realloc(char *, unsigned int);
char *xmalloc(unsigned int);
char *xrealloc(char *p, unsigned int n) {
  if (p == 0)
    return xmalloc(n);
  if (n == 0) {
    free(p);
    return 0;
  }
  p = realloc(p, n);
  if (p == 0) {
    error(0, 0, "virtual memory exhausted");
    cleanup();
    exit(2);
  }
  return p;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <xrealloc+0x2b>
mov    -0x14(%rbp),%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11ef <xrealloc+0x8f>
cmpl   $0x0,-0x14(%rbp)
jne    11ab <xrealloc+0x4b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ef <xrealloc+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e7 <xrealloc+0x87>
xor    %esi,%esi
lea    0xe31(%rip),%rdx
mov    %esi,%edi
call   1060 <error@plt>
mov    $0x0,%al
call   1070 <cleanup@plt>
mov    $0x2,%edi
call   1080 <exit@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   %rax
test   %rdi,%rdi
je     1176 <xrealloc+0x16>
test   %esi,%esi
je     117e <xrealloc+0x1e>
call   1050 <realloc@plt>
test   %rax,%rax
je     1187 <xrealloc+0x27>
pop    %rcx
ret
mov    %esi,%edi
pop    %rax
jmp    1040 <xmalloc@plt>
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe72(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1060 <error@plt>
xor    %eax,%eax
call   1070 <cleanup@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"virtual memory exhausted\00"
define i8* @xrealloc(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* @xmalloc(i32 %9)
store i8* %10, i8** %3
br label %29
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 %29
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i8* @realloc(i8* %18, i32 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = call i32 (...) @cleanup()
%26 = call i32 @exit(i32 2)
unreachable
27: ; preds = %17
%28 = load i8*, i8** %4
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %14, %8
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@.str = [25 x i8] c"virtual memory exhausted\00"
define i8* @xrealloc(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @xmalloc(i32 %1)
br label %17
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 %17
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i32 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 (...) @cleanup()
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %10, %8, %4
%18 = phi i8* [ %5, %4 ], [ null, %8 ], [ %11, %10 ]
ret i8* %18
}