#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 ALU(int, int, int, int, int *, int *, int *);
int decode(int, int, int);
int fetch_instruction(int);
int /*<<< orphan*/ hex32(int);
int /*<<< orphan*/ increment_PC(int *);
int /*<<< orphan*/ printf(char *, ...);
int read_ram(int);
int /*<<< orphan*/ show_ram(int, int);
int /*<<< orphan*/ write_ram(int, int);
void CPU_old() {
  int instruction;
  int PC;
  int MAR;
  int DRR;
  int A;
  int opcodes, address, Binvert, cpu_codes;
  int carryOut;
  int overflow;
  int zr;
  int op_shift = 16, address_shift = 0, Binvert_shift = 18, CPUcodes_shift = 20;
  int mask_CPUcodes = 0x00f00000;
  int mask_opcodes = 0x00030000;
  int mask_Binvert = 0x00040000;
  int mask_address = 0x0000ffff;
  PC = 0;
  int clock_counter = 0;
  while ((clock_counter++) < 100) {
    printf("Clock Cycle: %d\tPC: %d\n", clock_counter, PC);
    show_ram(PC, PC);
    MAR = PC;
    increment_PC(&PC);
    instruction = fetch_instruction(MAR);
    cpu_codes = decode(instruction, mask_CPUcodes, CPUcodes_shift);
    opcodes = decode(instruction, mask_opcodes, op_shift);
    address = decode(instruction, mask_address, address_shift);
    Binvert = decode(instruction, mask_Binvert, Binvert_shift);
    if (cpu_codes == 1) {
      A = read_ram(address);
      continue;
    } else if (cpu_codes == 2) {
      A = address;
      continue;
    } else if (cpu_codes == 3) {
      write_ram(A, address);
      continue;
    } else if (cpu_codes == 4) {
      A += 1;
      continue;
    } else if (cpu_codes == 12) {
      printf("Syscall output: %d\n", A);
      continue;
    } else if (cpu_codes == 13) {
      PC = address;
      continue;
    } else if (cpu_codes == 14) {
      continue;
    } else if (cpu_codes == 15) {
      printf("Program Halted execution at PC: %d\n", PC);
      return;
    }
    DRR = read_ram(address);
    printf("A_reg: ");
    hex32(A);
    printf("B_reg: ");
    hex32(DRR);
    A = ALU(A, DRR, opcodes, Binvert, &carryOut, &zr, &overflow);
    clock_counter++;
  }
}


