#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*/ fatal_error(char *, size_t);
void *malloc(int);
void *realloc(void *, size_t);
void *xrealloc(void *ptr, size_t size) {
  void *p = realloc(ptr, size);
  if (p)
    return p;
  if (!size) {
    p = malloc(1);
    if (p)
      return p;
  }
  fatal_error("Out of memory: tried to reallocate %zu bytes", size);
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1169 <xrealloc+0x39>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <xrealloc+0x7f>
cmpq   $0x0,-0x18(%rbp)
jne    119f <xrealloc+0x6f>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119a <xrealloc+0x6a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <xrealloc+0x7f>
jmp    119f <xrealloc+0x6f>
mov    -0x18(%rbp),%rsi
lea    0xe56(%rip),%rdi
call   1030 <fatal_error@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
mov    %rsi,%rbx
call   1050 <realloc@plt>
test   %rax,%rax
je     1140 <xrealloc+0x10>
pop    %rbx
ret
test   %rbx,%rbx
jne    1154 <xrealloc+0x24>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
jne    113e <xrealloc+0xe>
lea    0xea5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <fatal_error@plt>
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [45 x i8] c"Out of memory: tried to reallocate %zu bytes\00"
define noalias i8* @xrealloc(i8* nocapture %0, i64 %1) {
%3 = tail call i8* @realloc(i8* %0, i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7, %5
%11 = tail call i32 @fatal_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %12
12: ; preds = %7, %2, %10
%13 = phi i8* [ undef, %10 ], [ %3, %2 ], [ %8, %7 ]
ret i8* %13
}