#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 EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int wait_for_file(const char *fname) {
  int fd, ret;
  char buf;
  fd = open(fname, O_RDONLY);
  if (fd == -1)
    return -errno;
  do {
    ret = read(fd, &buf, 1);
  } while (ret == -1 && errno == EINTR);
  close(fd);
  return ret == -1 ? -errno : 0;
}


x86 assembly (clang, -O0):
<wait_for_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    116e <wait_for_file+0x3e>
xor    %eax,%eax
mov    0x2e74(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <wait_for_file+0xbe>
jmp    1173 <wait_for_file+0x43>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x1a(%rbp)
jne    11ae <wait_for_file+0x7e>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    1173 <wait_for_file+0x43>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11de <wait_for_file+0xae>
xor    %eax,%eax
mov    0x2e04(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    11e8 <wait_for_file+0xb8>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    11e8 <wait_for_file+0xb8>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a5 <wait_for_file+0x75>
mov    %eax,%ebx
lea    0xf(%rsp),%r14
mov    0x2e7b(%rip),%r15
mov    0x2e6c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    118f <wait_for_file+0x5f>
mov    (%r12),%eax
cmp    %eax,(%r15)
je     1170 <wait_for_file+0x40>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
sub    (%r15),%eax
xor    %ecx,%ecx
cmp    $0xffffffff,%ebp
cmovne %ecx,%eax
jmp    11b0 <wait_for_file+0x80>
mov    0x2e2c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @wait_for_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %39
15: ; preds = %1
br label %16
16: ; preds = %26, %15
%17 = load i32, i32* %4
%18 = call i32 @read(i32 %17, i8* %6, i32 1)
store i32 %18, i32* %5
br label %19
19: ; preds = %16
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br label %26
26: ; preds = %22, %19
%27 = phi i1 [ false, %19 ], [ %25, %22 ]
br i1 %27, label %16, label %28
28: ; preds = %26
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
br label %37
36: ; preds = %28
br label %37
37: ; preds = %36, %33
%38 = phi i32 [ %35, %33 ], [ 0, %36 ]
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %12
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @wait_for_file(i8* nocapture %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %21
9: ; preds = %1, %9
%10 = call i32 @read(i32 %4, i8* %2, i32 1)
%11 = icmp eq i32 %10, -1
%12 = load i32, i32* @errno
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %9
%17 = tail call i32 @close(i32 %4)
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
%20 = select i1 %11, i32 %19, i32 0
br label %21
21: ; preds = %16, %6
%22 = phi i32 [ %8, %6 ], [ %20, %16 ]
ret i32 %22
}