#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 /*<<< orphan*/ SHELL_RunCmd(char *, int *);
int /*<<< orphan*/ autosar_fputc(char, int /*<<< orphan*/);
int /*<<< orphan*/ doPrompt();
char fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int SHELL_Mainloop(void) {
  char c;
  static char cmdLine[40];
  int lineIndex = 0;
  int cmdRv;
  puts("ArcCore Shell version 0.1\n");
  doPrompt();
  for (;;) {
    c = fgetc(stdin);
    if (lineIndex >= 40) {
      lineIndex = 0;
    }
    if (c == '\b') {
      lineIndex--;
      autosar_fputc(c, stdout);
    } else if (c == '\n' || c == '\r') {
      puts("\n");
      cmdLine[lineIndex++] = '\n';
      cmdLine[lineIndex] = '\0';
      SHELL_RunCmd(cmdLine, &cmdRv);
      lineIndex = 0;
      doPrompt();
    } else {
      cmdLine[lineIndex++] = c;
      autosar_fputc(c, stdout);
    }
  }
}


x86 assembly (clang, -O0):
<SHELL_Mainloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1060 <doPrompt@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %al,-0x1(%rbp)
cmpl   $0x28,-0x8(%rbp)
jl     1194 <SHELL_Mainloop+0x44>
movl   $0x0,-0x8(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x8,%eax
jne    11c3 <SHELL_Mainloop+0x73>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x1(%rbp),%al
mov    0x2e24(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1070 <autosar_fputc@plt>
jmp    1268 <SHELL_Mainloop+0x118>
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
je     11dd <SHELL_Mainloop+0x8d>
movsbl -0x1(%rbp),%eax
cmp    $0xd,%eax
jne    1234 <SHELL_Mainloop+0xe4>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2e62(%rip),%rax
movb   $0xa,(%rax,%rcx,1)
movslq -0x8(%rbp),%rcx
lea    0x2e53(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e48(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <SHELL_RunCmd@plt>
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1060 <doPrompt@plt>
jmp    1263 <SHELL_Mainloop+0x113>
mov    -0x1(%rbp),%dl
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
movslq %eax,%rcx
lea    0x2e14(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x1(%rbp),%al
mov    0x2d7f(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1070 <autosar_fputc@plt>
jmp    1268 <SHELL_Mainloop+0x118>
jmp    1172 <SHELL_Mainloop+0x22>

x86 assembly (clang, -O2):
<SHELL_Mainloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <doPrompt@plt>
mov    0x2e70(%rip),%rbp
lea    0x2ee1(%rip),%rbx
lea    0x4(%rsp),%r15
mov    0x2e4d(%rip),%r13
xor    %r14d,%r14d
jmp    11ba <SHELL_Mainloop+0x6a>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
movslq %r14d,%rax
movw   $0xa,(%rax,%rbx,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <SHELL_RunCmd@plt>
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <doPrompt@plt>
mov    0x0(%rbp),%edi
call   1050 <fgetc@plt>
cmp    $0x28,%r14d
cmovge %r12d,%r14d
cmp    $0xd,%al
je     1190 <SHELL_Mainloop+0x40>
cmp    $0xa,%al
je     1190 <SHELL_Mainloop+0x40>
cmp    $0x8,%al
jne    11f0 <SHELL_Mainloop+0xa0>
add    $0xffffffff,%r14d
mov    0x0(%r13),%esi
mov    $0x8,%edi
call   1070 <autosar_fputc@plt>
jmp    11ba <SHELL_Mainloop+0x6a>
nopw   0x0(%rax,%rax,1)
movslq %r14d,%rcx
add    $0x1,%r14d
mov    %al,(%rcx,%rbx,1)
mov    0x0(%r13),%esi
movsbl %al,%edi
call   1070 <autosar_fputc@plt>
jmp    11ba <SHELL_Mainloop+0x6a>

LLVM IR (-O0):
@SHELL_Mainloop.cmdLine = internal global [40 x i8] zeroinitializer
@.str = [27 x i8] c"ArcCore Shell version 0.1\0A\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @SHELL_Mainloop() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %2
%4 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (...) @doPrompt()
br label %6
6: ; preds = %51, %0
%7 = load i32, i32* @stdin
%8 = call signext i8 @fgetc(i32 %7)
store i8 %8, i8* %1
%9 = load i32, i32* %2
%10 = icmp sge i32 %9, 40
br i1 %10, label %11, label %12
11: ; preds = %6
store i32 0, i32* %2
br label %12
12: ; preds = %11, %6
%13 = load i8, i8* %1
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 8
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %2
%18 = add nsw i32 %17, -1
store i32 %18, i32* %2
%19 = load i8, i8* %1
%20 = load i32, i32* @stdout
%21 = call i32 @autosar_fputc(i8 signext %19, i32 %20)
br label %51
22: ; preds = %12
%23 = load i8, i8* %1
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i8, i8* %1
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 13
br i1 %29, label %30, label %41
30: ; preds = %26, %22
%31 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %34
store i8 10, i8* %35
%36 = load i32, i32* %2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 @SHELL_RunCmd(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 0), i32* %3)
store i32 0, i32* %2
%40 = call i32 (...) @doPrompt()
br label %50
41: ; preds = %26
%42 = load i8, i8* %1
%43 = load i32, i32* %2
%44 = add nsw i32 %43, 1
store i32 %44, i32* %2
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %45
store i8 %42, i8* %46
%47 = load i8, i8* %1
%48 = load i32, i32* @stdout
%49 = call i32 @autosar_fputc(i8 signext %47, i32 %48)
br label %50
50: ; preds = %41, %30
br label %51
51: ; preds = %50, %16
br label %6
}

LLVM IR (-O2):
@SHELL_Mainloop.cmdLine = internal global [40 x i8] zeroinitializer
@.str = [27 x i8] c"ArcCore Shell version 0.1\0A\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @SHELL_Mainloop() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 (...) @doPrompt()
br label %5
5: ; preds = %24, %0
%6 = phi i32 [ 0, %0 ], [ %25, %24 ]
%7 = load i32, i32* @stdin
%8 = call signext i8 @fgetc(i32 %7)
%9 = icmp sgt i32 %6, 39
%10 = select i1 %9, i32 0, i32 %6
switch i8 %8, label %26 [
i8 8, label %11
i8 13, label %15
i8 10, label %15
]
11: ; preds = %5
%12 = add nsw i32 %10, -1
%13 = load i32, i32* @stdout
%14 = call i32 @autosar_fputc(i8 signext 8, i32 %13)
br label %24
15: ; preds = %5, %5
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = add nsw i32 %10, 1
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %18
store i8 10, i8* %19
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @SHELL_RunCmd(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 0), i32* %1)
%23 = call i32 (...) @doPrompt()
br label %24
24: ; preds = %15, %26, %11
%25 = phi i32 [ %12, %11 ], [ 0, %15 ], [ %27, %26 ]
br label %5
26: ; preds = %5
%27 = add nsw i32 %10, 1
%28 = sext i32 %10 to i64
%29 = getelementptr inbounds [40 x i8], [40 x i8]* @SHELL_Mainloop.cmdLine, i64 0, i64 %28
store i8 %8, i8* %29
%30 = load i32, i32* @stdout
%31 = call i32 @autosar_fputc(i8 signext %8, i32 %30)
br label %24
}