// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
movq %rdx, %r14
movq %rdi, %rbx
movl $0x48, %edi
callq 0x5220
movq %rax, %r12
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x13ae0(%rip), %rax     # 0x20b30
movq %rax, (%r12)
leaq 0x10(%r12), %r13
leaq 0x20(%r12), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x30(%r12)
xorl %ecx, %ecx
movq %rcx, 0x40(%r12)
movq %rax, 0x10(%r12)
movq %rcx, 0x18(%r12)
movq %r12, 0x8(%rbx)
movq %rbx, 0x18(%rsp)
movq %r13, (%rbx)
movq %r14, 0x10(%rsp)
movq (%r14), %rdi
callq 0x9780
movzbl %al, %ecx
movl %ecx, 0x8(%rsp)
movl %ecx, %ebx
shll $0x4, %ebx
leaq 0x129f6(%rip), %r15     # 0x1faa0
movq (%rbx,%r15), %rbp
movb %al, 0x7(%rsp)
movq 0x18(%r12), %r14
movq %rbp, %rdi
callq 0x5150
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbp, %rcx
movq %rax, %r8
callq 0x5360
addq %rbx, %r15
movzwl 0xc(%r15), %eax
movw %ax, 0x30(%r12)
movl 0x8(%r15), %eax
leaq 0xc(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x435d6(%rip), %rdi     # 0x506c8
callq 0xeefc
movl (%rax), %eax
movw %ax, 0x32(%r12)
movl 0xc(%rsp), %ecx
cmpl $0x3, %ecx
movq 0x20(%rsp), %r15
je 0xd168
cmpl $0x2, %ecx
movq 0x10(%rsp), %rbx
jne 0xd1cc
movl 0x4(%r15), %ebp
testb $0x20, %bpl
je 0xd12d
decl %eax
movw %ax, 0x32(%r12)
leaq 0x4(%r15), %r14
movzwl %ax, %eax
addl %eax, (%r15)
movl %ebp, %eax
andl $0xff0130, %eax         # imm = 0xFF0130
movl %eax, 0x4(%r15)
movzwl 0x8(%r15), %eax
movw %ax, 0x40(%r12)
movw $0x0, 0x8(%r15)
movq (%rbx), %rdi
testb $0x20, %bpl
jne 0xd21d
callq 0x975e
jmp 0xd1b4
movl 0x4(%r15), %ebp
testb $0x10, %bpl
movq 0x10(%rsp), %rbx
je 0xd17f
decl %eax
movw %ax, 0x32(%r12)
leaq 0x4(%r15), %r14
movzwl %ax, %eax
addl %eax, (%r15)
movl %ebp, %eax
andl $0xff0130, %eax         # imm = 0xFF0130
movl %eax, 0x4(%r15)
movzwl 0x8(%r15), %eax
movw %ax, 0x40(%r12)
movw $0x0, 0x8(%r15)
movq (%rbx), %rdi
testb $0x10, %bpl
jne 0xd224
callq 0x975e
movzwl %ax, %ecx
movl %ecx, 0x38(%r12)
movl $0x2, 0x34(%r12)
movw %ax, 0x8(%r15)
jmp 0xd23a
movzwl %ax, %r13d
addl (%r15), %r13d
movl %r13d, (%r15)
leaq 0x4(%r15), %r14
movl 0x4(%r15), %ebp
movl %ebp, %eax
andl $0xff0130, %eax         # imm = 0xFF0130
movl %eax, 0x4(%r15)
movzwl 0x8(%r15), %eax
movw %ax, 0x40(%r12)
movw $0x0, 0x8(%r15)
cmpl $0x1b, %ecx
ja 0xd23a
movl %ecx, %eax
leaq 0xa147(%rip), %rcx      # 0x17350
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, 0x34(%r12)
jmp 0xd23a
callq 0x9780
jmp 0xd229
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x1, 0x34(%r12)
cmpb $0x38, 0x7(%rsp)
je 0xd443
cmpl $0x18, 0x8(%rsp)
jne 0xd465
movq (%rbx), %rdi
callq 0x9780
cmpb $-0x5, %al
jne 0xd261
andb $-0x2, 0x1(%r14)
movq (%rbx), %rdi
movq $-0x1, %rsi
callq 0x98b8
jmp 0xd465
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x15, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x975e
cwtl
jmp 0xd614
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl 0x30(%r12), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd62a
movzbl 0x2(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r12)
movl $0xd, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x972c
jmp 0xd4cb
movl 0x8(%rsp), %eax
jmp 0xd4d2
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x8, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x975e
jmp 0xd1b4
movq (%rbx), %rdi
callq 0x972c
jmp 0xd4d2
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl $0x17, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0xc, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x16, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl 0x30(%r12), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd638
movzbl 0x2(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r12)
jmp 0xd527
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0xb, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl 0x30(%r12), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd646
movzbl 0x2(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r12)
jmp 0xd61c
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x1, 0x34(%r12)
movl 0x8(%rsp), %ecx
cmpl $0xc1, %ecx
jg 0xd654
cmpl $0x18, %ecx
je 0xd250
cmpl $0x38, %ecx
jne 0xd465
movq (%rbx), %rdi
callq 0x9780
cmpb $-0x5, %al
jne 0xd456
orl $0x130, (%r14)          # imm = 0x130
movq (%rbx), %rdi
movq $-0x1, %rsi
callq 0x98b8
movl (%r14), %eax
movl %eax, %ecx
xorl %ebp, %ecx
movl %ecx, %edx
andl $0x100, %edx            # imm = 0x100
movl %eax, %esi
orl $0x2000, %esi           # imm = 0x2000
movl %esi, %edi
xorl %ebp, %edi
testl %edx, %edx
cmovel %ecx, %edi
cmovel %eax, %esi
movl %edi, %eax
andl $0x10, %eax
xorl %esi, %ebp
testl %eax, %eax
cmovel %edi, %ebp
andl $0x20, %ebp
orl %eax, %ebp
movl %ebp, %eax
shll $0x5, %eax
orl %esi, %eax
orl %edx, %ebp
je 0xd4a7
movl %eax, (%r14)
movl %eax, 0x3c(%r12)
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
shll $0x8, %eax
addl 0x8(%rsp), %eax
movl %eax, 0x38(%r12)
jmp 0xd61c
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl 0x30(%r12), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd670
movzbl 0x2(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r12)
movl $0x9, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x972c
movl %eax, 0x38(%r12)
movl $0x6, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0xf, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x14, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x5, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x975e
movzwl %ax, %eax
movl %eax, 0x38(%r12)
movl 0x30(%r12), %ecx
decl %ecx
cmpw $0x1, %cx
ja 0xd67e
movzbl 0x2(%r15), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%r12)
movl $0x10, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x13, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movzbl %al, %eax
movl %eax, 0x38(%r12)
movl $0x12, 0x34(%r12)
jmp 0xd23a
movq (%rbx), %rdi
callq 0x9780
movsbl %al, %eax
addl %eax, %r13d
movl %r13d, 0x38(%r12)
movl $0x3, 0x34(%r12)
jmp 0xd23a
movl $0xe, 0x34(%r12)
jmp 0xd23a
movl $0x7, 0x34(%r12)
jmp 0xd23a
movl $0x4, 0x34(%r12)
jmp 0xd23a
cmpl $0xc2, %ecx
je 0xd68c
cmpl $0xe2, %ecx
jne 0xd465
andl $0x30, %eax
orl (%r14), %eax
jmp 0xd691
movl $0xa, 0x34(%r12)
jmp 0xd23a
movl $0x11, 0x34(%r12)
jmp 0xd23a
notl %eax
andl (%r14), %eax
movl %eax, (%r14)
jmp 0xd465
jmp 0xd69d
jmp 0xd69d
movq %rax, %r14
movq %r12, %rdi
callq 0x671a
movq %r14, %rdi
callq 0x53e0