// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %r14d
movzwl %r14w, %eax
shrl $0xb, %eax
andw $0x7, %ax
je 0x347a
movl %r14d, %ecx
shrl $0x9, %ecx
andl $0x1f, %ecx
addl $-0xc, %ecx
cmpl $0x3, %ecx
ja 0x3504
leaq 0x2d17(%rip), %rax      # 0x6128
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x5dcb(%rip), %rax      # 0x91ec
pushq $0x3
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x35db
movq 0x5b9b(%rip), %rdi      # 0x8fd0
leaq 0x2d92(%rip), %rsi      # 0x61ce
callq 0x2150
movl %r14d, %esi
andl $0x7, %esi
movq %rax, %rdi
callq 0x2210
leaq 0x2dd8(%rip), %r15      # 0x622e
movq %rax, %rdi
movq %r15, %rsi
callq 0x2150
movl %r14d, %esi
shrl $0x3, %esi
andl $0x7, %esi
movq %rax, %rdi
callq 0x2210
movq %rax, %rdi
movq %r15, %rsi
jmp 0x34e0
xorl %ebx, %ebx
jmp 0x35db
leaq 0x5d64(%rip), %rax      # 0x91ec
pushq $0x5
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x35db
movq 0x5b34(%rip), %rdi      # 0x8fd0
leaq 0x2d2b(%rip), %rsi      # 0x61ce
callq 0x2150
movl %r14d, %esi
andl $0x7, %esi
movq %rax, %rdi
callq 0x2210
leaq 0x2d71(%rip), %rsi      # 0x622e
movq %rax, %rdi
callq 0x2150
movl %r14d, %esi
shrl $0x3, %esi
andl $0x7, %esi
movq %rax, %rdi
callq 0x2210
leaq 0x2de9(%rip), %rsi      # 0x62c6
movq %rax, %rdi
callq 0x2150
shrl $0x6, %r14d
andl $0x7, %r14d
movq %rax, %rdi
movl %r14d, %esi
jmp 0x35ce
pushq $0x6
jmp 0x34fe
pushq $0x4
popq %rbx
jmp 0x35db
movzwl %ax, %eax
addl $-0x4, %eax
cmpl $0x3, %eax
ja 0x35e8
leaq 0x2c1e(%rip), %rcx      # 0x6138
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x5cc2(%rip), %rax      # 0x91ec
pushq $0x7
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x35db
movq 0x5a92(%rip), %rdi      # 0x8fd0
leaq 0x2c96(%rip), %rsi      # 0x61db
jmp 0x358d
leaq 0x5c9e(%rip), %rax      # 0x91ec
pushq $0x9
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x35db
movq 0x5a6e(%rip), %rdi      # 0x8fd0
leaq 0x2c65(%rip), %rsi      # 0x61ce
jmp 0x358d
pushq $0xa
jmp 0x34fe
leaq 0x5c76(%rip), %rax      # 0x91ec
pushq $0x8
popq %rbx
cmpb $0x1, 0x2(%rax)
jne 0x35db
movq 0x5a4a(%rip), %rdi      # 0x8fd0
leaq 0x2c48(%rip), %rsi      # 0x61d5
callq 0x2150
movl %r14d, %esi
shrl $0x8, %esi
andl $0x7, %esi
movq %rax, %rdi
callq 0x2210
leaq 0x2d1c(%rip), %rsi      # 0x62c6
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)
movzbl %r14b, %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 0x59e1(%rip), %rdi      # 0x8fd0
leaq 0x2bec(%rip), %rsi      # 0x61e2
callq 0x2150
movq %rax, %rdi
callq 0x20c0
pushq $0x1
popq %rdi
callq 0x2190