#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 */
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ calc_init();
int /*<<< orphan*/ led_blink(unsigned int volatile *, int);
unsigned int *led_init();
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ reset_string(char *, int);
char uart_getc();
int /*<<< orphan*/ uart_init();
int /*<<< orphan*/ uart_putc(char);
int /*<<< orphan*/ uart_puts(char *);
void kernel_main(uint32_t r0, uint32_t r1, uint32_t atags) {
  (void)r0;
  (void)r1;
  (void)atags;
  uart_init();
  volatile unsigned int *gpio = led_init();
  (void)r0;
  (void)r1;
  (void)atags;
  uart_init();
  uart_puts("> Hello, World!\r\n");
  int str_len = 80;
  char stringin[str_len];
  int i = 0;
  uart_puts("> ");
  int x = 0;
  int *calc = &x;
  while (true) {
    char x = uart_getc();
    if (i > str_len - 1) {
      uart_puts(stringin);
      reset_string(stringin, i);
      uart_puts("Max string len reached\r\n");
      i = 0;
    } else if (x == '\r') {
      uart_puts("\r\n");
      if (memcmp(stringin, "calc", sizeof("calc")) == 0) {
        uart_puts("CALC RECOGNIZED!\r\n");
        calc_init();
        *calc = 1;
      }
      if (memcmp(stringin, "blink", sizeof("blink")) == 0) {
        uart_puts("LEDDDDDD!\r\n");
        led_blink(gpio, 1);
      }
      if (memcmp(stringin, "stop", sizeof("stop")) == 0) {
        uart_puts("LEDDDDDD STOP!\r\n");
        led_blink(gpio, 0);
      } else {
        uart_puts(stringin);
        uart_puts("\n");
      }
      reset_string(stringin, i);
      uart_puts("> ");
      i = 0;
    } else if (x == 127 || x == 8) {
      if (i > 0) {
        uart_putc('\b');
        uart_putc(' ');
        uart_putc('\b');
        stringin[i - 1] = 0x00;
        i--;
      }
    } else {
      stringin[i] = x;
      uart_putc(stringin[i]);
      i++;
    }
  }
}


