#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int O_APPEND;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ myerr(char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  ssize_t ret;
  char buf[16];
  int len = 16;
  if (argc != 2) {
    myerr("usage: ./a.out filename");
  }
  fd = open(argv[1], O_RDWR | O_APPEND);
  if (fd == -1) {
    myerr("open error");
  }
  printf("fd = %d\n", fd);
  while ((ret = read(fd, buf, len)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      perror("read");
      break;
    }
    write(1, buf, ret);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x10,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a3 <main+0x33>
lea    0xe62(%rip),%rdi
call   1070 <myerr@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11db <main+0x6b>
lea    0xe42(%rip),%rdi
call   1070 <myerr@plt>
mov    -0x14(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1255 <main+0xe5>
cmpl   $0xffffffff,-0x18(%rbp)
jne    123f <main+0xcf>
mov    0x2dc2(%rip),%rax
mov    (%rax),%rax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <main+0xbe>
jmp    11ea <main+0x7a>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
jmp    1255 <main+0xe5>
lea    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11ea <main+0x7a>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     118e <main+0x1e>
lea    0xe77(%rip),%rdi
call   1070 <myerr@plt>
mov    0x8(%rbx),%rdi
mov    0x2e4f(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11bc <main+0x4c>
lea    0xe61(%rip),%rdi
call   1070 <myerr@plt>
lea    0xe60(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rsp,%rbx
mov    0x2e04(%rip),%r14
mov    0x2df5(%rip),%r15
jmp    11e8 <main+0x78>
nopl   (%rax)
mov    (%r14),%rax
cmp    (%r15),%rax
jne    1211 <main+0xa1>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x70>
test   %eax,%eax
je     121d <main+0xad>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11e8 <main+0x78>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"usage: ./a.out filename\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [9 x i8] c"fd = %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 16, i32* %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @myerr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_APPEND
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @myerr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %14
%27 = load i32, i32* %6
%28 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %45, %42, %26
%30 = load i32, i32* %6
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* %9
%33 = call i32 @read(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %29
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
br label %29
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %49
45: ; preds = %35
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%47 = load i32, i32* %7
%48 = call i32 @write(i32 1, i8* %46, i32 %47)
br label %29
49: ; preds = %43, %29
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"usage: ./a.out filename\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [9 x i8] c"fd = %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @myerr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_APPEND
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @myerr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
%19 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %20
20: ; preds = %30, %18
%21 = call i32 @read(i32 %14, i8* %4, i32 16)
switch i32 %21, label %28 [
i32 0, label %31
i32 -1, label %22
]
22: ; preds = %20
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %31
28: ; preds = %20
%29 = call i32 @write(i32 1, i8* %4, i32 %21)
br label %30
30: ; preds = %28, %22
br label %20
31: ; preds = %20, %26
%32 = tail call i32 @close(i32 %14)
ret i32 0
}