#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 */
struct timespec {
  unsigned long tv_nsec;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
void *malloc(size_t);
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ stderr;
void *xmalloc(size_t size) {
  for (int tries = 5; tries > 0; --tries) {
    void *ptr = malloc(size);
    if (ptr != NULL)
      return ptr;
    static const struct timespec ts = {.tv_nsec = 100UL * 1000UL * 1000UL};
    struct timespec remaining = ts;
    while (nanosleep(&remaining, &remaining) == EINTR)
      ;
  }
  fprintf(stderr, "Failed to allocate %zu bytes.\n", size);
  return NULL;
}


x86 assembly (clang, -O0):
<xmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b1 <xmalloc+0x81>
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1172 <xmalloc+0x42>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d2 <xmalloc+0xa2>
mov    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    119e <xmalloc+0x6e>
jmp    117d <xmalloc+0x4d>
jmp    11a3 <xmalloc+0x73>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1143 <xmalloc+0x13>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmalloc>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1050 <malloc@plt>
test   %rax,%rax
jne    124a <xmalloc+0x11a>
movq   $0x5f5e100,0x8(%rsp)
f5 05
lea    0x8(%rsp),%rbx
mov    0x2e79(%rip),%r15
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r15),%rax
je     1160 <xmalloc+0x30>
mov    %r14,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    124a <xmalloc+0x11a>
movq   $0x5f5e100,0x8(%rsp)
f5 05
lea    0x8(%rsp),%rbx
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r15),%rax
je     1190 <xmalloc+0x60>
mov    %r14,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    124a <xmalloc+0x11a>
movq   $0x5f5e100,0x8(%rsp)
f5 05
lea    0x8(%rsp),%rbx
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r15),%rax
je     11c0 <xmalloc+0x90>
mov    %r14,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    124a <xmalloc+0x11a>
movq   $0x5f5e100,0x8(%rsp)
f5 05
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r15),%rax
je     11f0 <xmalloc+0xc0>
mov    %r14,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    124a <xmalloc+0x11a>
movq   $0x5f5e100,0x8(%rsp)
f5 05
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r15),%rax
je     1220 <xmalloc+0xf0>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
@xmalloc.ts = internal constant %struct.timespec { i64 100000000 }
@EINTR = global i64 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to allocate %zu bytes.\0A\00"
define i8* @xmalloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.timespec
store i64 %0, i64* %3
store i32 5, i32* %4
br label %7
7: ; preds = %25, %1
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = load i64, i64* %3
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i8*, i8** %5
store i8* %16, i8** %2
br label %32
17: ; preds = %10
%18 = bitcast %struct.timespec* %6 to i8*
br label %19
19: ; preds = %23, %17
%20 = call i64 @nanosleep(%struct.timespec* %6, %struct.timespec* %6)
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %19
24: ; preds = %19
br label %25
25: ; preds = %24
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
br label %7
28: ; preds = %7
%29 = load i32, i32* @stderr
%30 = load i64, i64* %3
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %30)
store i8* null, i8** %2
br label %32
32: ; preds = %28, %15
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@EINTR = global i64 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to allocate %zu bytes.\0A\00"
define noalias i8* @xmalloc(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%5 = call i8* @malloc(i64 %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %42, label %47
7: ; preds = %43
%8 = call i8* @malloc(i64 %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %47
10: ; preds = %7
store i64 100000000, i64* %4
br label %11
11: ; preds = %11, %10
%12 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %11, label %15
15: ; preds = %11
%16 = call i8* @malloc(i64 %0)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %47
18: ; preds = %15
store i64 100000000, i64* %4
br label %19
19: ; preds = %19, %18
%20 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %19, label %23
23: ; preds = %19
%24 = call i8* @malloc(i64 %0)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %47
26: ; preds = %23
store i64 100000000, i64* %4
br label %27
27: ; preds = %27, %26
%28 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %27, label %31
31: ; preds = %27
%32 = call i8* @malloc(i64 %0)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %47
34: ; preds = %31
store i64 100000000, i64* %4
br label %35
35: ; preds = %35, %34
%36 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %35, label %39
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %0)
br label %47
42: ; preds = %1
store i64 100000000, i64* %4
br label %43
43: ; preds = %43, %42
%44 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%45 = load i64, i64* @EINTR
%46 = icmp eq i64 %44, %45
br i1 %46, label %43, label %7
47: ; preds = %1, %7, %15, %23, %31, %39
%48 = phi i8* [ null, %39 ], [ %5, %1 ], [ %8, %7 ], [ %16, %15 ], [ %24, %23 ], [ %32, %31 ]
ret i8* %48
}