#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 *);
scalar_t__ malloc(size_t);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *xrealloc(void *oldmem, size_t size) {
  void *newmem;
  if (size == 0) {
    size = 1;
  }
  newmem = (oldmem) ? (void *)realloc(oldmem, size) : (void *)malloc(size);
  if (!newmem) {
    fprintf(stderr, "xrealloc: Virtual memory exhausted.\n");
    exit(1);
  }
  return newmem;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <xrealloc+0x23>
movq   $0x1,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1184 <xrealloc+0x44>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1191 <xrealloc+0x51>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <xrealloc+0x83>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rax
mov    %rsi,%rax
test   %rsi,%rsi
mov    $0x1,%esi
cmovne %rax,%rsi
test   %rdi,%rdi
je     1161 <xrealloc+0x21>
call   1050 <realloc@plt>
test   %rax,%rax
je     116e <xrealloc+0x2e>
pop    %rcx
ret
mov    %rsi,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
jne    115f <xrealloc+0x1f>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"xrealloc: Virtual memory exhausted.\0A\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = select i1 %3, i64 1, i64 %1
%5 = icmp eq i8* %0, null
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i64 @realloc(i8* %0, i64 %4)
br label %10
8: ; preds = %2
%9 = tail call i64 @malloc(i64 %4)
br label %10
10: ; preds = %8, %6
%11 = phi i64 [ %7, %6 ], [ %9, %8 ]
%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 ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = inttoptr i64 %11 to i8*
ret i8* %18
}