#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 *gets(int);
int /*<<< orphan*/ kprint(char const *);
int /*<<< orphan*/ parse_exec(char *);
int kernel_sh() {
  const char *prompt = "\n# ";
  kprint(prompt);
  int cl = 256;
  while (1) {
    char *command = gets(cl);
    for (int i = 0; command[i]; i++) {
      if (command[i] == '\n') {
        command[i] = 0;
      }
    }
    if (!parse_exec(command))
      return 1;
    kprint(prompt);
  }
}


x86 assembly (clang, -O0):
<kernel_sh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <kprint@plt>
movl   $0x100,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <gets@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11ac <kernel_sh+0x7c>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1199 <kernel_sh+0x69>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119e <kernel_sh+0x6e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <kernel_sh+0x36>
mov    -0x18(%rbp),%rdi
call   1050 <parse_exec@plt>
cmp    $0x0,%eax
jne    11c9 <kernel_sh+0x99>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%rdi
call   1040 <kprint@plt>
jmp    1153 <kernel_sh+0x23>

x86 assembly (clang, -O2):
<kernel_sh>:
push   %rbx
lea    0xec8(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <kprint@plt>
mov    $0x100,%edi
call   1030 <gets@plt>
mov    %rax,%rcx
jmp    1167 <kernel_sh+0x37>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%rcx)
add    $0x1,%rcx
movzbl (%rcx),%edx
cmp    $0xa,%dl
je     1160 <kernel_sh+0x30>
test   %dl,%dl
jne    1163 <kernel_sh+0x33>
mov    %rax,%rdi
call   1050 <parse_exec@plt>
test   %eax,%eax
jne    1140 <kernel_sh+0x10>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\0A# \00"
define i32 @kernel_sh() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %1
%5 = load i8*, i8** %1
%6 = call i32 @kprint(i8* %5)
store i32 256, i32* %2
br label %7
7: ; preds = %0, %39
%8 = load i32, i32* %2
%9 = call i8* @gets(i32 %8)
store i8* %9, i8** %3
store i32 0, i32* %4
br label %10
10: ; preds = %31, %7
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %10
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %25, %17
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %10
34: ; preds = %10
%35 = load i8*, i8** %3
%36 = call i32 @parse_exec(i8* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %34
ret i32 1
39: ; preds = %34
%40 = load i8*, i8** %1
%41 = call i32 @kprint(i8* %40)
br label %7
}

LLVM IR (-O2):
@.str = [4 x i8] c"\0A# \00"
define i32 @kernel_sh() {
br label %1
1: ; preds = %8, %0
%2 = tail call i32 @kprint(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @gets(i32 256)
br label %4
4: ; preds = %12, %1
%5 = phi i64 [ %13, %12 ], [ 0, %1 ]
%6 = getelementptr inbounds i8, i8* %3, i64 %5
%7 = load i8, i8* %6
switch i8 %7, label %12 [
i8 0, label %8
i8 10, label %11
]
8: ; preds = %4
%9 = tail call i32 @parse_exec(i8* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %1
11: ; preds = %4
store i8 0, i8* %6
br label %12
12: ; preds = %4, %11
%13 = add nuw i64 %5, 1
br label %4
14: ; preds = %8
ret i32 1
}