#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 */
typedef int /*<<< orphan*/ u_long;

/* Variables and functions */
int /*<<< orphan*/ fatal(char *, ...);
void *malloc(size_t);
void *realloc(void *, size_t);
void *xrealloc(void *ptr, size_t new_size) {
  void *new_ptr;
  if (!new_size)
    fatal("xrealloc: zero size");
  if (!ptr)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (!new_ptr)
    fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long)new_size);
  return new_ptr;
}


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    1159 <xrealloc+0x29>
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1050 <fatal@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1176 <xrealloc+0x46>
mov    -0x10(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    1187 <xrealloc+0x57>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <xrealloc+0x76>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <fatal@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     1151 <xrealloc+0x21>
test   %rbx,%rbx
je     1164 <xrealloc+0x34>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <realloc@plt>
jmp    116c <xrealloc+0x3c>
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1050 <fatal@plt>
test   %rbx,%rbx
jne    1144 <xrealloc+0x14>
mov    %r14,%rdi
call   1030 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1185 <xrealloc+0x55>
lea    0xe99(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <fatal@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"xrealloc: zero size\00"
@.str.1 = [45 x i8] c"xrealloc: out of memory (new_size %lu bytes)\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 ne i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i64, i64* %4
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %5
br label %20
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = load i64, i64* %4
%19 = call i8* @realloc(i8* %17, i64 %18)
store i8* %19, i8** %5
br label %20
20: ; preds = %16, %13
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i64, i64* %4
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %20
%28 = load i8*, i8** %5
ret i8* %28
}

LLVM IR (-O2):
@.str = [20 x i8] c"xrealloc: zero size\00"
@.str.1 = [45 x i8] c"xrealloc: out of memory (new_size %lu bytes)\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i8* @malloc(i64 %1)
br label %12
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i64 %1)
br label %12
12: ; preds = %10, %8
%13 = phi i8* [ %11, %10 ], [ %9, %8 ]
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = trunc i64 %1 to i32
%17 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %12
ret i8* %13
}