#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);
int /*<<< orphan*/ release_pack_memory(size_t, int);
void *xrealloc(void *ptr, size_t size) {
  void *ret = realloc(ptr, size);
  if (!ret && !size)
    ret = realloc(ptr, 1);
  if (!ret) {
    release_pack_memory(size, -1);
    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   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117b <xrealloc+0x4b>
cmpq   $0x0,-0x10(%rbp)
jne    117b <xrealloc+0x4b>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11eb <xrealloc+0xbb>
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
call   1030 <release_pack_memory@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <xrealloc+0x9f>
cmpq   $0x0,-0x10(%rbp)
jne    11cf <xrealloc+0x9f>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e6 <xrealloc+0xb6>
lea    0xe1f(%rip),%rdi
call   1050 <die@plt>
jmp    11eb <xrealloc+0xbb>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <realloc@plt>
test   %rbx,%rbx
jne    1156 <xrealloc+0x26>
test   %rax,%rax
jne    1156 <xrealloc+0x26>
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
test   %rax,%rax
jne    119c <xrealloc+0x6c>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1030 <release_pack_memory@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <realloc@plt>
test   %rbx,%rbx
jne    1189 <xrealloc+0x59>
test   %rax,%rax
jne    1189 <xrealloc+0x59>
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
test   %rax,%rax
jne    119c <xrealloc+0x6c>
lea    0xe6b(%rip),%rdi
call   1050 <die@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 %42, label %21
21: ; preds = %18
%22 = load i64, i64* %4
%23 = call i32 @release_pack_memory(i64 %22, i32 -1)
%24 = load i8*, i8** %3
%25 = load i64, i64* %4
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %24, i32 %26)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %36, label %30
30: ; preds = %21
%31 = load i64, i64* %4
%32 = icmp ne i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i8*, i8** %3
%35 = call i8* @realloc(i8* %34, i32 1)
store i8* %35, i8** %5
br label %36
36: ; preds = %33, %30, %21
%37 = load i8*, i8** %5
%38 = icmp ne i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
br label %42
42: ; preds = %41, %18
%43 = load i8*, i8** %5
ret i8* %43
}

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 %25
13: ; preds = %10
%14 = tail call i32 @release_pack_memory(i64 %1, i32 -1)
%15 = tail call i8* @realloc(i8* %0, i32 %3)
%16 = icmp ne i8* %15, null
%17 = or i1 %6, %16
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = tail call i8* @realloc(i8* %0, i32 1)
br label %20
20: ; preds = %18, %13
%21 = phi i8* [ %15, %13 ], [ %19, %18 ]
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %20, %23, %10
%26 = phi i8* [ %11, %10 ], [ %21, %20 ], [ null, %23 ]
ret i8* %26
}