x86 assembly (clang, -O0):
<kernel_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
xor    %eax,%eax
mov    %al,-0x51(%rbp)
call   1040 <uart_init@plt>
mov    -0x51(%rbp),%al
call   1070 <led_init@plt>
mov    %rax,%rcx
mov    -0x51(%rbp),%al
mov    %rcx,-0x18(%rbp)
call   1040 <uart_init@plt>
lea    0xe37(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0x50,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xe09(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0x0,-0x38(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1222 <kernel_main+0x92>
mov    $0x0,%al
call   1090 <uart_getc@plt>
mov    %al,-0x41(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    126a <kernel_main+0xda>
mov    -0x50(%rbp),%rdi
call   10b0 <uart_puts@plt>
mov    -0x50(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <reset_string@plt>
lea    0xdbc(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0x0,-0x34(%rbp)
jmp    13fb <kernel_main+0x26b>
movsbl -0x41(%rbp),%eax
cmp    $0xd,%eax
jne    136c <kernel_main+0x1dc>
lea    0xdd7(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x50(%rbp),%rdi
lea    0xda0(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    12bf <kernel_main+0x12f>
lea    0xd8a(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    $0x0,%al
call   1050 <calc_init@plt>
mov    -0x40(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x50(%rbp),%rdi
lea    0xd7c(%rip),%rsi
mov    $0x6,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    12f8 <kernel_main+0x168>
lea    0xd67(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1060 <led_blink@plt>
mov    -0x50(%rbp),%rdi
lea    0xd55(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    1333 <kernel_main+0x1a3>
lea    0xd3f(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <led_blink@plt>
jmp    1348 <kernel_main+0x1b8>
mov    -0x50(%rbp),%rdi
call   10b0 <uart_puts@plt>
lea    0xd13(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    -0x50(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <reset_string@plt>
lea    0xcb7(%rip),%rdi
call   10b0 <uart_puts@plt>
movl   $0x0,-0x34(%rbp)
jmp    13f6 <kernel_main+0x266>
movsbl -0x41(%rbp),%eax
cmp    $0x7f,%eax
je     1386 <kernel_main+0x1f6>
movsbl -0x41(%rbp),%eax
cmp    $0x8,%eax
jne    13cd <kernel_main+0x23d>
cmpl   $0x0,-0x34(%rbp)
jle    13c8 <kernel_main+0x238>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
mov    $0x20,%edi
call   1030 <uart_putc@plt>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
mov    -0x50(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    13f1 <kernel_main+0x261>
mov    -0x50(%rbp),%rax
mov    -0x41(%rbp),%dl
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <uart_putc@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    13f6 <kernel_main+0x266>
jmp    13fb <kernel_main+0x26b>
jmp    121d <kernel_main+0x8d>

x86 assembly (clang, -O2):
<kernel_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
xor    %eax,%eax
call   1040 <uart_init@plt>
xor    %eax,%eax
call   1070 <led_init@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <uart_init@plt>
lea    0xe43(%rip),%rdi
call   10b0 <uart_puts@plt>
lea    0xe49(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    %rsp,%rbx
lea    0xe3d(%rip),%r12
lea    0xe67(%rip),%r14
lea    0xe72(%rip),%r13
jmp    120a <kernel_main+0x7a>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <uart_puts@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <reset_string@plt>
mov    %r12,%rdi
call   10b0 <uart_puts@plt>
xor    %ebp,%ebp
jmp    1214 <kernel_main+0x84>
xchg   %ax,%ax
test   %ebp,%ebp
jg     123f <kernel_main+0xaf>
xor    %eax,%eax
call   1090 <uart_getc@plt>
cmp    $0x50,%ebp
jge    11f0 <kernel_main+0x60>
cmp    $0x8,%al
je     1210 <kernel_main+0x80>
cmp    $0x7f,%al
je     1210 <kernel_main+0x80>
cmp    $0xd,%al
je     1270 <kernel_main+0xe0>
movslq %ebp,%rcx
mov    %al,(%rsp,%rcx,1)
movsbl %al,%edi
call   1030 <uart_putc@plt>
add    $0x1,%ebp
jmp    1214 <kernel_main+0x84>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
mov    $0x20,%edi
call   1030 <uart_putc@plt>
mov    $0x8,%edi
call   1030 <uart_putc@plt>
add    $0xffffffff,%ebp
movb   $0x0,(%rsp,%rbp,1)
jmp    1214 <kernel_main+0x84>
cs nopw 0x0(%rax,%rax,1)
lea    0xdde(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    %rbx,%rdi
lea    0xda8(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    12a8 <kernel_main+0x118>
lea    0xd97(%rip),%rdi
call   10b0 <uart_puts@plt>
xor    %eax,%eax
call   1050 <calc_init@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    12d6 <kernel_main+0x146>
lea    0xd88(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1060 <led_blink@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     1301 <kernel_main+0x171>
mov    %rbx,%rdi
call   10b0 <uart_puts@plt>
lea    0xd5c(%rip),%rdi
call   10b0 <uart_puts@plt>
jmp    1317 <kernel_main+0x187>
lea    0xd55(%rip),%rdi
call   10b0 <uart_puts@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <led_blink@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <reset_string@plt>
lea    0xcea(%rip),%rdi
call   10b0 <uart_puts@plt>
jmp    120a <kernel_main+0x7a>

LLVM IR (-O0):
@.str = [18 x i8] c"> Hello, World!\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [25 x i8] c"Max string len reached\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@.str.4 = [5 x i8] c"calc\00"
@.str.5 = [19 x i8] c"CALC RECOGNIZED!\0D\0A\00"
@.str.6 = [6 x i8] c"blink\00"
@.str.7 = [12 x i8] c"LEDDDDDD!\0D\0A\00"
@.str.8 = [5 x i8] c"stop\00"
@.str.9 = [17 x i8] c"LEDDDDDD STOP!\0D\0A\00"
@.str.10 = [2 x i8] c"\0A\00"
define void @kernel_main(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = call i32 (...) @uart_init()
%19 = call i32* (...) @led_init()
store i32* %19, i32** %7
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = call i32 (...) @uart_init()
%24 = call i32 @uart_puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 80, i32* %8
%25 = load i32, i32* %8
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %9
%28 = alloca i8, i64 %26
store i64 %26, i64* %10
store i32 0, i32* %11
%29 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %12
store i32* %12, i32** %13
br label %30
30: ; preds = %111, %3
br label %31
31: ; preds = %30
%32 = call signext i8 (...) @uart_getc()
store i8 %32, i8* %14
%33 = load i32, i32* %11
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 1
%36 = icmp sgt i32 %33, %35
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = call i32 @uart_puts(i8* %28)
%39 = load i32, i32* %11
%40 = call i32 @reset_string(i8* %28, i32 %39)
%41 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %11
br label %111
42: ; preds = %31
%43 = load i8, i8* %14
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %46, label %75
46: ; preds = %42
%47 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%48 = call i64 @memcmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 5)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = call i32 @uart_puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 (...) @calc_init()
%53 = load i32*, i32** %13
store i32 1, i32* %53
br label %54
54: ; preds = %50, %46
%55 = call i64 @memcmp(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 6)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = call i32 @uart_puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%59 = load i32*, i32** %7
%60 = call i32 @led_blink(i32* %59, i32 1)
br label %61
61: ; preds = %57, %54
%62 = call i64 @memcmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 5)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %61
%65 = call i32 @uart_puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%66 = load i32*, i32** %7
%67 = call i32 @led_blink(i32* %66, i32 0)
br label %71
68: ; preds = %61
%69 = call i32 @uart_puts(i8* %28)
%70 = call i32 @uart_puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %71
71: ; preds = %68, %64
%72 = load i32, i32* %11
%73 = call i32 @reset_string(i8* %28, i32 %72)
%74 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %11
br label %110
75: ; preds = %42
%76 = load i8, i8* %14
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 127
br i1 %78, label %83, label %79
79: ; preds = %75
%80 = load i8, i8* %14
%81 = sext i8 %80 to i32
%82 = icmp eq i32 %81, 8
br i1 %82, label %83, label %97
83: ; preds = %79, %75
%84 = load i32, i32* %11
%85 = icmp sgt i32 %84, 0
br i1 %85, label %86, label %96
86: ; preds = %83
%87 = call i32 @uart_putc(i8 signext 8)
%88 = call i32 @uart_putc(i8 signext 32)
%89 = call i32 @uart_putc(i8 signext 8)
%90 = load i32, i32* %11
%91 = sub nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %28, i64 %92
store i8 0, i8* %93
%94 = load i32, i32* %11
%95 = add nsw i32 %94, -1
store i32 %95, i32* %11
br label %96
96: ; preds = %86, %83
br label %109
97: ; preds = %79
%98 = load i8, i8* %14
%99 = load i32, i32* %11
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %28, i64 %100
store i8 %98, i8* %101
%102 = load i32, i32* %11
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %28, i64 %103
%105 = load i8, i8* %104
%106 = call i32 @uart_putc(i8 signext %105)
%107 = load i32, i32* %11
%108 = add nsw i32 %107, 1
store i32 %108, i32* %11
br label %109
109: ; preds = %97, %96
br label %110
110: ; preds = %109, %71
br label %111
111: ; preds = %110, %37
br label %30
}

LLVM IR (-O2):
@.str = [18 x i8] c"> Hello, World!\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [25 x i8] c"Max string len reached\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@.str.4 = [5 x i8] c"calc\00"
@.str.5 = [19 x i8] c"CALC RECOGNIZED!\0D\0A\00"
@.str.6 = [6 x i8] c"blink\00"
@.str.7 = [12 x i8] c"LEDDDDDD!\0D\0A\00"
@.str.8 = [5 x i8] c"stop\00"
@.str.9 = [17 x i8] c"LEDDDDDD STOP!\0D\0A\00"
@.str.10 = [2 x i8] c"\0A\00"
define void @kernel_main(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 (...) @uart_init()
%5 = tail call i32* (...) @led_init()
%6 = tail call i32 (...) @uart_init()
%7 = tail call i32 @uart_puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = alloca [80 x i8]
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%10 = tail call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %21, %3
%12 = phi i32 [ 0, %3 ], [ %22, %21 ]
%13 = icmp sgt i32 %12, 79
%14 = icmp sgt i32 %12, 0
br label %15
15: ; preds = %11, %49
%16 = call signext i8 (...) @uart_getc()
br i1 %13, label %17, label %23
17: ; preds = %15
%18 = call i32 @uart_puts(i8* %9)
%19 = call i32 @reset_string(i8* %9, i32 %12)
%20 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %17, %46, %50, %57
%22 = phi i32 [ %61, %57 ], [ %54, %50 ], [ 0, %46 ], [ 0, %17 ]
br label %11
23: ; preds = %15
switch i8 %16, label %57 [
i8 13, label %24
i8 127, label %49
i8 8, label %49
]
24: ; preds = %23
%25 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = call i64 @memcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @uart_puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 (...) @calc_init()
br label %31
31: ; preds = %28, %24
%32 = call i64 @memcmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 6)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @uart_puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%36 = call i32 @led_blink(i32* %5, i32 1)
br label %37
37: ; preds = %34, %31
%38 = call i64 @memcmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i32 5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @uart_puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%42 = call i32 @led_blink(i32* %5, i32 0)
br label %46
43: ; preds = %37
%44 = call i32 @uart_puts(i8* %9)
%45 = call i32 @uart_puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %46
46: ; preds = %43, %40
%47 = call i32 @reset_string(i8* %9, i32 %12)
%48 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %21
49: ; preds = %23, %23
br i1 %14, label %50, label %15
50: ; preds = %49
%51 = call i32 @uart_putc(i8 signext 8)
%52 = call i32 @uart_putc(i8 signext 32)
%53 = call i32 @uart_putc(i8 signext 8)
%54 = add nsw i32 %12, -1
%55 = zext i32 %54 to i64
%56 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 %55
store i8 0, i8* %56
br label %21
57: ; preds = %23
%58 = sext i32 %12 to i64
%59 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 %58
store i8 %16, i8* %59
%60 = call i32 @uart_putc(i8 signext %16)
%61 = add nsw i32 %12, 1
br label %21
}