// File: Lupo-CalPoly[P]cpe315-thumbsim/decode.cpp
// Function: decode(ALU_Type)
ALU_Ops decode (const ALU_Type data) {
  if (data.instr.lsli.op == ALU_LSLI_OP) {
    // 315: insert code here to print lsls instruction
    return ALU_LSLI;
  }
  else if (data.instr.addr.op == ALU_ADDR_OP) {
    if (opts.instrs) { 
      cout << "adds r" << data.instr.addr.rd  << ", r" << data.instr.addr.rn << ", r" << data.instr.addr.rm << endl;
    }
    return ALU_ADDR;
  }
  else if (data.instr.subr.op == ALU_SUBR_OP) {
    // 315: insert code here to print subs instruction
    return ALU_SUBR;
  }
  else if (data.instr.add3i.op == ALU_ADD3I_OP) {
    // complete
    if (opts.instrs) { 
      cout << "adds r" << data.instr.add3i.rd << ", r" << data.instr.add3i.rn << ", #" << data.instr.add3i.imm << endl;
    }
    return ALU_ADD3I;
  }
  else if (data.instr.sub3i.op == ALU_SUB3I_OP) {
    // 315: insert code here to print subs instruction
    return ALU_SUB3I;
  }
  else if (data.instr.add8i.op == ALU_ADD8I_OP) {
    // complete
    if (opts.instrs) { 
      cout << "adds r" << data.instr.add8i.rdn << ", #" << setbase(10) << data.instr.add8i.imm << endl;
    }
    return ALU_ADD8I;
  }
  else if (data.instr.sub8i.op == ALU_SUB8I_OP) {
    // 315: insert code here to print subs instruction
    return ALU_SUB8I;
  }
  else if (data.instr.cmp.op == ALU_CMP_OP) { 
    // complete
    if (opts.instrs) { 
      cout << "cmp r" << data.instr.cmp.rdn << ", #" << setbase(10) << data.instr.cmp.imm << endl;
    }
    return ALU_CMP;
  }
  else if (data.instr.mov.op == ALU_MOV_OP) { 
    // complete
    if (opts.instrs) { 
      cout << "movs r" << data.instr.mov.rdn << ", #" << setbase(10) << (data.instr.mov.imm) << endl;
    }
    return ALU_MOV;
  }
  cout << "[ERROR] Unknown Instruction to be decoded" << endl;
  exit(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movw %di, %ax
movw %ax, -0x6(%rbp)
movw -0x6(%rbp), %ax
shrw $0xb, %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x677f
movl $0x0, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0x9, %ax
andw $0x1f, %ax
movzwl %ax, %eax
cmpl $0xc, %eax
jne 0x6835
leaq 0xeae6(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x6829
movq 0xe819(%rip), %rdi      # 0x14fc8
leaq 0x79f0(%rip), %rsi      # 0xe1a6
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x7a2e(%rip), %rsi      # 0xe206
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x7a08(%rip), %rsi      # 0xe206
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x6, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe78c(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x3, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0x9, %ax
andw $0x1f, %ax
movzwl %ax, %eax
cmpl $0xd, %eax
jne 0x6855
movl $0x4, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0x9, %ax
andw $0x1f, %ax
movzwl %ax, %eax
cmpl $0xe, %eax
jne 0x690b
leaq 0xea10(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x68ff
movq 0xe743(%rip), %rdi      # 0x14fc8
leaq 0x791a(%rip), %rsi      # 0xe1a6
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x7958(%rip), %rsi      # 0xe206
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x3, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x79ca(%rip), %rsi      # 0xe29e
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x6, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe6b6(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x5, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0x9, %ax
andw $0x1f, %ax
movzwl %ax, %eax
cmpl $0xf, %eax
jne 0x692b
movl $0x6, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0xb, %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x6, %eax
jne 0x69d4
leaq 0xe93a(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x69c8
movq 0xe671(%rip), %rdi      # 0x14fc8
leaq 0x7848(%rip), %rsi      # 0xe1a6
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x791a(%rip), %rsi      # 0xe29e
callq 0x21d0
movq %rax, -0x20(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x20(%rbp), %rdi
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0xff, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe5ed(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x9, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0xb, %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x7, %eax
jne 0x69f4
movl $0xa, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0xb, %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x5, %eax
jne 0x6a9d
leaq 0xe871(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x6a91
movq 0xe5a8(%rip), %rdi      # 0x14fc8
leaq 0x7786(%rip), %rsi      # 0xe1ad
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x7851(%rip), %rsi      # 0xe29e
callq 0x21d0
movq %rax, -0x28(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x28(%rbp), %rdi
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0xff, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe524(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x8, -0x4(%rbp)
jmp 0x6b7f
movw -0x6(%rbp), %ax
shrw $0xb, %ax
andw $0x7, %ax
movzwl %ax, %eax
cmpl $0x4, %eax
jne 0x6b43
leaq 0xe7c8(%rip), %rax      # 0x15284
testb $0x1, 0x2(%rax)
je 0x6b3a
movq 0xe4ff(%rip), %rdi      # 0x14fc8
leaq 0x76e3(%rip), %rsi      # 0xe1b3
callq 0x21d0
movq %rax, %rdi
movw -0x6(%rbp), %ax
shrw $0x8, %ax
andw $0x7, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
leaq 0x77a8(%rip), %rsi      # 0xe29e
callq 0x21d0
movq %rax, -0x30(%rbp)
movl $0xa, %edi
callq 0x7fb0
movq -0x30(%rbp), %rdi
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
callq 0x2190
movq %rax, %rdi
movw -0x6(%rbp), %ax
andw $0xff, %ax
movzwl %ax, %esi
callq 0x2340
movq %rax, %rdi
movq 0xe47b(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x7, -0x4(%rbp)
jmp 0x6b7f
jmp 0x6b45
jmp 0x6b47
jmp 0x6b49
jmp 0x6b4b
jmp 0x6b4d
jmp 0x6b4f
jmp 0x6b51
jmp 0x6b53
movq 0xe46e(%rip), %rdi      # 0x14fc8
leaq 0x7659(%rip), %rsi      # 0xe1ba
callq 0x21d0
movq %rax, %rdi
movq 0xe440(%rip), %rsi      # 0x14fb0
callq 0x2200
movl $0x1, %edi
callq 0x2260
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)