#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int open_file(char *file, int oflag, int *fd) {
  int tryOpen = 0;
  while (((*fd = open(file, oflag)) == -1) && (++tryOpen <= 5)) {
    sleep(5);
  }
  if (*fd == -1) {
    fprintf(stderr, "ERROR: Failed to open %s\n", file);
    return (-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <open@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x25(%rbp)
jne    117e <open_file+0x4e>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmp    $0x5,%eax
setle  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    118e <open_file+0x5e>
jmp    119d <open_file+0x6d>
mov    $0x5,%edi
call   1050 <sleep@plt>
jmp    114a <open_file+0x1a>
mov    -0x20(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11cf <open_file+0x9f>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe42(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <open_file+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
call   1040 <open@plt>
mov    %eax,(%r15)
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
jne    119a <open_file+0x6a>
xor    %ebx,%ebx
mov    $0x5,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <open@plt>
mov    %eax,(%r15)
cmp    $0xffffffff,%eax
jne    1176 <open_file+0x46>
lea    0x1(%rbx),%ecx
cmp    $0x4,%ebx
mov    %ecx,%ebx
jb     1150 <open_file+0x20>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
jne    119a <open_file+0x6a>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"ERROR: Failed to open %s\0A\00"
define i32 @open_file(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 0, i32* %8
br label %9
9: ; preds = %21, %3
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @open(i8* %10, i32 %11)
%13 = load i32*, i32** %7
store i32 %12, i32* %13
%14 = icmp eq i32 %12, -1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = icmp sle i32 %17, 5
br label %19
19: ; preds = %15, %9
%20 = phi i1 [ false, %9 ], [ %18, %15 ]
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = call i32 @sleep(i32 5)
br label %9
23: ; preds = %19
%24 = load i32*, i32** %7
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %5
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 -1, i32* %4
br label %32
31: ; preds = %23
store i32 0, i32* %4
br label %32
32: ; preds = %31, %27
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"ERROR: Failed to open %s\0A\00"
define i32 @open_file(i8* %0, i32 %1, i32* nocapture %2) {
%4 = tail call i32 @open(i8* %0, i32 %1)
store i32 %4, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %18
6: ; preds = %3, %6
%7 = phi i32 [ %8, %6 ], [ 0, %3 ]
%8 = add nuw nsw i32 %7, 1
%9 = tail call i32 @sleep(i32 5)
%10 = tail call i32 @open(i8* %0, i32 %1)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
%12 = icmp ult i32 %7, 4
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %6, label %14
14: ; preds = %6
br i1 %11, label %15, label %18
15: ; preds = %14
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %3, %14, %15
%19 = phi i32 [ -1, %15 ], [ 0, %14 ], [ 0, %3 ]
ret i32 %19
}