#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*/ clearHeap();
int /*<<< orphan*/ clearMemory();
int /*<<< orphan*/ clearStack();
int displayMenu();
int /*<<< orphan*/ dumpCPU();
int /*<<< orphan*/ dumpHeap();
int /*<<< orphan*/ dumpMemory();
int /*<<< orphan*/ dumpStack();
int /*<<< orphan*/ initializeCPU();
int /*<<< orphan*/ isHalt();
int /*<<< orphan*/ loadProgram();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ run();
int main() {
  int menuChoice;
  int exitFlag = 0;
  clearMemory();
  clearHeap();
  initializeCPU();
  clearStack();
  printf("=======================\n");
  printf("== Welcome to my CPU ==\n");
  printf("=======================\n\n");
  while (!exitFlag) {
    menuChoice = displayMenu();
    switch (menuChoice) {
    case '1':
      initializeCPU();
      break;
    case '2':
      clearMemory();
      break;
    case '3':
      dumpCPU();
      break;
    case '4':
      dumpMemory();
      break;
    case '5':
      dumpHeap();
      break;
    case '6':
      dumpStack();
      break;
    case '7':
      if (isHalt()) {
        printf("ERROR: CPU is HALTED! Cannot run!\n");
      } else {
        run();
      }
      break;
    case '8':
      loadProgram();
      break;
    case 'X':
    case 'x':
      exitFlag = 1;
      break;
    default:
      printf("ERROR: Bad selection! Try again.\n");
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   10a0 <clearMemory@plt>
mov    $0x0,%al
call   1080 <clearHeap@plt>
mov    $0x0,%al
call   1040 <initializeCPU@plt>
mov    $0x0,%al
call   10f0 <clearStack@plt>
lea    0xf17(%rip),%rdi
call   1060 <printf@plt>
lea    0xf24(%rip),%rdi
call   1060 <printf@plt>
lea    0xf31(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123c <main+0x6c>
jmp    130d <main+0x13d>
xor    %eax,%eax
call   1030 <displayMenu@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffcf,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x47,%eax
ja     12fc <main+0x12c>
mov    -0x18(%rbp),%rax
lea    0xd9a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   1040 <initializeCPU@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   10a0 <clearMemory@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   1070 <dumpCPU@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   1090 <dumpMemory@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   10d0 <dumpHeap@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   10b0 <dumpStack@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   10e0 <isHalt@plt>
cmp    $0x0,%eax
je     12d8 <main+0x108>
lea    0xe9e(%rip),%rdi
call   1060 <printf@plt>
jmp    12df <main+0x10f>
mov    $0x0,%al
call   1050 <run@plt>
jmp    1308 <main+0x138>
mov    $0x0,%al
call   10c0 <loadProgram@plt>
jmp    1308 <main+0x138>
movl   $0x1,-0xc(%rbp)
jmp    1308 <main+0x138>
lea    0xe8c(%rip),%rdi
call   1060 <printf@plt>
jmp    1226 <main+0x56>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   10a0 <clearMemory@plt>
xor    %eax,%eax
call   1080 <clearHeap@plt>
xor    %eax,%eax
call   1050 <initializeCPU@plt>
xor    %eax,%eax
call   10f0 <clearStack@plt>
lea    0xf28(%rip),%rdi
call   1030 <puts@plt>
lea    0xf34(%rip),%rdi
call   1030 <puts@plt>
lea    0xf40(%rip),%rdi
call   1030 <puts@plt>
lea    0xf6f(%rip),%r15
lea    0xddd(%rip),%rbx
lea    0xf3f(%rip),%r14
jmp    1240 <main+0x70>
xor    %eax,%eax
call   10c0 <loadProgram@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1040 <displayMenu@plt>
add    $0xffffffcf,%eax
cmp    $0x47,%eax
ja     12a3 <main+0xd3>
movslq (%rbx,%rax,4),%rax
add    %rbx,%rax
jmp    *%rax
xor    %eax,%eax
call   1050 <initializeCPU@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   10a0 <clearMemory@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   1070 <dumpCPU@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   1090 <dumpMemory@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   10d0 <dumpHeap@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   10b0 <dumpStack@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   10e0 <isHalt@plt>
test   %eax,%eax
je     12ad <main+0xdd>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    1240 <main+0x70>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
call   1060 <run@plt>
jmp    1240 <main+0x70>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"=======================\0A\00"
@.str.1 = [25 x i8] c"== Welcome to my CPU ==\0A\00"
@.str.2 = [26 x i8] c"=======================\0A\0A\00"
@.str.3 = [35 x i8] c"ERROR: CPU is HALTED! Cannot run!\0A\00"
@.str.4 = [34 x i8] c"ERROR: Bad selection! Try again.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i32 (...) @clearMemory()
%5 = call i32 (...) @clearHeap()
%6 = call i32 (...) @initializeCPU()
%7 = call i32 (...) @clearStack()
%8 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %43, %0
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %44
15: ; preds = %11
%16 = call i32 (...) @displayMenu()
store i32 %16, i32* %2
%17 = load i32, i32* %2
switch i32 %17, label %41 [
i32 49, label %18
i32 50, label %20
i32 51, label %22
i32 52, label %24
i32 53, label %26
i32 54, label %28
i32 55, label %30
i32 56, label %38
i32 88, label %40
i32 120, label %40
]
18: ; preds = %15
%19 = call i32 (...) @initializeCPU()
br label %43
20: ; preds = %15
%21 = call i32 (...) @clearMemory()
br label %43
22: ; preds = %15
%23 = call i32 (...) @dumpCPU()
br label %43
24: ; preds = %15
%25 = call i32 (...) @dumpMemory()
br label %43
26: ; preds = %15
%27 = call i32 (...) @dumpHeap()
br label %43
28: ; preds = %15
%29 = call i32 (...) @dumpStack()
br label %43
30: ; preds = %15
%31 = call i32 (...) @isHalt()
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %37
35: ; preds = %30
%36 = call i32 (...) @run()
br label %37
37: ; preds = %35, %33
br label %43
38: ; preds = %15
%39 = call i32 (...) @loadProgram()
br label %43
40: ; preds = %15, %15
store i32 1, i32* %3
br label %43
41: ; preds = %15
%42 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %40, %38, %37, %28, %26, %24, %22, %20, %18
br label %11
44: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@str = [24 x i8] c"=======================\00"
@str.5 = [24 x i8] c"== Welcome to my CPU ==\00"
@str.6 = [25 x i8] c"=======================\0A\00"
@str.7 = [34 x i8] c"ERROR: CPU is HALTED! Cannot run!\00"
@str.8 = [33 x i8] c"ERROR: Bad selection! Try again.\00"
define i32 @main() {
%1 = tail call i32 (...) @clearMemory()
%2 = tail call i32 (...) @clearHeap()
%3 = tail call i32 (...) @initializeCPU()
%4 = tail call i32 (...) @clearStack()
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %8
8: ; preds = %27, %0
%9 = tail call i32 (...) @displayMenu()
switch i32 %9, label %32 [
i32 49, label %10
i32 50, label %12
i32 51, label %14
i32 52, label %16
i32 53, label %18
i32 54, label %20
i32 55, label %22
i32 56, label %30
i32 88, label %34
i32 120, label %34
]
10: ; preds = %8
%11 = tail call i32 (...) @initializeCPU()
br label %27
12: ; preds = %8
%13 = tail call i32 (...) @clearMemory()
br label %27
14: ; preds = %8
%15 = tail call i32 (...) @dumpCPU()
br label %27
16: ; preds = %8
%17 = tail call i32 (...) @dumpMemory()
br label %27
18: ; preds = %8
%19 = tail call i32 (...) @dumpHeap()
br label %27
20: ; preds = %8
%21 = tail call i32 (...) @dumpStack()
br label %27
22: ; preds = %8
%23 = tail call i32 (...) @isHalt()
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.7, i64 0, i64 0))
br label %27
27: ; preds = %25, %28, %32, %30, %20, %18, %16, %14, %12, %10
br label %8
28: ; preds = %22
%29 = tail call i32 (...) @run()
br label %27
30: ; preds = %8
%31 = tail call i32 (...) @loadProgram()
br label %27
32: ; preds = %8
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
br label %27
34: ; preds = %8, %8
ret i32 0
}