// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
pushq %rax
movl %edi, %ebx
shrl $0x8, %ebx
andl $0x3, %ebx
leaq 0x2aec(%rip), %rax      # 0x6148
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x1
popq %rbx
jmp 0x37e3
leaq 0x5b78(%rip), %rax      # 0x91ec
pushq $0x2
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x37e3
movl %edi, %r14d
movq 0x5945(%rip), %rdi      # 0x8fd0
leaq 0x2b95(%rip), %rsi      # 0x6227
callq 0x2150
movl %r14d, %r15d
testb %r14b, %r14b
js 0x36d5
shrl $0x3, %r15d
andl $0xf, %r15d
movq 0x5922(%rip), %rdi      # 0x8fd0
leaq 0x2b7a(%rip), %rsi      # 0x622f
callq 0x2150
cmpw $0xd, %r15w
jne 0x371b
andl $0x7, %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x2210
jmp 0x379a
andl $0x7, %r15d
cmpw $0x5, %r15w
jne 0x3754
movq 0x58e9(%rip), %rdi      # 0x8fd0
leaq 0x2b3d(%rip), %rsi      # 0x622b
callq 0x2150
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
shrl $0x3, %r14d
andl $0xf, %r14d
movq %rax, %rdi
movl %r14d, %esi
jmp 0x37d6
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
andl $0x7, %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x2210
leaq 0x2ae8(%rip), %rsi      # 0x622e
movq %rax, %rdi
callq 0x2150
movzwl %r15w, %esi
jmp 0x37d3
shrl $0x3, %r14d
andl $0xf, %r14d
movq 0x586d(%rip), %rdi      # 0x8fd0
leaq 0x2ac5(%rip), %rsi      # 0x622f
callq 0x2150
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movzwl %r15w, %esi
orl $0x8, %esi
movq %rax, %rdi
callq 0x2230
cmpw $0xd, %r14w
jne 0x37ab
leaq 0x2a91(%rip), %rsi      # 0x6232
movq %rax, %rdi
callq 0x2150
jmp 0x37db
leaq 0x2a7c(%rip), %rsi      # 0x622e
movq %rax, %rdi
callq 0x2150
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movzwl %r14w, %esi
movq %rax, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x20c0
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x57f9(%rip), %rdi      # 0x8ff0
leaq 0x2a39(%rip), %rsi      # 0x6237
callq 0x2150
movq %rax, %rdi
callq 0x20c0
pushq $0x1
popq %rdi
callq 0x2190