#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 atoi(char const *);
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int read(int, char **, int);
int strcmp(char const *, char *);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, ...);
int main(int argc, const char *argv[]) {
  if (argc < 3)
    return -1;
  int test_write = strcmp(argv[2], "read");
  int nbytes = argc > 3 ? atoi(argv[3]) : 0;
  char buf[1] = {'1'};
  int fd;
  if (test_write) {
    fd = open(argv[1], 0x0002 | 0x0200 | 0x0400, 0000700 | 0000070 | 0000007);
  } else {
    fd = open(argv[1], 0x0000, 0);
  }
  if (fd < 0) {
    write(1, argv[1], strlen(argv[1]));
    const char *err_msg = ":No such file or directory\n";
    write(1, err_msg, strlen(err_msg));
    return -1;
  }
  if (test_write) {
    for (int i = 0; i < nbytes; i++) {
      write(fd, &buf, 1);
    }
  } else {
    while (read(fd, &buf, 1) == 1) {
    };
  }
  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)
cmpl   $0x3,-0x8(%rbp)
jge    119c <main+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x1a2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe55(%rip),%rsi
call   1070 <strcmp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    11d2 <main+0x62>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    11dc <main+0x6c>
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
jmp    11dc <main+0x6c>
mov    -0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0xe39(%rip),%al
mov    %al,-0x19(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1214 <main+0xa4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x602,%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
jmp    1228 <main+0xb8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1297 <main+0x127>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write@plt>
lea    0xda1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x1a2>
cmpl   $0x0,-0x14(%rbp)
je     12da <main+0x16a>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12d5 <main+0x165>
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12a8 <main+0x138>
jmp    1303 <main+0x193>
jmp    12df <main+0x16f>
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    12fe <main+0x18e>
jmp    12df <main+0x16f>
jmp    1303 <main+0x193>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
mov    $0xffffffff,%eax
cmp    $0x3,%edi
jl     127b <main+0x10b>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x10(%rsi),%rdi
lea    0xe6b(%rip),%rsi
call   1070 <strcmp@plt>
mov    %eax,%r15d
xor    %ebx,%ebx
cmp    $0x3,%ebp
je     11af <main+0x3f>
mov    0x18(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
movb   $0x31,0x7(%rsp)
mov    0x8(%r14),%rdi
test   %r15d,%r15d
je     11f8 <main+0x88>
mov    $0x602,%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
test   %eax,%eax
js     122f <main+0xbf>
mov    %eax,%ebp
test   %ebx,%ebx
jle    1224 <main+0xb4>
lea    0x7(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x70>
jmp    1224 <main+0xb4>
xor    %esi,%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     122f <main+0xbf>
mov    %eax,%ebp
lea    0x7(%rsp),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     1210 <main+0xa0>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1271 <main+0x101>
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0xdb2(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@__const.main.buf = [1 x i8] c"1"
@.str.1 = [28 x i8] c":No such file or directory\0A\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 [1 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %85
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @strcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %6
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 3
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 3
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
br label %28
27: ; preds = %15
br label %28
28: ; preds = %27, %22
%29 = phi i32 [ %26, %22 ], [ 0, %27 ]
store i32 %29, i32* %7
%30 = bitcast [1 x i8]* %8 to i8*
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @open(i8* %36, i32 1538, i32 511)
store i32 %37, i32* %9
br label %43
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @open(i8* %41, i32 0, i32 0)
store i32 %42, i32* %9
br label %43
43: ; preds = %38, %33
%44 = load i32, i32* %9
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %43
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @strlen(i8* %52)
%54 = call i32 (i32, ...) @write(i32 1, i8* %49, i32 %53)
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8** %10
%55 = load i8*, i8** %10
%56 = load i8*, i8** %10
%57 = call i32 @strlen(i8* %56)
%58 = call i32 (i32, ...) @write(i32 1, i8* %55, i32 %57)
store i32 -1, i32* %3
br label %85
59: ; preds = %43
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %74
62: ; preds = %59
store i32 0, i32* %11
br label %63
63: ; preds = %70, %62
%64 = load i32, i32* %11
%65 = load i32, i32* %7
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %73
67: ; preds = %63
%68 = load i32, i32* %9
%69 = call i32 (i32, ...) @write(i32 %68, [1 x i8]* %8, i32 1)
br label %70
70: ; preds = %67
%71 = load i32, i32* %11
%72 = add nsw i32 %71, 1
store i32 %72, i32* %11
br label %63
73: ; preds = %63
br label %82
74: ; preds = %59
br label %75
75: ; preds = %80, %74
%76 = load i32, i32* %9
%77 = bitcast [1 x i8]* %8 to i8**
%78 = call i32 @read(i32 %76, i8** %77, i32 1)
%79 = icmp eq i32 %78, 1
br i1 %79, label %80, label %81
80: ; preds = %75
br label %75
81: ; preds = %75
br label %82
82: ; preds = %81, %73
%83 = load i32, i32* %9
%84 = call i32 @close(i32 %83)
store i32 0, i32* %3
br label %85
85: ; preds = %82, %46, %14
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [28 x i8] c":No such file or directory\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x i8]
%4 = icmp slt i32 %0, 3
br i1 %4, label %50, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 2
%7 = load i8*, i8** %6
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %0, 3
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8*, i8** %1, i64 3
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %5, %10
%15 = phi i32 [ %13, %10 ], [ 0, %5 ]
%16 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 49, i8* %16
%17 = icmp eq i32 %8, 0
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
br i1 %17, label %20, label %23
20: ; preds = %14
%21 = tail call i32 @open(i8* %19, i32 0, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %26, label %35
23: ; preds = %14
%24 = tail call i32 @open(i8* %19, i32 1538, i32 511)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23, %20
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @strlen(i8* %28)
%30 = tail call i32 (i32, ...) @write(i32 1, i8* %28, i32 %29)
%31 = tail call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %48
33: ; preds = %23
%34 = icmp sgt i32 %15, 0
br i1 %34, label %37, label %45
35: ; preds = %20
%36 = bitcast [1 x i8]* %3 to i8**
br label %42
37: ; preds = %33, %37
%38 = phi i32 [ %40, %37 ], [ 0, %33 ]
%39 = call i32 (i32, ...) @write(i32 %24, [1 x i8]* %3, i32 1)
%40 = add nuw nsw i32 %38, 1
%41 = icmp eq i32 %40, %15
br i1 %41, label %45, label %37
42: ; preds = %35, %42
%43 = call i32 @read(i32 %21, i8** %36, i32 1)
%44 = icmp eq i32 %43, 1
br i1 %44, label %42, label %45
45: ; preds = %37, %42, %33
%46 = phi i32 [ %24, %33 ], [ %21, %42 ], [ %24, %37 ]
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %26
%49 = phi i32 [ -1, %26 ], [ 0, %45 ]
br label %50
50: ; preds = %2, %48
%51 = phi i32 [ %49, %48 ], [ -1, %2 ]
ret i32 %51
}