#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 */
char getChar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putChar(char);
int readCommand(char *, int);
void terminal() {
  char command[128] = {0};
  int index = 0;
  int ret = 1;
  printf("Welcome! Type 'help' for the full command list\n");
  putChar('>');
  char c;
  while (1) {
    c = getChar();
    if (c != 0) {
      if (c == '\n') {
        putChar(c);
        if (index != 0) {
          ret = readCommand(command, ret);
          index = 0;
          command[0] = 0;
        }
        putChar('>');
      } else if (c == 8) {
        if (index > 0) {
          putChar(c);
          index--;
          command[index] = 0;
        }
      } else {
        if (index <= 128 - 1) {
          command[index++] = c;
          command[index] = 0;
        } else {
          printf("Maximum command length reached\n");
          putChar('\n');
          index = 1;
          command[1] = 0;
        }
        if (ret != 3) {
          putChar(c);
        } else {
          putChar('*');
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
movl   $0x0,-0x84(%rbp)
movl   $0x1,-0x88(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3e,%edi
call   1070 <putChar@plt>
mov    $0x0,%al
call   1030 <getChar@plt>
mov    %al,-0x89(%rbp)
movsbl -0x89(%rbp),%eax
cmp    $0x0,%eax
je     12ec <terminal+0x19c>
movsbl -0x89(%rbp),%eax
cmp    $0xa,%eax
jne    120d <terminal+0xbd>
movsbl -0x89(%rbp),%edi
call   1070 <putChar@plt>
cmpl   $0x0,-0x84(%rbp)
je     11fe <terminal+0xae>
lea    -0x80(%rbp),%rdi
mov    -0x88(%rbp),%esi
call   1060 <readCommand@plt>
mov    %eax,-0x88(%rbp)
movl   $0x0,-0x84(%rbp)
movb   $0x0,-0x80(%rbp)
mov    $0x3e,%edi
call   1070 <putChar@plt>
jmp    12e7 <terminal+0x197>
movsbl -0x89(%rbp),%eax
cmp    $0x8,%eax
jne    1256 <terminal+0x106>
cmpl   $0x0,-0x84(%rbp)
jle    1251 <terminal+0x101>
movsbl -0x89(%rbp),%edi
call   1070 <putChar@plt>
mov    -0x84(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x84(%rbp)
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
jmp    12e2 <terminal+0x192>
cmpl   $0x7f,-0x84(%rbp)
jg     1291 <terminal+0x141>
mov    -0x89(%rbp),%cl
mov    -0x84(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x84(%rbp)
cltq
mov    %cl,-0x80(%rbp,%rax,1)
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
jmp    12b5 <terminal+0x165>
lea    0xd98(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <putChar@plt>
movl   $0x1,-0x84(%rbp)
movb   $0x0,-0x7f(%rbp)
cmpl   $0x3,-0x88(%rbp)
je     12d3 <terminal+0x183>
movsbl -0x89(%rbp),%edi
call   1070 <putChar@plt>
jmp    12dd <terminal+0x18d>
mov    $0x2a,%edi
call   1070 <putChar@plt>
jmp    12e2 <terminal+0x192>
jmp    12e7 <terminal+0x197>
jmp    12ec <terminal+0x19c>
jmp    1195 <terminal+0x45>

x86 assembly (clang, -O2):
<terminal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3e,%edi
call   1060 <putChar@plt>
mov    $0x1,%r13d
lea    0x1(%rsp),%r12
mov    %rsp,%r15
lea    0xe89(%rip),%r14
jmp    11ba <terminal+0x7a>
nopl   0x0(%rax,%rax,1)
mov    $0x3e,%edi
call   1060 <putChar@plt>
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getChar@plt>
test   %al,%al
je     11c0 <terminal+0x80>
mov    %eax,%ebx
cmp    $0x8,%al
jne    11e8 <terminal+0xa8>
test   %ebp,%ebp
jle    11c0 <terminal+0x80>
mov    $0x8,%edi
call   1060 <putChar@plt>
add    $0xffffffff,%ebp
movb   $0x0,(%rsp,%rbp,1)
jmp    11c0 <terminal+0x80>
cmp    $0xa,%bl
je     1250 <terminal+0x110>
cmp    $0x7f,%ebp
jg     1204 <terminal+0xc4>
movslq %ebp,%rax
add    $0x1,%ebp
mov    %bl,(%rsp,%rax,1)
add    %rsp,%rax
add    $0x1,%rax
jmp    121e <terminal+0xde>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1060 <putChar@plt>
mov    $0x1,%ebp
mov    %r12,%rax
movb   $0x0,(%rax)
cmp    $0x3,%r13d
je     1231 <terminal+0xf1>
movsbl %bl,%edi
call   1060 <putChar@plt>
jmp    11c0 <terminal+0x80>
mov    $0x2a,%edi
call   1060 <putChar@plt>
mov    $0x3,%r13d
jmp    11c0 <terminal+0x80>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1060 <putChar@plt>
test   %ebp,%ebp
je     11b0 <terminal+0x70>
mov    %r15,%rdi
mov    %r13d,%esi
call   1050 <readCommand@plt>
mov    %eax,%r13d
movb   $0x0,(%rsp)
jmp    11b0 <terminal+0x70>

LLVM IR (-O0):
@.str = [48 x i8] c"Welcome! Type 'help' for the full command list\0A\00"
@.str.1 = [32 x i8] c"Maximum command length reached\0A\00"
define void @terminal() {
%1 = alloca [128 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = bitcast [128 x i8]* %1 to i8*
store i32 0, i32* %2
store i32 1, i32* %3
%6 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @putChar(i8 signext 62)
br label %8
8: ; preds = %0, %72
%9 = call signext i8 (...) @getChar()
store i8 %9, i8* %4
%10 = load i8, i8* %4
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %72
13: ; preds = %8
%14 = load i8, i8* %4
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i8, i8* %4
%19 = call i32 @putChar(i8 signext %18)
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%24 = load i32, i32* %3
%25 = call i32 @readCommand(i8* %23, i32 %24)
store i32 %25, i32* %3
store i32 0, i32* %2
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
store i8 0, i8* %26
br label %27
27: ; preds = %22, %17
%28 = call i32 @putChar(i8 signext 62)
br label %71
29: ; preds = %13
%30 = load i8, i8* %4
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 8
br i1 %32, label %33, label %45
33: ; preds = %29
%34 = load i32, i32* %2
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i8, i8* %4
%38 = call i32 @putChar(i8 signext %37)
%39 = load i32, i32* %2
%40 = add nsw i32 %39, -1
store i32 %40, i32* %2
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %36, %33
br label %70
45: ; preds = %29
%46 = load i32, i32* %2
%47 = icmp sle i32 %46, 127
br i1 %47, label %48, label %57
48: ; preds = %45
%49 = load i8, i8* %4
%50 = load i32, i32* %2
%51 = add nsw i32 %50, 1
store i32 %51, i32* %2
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %52
store i8 %49, i8* %53
%54 = load i32, i32* %2
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %55
store i8 0, i8* %56
br label %61
57: ; preds = %45
%58 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%59 = call i32 @putChar(i8 signext 10)
store i32 1, i32* %2
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 1
store i8 0, i8* %60
br label %61
61: ; preds = %57, %48
%62 = load i32, i32* %3
%63 = icmp ne i32 %62, 3
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i8, i8* %4
%66 = call i32 @putChar(i8 signext %65)
br label %69
67: ; preds = %61
%68 = call i32 @putChar(i8 signext 42)
br label %69
69: ; preds = %67, %64
br label %70
70: ; preds = %69, %44
br label %71
71: ; preds = %70, %27
br label %72
72: ; preds = %71, %8
br label %8
}

LLVM IR (-O2):
@str = [47 x i8] c"Welcome! Type 'help' for the full command list\00"
@str.2 = [31 x i8] c"Maximum command length reached\00"
define void @terminal() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @putChar(i8 signext 62)
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 1
br label %6
6: ; preds = %24, %0
%7 = phi i32 [ 1, %0 ], [ %25, %24 ]
%8 = phi i32 [ 0, %0 ], [ %26, %24 ]
%9 = icmp eq i32 %7, 3
br label %10
10: ; preds = %33, %6
%11 = phi i32 [ %8, %6 ], [ %34, %33 ]
%12 = icmp sgt i32 %11, 0
br label %13
13: ; preds = %15, %10
%14 = call signext i8 (...) @getChar()
switch i8 %14, label %35 [
i8 0, label %15
i8 10, label %16
i8 8, label %27
]
15: ; preds = %13, %27
br label %13
16: ; preds = %13
%17 = call i32 @putChar(i8 signext 10)
%18 = icmp eq i32 %11, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @readCommand(i8* %2, i32 %7)
store i8 0, i8* %2
br label %21
21: ; preds = %19, %16
%22 = phi i32 [ %20, %19 ], [ %7, %16 ]
%23 = call i32 @putChar(i8 signext 62)
br label %24
24: ; preds = %21, %51
%25 = phi i32 [ 3, %51 ], [ %22, %21 ]
%26 = phi i32 [ %48, %51 ], [ 0, %21 ]
br label %6
27: ; preds = %13
br i1 %12, label %28, label %15
28: ; preds = %27
%29 = call i32 @putChar(i8 signext 8)
%30 = add nsw i32 %11, -1
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %28, %49
%34 = phi i32 [ %48, %49 ], [ %30, %28 ]
br label %10
35: ; preds = %13
%36 = icmp slt i32 %11, 128
br i1 %36, label %37, label %43
37: ; preds = %35
%38 = add nsw i32 %11, 1
%39 = sext i32 %11 to i64
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %39
store i8 %14, i8* %40
%41 = sext i32 %38 to i64
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %41
br label %46
43: ; preds = %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.2, i64 0, i64 0))
%45 = call i32 @putChar(i8 signext 10)
br label %46
46: ; preds = %43, %37
%47 = phi i8* [ %5, %43 ], [ %42, %37 ]
%48 = phi i32 [ 1, %43 ], [ %38, %37 ]
store i8 0, i8* %47
br i1 %9, label %51, label %49
49: ; preds = %46
%50 = call i32 @putChar(i8 signext %14)
br label %33
51: ; preds = %46
%52 = call i32 @putChar(i8 signext 42)
br label %24
}