#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 ece391_execute(char *);
int /*<<< orphan*/ ece391_fdputs(int, char *);
int ece391_read(int /*<<< orphan*/, char *, int);
scalar_t__ ece391_strcmp(char *, char *);
int main() {
  int32_t cnt, rval;
  uint8_t buf[1024];
  ece391_fdputs(1, (uint8_t *)"Starting 391 Shell\n");
  while (1) {
    ece391_fdputs(1, (uint8_t *)"391OS> ");
    if (-1 == (cnt = ece391_read(0, buf, 1024 - 1))) {
      ece391_fdputs(1, (uint8_t *)"read from keyboard failed\n");
      return 3;
    }
    if (cnt > 0 && '\n' == buf[cnt - 1])
      cnt--;
    buf[cnt] = '\0';
    if (0 == ece391_strcmp(buf, (uint8_t *)"exit"))
      return 0;
    if ('\0' == buf[0])
      continue;
    rval = ece391_execute(buf);
    if (-1 == rval)
      ece391_fdputs(1, (uint8_t *)"no such command\n");
    else if (256 == rval)
      ece391_fdputs(1, (uint8_t *)"program terminated by exception\n");
    else if (0 != rval)
      ece391_fdputs(1, (uint8_t *)"program terminated abnormally\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
lea    0xea2(%rip),%rsi
call   1040 <ece391_fdputs@plt>
mov    $0x1,%edi
lea    0xea5(%rip),%rsi
call   1040 <ece391_fdputs@plt>
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x3ff,%edx
call   1060 <ece391_read@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11b6 <main+0x76>
mov    $0x1,%edi
lea    0xe77(%rip),%rsi
call   1040 <ece391_fdputs@plt>
movl   $0x3,-0x4(%rbp)
jmp    12b7 <main+0x177>
cmpl   $0x0,-0x8(%rbp)
jle    11e6 <main+0xa6>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%ecx
ff
mov    $0xa,%eax
cmp    %ecx,%eax
jne    11e6 <main+0xa6>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1030 <ece391_strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    121f <main+0xdf>
movl   $0x0,-0x4(%rbp)
jmp    12b7 <main+0x177>
movsbl -0x410(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1235 <main+0xf5>
jmp    1163 <main+0x23>
lea    -0x410(%rbp),%rdi
call   1050 <ece391_execute@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    1268 <main+0x128>
mov    $0x1,%edi
lea    0xdde(%rip),%rsi
call   1040 <ece391_fdputs@plt>
jmp    12b2 <main+0x172>
mov    $0x100,%eax
cmp    -0xc(%rbp),%eax
jne    128c <main+0x14c>
mov    $0x1,%edi
lea    0xdcb(%rip),%rsi
call   1040 <ece391_fdputs@plt>
jmp    12ad <main+0x16d>
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     12a8 <main+0x168>
mov    $0x1,%edi
lea    0xdcb(%rip),%rsi
call   1040 <ece391_fdputs@plt>
jmp    12ad <main+0x16d>
jmp    12b2 <main+0x172>
jmp    1163 <main+0x23>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
lea    0xea8(%rip),%rsi
mov    $0x1,%edi
call   1040 <ece391_fdputs@plt>
lea    0xeab(%rip),%rsi
mov    $0x1,%edi
call   1040 <ece391_fdputs@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x3ff,%edx
call   1060 <ece391_read@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xeb>
lea    0xea5(%rip),%r14
mov    %rsp,%rbx
lea    0xe78(%rip),%r15
lea    0xe99(%rip),%r12
lea    0xea3(%rip),%r13
lea    0xebd(%rip),%rbp
jmp    11e1 <main+0xa1>
mov    %rbp,%rsi
mov    $0x1,%edi
call   1040 <ece391_fdputs@plt>
mov    $0x1,%edi
mov    %r15,%rsi
call   1040 <ece391_fdputs@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <ece391_read@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xeb>
test   %eax,%eax
jle    11ef <main+0xaf>
lea    -0x1(%rax),%ecx
cmpb   $0xa,(%rsp,%rcx,1)
cmove  %ecx,%eax
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <ece391_strcmp@plt>
test   %rax,%rax
je     1243 <main+0x103>
cmpb   $0x0,(%rsp)
je     11c0 <main+0x80>
mov    %rbx,%rdi
call   1050 <ece391_execute@plt>
mov    %r12,%rsi
cmp    $0xffffffff,%eax
je     11b6 <main+0x76>
test   %eax,%eax
je     11c0 <main+0x80>
cmp    $0x100,%eax
jne    11b3 <main+0x73>
mov    %r13,%rsi
jmp    11b6 <main+0x76>
lea    0xdea(%rip),%rsi
mov    $0x1,%edi
call   1040 <ece391_fdputs@plt>
mov    $0x3,%eax
jmp    1245 <main+0x105>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Starting 391 Shell\0A\00"
@.str.1 = [8 x i8] c"391OS> \00"
@.str.2 = [27 x i8] c"read from keyboard failed\0A\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [17 x i8] c"no such command\0A\00"
@.str.5 = [33 x i8] c"program terminated by exception\0A\00"
@.str.6 = [31 x i8] c"program terminated abnormally\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %0, %40, %60
%7 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @ece391_read(i32 0, i8* %8, i32 1023)
store i32 %9, i32* %2
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %1
br label %61
13: ; preds = %6
%14 = load i32, i32* %2
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = load i32, i32* %2
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 10, %22
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* %2
%26 = add nsw i32 %25, -1
store i32 %26, i32* %2
br label %27
27: ; preds = %24, %16, %13
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i64 @ece391_strcmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 0, %32
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 0, i32* %1
br label %61
35: ; preds = %27
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 0, %38
br i1 %39, label %40, label %41
40: ; preds = %35
br label %6
41: ; preds = %35
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%43 = call i32 @ece391_execute(i8* %42)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp eq i32 -1, %44
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %60
48: ; preds = %41
%49 = load i32, i32* %3
%50 = icmp eq i32 256, %49
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %59
53: ; preds = %48
%54 = load i32, i32* %3
%55 = icmp ne i32 0, %54
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
br label %59
59: ; preds = %58, %51
br label %60
60: ; preds = %59, %46
br label %6
61: ; preds = %34, %11
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [20 x i8] c"Starting 391 Shell\0A\00"
@.str.1 = [8 x i8] c"391OS> \00"
@.str.2 = [27 x i8] c"read from keyboard failed\0A\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [17 x i8] c"no such command\0A\00"
@.str.5 = [33 x i8] c"program terminated by exception\0A\00"
@.str.6 = [31 x i8] c"program terminated abnormally\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @ece391_read(i32 0, i8* %2, i32 1023)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %35, %0
%8 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %39
9: ; preds = %0, %35
%10 = phi i32 [ %37, %35 ], [ %5, %0 ]
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = add nsw i32 %10, -1
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
%18 = select i1 %17, i32 %13, i32 %10
br label %19
19: ; preds = %12, %9
%20 = phi i32 [ %10, %9 ], [ %18, %12 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i64 @ece391_strcmp(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %39, label %25
25: ; preds = %19
%26 = load i8, i8* %2
%27 = icmp eq i8 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = call i32 @ece391_execute(i8* %2)
switch i32 %29, label %31 [
i32 -1, label %32
i32 256, label %30
i32 0, label %35
]
30: ; preds = %28
br label %32
31: ; preds = %28
br label %32
32: ; preds = %28, %30, %31
%33 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), %31 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), %30 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), %28 ]
%34 = call i32 @ece391_fdputs(i32 1, i8* %33)
br label %35
35: ; preds = %32, %28, %25
%36 = call i32 @ece391_fdputs(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @ece391_read(i32 0, i8* %2, i32 1023)
%38 = icmp eq i32 %37, -1
br i1 %38, label %7, label %9
39: ; preds = %19, %7
%40 = phi i32 [ 3, %7 ], [ 0, %19 ]
ret i32 %40
}