#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*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
void *realloc(void *, size_t);
void *xmalloc(size_t);
void *xrealloc(void *p, size_t n) {
  void *result;
  if (p == 0)
    return xmalloc(n);
  if (n == 0)
    return NULL;
  result = realloc(p, n);
  if (result == NULL)
    error(EXIT_FAILURE, errno, "realloc");
  return result;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <xrealloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1030 <xmalloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11b7 <xrealloc+0x87>
cmpq   $0x0,-0x18(%rbp)
jne    1175 <xrealloc+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <xrealloc+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <xrealloc+0x7f>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdx
call   1050 <error@plt>
mov    -0x20(%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     1147 <xrealloc+0x17>
push   %rax
test   %rsi,%rsi
je     116d <xrealloc+0x3d>
call   1040 <realloc@plt>
test   %rax,%rax
je     114f <xrealloc+0x1f>
pop    %rcx
ret
mov    %rsi,%rdi
jmp    1030 <xmalloc@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdx
call   1050 <error@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [8 x i8] c"realloc\00"
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 i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = call i8* @xmalloc(i64 %10)
store i8* %11, i8** %3
br label %28
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i8* null, i8** %3
br label %28
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = call i8* @realloc(i8* %17, i64 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i32, i32* @EXIT_FAILURE
%24 = load i32, i32* @errno
%25 = call i32 @error(i32 %23, i32 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %22, %16
%27 = load i8*, i8** %6
store i8* %27, i8** %3
br label %28
28: ; preds = %26, %15, %9
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [8 x i8] c"realloc\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 i8* @xmalloc(i64 %1)
br label %15
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = tail call i8* @realloc(i8* %0, i64 %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @EXIT_FAILURE
%13 = load i32, i32* @errno
%14 = tail call i32 @error(i32 %12, i32 %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %8, %11, %6, %4
%16 = phi i8* [ %5, %4 ], [ null, %6 ], [ null, %11 ], [ %9, %8 ]
ret i8* %16
}