#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ dummy();
int get_mem(int);
int /*<<< orphan*/ put_mem(int, int);
void *uart_get();
int /*<<< orphan*/ uart_init();
int /*<<< orphan*/ uart_puthex(int);
int /*<<< orphan*/ uart_puts(char *);
int kernel_main() {
  uart_init();
  uart_puts("debugc\r\n");
  while (true) {
    const char c = uart_get();
    if (c == 0x02) {
      break;
    }
  }
  bool comment_mode = false;
  uint32_t temp_num = 0x00000000;
  uint8_t num_shift = 28;
  uint32_t mem_pos = 0x4000;
  uint32_t vals_read = 0;
  while (true) {
    char c = uart_get();
    if (c == 0x03) {
      break;
    }
    if (c == 0x23) {
      comment_mode = true;
    }
    if (comment_mode) {
      if (c == 0x10) {
        comment_mode = false;
      }
      continue;
    }
    if (c < 0x30) {
      continue;
    }
    if (c <= 0x39) {
      c -= 0x30;
    } else {
      c -= 0x57;
    }
    temp_num += (((uint32_t)c) << num_shift);
    if (num_shift == 0) {
      num_shift = 28;
      uart_puthex(temp_num);
      put_mem(mem_pos, temp_num);
      mem_pos += sizeof(uint32_t);
      temp_num = 0x00000000;
      ++vals_read;
    } else {
      num_shift -= 4;
    }
  }
  for (int i = 0; i < vals_read; ++i) {
    uart_puthex(get_mem(0x4000 + (i * 4)));
  }
  while (true) {
    dummy();
  }
}


