// File: /workspace/github_c_makefile_star2_O1/mrkite[P]regs/src/disasm.cc
// Function: Disassembler::decodeInst(std::shared_ptr<TheHandle>, Entry*)
std::shared_ptr<Inst> Disassembler::decodeInst(Handle f, Entry *entry) {
  auto inst = std::make_shared<Inst>();
  auto opcode = f->r8();
  inst->name = opcodes[opcode].inst;
  inst->type = opcodes[opcode].type;
  auto mode = opcodes[opcode].addressing;
  inst->length = sizes[mode];
  if (mode == IMMM && (entry->flags & IsM8)) {
    inst->length--;
  }
  if (mode == IMMX && (entry->flags & IsX8)) {
    inst->length--;
  }
  entry->org += inst->length;
  uint32_t addr = entry->org;
  entry->flags &= IsFlags;  // clear changed flags
  uint32_t oldFlags = entry->flags;
  inst->prevWord = entry->prevWord;
  entry->prevWord = 0;
  switch (mode) {
    case IMP:
      inst->operType = Opr::None;
      break;
    case IMM:
      inst->oper = f->r8();
      inst->operType = Opr::Imm8;
      if (opcode == 0xe2) {
        entry->flags |= inst->oper & IsFlags;
      } else if (opcode == 0xc2) {
        entry->flags &= ~inst->oper;
      }
      break;
    case IMMM:
      if (entry->flags & IsM8) {
        inst->oper = f->r8();
        inst->operType = Opr::Imm8;
      } else {
        inst->oper = f->r16();
        inst->operType = Opr::Imm16;
        entry->prevWord = inst->oper;
      }
      break;
    case IMMX:
      if (entry->flags & IsX8) {
        inst->oper = f->r8();
        inst->operType = Opr::Imm8;
      } else {
        inst->oper = f->r16();
        inst->operType = Opr::Imm16;
        entry->prevWord = inst->oper;
      }
      break;
    case IMMS:
      inst->oper = f->r16();
      inst->operType = Opr::Imm16;
      entry->prevWord = inst->oper;
      break;
    case ABS:
      inst->oper = f->r16();
      if (inst->type == InsType::Jump || inst->type == InsType::Call) {
        inst->oper |= entry->org & 0xff0000;  // K
        inst->operType = Opr::Abs;
      } else {
        inst->operType = Opr::AbsB;
      }
      break;
    case ABL:
      inst->oper = f->r24();
      inst->operType = Opr::Abs;
      break;
    case ABX:
      inst->oper = f->r16();
      if (inst->type == InsType::Jump || inst->type == InsType::Call) {
        inst->oper |= entry->org & 0xff0000;  // K
        inst->operType = Opr::AbsX;
      } else {
        inst->operType = Opr::AbsXB;
      }
      break;
    case ABY:
      inst->oper = f->r16();
      if (inst->type == InsType::Jump || inst->type == InsType::Call) {
        inst->oper |= entry->org & 0xff0000;  // K
        inst->operType = Opr::AbsY;
      } else {
        inst->operType = Opr::AbsYB;
      }
      break;
    case ABLX:
      inst->oper = f->r24();
      inst->operType = Opr::AbsX;
      break;
    case AIX:
      inst->oper = f->r16();
      if (inst->type == InsType::Jump || inst->type == InsType::Call) {
        inst->oper |= entry->org & 0xff0000;  // K
        inst->operType = Opr::IndX;
      } else {
        inst->operType = Opr::IndXB;
      }
      break;
    case ZP:
      inst->oper = f->r8();
      inst->operType = Opr::AbsD;
      break;
    case ZPX:
      inst->oper = f->r8();
      inst->operType = Opr::AbsXD;
      break;
    case ZPY:
      inst->oper = f->r8();
      inst->operType = Opr::AbsYD;
      break;
    case ZPS:
      inst->oper = f->r8();
      inst->operType = Opr::AbsS;
      break;
    case IND:
      inst->oper = f->r16();
      if (inst->type == InsType::Jump || inst->type == InsType::Call) {
        inst->oper |= entry->org & 0xff0000;  // K
        inst->operType = Opr::Ind;
      } else {
        inst->operType = Opr::IndB;
      }
      break;
    case INZ:
      inst->oper = f->r8();
      inst->operType = Opr::IndD;
      break;
    case INL:
      inst->oper = f->r8();
      inst->operType = Opr::IndL;
      break;
    case INX:
      inst->oper = f->r8();
      inst->operType = Opr::IndXD;
      break;
    case INY:
      inst->oper = f->r8();
      inst->operType = Opr::IndY;
      break;
    case INLY:
      inst->oper = f->r8();
      inst->operType = Opr::IndLY;
      break;
    case INS:
      inst->oper = f->r8();
      inst->operType = Opr::IndS;
      break;
    case REL:
      inst->oper = addr + static_cast<int8_t>(f->r8());
      inst->operType = Opr::Abs;
      break;
    case RELL:
      inst->oper = addr + static_cast<int16_t>(f->r16());
      inst->operType = Opr::Abs;
      break;
    case BANK:
      inst->oper = f->r16();
      inst->operType = Opr::Bank;
      break;
    case DB:
      inst->oper = opcode;
      inst->operType = Opr::Abs;
      break;
    case DW:
      inst->oper = opcode | (f->r8() << 8);
      inst->operType = Opr::Abs;
      break;
    case DD:
      inst->oper = opcode | (f->r24() << 8);
      inst->operType = Opr::Abs;
      break;
  }
  if (opcode == 0x18) {
    if (f->r8() == 0xfb) {  // clc xce
      entry->flags &= ~IsEmu;
    }
    f->skip(-1);
  }
  if (opcode == 0x38) {
    if (f->r8() == 0xfb) {  // sec xce
      entry->flags |= IsEmu | IsM8 | IsX8;
    }
    f->skip(-1);
  }
  if ((entry->flags ^ oldFlags) & IsEmu) {
    entry->flags |= IsEmuChanged;
  }
  if ((entry->flags ^ oldFlags) & IsX8) {
    entry->flags |= IsX8Changed;
  }
  if ((entry->flags ^ oldFlags) & IsM8) {
    entry->flags |= IsM8Changed;
  }
  inst->flags = entry->flags;
  return inst;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r15
movq %rdi, %r14
xorl %ebx, %ebx
movq %rbx, (%rdi)
movl $0x48, %edi
callq 0x5220
movq %rax, %r13
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r13)
leaq 0x1386c(%rip), %rax     # 0x20b30
movq %rax, (%r13)
leaq 0x10(%r13), %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x30(%r13)
movq %rbx, 0x40(%r13)
leaq 0x20(%r13), %rax
movq %rax, 0x10(%r13)
movq %rbx, 0x18(%r13)
movb $0x0, 0x20(%r13)
movq %r13, 0x8(%r14)
movq %r14, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq %rcx, (%r14)
movq %r15, 0x10(%rsp)
movq (%r15), %rdi
callq 0x999c
movl %eax, %r12d
movzbl %al, %eax
movl %eax, (%rsp)
movl %eax, %r14d
shll $0x4, %r14d
leaq 0x12778(%rip), %rbx     # 0x1faa0
movq (%r14,%rbx), %rbp
movq 0x18(%r13), %r15
movq %rbp, %rdi
callq 0x5150
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
callq 0x5360
addq %r14, %rbx
movzwl 0xc(%rbx), %eax
movw %ax, 0x30(%r13)
movl 0x8(%rbx), %eax
leaq 0x4(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x4335e(%rip), %rdi     # 0x506c8
callq 0xf0f0
movzwl (%rax), %ecx
movw %cx, 0x32(%r13)
movl 0x4(%rsp), %eax
cmpl $0x3, %eax
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
je 0xd3a1
cmpl $0x2, %eax
jne 0xd3ac
testb $0x20, 0x4(%rbx)
je 0xd39c
decl %ecx
movw %cx, 0x32(%r13)
cmpl $0x3, %eax
jne 0xd3ac
testb $0x10, 0x4(%rbx)
je 0xd3ac
decw 0x32(%r13)
movzwl 0x32(%r13), %r15d
addl (%rbx), %r15d
movl %r15d, (%rbx)
movl 0x4(%rbx), %ebp
movl %ebp, %ecx
andl $0xff0130, %ecx         # imm = 0xFF0130
movl %ecx, 0x4(%rbx)
movzwl 0x8(%rbx), %ecx
movw %cx, 0x40(%r13)
movw $0x0, 0x8(%rbx)
cmpl $0x1b, %eax
ja 0xd737
movl %eax, %eax
leaq 0x9f6a(%rip), %rcx      # 0x17350
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x9948
jmp 0xd725
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0xf, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0xc, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x5, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
jmp 0xd51a
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x8, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x9948
movl %eax, 0x38(%r13)
jmp 0xd55e
movl (%rsp), %eax
jmp 0xd72b
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl $0x17, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
testb $0x20, %bpl
jne 0xd7e3
callq 0x997a
jmp 0xd51a
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl 0x30(%r13), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd803
movzbl 0x2(%rbx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r13)
movl $0xd, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
testb $0x10, %bpl
jne 0xd7ea
callq 0x997a
movzwl %ax, %ecx
movl %ecx, 0x38(%r13)
movl $0x2, 0x34(%r13)
movw %ax, 0x8(%rbx)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl 0x30(%r13), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd810
movzbl 0x2(%rbx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r13)
movl $0x6, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x1, 0x34(%r13)
movl (%rsp), %ecx
cmpl $0xc1, %ecx
jg 0xd81d
cmpl $0x18, %ecx
je 0xd743
cmpl $0x38, (%rsp)
je 0xd768
jmp 0xd78a
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x14, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x15, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl 0x30(%r13), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd83c
movzbl 0x2(%rbx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r13)
jmp 0xd72f
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x16, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0xb, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x9948
jmp 0xd72b
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x12, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl 0x30(%r13), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd849
movzbl 0x2(%rbx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r13)
movl $0x10, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
movzwl %ax, %eax
movl %eax, 0x38(%r13)
movl 0x30(%r13), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd856
movzbl 0x2(%rbx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r13)
movl $0x9, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x999c
movsbl %al, %eax
jmp 0xd711
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x13, 0x34(%r13)
jmp 0xd737
movq (%r14), %rdi
callq 0x997a
cwtl
addl %eax, %r15d
movl %r15d, 0x38(%r13)
jmp 0xd72f
movq (%r14), %rdi
callq 0x999c
movzbl %al, %eax
shll $0x8, %eax
addl (%rsp), %eax
movl %eax, 0x38(%r13)
movl $0x3, 0x34(%r13)
cmpb $0x38, %r12b
je 0xd768
cmpl $0x18, (%rsp)
jne 0xd78a
movq (%r14), %rdi
callq 0x999c
cmpb $-0x5, %al
jne 0xd753
andb $-0x2, 0x5(%rbx)
movq (%r14), %rdi
movq $-0x1, %rsi
callq 0x9ad4
cmpb $0x38, %r12b
jne 0xd78a
movq (%r14), %rdi
callq 0x999c
cmpb $-0x5, %al
jne 0xd77b
orl $0x130, 0x4(%rbx)       # imm = 0x130
movq (%r14), %rdi
movq $-0x1, %rsi
callq 0x9ad4
movl 0x4(%rbx), %eax
movl %eax, %ecx
xorl %ebp, %ecx
btl $0x8, %ecx
jae 0xd79f
orl $0x2000, %eax           # imm = 0x2000
movl %eax, 0x4(%rbx)
movl 0x4(%rbx), %eax
movl %eax, %ecx
xorl %ebp, %ecx
testb $0x10, %cl
je 0xd7b3
orl $0x200, %eax            # imm = 0x200
movl %eax, 0x4(%rbx)
movl 0x4(%rbx), %eax
xorl %eax, %ebp
testb $0x20, %bpl
je 0xd7c6
orl $0x400, %eax            # imm = 0x400
movl %eax, 0x4(%rbx)
movl 0x4(%rbx), %edx
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movl %edx, 0x2c(%rcx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x999c
jmp 0xd7ef
callq 0x999c
movzbl %al, %eax
movl %eax, 0x38(%r13)
movl $0x1, 0x34(%r13)
jmp 0xd737
movl $0xe, 0x34(%r13)
jmp 0xd737
movl $0x7, 0x34(%r13)
jmp 0xd737
cmpl $0xc2, %ecx
je 0xd863
cmpl $0xe2, %ecx
jne 0xd78a
andl $0x30, %eax
orl %eax, 0x4(%rbx)
jmp 0xd737
movl $0x4, 0x34(%r13)
jmp 0xd737
movl $0x11, 0x34(%r13)
jmp 0xd737
movl $0xa, 0x34(%r13)
jmp 0xd737
notl %eax
andl %eax, 0x4(%rbx)
jmp 0xd737
jmp 0xd871
jmp 0xd871
movq %rax, %r14
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0xd887
callq 0x6752
movq %r14, %rdi
callq 0x53e0
nop