#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*/ io_printf(char *);
int /*<<< orphan*/ io_put_char(char);
scalar_t__ keyboard_get_pointer();
char keyboard_pull();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ shell_remove_from_buffer(char *, int *);
scalar_t__ shell_update_buffer(char *, int *, char);
void shell_prompt() {
  char command[256];
  memset(command, 0, 256);
  int command_index = 0;
  command[0] = '\0';
  io_printf("$>");
  int processed = 0;
  while (!processed) {
    if (keyboard_get_pointer() > 0) {
      char keyPress = keyboard_pull();
      if (keyPress == '\b') {
        if (command_index > 0) {
          io_put_char(keyPress);
          shell_remove_from_buffer(command, &command_index);
        }
      } else if (command_index < 255) {
        io_put_char(keyPress);
        if (shell_update_buffer(command, &command_index, keyPress)) {
          processed = 1;
        }
      } else {
        if (keyPress == '\n') {
          io_put_char(keyPress);
        }
        if (shell_update_buffer(command, &command_index, keyPress)) {
          processed = 1;
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<shell_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
movl   $0x0,-0x104(%rbp)
movb   $0x0,-0x100(%rbp)
lea    0xe5a(%rip),%rdi
call   1080 <io_printf@plt>
movl   $0x0,-0x108(%rbp)
cmpl   $0x0,-0x108(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ce <shell_prompt+0x5e>
jmp    12da <shell_prompt+0x16a>
mov    $0x0,%al
call   1060 <keyboard_get_pointer@plt>
cmp    $0x0,%rax
jle    12d5 <shell_prompt+0x165>
mov    $0x0,%al
call   1030 <keyboard_pull@plt>
mov    %al,-0x109(%rbp)
movsbl -0x109(%rbp),%eax
cmp    $0x8,%eax
jne    122d <shell_prompt+0xbd>
cmpl   $0x0,-0x104(%rbp)
jle    1228 <shell_prompt+0xb8>
movsbl -0x109(%rbp),%edi
call   1070 <io_put_char@plt>
lea    -0x100(%rbp),%rdi
lea    -0x104(%rbp),%rsi
call   1040 <shell_remove_from_buffer@plt>
jmp    12d0 <shell_prompt+0x160>
cmpl   $0xff,-0x104(%rbp)
jge    127c <shell_prompt+0x10c>
movsbl -0x109(%rbp),%edi
call   1070 <io_put_char@plt>
lea    -0x100(%rbp),%rdi
lea    -0x104(%rbp),%rsi
movsbl -0x109(%rbp),%edx
call   1050 <shell_update_buffer@plt>
cmp    $0x0,%rax
je     1277 <shell_prompt+0x107>
movl   $0x1,-0x108(%rbp)
jmp    12cb <shell_prompt+0x15b>
movsbl -0x109(%rbp),%eax
cmp    $0xa,%eax
jne    1298 <shell_prompt+0x128>
movsbl -0x109(%rbp),%edi
call   1070 <io_put_char@plt>
lea    -0x100(%rbp),%rdi
lea    -0x104(%rbp),%rsi
movsbl -0x109(%rbp),%edx
call   1050 <shell_update_buffer@plt>
cmp    $0x0,%rax
je     12c6 <shell_prompt+0x156>
movl   $0x1,-0x108(%rbp)
jmp    12cb <shell_prompt+0x15b>
jmp    12d0 <shell_prompt+0x160>
jmp    12d5 <shell_prompt+0x165>
jmp    11b5 <shell_prompt+0x45>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_prompt>:
push   %r15
push   %r14
push   %rbx
sub    $0x110,%rsp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
movl   $0x0,0xc(%rsp)
movb   $0x0,0x10(%rsp)
lea    0xe5c(%rip),%rdi
call   1080 <io_printf@plt>
lea    0xc(%rsp),%r15
jmp    11c3 <shell_prompt+0x53>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <shell_update_buffer@plt>
test   %rax,%rax
jne    122c <shell_prompt+0xbc>
xor    %eax,%eax
call   1060 <keyboard_get_pointer@plt>
test   %rax,%rax
jle    11c3 <shell_prompt+0x53>
xor    %eax,%eax
call   1030 <keyboard_pull@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%eax
cmp    $0x8,%bl
jne    1200 <shell_prompt+0x90>
test   %eax,%eax
jle    11c3 <shell_prompt+0x53>
mov    $0x8,%edi
call   1070 <io_put_char@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <shell_remove_from_buffer@plt>
jmp    11c3 <shell_prompt+0x53>
nopl   0x0(%rax)
cmp    $0xfe,%eax
jg     121b <shell_prompt+0xab>
movsbl %bl,%ebx
mov    %ebx,%edi
call   1070 <io_put_char@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
jmp    11b9 <shell_prompt+0x49>
cmp    $0xa,%bl
jne    11b0 <shell_prompt+0x40>
mov    $0xa,%edi
call   1070 <io_put_char@plt>
jmp    11b0 <shell_prompt+0x40>
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"$>\00"
define void @shell_prompt() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 256)
store i32 0, i32* %2
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
store i8 0, i8* %7
%8 = call i32 @io_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %9
9: ; preds = %58, %0
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
br i1 %12, label %13, label %59
13: ; preds = %9
%14 = call i64 (...) @keyboard_get_pointer()
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %58
16: ; preds = %13
%17 = call signext i8 (...) @keyboard_pull()
store i8 %17, i8* %4
%18 = load i8, i8* %4
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 8
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = load i32, i32* %2
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8, i8* %4
%26 = call i32 @io_put_char(i8 signext %25)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%28 = call i32 @shell_remove_from_buffer(i8* %27, i32* %2)
br label %29
29: ; preds = %24, %21
br label %57
30: ; preds = %16
%31 = load i32, i32* %2
%32 = icmp slt i32 %31, 255
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i8, i8* %4
%35 = call i32 @io_put_char(i8 signext %34)
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%37 = load i8, i8* %4
%38 = call i64 @shell_update_buffer(i8* %36, i32* %2, i8 signext %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 1, i32* %3
br label %41
41: ; preds = %40, %33
br label %56
42: ; preds = %30
%43 = load i8, i8* %4
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i8, i8* %4
%48 = call i32 @io_put_char(i8 signext %47)
br label %49
49: ; preds = %46, %42
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%51 = load i8, i8* %4
%52 = call i64 @shell_update_buffer(i8* %50, i32* %2, i8 signext %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 1, i32* %3
br label %55
55: ; preds = %54, %49
br label %56
56: ; preds = %55, %41
br label %57
57: ; preds = %56, %29
br label %58
58: ; preds = %57, %13
br label %9
59: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"$>\00"
define void @shell_prompt() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 256)
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
store i8 0, i8* %3
%6 = call i32 @io_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %10, %0
%8 = call i64 (...) @keyboard_get_pointer()
%9 = icmp sgt i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %7, %15, %22, %30, %17
br label %7
11: ; preds = %7
%12 = call signext i8 (...) @keyboard_pull()
%13 = icmp eq i8 %12, 8
%14 = load i32, i32* %2
br i1 %13, label %15, label %20
15: ; preds = %11
%16 = icmp sgt i32 %14, 0
br i1 %16, label %17, label %10
17: ; preds = %15
%18 = call i32 @io_put_char(i8 signext 8)
%19 = call i32 @shell_remove_from_buffer(i8* %3, i32* %2)
br label %10
20: ; preds = %11
%21 = icmp slt i32 %14, 255
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = call i32 @io_put_char(i8 signext %12)
%24 = call i64 @shell_update_buffer(i8* %3, i32* %2, i8 signext %12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %10, label %33
26: ; preds = %20
%27 = icmp eq i8 %12, 10
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = call i32 @io_put_char(i8 signext 10)
br label %30
30: ; preds = %28, %26
%31 = call i64 @shell_update_buffer(i8* %3, i32* %2, i8 signext %12)
%32 = icmp eq i64 %31, 0
br i1 %32, label %10, label %33
33: ; preds = %30, %22
ret void
}