// File: /workspace/github_cpp_makefile_star2_O1/Lupo-CalPoly[P]cpe315-thumbsim/decode.cpp
// Function: decode(SP_Type)
SP_Ops decode (const SP_Type data) {
  if (data.instr.mov.op == 2) {
    if (opts.instrs) { 
      cout << "mov";
      if (data.instr.mov.d) {
        // These two cases handle stack pointer printing
        if (data.instr.mov.rd == 5) {
          cout << " sp, r" << setbase(10) << data.instr.mov.rm << endl;
        }
        else if (data.instr.mov.rm == 13) {
          cout << " r" << setbase(10) << (8+data.instr.mov.rd) << ", sp" << endl;
        }
        // this case is for registers greater than r7 that aren't sp
        else {
          cout << " r" << setbase(10) << (8+data.instr.mov.rd) << ", r" << setbase(10) << data.instr.mov.rm << endl;
        }
      }
      // another stack pointer case
      else if (data.instr.mov.rm == 13) {
        cout << " r" << data.instr.mov.rd << ", sp" << endl;
      }
      else {
        cout << " r" << setbase(10) << data.instr.mov.rd << ", r" << data.instr.mov.rm << endl;
      }
    }
    return SP_MOV;
  }
  else if (data.instr.add.op == 0) {
    // Here you'll need to SP_ADD similar to above
    return SP_ADD;
  }
  else if (data.instr.cmp.op == 1) {
    // Here you'll need to SP_CMP similar to above
    return SP_CMP;
  }
  else {
    cerr << "UNKNOWN SP TYPE -- Exiting" << endl;
    exit(1);
  }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edi, %ebx
shrl $0x8, %ebx
andl $0x3, %ebx
leaq 0x2d41(%rip), %rax      # 0x6164
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x1, %ebx
jmp 0x3641
leaq 0x5d97(%rip), %rax      # 0x91d4
movl $0x2, %ebx
cmpb $0x1, 0x2(%rax)
jne 0x3641
movl %edi, %r14d
movq 0x5b7a(%rip), %r15      # 0x8fd0
leaq 0x2de6(%rip), %rsi      # 0x6243
movl $0x3, %edx
movq %r15, %rdi
callq 0x2170
movl %r14d, %r12d
testb %r14b, %r14b
js 0x34ce
shrl $0x3, %r12d
andl $0xf, %r12d
movq 0x5b4f(%rip), %r15      # 0x8fd0
leaq 0x2dc3(%rip), %rsi      # 0x624b
movl $0x2, %edx
movq %r15, %rdi
callq 0x2170
cmpw $0xd, %r12w
jne 0x351b
andl $0x7, %r14d
movq 0x5b29(%rip), %rdi      # 0x8fd0
movq %r14, %rsi
callq 0x20c0
movq %rax, %r14
leaq 0x2d95(%rip), %rsi      # 0x624e
movl $0x4, %edx
movq %rax, %rdi
callq 0x2170
movq (%r14), %rax
jmp 0x361d
andl $0x7, %r12d
cmpw $0x5, %r12w
jne 0x3564
leaq 0x2d63(%rip), %rsi      # 0x6247
movl $0x6, %edx
movq %r15, %rdi
callq 0x2170
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r15,%rax)
shrl $0x3, %r14d
andl $0xf, %r14d
movq %r15, %rdi
movq %r14, %rsi
jmp 0x3612
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r15,%rax)
andl $0x7, %r14d
movq %r15, %rdi
movq %r14, %rsi
callq 0x20c0
movq %rax, %r14
leaq 0x2cff(%rip), %rsi      # 0x624a
movl $0x3, %edx
movq %rax, %rdi
callq 0x2170
movzwl %r12w, %esi
movq %r14, %rdi
jmp 0x3612
shrl $0x3, %r14d
andl $0xf, %r14d
leaq 0x2cd8(%rip), %rsi      # 0x624b
movl $0x2, %edx
movq %r15, %rdi
callq 0x2170
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r15,%rax)
movzwl %r12w, %esi
orl $0x8, %esi
movq %r15, %rdi
callq 0x2210
movq %rax, %r15
cmpw $0xd, %r14w
jne 0x35e0
leaq 0x2c97(%rip), %rsi      # 0x624e
movl $0x4, %edx
movq %r15, %rdi
callq 0x2170
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r15, %rdi
jmp 0x3634
leaq 0x2c63(%rip), %rsi      # 0x624a
movl $0x3, %edx
movq %r15, %rdi
callq 0x2170
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r15,%rax)
movzwl %r14w, %esi
movq %r15, %rdi
callq 0x20c0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x599d(%rip), %rdi      # 0x8ff0
leaq 0x2bf9(%rip), %rsi      # 0x6253
callq 0x2130
movq %rax, %rdi
callq 0x2090
movl $0x1, %edi
callq 0x2180