#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*/ errorf(char *);
void *realloc(void *, size_t);
int /*<<< orphan*/ sleep(int);
void *xrealloc(const void *p, size_t size) {
  void *q;
  while ((q = realloc((void *)p, size)) == NULL && size != 0) {
    errorf("out of memory");
    sleep(1);
  }
  return q;
}


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),%rsi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jne    116e <xrealloc+0x3e>
cmpq   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117e <xrealloc+0x4e>
jmp    1199 <xrealloc+0x69>
lea    0xe7b(%rip),%rdi
call   1030 <errorf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1140 <xrealloc+0x10>
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   1040 <realloc@plt>
test   %rbx,%rbx
je     1182 <xrealloc+0x52>
test   %rax,%rax
jne    1182 <xrealloc+0x52>
lea    0xeaf(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <errorf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <realloc@plt>
test   %rax,%rax
je     1160 <xrealloc+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

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

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