#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*/ delay(unsigned int);
unsigned int read_from_address(int);
int /*<<< orphan*/ uart_write_string(char *);
int /*<<< orphan*/ write_to_address(int, int);
int kernel_main(void) {
  unsigned int register_a;
  unsigned int register_b;
  write_to_address(0x3F200094, 0);
  for (register_a = 0; register_a < 150; register_a++) {
    delay(register_a);
  }
  write_to_address(0x3F200098, (1 << 14) | (1 << 15) | (1 << 21));
  for (register_a = 0; register_a < 150; register_a++) {
    delay(register_a);
  }
  write_to_address(0x3F200098, 0);
  write_to_address(0x3F201044, 0x7FF);
  write_to_address(0x3F201024, 1);
  write_to_address(0x3F201028, 40);
  write_to_address(0x3F20102C, (1 << 4) | (1 << 5) | (1 << 6));
  write_to_address(0x3F201038, (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) |
                                   (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10));
  write_to_address(0x3F201030, (1 << 0) | (1 << 8) | (1 << 9));
  write_to_address(0x3F00B408, 0x00F90000);
  write_to_address(0x3F00B408, 0x00F90200);
  register_a = read_from_address(0x3F200010);
  register_a |= (1 << 21);
  write_to_address(0x3F200010, register_a);
  register_b = read_from_address(0x3F00B420);
  while (1) {
    write_to_address(0x3F200020, 1 << 15);
    uart_write_string("Hello, kernel world!\r\n");
    while (1) {
      register_a = read_from_address(0x3F00B420);
      if ((register_a - register_b) >= 1000000) {
        break;
      }
    }
    register_b += 1000000;
    write_to_address(0x3F20002C, 1 << 15);
    uart_write_string("Goodbye, kernel world!\r\n");
    while (1) {
      register_a = read_from_address(0x3F00B420);
      if ((register_a - register_b) >= 1000000) {
        break;
      }
    }
    register_b += 1000000;
  }
  return (0);
}


