#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int i;
  char buf[4096];
  int fd;
  int ret;
  int fail = 0;
  for (i = 1; i < argc; i++) {
    fd = open(argv[i], O_RDONLY);
    if (fd < 0) {
      fail = 1;
      continue;
    }
    while (1) {
      ret = read(fd, buf, sizeof buf);
      if (ret == 0)
        break;
      if (ret < 0) {
        if (errno == EAGAIN || errno == EINTR)
          continue;
        fail = 1;
        break;
      }
    }
    close(fd);
  }
  return fail;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x102c(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1249 <main+0x119>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    11a2 <main+0x72>
movl   $0x1,-0x102c(%rbp)
jmp    123b <main+0x10b>
jmp    11a7 <main+0x77>
mov    -0x1024(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jne    11d6 <main+0xa6>
jmp    1230 <main+0x100>
cmpl   $0x0,-0x1028(%rbp)
jge    122b <main+0xfb>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <main+0xe7>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <main+0xec>
jmp    11a7 <main+0x77>
movl   $0x1,-0x102c(%rbp)
jmp    1230 <main+0x100>
jmp    11a7 <main+0x77>
mov    -0x1024(%rbp),%edi
call   1030 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <main+0x2a>
mov    -0x102c(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,0x8(%rsp)
movl   $0x0,0x4(%rsp)
cmp    $0x2,%edi
jl     11fa <main+0xca>
mov    %edi,%r12d
movl   $0x0,0x4(%rsp)
mov    $0x1,%ebp
lea    0x10(%rsp),%r13
mov    0x2e65(%rip),%r14
mov    0x2e46(%rip),%r15
jmp    1190 <main+0x60>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%rbp
cmp    %r12,%rbp
je     11fa <main+0xca>
mov    0x8(%rsp),%rax
mov    (%rax,%rbp,8),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11f0 <main+0xc0>
mov    %eax,%ebx
nopl   (%rax)
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1180 <main+0x50>
test   %eax,%eax
jns    11b0 <main+0x80>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11b0 <main+0x80>
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <main+0x80>
movl   $0x1,0x4(%rsp)
jmp    1180 <main+0x50>
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x1,0x4(%rsp)
jmp    1187 <main+0x57>
mov    0x4(%rsp),%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
store i32 1, i32* %6
br label %11
11: ; preds = %51, %2
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %54
15: ; preds = %11
%16 = load i8**, i8*** %5
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 1, i32* %10
br label %51
26: ; preds = %15
br label %27
27: ; preds = %26, %45, %47
%28 = load i32, i32* %8
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 4096)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %48
34: ; preds = %27
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EAGAIN
%40 = icmp eq i64 %38, %39
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41, %37
br label %27
46: ; preds = %41
store i32 1, i32* %10
br label %48
47: ; preds = %34
br label %27
48: ; preds = %46, %33
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %25
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %11
54: ; preds = %11
%55 = load i32, i32* %10
ret i32 %55
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %39
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %35
%9 = phi i64 [ 1, %6 ], [ %37, %35 ]
%10 = phi i32 [ 0, %6 ], [ %36, %35 ]
%11 = getelementptr inbounds i8*, i8** %1, i64 %9
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %35, label %16
16: ; preds = %8
%17 = call i32 @read(i32 %14, i8* %4, i32 4096)
%18 = icmp eq i32 %17, 0
br i1 %18, label %32, label %19
19: ; preds = %16, %22
%20 = phi i32 [ %23, %22 ], [ %17, %16 ]
%21 = icmp slt i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19, %25
%23 = call i32 @read(i32 %14, i8* %4, i32 4096)
%24 = icmp eq i32 %23, 0
br i1 %24, label %32, label %19
25: ; preds = %19
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %26, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %22, label %32
32: ; preds = %22, %25, %16
%33 = phi i32 [ %10, %16 ], [ 1, %25 ], [ %10, %22 ]
%34 = tail call i32 @close(i32 %14)
br label %35
35: ; preds = %8, %32
%36 = phi i32 [ %33, %32 ], [ 1, %8 ]
%37 = add nuw nsw i64 %9, 1
%38 = icmp eq i64 %37, %7
br i1 %38, label %39, label %8
39: ; preds = %35, %2
%40 = phi i32 [ 0, %2 ], [ %36, %35 ]
ret i32 %40
}