// File: 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
subq $0x50, %rsp
movw %di, %ax
movw %ax, -0x6(%rbp)
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x3, %ax
movzwl %ax, %eax
cmpl $0x2, %eax
jne 0x6eb7
leaq 0xe666(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x6eae
movq 0xe399(%rip), %rdi      # 0x14fc8
leaq 0x75c9(%rip), %rsi      # 0xe1ff
callq 0x21d0
movw -0x6(%rbp), %ax
shrw $0x7, %ax
andw $0x1, %ax
cmpw $0x0, %ax
je 0x6dd8
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x5, %eax
jne 0x6cbc
movq 0xe360(%rip), %rdi      # 0x14fc8
leaq 0x7594(%rip), %rsi      # 0xe203
callq 0x21d0
movq %rax, -0x28(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x28(%rbp), %rdi
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0xf, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe2fe(%rip), %rsi      # 0x14fb0
callq 0x2200
jmp 0x6dd3
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0xf, %ax
movzwl %ax, %eax
cmpl $0xd, %eax
jne 0x6d39
movq 0xe2f1(%rip), %rdi      # 0x14fc8
leaq 0x7529(%rip), %rsi      # 0xe207
callq 0x21d0
movq %rax, -0x30(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x30(%rbp), %rdi
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
addl $0x8, %esi
callq 0x2360
movq %rax, %rdi
leaq 0x74ea(%rip), %rsi      # 0xe20a
callq 0x21d0
movq %rax, %rdi
movq 0xe281(%rip), %rsi      # 0x14fb0
callq 0x2200
jmp 0x6dd1
movq 0xe288(%rip), %rdi      # 0x14fc8
leaq 0x74c0(%rip), %rsi      # 0xe207
callq 0x21d0
movq %rax, -0x40(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x40(%rbp), %rdi
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
addl $0x8, %esi
callq 0x2360
movq %rax, %rdi
leaq 0x747d(%rip), %rsi      # 0xe206
callq 0x21d0
movq %rax, -0x38(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x38(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0xf, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe1e4(%rip), %rsi      # 0x14fb0
callq 0x2200
jmp 0x6dd3
jmp 0x6eac
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0xf, %ax
movzwl %ax, %eax
cmpl $0xd, %eax
jne 0x6e32
movq 0xe1d5(%rip), %rdi      # 0x14fc8
leaq 0x740d(%rip), %rsi      # 0xe207
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x73ee(%rip), %rsi      # 0xe20a
callq 0x21d0
movq %rax, %rdi
movq 0xe185(%rip), %rsi      # 0x14fb0
callq 0x2200
jmp 0x6eaa
movq 0xe18f(%rip), %rdi      # 0x14fc8
leaq 0x73c7(%rip), %rsi      # 0xe207
callq 0x21d0
movq %rax, -0x48(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x48(%rbp), %rdi
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x7387(%rip), %rsi      # 0xe206
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0xf, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe10b(%rip), %rsi      # 0x14fb0
callq 0x2200
jmp 0x6eac
jmp 0x6eae
movl $0x2, -0x4(%rbp)
jmp 0x6f1d
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x3, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x6ed4
movl $0x0, -0x4(%rbp)
jmp 0x6f1d
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x3, %ax
movzwl %ax, %eax
cmpl $0x1, %eax
jne 0x6ef1
movl $0x1, -0x4(%rbp)
jmp 0x6f1d
movq 0xe0f8(%rip), %rdi      # 0x14ff0
leaq 0x7310(%rip), %rsi      # 0xe20f
callq 0x21d0
movq %rax, %rdi
movq 0xe0a2(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x1, %edi
callq 0x2260
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)