#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*/ execvp(char *, char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ putchar(unsigned char);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ simple_printf(char *);
int /*<<< orphan*/ stdout;
int str2list(unsigned char *, char **, int);
scalar_t__ strcmp(unsigned char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void shell(int std_in) {
  unsigned char cmd[1024] = {0};
  int len, status;
  unsigned char ch, eoc;
  char *argv[10];
  int arg_count;
  int pid;
  simple_printf("\r\n");
  for (;;) {
    simple_printf(">");
    len = 0;
    eoc = 0;
    do {
      if (read(std_in, (char *)&ch, 1) > 0) {
        switch (ch) {
        case 0x08:
        case 0x7f:
          if (len > 0) {
            len--;
            simple_printf("\b \b");
          }
          break;
        case 0x0D:
        case 0x0A:
          cmd[len] = '\0';
          eoc = 1;
          simple_printf("\r\n");
          break;
        default:
          cmd[len] = ch;
          if ((ch >= 0x20) && (ch <= 0x7E) && (len < 1024 - 2)) {
            len++;
            putchar(ch);
            fflush(stdout);
          }
          break;
        }
      }
    } while (!eoc);
    if (cmd[0] != '\0') {
      if (strcmp(cmd, "exit") == 0)
        break;
      arg_count = str2list(cmd, argv, 10);
      if (arg_count > 0) {
        pid = fork();
        if (pid < 0) {
          return;
        } else if (pid == 0) {
          execvp(argv[0], argv);
        } else {
          waitpid(pid, &status, 0);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0xe38(%rip),%rdi
call   1040 <simple_printf@plt>
lea    0xe2f(%rip),%rdi
call   1040 <simple_printf@plt>
movl   $0x0,-0x414(%rbp)
movb   $0x0,-0x41a(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x419(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    132a <shell+0x18a>
movzbl -0x419(%rbp),%eax
mov    %eax,-0x47c(%rbp)
sub    $0x8,%eax
je     125f <shell+0xbf>
jmp    1223 <shell+0x83>
mov    -0x47c(%rbp),%eax
sub    $0xa,%eax
je     128c <shell+0xec>
jmp    1237 <shell+0x97>
mov    -0x47c(%rbp),%eax
sub    $0xd,%eax
je     128c <shell+0xec>
jmp    124b <shell+0xab>
mov    -0x47c(%rbp),%eax
sub    $0x7f,%eax
jne    12b3 <shell+0x113>
jmp    125f <shell+0xbf>
cmpl   $0x0,-0x414(%rbp)
jle    1287 <shell+0xe7>
mov    -0x414(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x414(%rbp)
lea    0xd83(%rip),%rdi
call   1040 <simple_printf@plt>
jmp    1325 <shell+0x185>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
movb   $0x1,-0x41a(%rbp)
lea    0xd57(%rip),%rdi
call   1040 <simple_printf@plt>
jmp    1325 <shell+0x185>
mov    -0x419(%rbp),%cl
movslq -0x414(%rbp),%rax
mov    %cl,-0x410(%rbp,%rax,1)
movzbl -0x419(%rbp),%eax
cmp    $0x20,%eax
jl     1320 <shell+0x180>
movzbl -0x419(%rbp),%eax
cmp    $0x7e,%eax
jg     1320 <shell+0x180>
cmpl   $0x3fe,-0x414(%rbp)
jge    1320 <shell+0x180>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
movzbl -0x419(%rbp),%edi
call   1030 <putchar@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
jmp    1325 <shell+0x185>
jmp    132a <shell+0x18a>
jmp    132f <shell+0x18f>
cmpb   $0x0,-0x41a(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ea <shell+0x4a>
movzbl -0x410(%rbp),%eax
cmp    $0x0,%eax
je     1407 <shell+0x267>
lea    -0x410(%rbp),%rdi
lea    0xca8(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1375 <shell+0x1d5>
jmp    140c <shell+0x26c>
lea    -0x410(%rbp),%rdi
lea    -0x470(%rbp),%rsi
mov    $0xa,%edx
call   10b0 <str2list@plt>
mov    %eax,-0x474(%rbp)
cmpl   $0x0,-0x474(%rbp)
jle    1402 <shell+0x262>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x478(%rbp)
cmpl   $0x0,-0x478(%rbp)
jge    13bf <shell+0x21f>
jmp    140c <shell+0x26c>
cmpl   $0x0,-0x478(%rbp)
jne    13e4 <shell+0x244>
mov    -0x470(%rbp),%rdi
lea    -0x470(%rbp),%rsi
call   10a0 <execvp@plt>
jmp    13f8 <shell+0x258>
mov    -0x478(%rbp),%edi
lea    -0x418(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    13fd <shell+0x25d>
jmp    1402 <shell+0x262>
jmp    1407 <shell+0x267>
jmp    11cd <shell+0x2d>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x468,%rsp
mov    %edi,%ebx
lea    0x60(%rsp),%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe32(%rip),%r15
mov    %r15,%rdi
call   1040 <simple_printf@plt>
lea    0xb(%rsp),%r13
jmp    11f0 <shell+0x50>
mov    0x10(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <execvp@plt>
nopl   0x0(%rax)
lea    0xe0c(%rip),%rdi
call   1040 <simple_printf@plt>
xor    %ebp,%ebp
movslq %ebp,%r12
jmp    1218 <shell+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x7f,%al
jne    123f <shell+0x9f>
test   %ebp,%ebp
jg     1280 <shell+0xe0>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1218 <shell+0x78>
movzbl 0xb(%rsp),%eax
cmp    $0xc,%al
jg     1210 <shell+0x70>
cmp    $0x8,%al
je     1214 <shell+0x74>
cmp    $0xa,%al
jne    1243 <shell+0xa3>
jmp    12a0 <shell+0x100>
cmp    $0xd,%al
je     12a0 <shell+0x100>
mov    %al,0x60(%rsp,%r12,1)
lea    -0x20(%rax),%ecx
cmp    $0x5e,%cl
ja     1218 <shell+0x78>
cmp    $0x3fd,%ebp
jg     1218 <shell+0x78>
add    $0x1,%ebp
movzbl %al,%edi
call   1030 <putchar@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
jmp    11fe <shell+0x5e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebp
lea    0xd7b(%rip),%rdi
call   1040 <simple_printf@plt>
jmp    11fe <shell+0x5e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,0x60(%rsp,%r12,1)
mov    %r15,%rdi
call   1040 <simple_printf@plt>
cmpb   $0x0,0x60(%rsp)
je     11f0 <shell+0x50>
mov    %r14,%rdi
lea    0xd46(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     130b <shell+0x16b>
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   10b0 <str2list@plt>
test   %eax,%eax
jle    11f0 <shell+0x50>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     130b <shell+0x16b>
je     11dd <shell+0x3d>
mov    %eax,%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    11f0 <shell+0x50>
add    $0x468,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [4 x i8] c"\08 \08\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"exit\00"
define void @shell(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca [10 x i8*]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
%11 = bitcast [1024 x i8]* %3 to i8*
%12 = call i32 @simple_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %99, %1
%14 = call i32 @simple_printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
store i8 0, i8* %7
br label %15
15: ; preds = %60, %13
%16 = load i32, i32* %2
%17 = call i64 @read(i32 %16, i8* %6, i32 1)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %59
19: ; preds = %15
%20 = load i8, i8* %6
%21 = zext i8 %20 to i32
switch i32 %21, label %35 [
i32 8, label %22
i32 127, label %22
i32 13, label %30
i32 10, label %30
]
22: ; preds = %19, %19
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
%28 = call i32 @simple_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %25, %22
br label %58
30: ; preds = %19, %19
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %32
store i8 0, i8* %33
store i8 1, i8* %7
%34 = call i32 @simple_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %58
35: ; preds = %19
%36 = load i8, i8* %6
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %38
store i8 %36, i8* %39
%40 = load i8, i8* %6
%41 = zext i8 %40 to i32
%42 = icmp sge i32 %41, 32
br i1 %42, label %43, label %57
43: ; preds = %35
%44 = load i8, i8* %6
%45 = zext i8 %44 to i32
%46 = icmp sle i32 %45, 126
br i1 %46, label %47, label %57
47: ; preds = %43
%48 = load i32, i32* %4
%49 = icmp slt i32 %48, 1022
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
%53 = load i8, i8* %6
%54 = call i32 @putchar(i8 zeroext %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %57
57: ; preds = %50, %47, %43, %35
br label %58
58: ; preds = %57, %30, %29
br label %59
59: ; preds = %58, %15
br label %60
60: ; preds = %59
%61 = load i8, i8* %7
%62 = icmp ne i8 %61, 0
%63 = xor i1 %62, true
br i1 %63, label %15, label %64
64: ; preds = %60
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %99
69: ; preds = %64
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
br label %100
74: ; preds = %69
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%76 = getelementptr inbounds [10 x i8*], [10 x i8*]* %8, i64 0, i64 0
%77 = call i32 @str2list(i8* %75, i8** %76, i32 10)
store i32 %77, i32* %9
%78 = load i32, i32* %9
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %98
80: ; preds = %74
%81 = call i32 (...) @fork()
store i32 %81, i32* %10
%82 = load i32, i32* %10
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %80
br label %100
85: ; preds = %80
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %93
88: ; preds = %85
%89 = getelementptr inbounds [10 x i8*], [10 x i8*]* %8, i64 0, i64 0
%90 = load i8*, i8** %89
%91 = getelementptr inbounds [10 x i8*], [10 x i8*]* %8, i64 0, i64 0
%92 = call i32 @execvp(i8* %90, i8** %91)
br label %96
93: ; preds = %85
%94 = load i32, i32* %10
%95 = call i32 @waitpid(i32 %94, i32* %5, i32 0)
br label %96
96: ; preds = %93, %88
br label %97
97: ; preds = %96
br label %98
98: ; preds = %97, %74
br label %99
99: ; preds = %98, %64
br label %13
100: ; preds = %84, %73
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [4 x i8] c"\08 \08\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"exit\00"
define void @shell(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i8
%5 = alloca [10 x i8*]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = bitcast i32* %3 to i8*
%8 = bitcast [10 x i8*]* %5 to i8*
%9 = tail call i32 @simple_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
br label %11
11: ; preds = %52, %1
%12 = call i32 @simple_printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %38, %11
%14 = phi i32 [ 0, %11 ], [ %39, %38 ]
%15 = icmp sgt i32 %14, 0
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %16
%18 = icmp slt i32 %14, 1022
br label %19
19: ; preds = %22, %13
%20 = call i64 @read(i32 %0, i8* %4, i32 1)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %19, %29, %25
br label %19
23: ; preds = %19
%24 = load i8, i8* %4
switch i8 %24, label %29 [
i8 8, label %25
i8 127, label %25
i8 13, label %40
i8 10, label %40
]
25: ; preds = %23, %23
br i1 %15, label %26, label %22
26: ; preds = %25
%27 = add nsw i32 %14, -1
%28 = call i32 @simple_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %38
29: ; preds = %23
store i8 %24, i8* %17
%30 = add i8 %24, -32
%31 = icmp ult i8 %30, 95
%32 = select i1 %31, i1 %18, i1 false
br i1 %32, label %33, label %22
33: ; preds = %29
%34 = add nsw i32 %14, 1
%35 = call i32 @putchar(i8 zeroext %24)
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
br label %38
38: ; preds = %33, %26
%39 = phi i32 [ %27, %26 ], [ %34, %33 ]
br label %13
40: ; preds = %23, %23
%41 = sext i32 %14 to i64
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %41
store i8 0, i8* %42
%43 = call i32 @simple_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%44 = load i8, i8* %6
%45 = icmp eq i8 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %40
%47 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %63, label %49
49: ; preds = %46
%50 = call i32 @str2list(i8* %6, i8** %10, i32 10)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %49, %58, %61, %40
br label %11
53: ; preds = %49
%54 = call i32 (...) @fork()
%55 = icmp slt i32 %54, 0
br i1 %55, label %63, label %56
56: ; preds = %53
%57 = icmp eq i32 %54, 0
br i1 %57, label %58, label %61
58: ; preds = %56
%59 = load i8*, i8** %10
%60 = call i32 @execvp(i8* %59, i8** %10)
br label %52
61: ; preds = %56
%62 = call i32 @waitpid(i32 %54, i32* %3, i32 0)
br label %52
63: ; preds = %46, %53
ret void
}