#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 /*<<< orphan*/ uint8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ ece391_fdputs(int, int /*<<< orphan*/ *);
scalar_t__ ece391_getargs(int /*<<< orphan*/ *, int);
int ece391_open(int /*<<< orphan*/ *);
int ece391_read(int, int /*<<< orphan*/ *, int);
int ece391_write(int, int /*<<< orphan*/ *, int);
int main() {
  int32_t fd, cnt;
  uint8_t buf[1024];
  if (0 != ece391_getargs(buf, 1024)) {
    ece391_fdputs(1, (uint8_t *)"could not read arguments\n");
    return 3;
  }
  if (-1 == (fd = ece391_open(buf))) {
    ece391_fdputs(1, (uint8_t *)"file not found\n");
    return 2;
  }
  while (0 != (cnt = ece391_read(fd, buf, 1024))) {
    if (-1 == cnt) {
      ece391_fdputs(1, (uint8_t *)"file read failed\n");
      return 3;
    }
    if (-1 == ece391_write(1, buf, cnt))
      return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
mov    $0x400,%esi
call   1040 <ece391_getargs@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     119e <main+0x4e>
mov    $0x1,%edi
lea    0xe73(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x3,-0x4(%rbp)
jmp    1267 <main+0x117>
lea    -0x1010(%rbp),%rdi
call   1070 <ece391_open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d9 <main+0x89>
mov    $0x1,%edi
lea    0xe52(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x2,-0x4(%rbp)
jmp    1267 <main+0x117>
jmp    11de <main+0x8e>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x400,%edx
call   1050 <ece391_read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     1260 <main+0x110>
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    122c <main+0xdc>
mov    $0x1,%edi
lea    0xe0f(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x3,-0x4(%rbp)
jmp    1267 <main+0x117>
lea    -0x1010(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1060 <ece391_write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    125b <main+0x10b>
movl   $0x3,-0x4(%rbp)
jmp    1267 <main+0x117>
jmp    11de <main+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1040 <ece391_getargs@plt>
mov    $0x3,%ebp
test   %rax,%rax
je     1191 <main+0x41>
lea    0xe87(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsp,%rdi
call   1070 <ece391_open@plt>
cmp    $0xffffffff,%eax
je     11e3 <main+0x93>
mov    %eax,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <ece391_read@plt>
cmp    $0xffffffff,%eax
je     11f1 <main+0xa1>
test   %eax,%eax
je     1185 <main+0x35>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <ece391_write@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x60>
mov    $0x3,%eax
jmp    1185 <main+0x35>
mov    $0x2,%ebp
lea    0xe2b(%rip),%rsi
jmp    1179 <main+0x29>
lea    0xe32(%rip),%rsi
jmp    1179 <main+0x29>

LLVM IR (-O0):
@.str = [26 x i8] c"could not read arguments\0A\00"
@.str.1 = [16 x i8] c"file not found\0A\00"
@.str.2 = [18 x i8] c"file read failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i32]
store i32 0, i32* %1
%5 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 0
%6 = call i64 @ece391_getargs(i32* %5, i32 1024)
%7 = icmp ne i64 0, %6
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([26 x i8]* @.str to i32*))
store i32 3, i32* %1
br label %35
10: ; preds = %0
%11 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 0
%12 = call i32 @ece391_open(i32* %11)
store i32 %12, i32* %2
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([16 x i8]* @.str.1 to i32*))
store i32 2, i32* %1
br label %35
16: ; preds = %10
br label %17
17: ; preds = %33, %16
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 0
%20 = call i32 @ece391_read(i32 %18, i32* %19, i32 1024)
store i32 %20, i32* %3
%21 = icmp ne i32 0, %20
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = load i32, i32* %3
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([18 x i8]* @.str.2 to i32*))
store i32 3, i32* %1
br label %35
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @ece391_write(i32 1, i32* %28, i32 %29)
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 3, i32* %1
br label %35
33: ; preds = %27
br label %17
34: ; preds = %17
store i32 0, i32* %1
br label %35
35: ; preds = %34, %32, %25, %14, %8
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [26 x i8] c"could not read arguments\0A\00"
@.str.1 = [16 x i8] c"file not found\0A\00"
@.str.2 = [18 x i8] c"file read failed\0A\00"
define i32 @main() {
%1 = alloca [1024 x i32]
%2 = bitcast [1024 x i32]* %1 to i8*
%3 = getelementptr inbounds [1024 x i32], [1024 x i32]* %1, i64 0, i64 0
%4 = call i64 @ece391_getargs(i32* %3, i32 1024)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %0
%7 = call i32 @ece391_open(i32* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %6, %11
%10 = call i32 @ece391_read(i32 %7, i32* %3, i32 1024)
switch i32 %10, label %11 [
i32 0, label %18
i32 -1, label %14
]
11: ; preds = %9
%12 = call i32 @ece391_write(i32 1, i32* %3, i32 %10)
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %9
14: ; preds = %9, %6, %0
%15 = phi i32* [ bitcast ([26 x i8]* @.str to i32*), %0 ], [ bitcast ([16 x i8]* @.str.1 to i32*), %6 ], [ bitcast ([18 x i8]* @.str.2 to i32*), %9 ]
%16 = phi i32 [ 3, %0 ], [ 2, %6 ], [ 3, %9 ]
%17 = call i32 @ece391_fdputs(i32 1, i32* %15)
br label %18
18: ; preds = %9, %11, %14
%19 = phi i32 [ %16, %14 ], [ %10, %9 ], [ 3, %11 ]
ret i32 %19
}