x86 assembly (clang, -O0):
<CPU_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x10,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x12,-0x3c(%rbp)
movl   $0x14,-0x40(%rbp)
movl   $0xf00000,-0x44(%rbp)
movl   $0x30000,-0x48(%rbp)
movl   $0x40000,-0x4c(%rbp)
movl   $0xffff,-0x50(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x54(%rbp)
cmp    $0x64,%eax
jge    13c0 <CPU_old+0x230>
mov    -0x54(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <show_ram@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
call   1030 <increment_PC@plt>
mov    -0xc(%rbp),%edi
call   1040 <fetch_instruction@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%edx
call   1080 <decode@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1080 <decode@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x50(%rbp),%esi
mov    -0x38(%rbp),%edx
call   1080 <decode@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x4c(%rbp),%esi
mov    -0x3c(%rbp),%edx
call   1080 <decode@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1289 <CPU_old+0xf9>
mov    -0x1c(%rbp),%edi
call   1090 <read_ram@plt>
mov    %eax,-0x14(%rbp)
jmp    11de <CPU_old+0x4e>
cmpl   $0x2,-0x24(%rbp)
jne    129e <CPU_old+0x10e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11de <CPU_old+0x4e>
cmpl   $0x3,-0x24(%rbp)
jne    12b8 <CPU_old+0x128>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10b0 <write_ram@plt>
jmp    11de <CPU_old+0x4e>
cmpl   $0x4,-0x24(%rbp)
jne    12d0 <CPU_old+0x140>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11de <CPU_old+0x4e>
cmpl   $0xc,-0x24(%rbp)
jne    12f0 <CPU_old+0x160>
mov    -0x14(%rbp),%esi
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11de <CPU_old+0x4e>
cmpl   $0xd,-0x24(%rbp)
jne    1305 <CPU_old+0x175>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    11de <CPU_old+0x4e>
cmpl   $0xe,-0x24(%rbp)
jne    1314 <CPU_old+0x184>
jmp    11de <CPU_old+0x4e>
cmpl   $0xf,-0x24(%rbp)
jne    1334 <CPU_old+0x1a4>
mov    -0x8(%rbp),%esi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13c0 <CPU_old+0x230>
jmp    1339 <CPU_old+0x1a9>
jmp    133e <CPU_old+0x1ae>
jmp    1343 <CPU_old+0x1b3>
jmp    1348 <CPU_old+0x1b8>
jmp    134d <CPU_old+0x1bd>
jmp    1352 <CPU_old+0x1c2>
jmp    1357 <CPU_old+0x1c7>
mov    -0x1c(%rbp),%edi
call   1090 <read_ram@plt>
mov    %eax,-0x10(%rbp)
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   10a0 <hex32@plt>
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   10a0 <hex32@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
lea    -0x2c(%rbp),%rax
mov    %rax,(%rsp)
call   1060 <ALU@plt>
mov    %eax,-0x14(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11de <CPU_old+0x4e>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CPU_old>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x0,0x10(%rsp)
mov    $0x1,%eax
xor    %edx,%edx
movl   $0x0,0x14(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%r12d
lea    0xe72(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%edi
mov    %edi,%esi
call   1070 <show_ram@plt>
mov    0x10(%rsp),%ebx
lea    0x10(%rsp),%rdi
call   1030 <increment_PC@plt>
mov    %ebx,%edi
call   1040 <fetch_instruction@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0xf00000,%esi
mov    $0x14,%edx
call   1080 <decode@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    $0x30000,%esi
mov    $0x10,%edx
call   1080 <decode@plt>
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
mov    $0xffff,%esi
xor    %edx,%edx
call   1080 <decode@plt>
mov    %eax,%r13d
mov    %ebp,%edi
mov    $0x40000,%esi
mov    $0x12,%edx
call   1080 <decode@plt>
mov    %eax,%r14d
add    $0xffffffff,%ebx
cmp    $0xe,%ebx
ja     128a <CPU_old+0xfa>
lea    0xdaf(%rip),%rcx
movslq (%rcx,%rbx,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %r13d,%edi
call   1090 <read_ram@plt>
mov    %eax,%r13d
cmp    $0x63,%r12d
jle    1326 <CPU_old+0x196>
jmp    1351 <CPU_old+0x1c1>
mov    %r15d,%edi
mov    %r13d,%esi
call   10b0 <write_ram@plt>
jmp    1309 <CPU_old+0x179>
add    $0x1,%r15d
jmp    1309 <CPU_old+0x179>
mov    %r13d,%edi
call   1090 <read_ram@plt>
mov    %eax,%ebx
lea    0xdf1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   10a0 <hex32@plt>
lea    0xde3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   10a0 <hex32@plt>
lea    0x20(%rsp),%rax
mov    %rax,(%rsp)
mov    %r15d,%edi
mov    %ebx,%esi
mov    0x18(%rsp),%edx
mov    %r14d,%ecx
lea    0x24(%rsp),%r8
lea    0x1c(%rsp),%r9
call   1060 <ALU@plt>
mov    %eax,%r13d
mov    0x14(%rsp),%eax
add    $0x2,%eax
mov    %eax,%r12d
cmp    $0x63,%r12d
jle    1326 <CPU_old+0x196>
jmp    1351 <CPU_old+0x1c1>
lea    0xd55(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%r13d
cmp    $0x63,%r12d
jle    1326 <CPU_old+0x196>
jmp    1351 <CPU_old+0x1c1>
mov    %r13d,0x10(%rsp)
mov    %r15d,%r13d
nopl   0x0(%rax)
cmp    $0x63,%r12d
jg     1351 <CPU_old+0x1c1>
lea    0x1(%r12),%eax
mov    0x10(%rsp),%edx
mov    %r12d,%ecx
mov    %r12d,0x14(%rsp)
mov    %r13d,%r15d
jmp    11c0 <CPU_old+0x30>
mov    0x10(%rsp),%esi
lea    0xd1e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Clock Cycle: %d\09PC: %d\0A\00"
@.str.1 = [20 x i8] c"Syscall output: %d\0A\00"
@.str.2 = [36 x i8] c"Program Halted execution at PC: %d\0A\00"
@.str.3 = [8 x i8] c"A_reg: \00"
@.str.4 = [8 x i8] c"B_reg: \00"
define void @CPU_old() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i32 16, i32* %13
store i32 0, i32* %14
store i32 18, i32* %15
store i32 20, i32* %16
store i32 15728640, i32* %17
store i32 196608, i32* %18
store i32 262144, i32* %19
store i32 65535, i32* %20
store i32 0, i32* %2
store i32 0, i32* %21
br label %22
22: ; preds = %104, %90, %85, %79, %73, %66, %61, %55, %0
%23 = load i32, i32* %21
%24 = add nsw i32 %23, 1
store i32 %24, i32* %21
%25 = icmp slt i32 %23, 100
br i1 %25, label %26, label %120
26: ; preds = %22
%27 = load i32, i32* %21
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28)
%30 = load i32, i32* %2
%31 = load i32, i32* %2
%32 = call i32 @show_ram(i32 %30, i32 %31)
%33 = load i32, i32* %2
store i32 %33, i32* %3
%34 = call i32 @increment_PC(i32* %2)
%35 = load i32, i32* %3
%36 = call i32 @fetch_instruction(i32 %35)
store i32 %36, i32* %1
%37 = load i32, i32* %1
%38 = load i32, i32* %17
%39 = load i32, i32* %16
%40 = call i32 @decode(i32 %37, i32 %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %1
%42 = load i32, i32* %18
%43 = load i32, i32* %13
%44 = call i32 @decode(i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %1
%46 = load i32, i32* %20
%47 = load i32, i32* %14
%48 = call i32 @decode(i32 %45, i32 %46, i32 %47)
store i32 %48, i32* %7
%49 = load i32, i32* %1
%50 = load i32, i32* %19
%51 = load i32, i32* %15
%52 = call i32 @decode(i32 %49, i32 %50, i32 %51)
store i32 %52, i32* %8
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %26
%56 = load i32, i32* %7
%57 = call i32 @read_ram(i32 %56)
store i32 %57, i32* %5
br label %22
58: ; preds = %26
%59 = load i32, i32* %9
%60 = icmp eq i32 %59, 2
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = load i32, i32* %7
store i32 %62, i32* %5
br label %22
63: ; preds = %58
%64 = load i32, i32* %9
%65 = icmp eq i32 %64, 3
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i32, i32* %5
%68 = load i32, i32* %7
%69 = call i32 @write_ram(i32 %67, i32 %68)
br label %22
70: ; preds = %63
%71 = load i32, i32* %9
%72 = icmp eq i32 %71, 4
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* %5
%75 = add nsw i32 %74, 1
store i32 %75, i32* %5
br label %22
76: ; preds = %70
%77 = load i32, i32* %9
%78 = icmp eq i32 %77, 12
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32, i32* %5
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %80)
br label %22
82: ; preds = %76
%83 = load i32, i32* %9
%84 = icmp eq i32 %83, 13
br i1 %84, label %85, label %87
85: ; preds = %82
%86 = load i32, i32* %7
store i32 %86, i32* %2
br label %22
87: ; preds = %82
%88 = load i32, i32* %9
%89 = icmp eq i32 %88, 14
br i1 %89, label %90, label %91
90: ; preds = %87
br label %22
91: ; preds = %87
%92 = load i32, i32* %9
%93 = icmp eq i32 %92, 15
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i32, i32* %2
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %95)
br label %120
97: ; preds = %91
br label %98
98: ; preds = %97
br label %99
99: ; preds = %98
br label %100
100: ; preds = %99
br label %101
101: ; preds = %100
br label %102
102: ; preds = %101
br label %103
103: ; preds = %102
br label %104
104: ; preds = %103
%105 = load i32, i32* %7
%106 = call i32 @read_ram(i32 %105)
store i32 %106, i32* %4
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%108 = load i32, i32* %5
%109 = call i32 @hex32(i32 %108)
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%111 = load i32, i32* %4
%112 = call i32 @hex32(i32 %111)
%113 = load i32, i32* %5
%114 = load i32, i32* %4
%115 = load i32, i32* %6
%116 = load i32, i32* %8
%117 = call i32 @ALU(i32 %113, i32 %114, i32 %115, i32 %116, i32* %10, i32* %12, i32* %11)
store i32 %117, i32* %5
%118 = load i32, i32* %21
%119 = add nsw i32 %118, 1
store i32 %119, i32* %21
br label %22
120: ; preds = %94, %22
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"Clock Cycle: %d\09PC: %d\0A\00"
@.str.1 = [20 x i8] c"Syscall output: %d\0A\00"
@.str.2 = [36 x i8] c"Program Halted execution at PC: %d\0A\00"
@.str.3 = [8 x i8] c"A_reg: \00"
@.str.4 = [8 x i8] c"B_reg: \00"
define void @CPU_old() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %1
br label %10
9: ; preds = %10
br label %27
10: ; preds = %31, %0
%11 = phi i32 [ 0, %0 ], [ %33, %31 ]
%12 = phi i32 [ 1, %0 ], [ %32, %31 ]
%13 = phi i32 [ 0, %0 ], [ %29, %31 ]
%14 = phi i32 [ undef, %0 ], [ %28, %31 ]
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %11)
%16 = load i32, i32* %1
%17 = call i32 @show_ram(i32 %16, i32 %16)
%18 = load i32, i32* %1
%19 = call i32 @increment_PC(i32* %1)
%20 = call i32 @fetch_instruction(i32 %18)
%21 = call i32 @decode(i32 %20, i32 15728640, i32 20)
%22 = call i32 @decode(i32 %20, i32 196608, i32 16)
%23 = call i32 @decode(i32 %20, i32 65535, i32 0)
%24 = call i32 @decode(i32 %20, i32 262144, i32 18)
switch i32 %21, label %44 [
i32 1, label %25
i32 2, label %27
i32 3, label %34
i32 4, label %36
i32 12, label %38
i32 13, label %40
i32 14, label %9
i32 15, label %41
]
25: ; preds = %10
%26 = call i32 @read_ram(i32 %23)
br label %27
27: ; preds = %25, %34, %36, %38, %40, %44, %9, %10
%28 = phi i32 [ %26, %25 ], [ %14, %34 ], [ %37, %36 ], [ %14, %38 ], [ %14, %40 ], [ %50, %44 ], [ %23, %10 ], [ %14, %9 ]
%29 = phi i32 [ %12, %25 ], [ %12, %34 ], [ %12, %36 ], [ %12, %38 ], [ %12, %40 ], [ %51, %44 ], [ %12, %10 ], [ %12, %9 ]
%30 = icmp slt i32 %29, 100
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = add nsw i32 %29, 1
%33 = load i32, i32* %1
br label %10
34: ; preds = %10
%35 = call i32 @write_ram(i32 %14, i32 %23)
br label %27
36: ; preds = %10
%37 = add nsw i32 %14, 1
br label %27
38: ; preds = %10
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %27
40: ; preds = %10
store i32 %23, i32* %1
br label %27
41: ; preds = %10
%42 = load i32, i32* %1
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %42)
br label %52
44: ; preds = %10
%45 = call i32 @read_ram(i32 %23)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @hex32(i32 %14)
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @hex32(i32 %45)
%50 = call i32 @ALU(i32 %14, i32 %45, i32 %22, i32 %24, i32* %2, i32* %4, i32* %3)
%51 = add nsw i32 %13, 2
br label %27
52: ; preds = %27, %41
ret void
}