#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*/ die(char *);
void *realloc(void *, int);
void *xrealloc(void *ptr, size_t size) {
  void *ret;
  ret = realloc(ptr, size);
  if (!ret && !size)
    ret = realloc(ptr, 1);
  if (!ret) {
    die("Out of memory, realloc failed");
  }
  return ret;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116b <xrealloc+0x4b>
cmpq   $0x0,-0x10(%rbp)
jne    116b <xrealloc+0x4b>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1030 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1182 <xrealloc+0x62>
lea    0xe83(%rip),%rdi
call   1040 <die@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <realloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
jne    114d <xrealloc+0x2d>
test   %r15,%r15
jne    114d <xrealloc+0x2d>
mov    %r14,%rdi
mov    $0x1,%esi
call   1030 <realloc@plt>
mov    %rax,%r15
test   %r15,%r15
jne    115e <xrealloc+0x3e>
lea    0xea7(%rip),%rdi
call   1040 <die@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [30 x i8] c"Out of memory, realloc failed\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = trunc i64 %1 to i32
%4 = tail call i8* @realloc(i8* %0, i32 %3)
%5 = icmp ne i8* %4, null
%6 = icmp ne i64 %1, 0
%7 = or i1 %6, %5
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i8* @realloc(i8* %0, i32 1)
br label %10
10: ; preds = %8, %2
%11 = phi i8* [ %4, %2 ], [ %9, %8 ]
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
ret i8* %11
}