char *xrealloc(p, n)
char *p;
unsigned 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    118f <xrealloc+0x2f>
mov    -0x14(%rbp),%edi
mov    $0x0,%al
call   1040 <xmalloc@plt>
cltq
mov    %rax,-0x8(%rbp)
jmp    11f7 <xrealloc+0x97>
cmpl   $0x0,-0x14(%rbp)
jne    11af <xrealloc+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <xrealloc+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ef <xrealloc+0x8f>
xor    %esi,%esi
lea    0xe2b(%rip),%rdx
mov    %esi,%edi
mov    $0x0,%al
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     1178 <xrealloc+0x18>
test   %esi,%esi
je     1185 <xrealloc+0x25>
mov    %esi,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     118e <xrealloc+0x2e>
pop    %rcx
ret
mov    %esi,%edi
xor    %eax,%eax
call   1040 <xmalloc@plt>
cltq
pop    %rcx
ret
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe6b(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
xor    %eax,%eax
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 %13
8: ; preds = %2
%9 = load i32, i32* %5
%10 = call i32 (i32, ...) bitcast (i32 (...)* @xmalloc to i32 (i32, ...)*)(i32 %9)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %3
br label %30
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i8*, i8** %4
store i8* null, i8** %3
br label %30
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = zext i32 %20 to i64
%22 = call i8* @realloc(i8* %19, i64 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @error to i32 (i32, i32, i8*, ...)*)(i32 0, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%27 = call i32 (...) @cleanup()
unreachable
28: ; preds = %18
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %30
30: ; preds = %28, %16, %8
%31 = load i8*, i8** %3
ret i8* %31
}

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