#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*/ O_RDONLY;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char buf[80];
  int fd = open(argv[1], O_RDONLY);
  int rc;
  if (fd < 0) {
    fprintf(stderr, "ERROR failed to open %s\n", argv[1]);
    return errno;
  }
  while ((rc = read(fd, buf, 80)) >= 0) {
    if (rc == 0) {
      printf("return 0\n");
      sleep(1);
    } else {
      printf("%d\n", rc);
    }
  }
  printf("ERROR: rc %d errno %d\n", rc, errno);
  perror("xxx");
  return errno;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11c7 <main+0x67>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1258 <main+0xf8>
jmp    11cc <main+0x6c>
mov    -0x64(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x50,%edx
call   1040 <read@plt>
mov    %eax,-0x68(%rbp)
cmp    $0x0,%eax
jl     1226 <main+0xc6>
cmpl   $0x0,-0x68(%rbp)
jne    1210 <main+0xb0>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1221 <main+0xc1>
mov    -0x68(%rbp),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cc <main+0x6c>
mov    -0x68(%rbp),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf3(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    11c2 <main+0x52>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe58(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x50,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1227 <main+0xb7>
lea    0xe56(%rip),%r14
mov    %rsp,%r15
lea    0xe44(%rip),%r12
jmp    120f <main+0x9f>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x50,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1227 <main+0xb7>
test   %eax,%eax
jne    11f0 <main+0x80>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11fc <main+0x8c>
mov    0x2da2(%rip),%rcx
mov    (%rcx),%edx
lea    0xde2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
jmp    11ad <main+0x3d>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"ERROR failed to open %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"return 0\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [23 x i8] c"ERROR: rc %d errno %d\0A\00"
@.str.4 = [4 x i8] c"xxx\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @errno
store i32 %22, i32* %3
br label %45
23: ; preds = %2
br label %24
24: ; preds = %38, %23
%25 = load i32, i32* %7
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 80)
store i32 %27, i32* %8
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @sleep(i32 1)
br label %38
35: ; preds = %29
%36 = load i32, i32* %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %32
br label %24
39: ; preds = %24
%40 = load i32, i32* %8
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %41)
%43 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* @errno
store i32 %44, i32* %3
br label %45
45: ; preds = %39, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"ERROR failed to open %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [23 x i8] c"ERROR: rc %d errno %d\0A\00"
@.str.4 = [4 x i8] c"xxx\00"
@str = [9 x i8] c"return 0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = call i32 @read(i32 %8, i8* %4, i32 80)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %17, label %28
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %33
17: ; preds = %10, %25
%18 = phi i32 [ %26, %25 ], [ %11, %10 ]
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @sleep(i32 1)
br label %25
23: ; preds = %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %25
25: ; preds = %23, %20
%26 = call i32 @read(i32 %8, i8* %4, i32 80)
%27 = icmp sgt i32 %26, -1
br i1 %27, label %17, label %28
28: ; preds = %25, %10
%29 = phi i32 [ %11, %10 ], [ %26, %25 ]
%30 = load i32, i32* @errno
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30)
%32 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %28, %13
%34 = load i32, i32* @errno
ret i32 %34
}