#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *xrealloc(void *ptr, size_t size) {
  void *vp;
  if (!size) {
    if (ptr)
      free(ptr);
    return NULL;
  }
  if (ptr)
    vp = realloc(ptr, size);
  else
    vp = malloc(size);
  if (!vp) {
    fprintf(stderr, "virtual memory exhausted\n");
    exit(EXIT_FAILURE);
  }
  return vp;
}


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    118c <xrealloc+0x3c>
cmpq   $0x0,-0x10(%rbp)
je     117f <xrealloc+0x2f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <xrealloc+0xa0>
cmpq   $0x0,-0x10(%rbp)
je     11ad <xrealloc+0x5d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11ba <xrealloc+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <xrealloc+0x98>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rax
test   %rsi,%rsi
je     1167 <xrealloc+0x17>
test   %rdi,%rdi
je     1175 <xrealloc+0x25>
call   1060 <realloc@plt>
test   %rax,%rax
je     1182 <xrealloc+0x32>
pop    %rcx
ret
test   %rdi,%rdi
je     1171 <xrealloc+0x21>
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    1165 <xrealloc+0x15>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"virtual memory exhausted\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp ne i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = call i32 @free(i8* %13)
br label %15
15: ; preds = %12, %9
store i8* null, i8** %3
br label %36
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = load i64, i64* %5
%22 = call i8* @realloc(i8* %20, i64 %21)
store i8* %22, i8** %6
br label %26
23: ; preds = %16
%24 = load i64, i64* %5
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %6
br label %26
26: ; preds = %23, %19
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = load i8*, i8** %6
store i8* %35, i8** %3
br label %36
36: ; preds = %34, %15
%37 = load i8*, i8** %3
ret i8* %37
}

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