x86 assembly (clang, -O0):
<kernel_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x3f200094,%edi
xor    %esi,%esi
call   1060 <write_to_address@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x96,-0x4(%rbp)
jae    117e <kernel_main+0x3e>
mov    -0x4(%rbp),%edi
call   1040 <delay@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    115b <kernel_main+0x1b>
mov    $0x3f200098,%edi
mov    $0x20c000,%esi
call   1060 <write_to_address@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x96,-0x4(%rbp)
jae    11b7 <kernel_main+0x77>
mov    -0x4(%rbp),%edi
call   1040 <delay@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1194 <kernel_main+0x54>
mov    $0x3f200098,%edi
xor    %esi,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201044,%edi
mov    $0x7ff,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201024,%edi
mov    $0x1,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201028,%edi
mov    $0x28,%esi
call   1060 <write_to_address@plt>
mov    $0x3f20102c,%edi
mov    $0x70,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201038,%edi
mov    $0x7f2,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201030,%edi
mov    $0x301,%esi
call   1060 <write_to_address@plt>
mov    $0x3f00b408,%edi
mov    $0xf90000,%esi
call   1060 <write_to_address@plt>
mov    $0x3f00b408,%edi
mov    $0xf90200,%esi
call   1060 <write_to_address@plt>
mov    $0x3f200010,%edi
call   1030 <read_from_address@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
or     $0x200000,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
mov    $0x3f200010,%edi
call   1060 <write_to_address@plt>
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
mov    %eax,-0x8(%rbp)
mov    $0x3f200020,%edi
mov    $0x8000,%esi
call   1060 <write_to_address@plt>
lea    0xd7d(%rip),%rdi
call   1050 <uart_write_string@plt>
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0xf4240,%eax
jb     12ab <kernel_main+0x16b>
jmp    12b0 <kernel_main+0x170>
jmp    1288 <kernel_main+0x148>
mov    -0x8(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x8(%rbp)
mov    $0x3f20002c,%edi
mov    $0x8000,%esi
call   1060 <write_to_address@plt>
lea    0xd46(%rip),%rdi
call   1050 <uart_write_string@plt>
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0xf4240,%eax
jb     12f9 <kernel_main+0x1b9>
jmp    12fe <kernel_main+0x1be>
jmp    12d6 <kernel_main+0x196>
mov    -0x8(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x8(%rbp)
jmp    126d <kernel_main+0x12d>

x86 assembly (clang, -O2):
<kernel_main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
mov    $0x3f200094,%edi
xor    %esi,%esi
call   1060 <write_to_address@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1040 <delay@plt>
add    $0x1,%ebx
cmp    $0x96,%ebx
jne    1160 <kernel_main+0x20>
mov    $0x3f200098,%edi
mov    $0x20c000,%esi
call   1060 <write_to_address@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1040 <delay@plt>
add    $0x1,%ebx
cmp    $0x96,%ebx
jne    1190 <kernel_main+0x50>
mov    $0x3f200098,%edi
xor    %esi,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201044,%edi
mov    $0x7ff,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201024,%edi
mov    $0x1,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201028,%edi
mov    $0x28,%esi
call   1060 <write_to_address@plt>
mov    $0x3f20102c,%edi
mov    $0x70,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201038,%edi
mov    $0x7f2,%esi
call   1060 <write_to_address@plt>
mov    $0x3f201030,%edi
mov    $0x301,%esi
call   1060 <write_to_address@plt>
mov    $0x3f00b408,%edi
mov    $0xf90000,%esi
call   1060 <write_to_address@plt>
mov    $0x3f00b408,%edi
mov    $0xf90200,%esi
call   1060 <write_to_address@plt>
mov    $0x3f200010,%edi
call   1030 <read_from_address@plt>
or     $0x200000,%eax
mov    $0x3f200010,%edi
mov    %eax,%esi
call   1060 <write_to_address@plt>
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
mov    %eax,%ebx
lea    0xdac(%rip),%r14
lea    0xdbc(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    $0x3f200020,%edi
mov    $0x8000,%esi
call   1060 <write_to_address@plt>
mov    %r14,%rdi
call   1050 <uart_write_string@plt>
nopw   0x0(%rax,%rax,1)
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
sub    %ebx,%eax
cmp    $0xf4240,%eax
jb     1280 <kernel_main+0x140>
mov    $0x3f20002c,%edi
mov    $0x8000,%esi
call   1060 <write_to_address@plt>
mov    %r15,%rdi
call   1050 <uart_write_string@plt>
mov    $0xfff0bdc0,%ebp
sub    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x3f00b420,%edi
call   1030 <read_from_address@plt>
add    %ebp,%eax
cmp    $0xf4240,%eax
jb     12c0 <kernel_main+0x180>
add    $0x1e8480,%ebx
jmp    1260 <kernel_main+0x120>

LLVM IR (-O0):
@.str = [23 x i8] c"Hello, kernel world!\0D\0A\00"
@.str.1 = [25 x i8] c"Goodbye, kernel world!\0D\0A\00"
define i32 @kernel_main() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @write_to_address(i32 1059061908, i32 0)
store i32 0, i32* %1
br label %4
4: ; preds = %10, %0
%5 = load i32, i32* %1
%6 = icmp ult i32 %5, 150
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = load i32, i32* %1
%9 = call i32 @delay(i32 %8)
br label %10
10: ; preds = %7
%11 = load i32, i32* %1
%12 = add i32 %11, 1
store i32 %12, i32* %1
br label %4
13: ; preds = %4
%14 = call i32 @write_to_address(i32 1059061912, i32 2146304)
store i32 0, i32* %1
br label %15
15: ; preds = %21, %13
%16 = load i32, i32* %1
%17 = icmp ult i32 %16, 150
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32, i32* %1
%20 = call i32 @delay(i32 %19)
br label %21
21: ; preds = %18
%22 = load i32, i32* %1
%23 = add i32 %22, 1
store i32 %23, i32* %1
br label %15
24: ; preds = %15
%25 = call i32 @write_to_address(i32 1059061912, i32 0)
%26 = call i32 @write_to_address(i32 1059065924, i32 2047)
%27 = call i32 @write_to_address(i32 1059065892, i32 1)
%28 = call i32 @write_to_address(i32 1059065896, i32 40)
%29 = call i32 @write_to_address(i32 1059065900, i32 112)
%30 = call i32 @write_to_address(i32 1059065912, i32 2034)
%31 = call i32 @write_to_address(i32 1059065904, i32 769)
%32 = call i32 @write_to_address(i32 1057010696, i32 16318464)
%33 = call i32 @write_to_address(i32 1057010696, i32 16318976)
%34 = call i32 @read_from_address(i32 1059061776)
store i32 %34, i32* %1
%35 = load i32, i32* %1
%36 = or i32 %35, 2097152
store i32 %36, i32* %1
%37 = load i32, i32* %1
%38 = call i32 @write_to_address(i32 1059061776, i32 %37)
%39 = call i32 @read_from_address(i32 1057010720)
store i32 %39, i32* %2
br label %40
40: ; preds = %24, %64
%41 = call i32 @write_to_address(i32 1059061792, i32 32768)
%42 = call i32 @uart_write_string(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %40, %50
%44 = call i32 @read_from_address(i32 1057010720)
store i32 %44, i32* %1
%45 = load i32, i32* %1
%46 = load i32, i32* %2
%47 = sub i32 %45, %46
%48 = icmp uge i32 %47, 1000000
br i1 %48, label %49, label %50
49: ; preds = %43
br label %51
50: ; preds = %43
br label %43
51: ; preds = %49
%52 = load i32, i32* %2
%53 = add i32 %52, 1000000
store i32 %53, i32* %2
%54 = call i32 @write_to_address(i32 1059061804, i32 32768)
%55 = call i32 @uart_write_string(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %51, %63
%57 = call i32 @read_from_address(i32 1057010720)
store i32 %57, i32* %1
%58 = load i32, i32* %1
%59 = load i32, i32* %2
%60 = sub i32 %58, %59
%61 = icmp uge i32 %60, 1000000
br i1 %61, label %62, label %63
62: ; preds = %56
br label %64
63: ; preds = %56
br label %56
64: ; preds = %62
%65 = load i32, i32* %2
%66 = add i32 %65, 1000000
store i32 %66, i32* %2
br label %40
}

LLVM IR (-O2):
@.str = [23 x i8] c"Hello, kernel world!\0D\0A\00"
@.str.1 = [25 x i8] c"Goodbye, kernel world!\0D\0A\00"
define i32 @kernel_main() {
%1 = tail call i32 @write_to_address(i32 1059061908, i32 0)
br label %2
2: ; preds = %0, %2
%3 = phi i32 [ 0, %0 ], [ %5, %2 ]
%4 = tail call i32 @delay(i32 %3)
%5 = add nuw nsw i32 %3, 1
%6 = icmp eq i32 %5, 150
br i1 %6, label %7, label %2
7: ; preds = %2
%8 = tail call i32 @write_to_address(i32 1059061912, i32 2146304)
br label %9
9: ; preds = %7, %9
%10 = phi i32 [ 0, %7 ], [ %12, %9 ]
%11 = tail call i32 @delay(i32 %10)
%12 = add nuw nsw i32 %10, 1
%13 = icmp eq i32 %12, 150
br i1 %13, label %14, label %9
14: ; preds = %9
%15 = tail call i32 @write_to_address(i32 1059061912, i32 0)
%16 = tail call i32 @write_to_address(i32 1059065924, i32 2047)
%17 = tail call i32 @write_to_address(i32 1059065892, i32 1)
%18 = tail call i32 @write_to_address(i32 1059065896, i32 40)
%19 = tail call i32 @write_to_address(i32 1059065900, i32 112)
%20 = tail call i32 @write_to_address(i32 1059065912, i32 2034)
%21 = tail call i32 @write_to_address(i32 1059065904, i32 769)
%22 = tail call i32 @write_to_address(i32 1057010696, i32 16318464)
%23 = tail call i32 @write_to_address(i32 1057010696, i32 16318976)
%24 = tail call i32 @read_from_address(i32 1059061776)
%25 = or i32 %24, 2097152
%26 = tail call i32 @write_to_address(i32 1059061776, i32 %25)
%27 = tail call i32 @read_from_address(i32 1057010720)
br label %28
28: ; preds = %44, %14
%29 = phi i32 [ %27, %14 ], [ %45, %44 ]
%30 = tail call i32 @write_to_address(i32 1059061792, i32 32768)
%31 = tail call i32 @uart_write_string(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %32, %28
%33 = tail call i32 @read_from_address(i32 1057010720)
%34 = sub i32 %33, %29
%35 = icmp ugt i32 %34, 999999
br i1 %35, label %36, label %32
36: ; preds = %32
%37 = tail call i32 @write_to_address(i32 1059061804, i32 32768)
%38 = tail call i32 @uart_write_string(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%39 = sub i32 -1000000, %29
br label %40
40: ; preds = %40, %36
%41 = tail call i32 @read_from_address(i32 1057010720)
%42 = add i32 %39, %41
%43 = icmp ugt i32 %42, 999999
br i1 %43, label %44, label %40
44: ; preds = %40
%45 = add i32 %29, 2000000
br label %28
}