x86 assembly (clang, -O0):
<kernel_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <uart_init@plt>
lea    0xe7a(%rip),%rdi
call   1090 <uart_puts@plt>
mov    $0x0,%al
call   1060 <uart_get@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x2,%eax
jne    11a7 <kernel_main+0x37>
jmp    11ac <kernel_main+0x3c>
jmp    118b <kernel_main+0x1b>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1c,-0x10(%rbp)
movl   $0x4000,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1060 <uart_get@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x3,%eax
jne    11eb <kernel_main+0x7b>
jmp    12bb <kernel_main+0x14b>
movsbl -0x19(%rbp),%eax
cmp    $0x23,%eax
jne    11ff <kernel_main+0x8f>
movl   $0x1,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1222 <kernel_main+0xb2>
movsbl -0x19(%rbp),%eax
cmp    $0x10,%eax
jne    121d <kernel_main+0xad>
movl   $0x0,-0x8(%rbp)
jmp    11cf <kernel_main+0x5f>
movsbl -0x19(%rbp),%eax
cmp    $0x30,%eax
jge    1234 <kernel_main+0xc4>
jmp    11cf <kernel_main+0x5f>
movsbl -0x19(%rbp),%eax
cmp    $0x39,%eax
jg     1250 <kernel_main+0xe0>
movsbl -0x19(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x19(%rbp)
jmp    125a <kernel_main+0xea>
movsbl -0x19(%rbp),%eax
sub    $0x57,%eax
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    %cl,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    12ad <kernel_main+0x13d>
movl   $0x1c,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <uart_puthex@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <put_mem@plt>
movslq -0x14(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12b6 <kernel_main+0x146>
mov    -0x10(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    11cf <kernel_main+0x5f>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12f4 <kernel_main+0x184>
mov    -0x20(%rbp),%edi
shl    $0x2,%edi
add    $0x4000,%edi
call   1070 <get_mem@plt>
mov    %eax,%edi
call   1080 <uart_puthex@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12c2 <kernel_main+0x152>
jmp    12f9 <kernel_main+0x189>
mov    $0x0,%al
call   1030 <dummy@plt>
jmp    12f9 <kernel_main+0x189>

x86 assembly (clang, -O2):
<kernel_main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1040 <uart_init@plt>
lea    0xe7a(%rip),%rdi
call   1090 <uart_puts@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <uart_get@plt>
cmp    $0x2,%al
jne    1190 <kernel_main+0x20>
mov    $0x4000,%r14d
xor    %ebp,%ebp
mov    $0x1c,%r15d
xor    %r12d,%r12d
xor    %ebx,%ebx
jmp    11bb <kernel_main+0x4b>
xor    %ebx,%ebx
cmp    $0x10000000,%ecx
setne  %bl
xor    %eax,%eax
call   1060 <uart_get@plt>
mov    %eax,%ecx
shl    $0x18,%ecx
cmp    $0x23000000,%ecx
je     11b0 <kernel_main+0x40>
cmp    $0x3000000,%ecx
je     1231 <kernel_main+0xc1>
test   %ebx,%ebx
jne    11b0 <kernel_main+0x40>
mov    $0x0,%ebx
cmp    $0x30000000,%ecx
jl     11bb <kernel_main+0x4b>
lea    0xa9(%rax),%edx
add    $0xd0,%eax
cmp    $0x3a000000,%ecx
cmovae %edx,%eax
movsbl %al,%eax
mov    %r15d,%ecx
shl    %cl,%eax
add    %eax,%r12d
test   %r15d,%r15d
je     1212 <kernel_main+0xa2>
add    $0xfffffffc,%r15d
jmp    11ac <kernel_main+0x3c>
mov    %r12d,%edi
call   1080 <uart_puthex@plt>
mov    %r14d,%edi
mov    %r12d,%esi
call   1050 <put_mem@plt>
add    $0x4,%r14d
add    $0x1,%ebp
jmp    11a3 <kernel_main+0x33>
test   %ebp,%ebp
je     1260 <kernel_main+0xf0>
mov    $0x4000,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <get_mem@plt>
mov    %eax,%edi
call   1080 <uart_puthex@plt>
add    $0x4,%ebx
add    $0xffffffff,%ebp
jne    1240 <kernel_main+0xd0>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <dummy@plt>
jmp    1260 <kernel_main+0xf0>

LLVM IR (-O0):
@.str = [9 x i8] c"debugc\0D\0A\00"
define i32 @kernel_main() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = call i32 (...) @uart_init()
%10 = call i32 @uart_puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %0, %18
%12 = call i8* (...) @uart_get()
%13 = ptrtoint i8* %12 to i8
store i8 %13, i8* %1
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %11
br label %19
18: ; preds = %11
br label %11
19: ; preds = %17
store i32 0, i32* %2
store i32 0, i32* %3
store i32 28, i32* %4
store i32 16384, i32* %5
store i32 0, i32* %6
br label %20
20: ; preds = %19, %40, %45, %84
%21 = call i8* (...) @uart_get()
%22 = ptrtoint i8* %21 to i8
store i8 %22, i8* %7
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %27
26: ; preds = %20
br label %85
27: ; preds = %20
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 35
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %2
br label %32
32: ; preds = %31, %27
%33 = load i32, i32* %2
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i8, i8* %7
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 16
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 0, i32* %2
br label %40
40: ; preds = %39, %35
br label %20
41: ; preds = %32
%42 = load i8, i8* %7
%43 = sext i8 %42 to i32
%44 = icmp slt i32 %43, 48
br i1 %44, label %45, label %46
45: ; preds = %41
br label %20
46: ; preds = %41
%47 = load i8, i8* %7
%48 = sext i8 %47 to i32
%49 = icmp sle i32 %48, 57
br i1 %49, label %50, label %55
50: ; preds = %46
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = sub nsw i32 %52, 48
%54 = trunc i32 %53 to i8
store i8 %54, i8* %7
br label %60
55: ; preds = %46
%56 = load i8, i8* %7
%57 = sext i8 %56 to i32
%58 = sub nsw i32 %57, 87
%59 = trunc i32 %58 to i8
store i8 %59, i8* %7
br label %60
60: ; preds = %55, %50
%61 = load i8, i8* %7
%62 = sext i8 %61 to i32
%63 = load i32, i32* %4
%64 = shl i32 %62, %63
%65 = load i32, i32* %3
%66 = add nsw i32 %65, %64
store i32 %66, i32* %3
%67 = load i32, i32* %4
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %81
69: ; preds = %60
store i32 28, i32* %4
%70 = load i32, i32* %3
%71 = call i32 @uart_puthex(i32 %70)
%72 = load i32, i32* %5
%73 = load i32, i32* %3
%74 = call i32 @put_mem(i32 %72, i32 %73)
%75 = load i32, i32* %5
%76 = sext i32 %75 to i64
%77 = add i64 %76, 4
%78 = trunc i64 %77 to i32
store i32 %78, i32* %5
store i32 0, i32* %3
%79 = load i32, i32* %6
%80 = add nsw i32 %79, 1
store i32 %80, i32* %6
br label %84
81: ; preds = %60
%82 = load i32, i32* %4
%83 = sub nsw i32 %82, 4
store i32 %83, i32* %4
br label %84
84: ; preds = %81, %69
br label %20
85: ; preds = %26
store i32 0, i32* %8
br label %86
86: ; preds = %96, %85
%87 = load i32, i32* %8
%88 = load i32, i32* %6
%89 = icmp slt i32 %87, %88
br i1 %89, label %90, label %99
90: ; preds = %86
%91 = load i32, i32* %8
%92 = mul nsw i32 %91, 4
%93 = add nsw i32 16384, %92
%94 = call i32 @get_mem(i32 %93)
%95 = call i32 @uart_puthex(i32 %94)
br label %96
96: ; preds = %90
%97 = load i32, i32* %8
%98 = add nsw i32 %97, 1
store i32 %98, i32* %8
br label %86
99: ; preds = %86
br label %100
100: ; preds = %99, %100
%101 = call i32 (...) @dummy()
br label %100
}

LLVM IR (-O2):
@.str = [9 x i8] c"debugc\0D\0A\00"
define i32 @kernel_main() {
%1 = tail call i32 (...) @uart_init()
%2 = tail call i32 @uart_puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %3
3: ; preds = %3, %0
%4 = tail call i8* (...) @uart_get()
%5 = ptrtoint i8* %4 to i64
%6 = and i64 %5, 255
%7 = icmp eq i64 %6, 2
br i1 %7, label %8, label %3
8: ; preds = %3, %40
%9 = phi i32 [ %43, %40 ], [ 16384, %3 ]
%10 = phi i32 [ %44, %40 ], [ 0, %3 ]
br label %11
11: ; preds = %8, %45
%12 = phi i32 [ %38, %45 ], [ 0, %8 ]
%13 = phi i32 [ %46, %45 ], [ 28, %8 ]
br label %14
14: ; preds = %27, %11
%15 = phi i32 [ 0, %11 ], [ %28, %27 ]
%16 = tail call i8* (...) @uart_get()
%17 = ptrtoint i8* %16 to i64
%18 = trunc i64 %17 to i32
%19 = shl i32 %18, 24
switch i32 %19, label %20 [
i32 50331648, label %47
i32 587202560, label %22
]
20: ; preds = %14
%21 = icmp eq i32 %15, 0
br i1 %21, label %25, label %22
22: ; preds = %14, %20
%23 = icmp ne i32 %19, 268435456
%24 = zext i1 %23 to i32
br label %27
25: ; preds = %20
%26 = icmp slt i32 %19, 805306368
br i1 %26, label %27, label %29
27: ; preds = %25, %22
%28 = phi i32 [ 0, %25 ], [ %24, %22 ]
br label %14
29: ; preds = %25
%30 = ptrtoint i8* %16 to i64
%31 = trunc i64 %30 to i32
%32 = icmp ult i32 %19, 973078528
%33 = select i1 %32, i32 208, i32 169
%34 = add i32 %33, %31
%35 = shl i32 %34, 24
%36 = ashr exact i32 %35, 24
%37 = shl i32 %36, %13
%38 = add nsw i32 %37, %12
%39 = icmp eq i32 %13, 0
br i1 %39, label %40, label %45
40: ; preds = %29
%41 = tail call i32 @uart_puthex(i32 %38)
%42 = tail call i32 @put_mem(i32 %9, i32 %38)
%43 = add i32 %9, 4
%44 = add nuw nsw i32 %10, 1
br label %8
45: ; preds = %29
%46 = add nsw i32 %13, -4
br label %11
47: ; preds = %14
%48 = icmp eq i32 %10, 0
br i1 %48, label %57, label %49
49: ; preds = %47, %49
%50 = phi i32 [ %55, %49 ], [ 0, %47 ]
%51 = shl nsw i32 %50, 2
%52 = add nuw nsw i32 %51, 16384
%53 = tail call i32 @get_mem(i32 %52)
%54 = tail call i32 @uart_puthex(i32 %53)
%55 = add nuw nsw i32 %50, 1
%56 = icmp eq i32 %55, %10
br i1 %56, label %57, label %49
57: ; preds = %49, %47
br label %58
58: ; preds = %57, %58
%59 = tail call i32 (...) @dummy()
br label %58
}