#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 char uint8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ ece391_fdputs(int, char *);
int ece391_open(char *);
int ece391_read(int, char *, int);
int ece391_write(int, char *, int);
int main() {
  int32_t fd, cnt;
  uint8_t buf[33];
  if (-1 == (fd = ece391_open((uint8_t *)"."))) {
    ece391_fdputs(1, (uint8_t *)"directory open failed\n");
    return 2;
  }
  while (0 != (cnt = ece391_read(fd, buf, 33 - 1))) {
    if (-1 == cnt) {
      ece391_fdputs(1, (uint8_t *)"directory entry read failed\n");
      return 3;
    }
    buf[cnt] = '\n';
    if (-1 == ece391_write(1, buf, cnt + 1))
      return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1060 <ece391_open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    118a <main+0x4a>
mov    $0x1,%edi
lea    0xe89(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x2,-0x4(%rbp)
jmp    121e <main+0xde>
jmp    118f <main+0x4f>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1040 <ece391_read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     1217 <main+0xd7>
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    11da <main+0x9a>
mov    $0x1,%edi
lea    0xe50(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x3,-0x4(%rbp)
jmp    121e <main+0xde>
movslq -0xc(%rbp),%rax
movb   $0xa,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
mov    $0x1,%edi
call   1050 <ece391_write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1212 <main+0xd2>
movl   $0x3,-0x4(%rbp)
jmp    121e <main+0xde>
jmp    118f <main+0x4f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xeb2(%rip),%rdi
call   1060 <ece391_open@plt>
cmp    $0xffffffff,%eax
je     1198 <main+0x58>
mov    %eax,%ebx
mov    %rsp,%r14
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1040 <ece391_read@plt>
test   %eax,%eax
je     11c6 <main+0x86>
cmp    $0xffffffff,%eax
je     11b0 <main+0x70>
movslq %eax,%rcx
movb   $0xa,(%rsp,%rcx,1)
add    $0x1,%eax
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <ece391_write@plt>
cmp    $0xffffffff,%eax
jne    1160 <main+0x20>
jmp    11c1 <main+0x81>
lea    0xe63(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    $0x2,%eax
jmp    11c6 <main+0x86>
lea    0xe62(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    $0x3,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [23 x i8] c"directory open failed\0A\00"
@.str.2 = [29 x i8] c"directory entry read failed\0A\00"
define i32 @main() {
%1 = alloca [33 x i8]
%2 = getelementptr inbounds [33 x i8], [33 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @ece391_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %17
7: ; preds = %0, %11
%8 = call i32 @ece391_read(i32 %3, i8* %2, i32 32)
switch i32 %8, label %11 [
i32 0, label %17
i32 -1, label %9
]
9: ; preds = %7
%10 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %17
11: ; preds = %7
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds [33 x i8], [33 x i8]* %1, i64 0, i64 %12
store i8 10, i8* %13
%14 = add nuw nsw i32 %8, 1
%15 = call i32 @ece391_write(i32 1, i8* %2, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %7
17: ; preds = %7, %11, %9, %5
%18 = phi i32 [ 2, %5 ], [ 3, %9 ], [ %8, %7 ], [ 3, %11 ]
ret i32 %18
}