#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*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(char *, unsigned int);
char *xmalloc(unsigned int);
char *xrealloc(char *p, unsigned n) {
  if (p == 0)
    return xmalloc(n);
  if (n == 0) {
    free(p);
    return 0;
  }
  p = (char *)realloc(p, n);
  if (p == 0)
    error(2, 0, "virtual memory exhausted");
  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    116b <xrealloc+0x2b>
mov    -0x14(%rbp),%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11c1 <xrealloc+0x81>
cmpl   $0x0,-0x14(%rbp)
jne    118b <xrealloc+0x4b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <xrealloc+0x81>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <xrealloc+0x79>
mov    $0x2,%edi
xor    %esi,%esi
lea    0xe4c(%rip),%rdx
call   1060 <error@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
test   %rdi,%rdi
je     116c <xrealloc+0x2c>
push   %rbx
test   %esi,%esi
je     1173 <xrealloc+0x33>
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    117a <xrealloc+0x3a>
lea    0xea2(%rip),%rdx
mov    $0x2,%edi
xor    %esi,%esi
call   1060 <error@plt>
jmp    117a <xrealloc+0x3a>
mov    %esi,%edi
jmp    1040 <xmalloc@plt>
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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 %28
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 %28
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i64 @realloc(i8* %18, i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @error(i32 2, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = load i8*, i8** %4
store i8* %27, i8** %3
br label %28
28: ; preds = %26, %14, %8
%29 = load i8*, i8** %3
ret i8* %29
}

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 %16
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 %16
10: ; preds = %6
%11 = tail call i64 @realloc(i8* %0, i32 %1)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @error(i32 2, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %10, %14, %8, %4
%17 = phi i8* [ %5, %4 ], [ null, %8 ], [ %12, %14 ], [ %12, %10 ]
ret i8* %17
}