#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);
scalar_t__ malloc(size_t);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ write(int, char *, int);
void *xrealloc(void *block, size_t size) {
  register void *val;
  if (!block)
    val = (void *)malloc(size);
  else
    val = (void *)realloc(block, size);
  if (!val && size) {
    write(2, "virtual memory exhausted\n", 25);
    exit(1);
  }
  return val;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116d <xrealloc+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    117e <xrealloc+0x3e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <xrealloc+0x74>
cmpq   $0x0,-0x10(%rbp)
je     11b4 <xrealloc+0x74>
mov    $0x2,%edi
lea    0xe60(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     1158 <xrealloc+0x18>
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rbx,%rbx
jne    1165 <xrealloc+0x25>
jmp    116a <xrealloc+0x2a>
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rbx,%rbx
je     116a <xrealloc+0x2a>
test   %rax,%rax
je     116c <xrealloc+0x2c>
pop    %rbx
ret
lea    0xe8d(%rip),%rsi
mov    $0x2,%edi
mov    $0x19,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"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 i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i64, i64* %4
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
br label %17
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = call i64 @realloc(i8* %13, i64 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %5
br label %17
17: ; preds = %12, %8
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = load i64, i64* %4
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @write(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20, %17
%27 = load i8*, i8** %5
ret i8* %27
}

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