// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rdi, %rbx
callq 0xc736
movq (%r14), %rdi
callq 0x8966
movl %eax, %ebp
movzbl %al, %eax
movl %eax, 0x4(%rsp)
movl %eax, %r13d
shll $0x4, %r13d
leaq 0x13839(%rip), %r12     # 0x1eaa0
movq (%r13,%r12), %rsi
movq (%rbx), %rdi
callq 0x5420
addq %r13, %r12
movzwl 0xc(%r12), %eax
movq (%rbx), %rcx
movw %ax, 0x20(%rcx)
movl 0x8(%r12), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x44431(%rip), %rdi     # 0x4f6c8
callq 0xc574
movl (%rax), %ecx
movq (%rbx), %rax
movw %cx, 0x22(%rax)
movl 0x14(%rsp), %edx
movl 0x4(%r15), %r12d
cmpl $0x3, %edx
je 0xb306
cmpl $0x2, %edx
jne 0xb362
testb $0x20, %r12b
je 0xb2c7
decl %ecx
movw %cx, 0x22(%rax)
movzwl %cx, %ecx
addl %ecx, (%r15)
movl %r12d, %ecx
andl $0xff0130, %ecx         # imm = 0xFF0130
movl %ecx, 0x4(%r15)
movzwl 0x8(%r15), %ecx
movw %cx, 0x30(%rax)
andw $0x0, 0x8(%r15)
leaq 0x4(%r15), %rax
movq %rax, 0x8(%rsp)
movq (%r14), %rdi
testb $0x20, %r12b
jne 0xb3ae
callq 0x8944
jmp 0xb34b
testb $0x10, %r12b
je 0xb312
decl %ecx
movw %cx, 0x22(%rax)
movzwl %cx, %ecx
addl %ecx, (%r15)
movl %r12d, %ecx
andl $0xff0130, %ecx         # imm = 0xFF0130
movl %ecx, 0x4(%r15)
movzwl 0x8(%r15), %ecx
movw %cx, 0x30(%rax)
andw $0x0, 0x8(%r15)
leaq 0x4(%r15), %rax
movq %rax, 0x8(%rsp)
movq (%r14), %rdi
testb $0x10, %r12b
jne 0xb3b5
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rdx
movl %ecx, 0x28(%rdx)
movl $0x2, 0x24(%rdx)
movw %ax, 0x8(%r15)
jmp 0xb3ca
movzwl %cx, %r13d
addl (%r15), %r13d
movl %r13d, (%r15)
leaq 0x4(%r15), %rcx
movq %rcx, 0x8(%rsp)
movl %r12d, %ecx
andl $0xff0130, %ecx         # imm = 0xFF0130
movl %ecx, 0x4(%r15)
movzwl 0x8(%r15), %ecx
movw %cx, 0x30(%rax)
andw $0x0, 0x8(%r15)
cmpl $0x1b, %edx
ja 0xb3ca
movl %edx, %ecx
leaq 0x8fb1(%rip), %rdx      # 0x14350
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
andl $0x0, 0x24(%rax)
jmp 0xb3ca
callq 0x8966
jmp 0xb3ba
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x1, 0x24(%rcx)
cmpb $0x38, %bpl
je 0xb5e2
cmpl $0x18, 0x4(%rsp)
jne 0xb604
movq (%r14), %rdi
callq 0x8966
cmpb $-0x5, %al
jne 0xb3f4
movq 0x8(%rsp), %rax
andb $-0x2, 0x1(%rax)
movq (%r14), %rdi
pushq $-0x1
popq %rsi
callq 0x8a72
jmp 0xb604
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x15, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
cwtl
jmp 0xb7b7
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rax
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %edx
decl %edx
cmpw $0x1, %dx
ja 0xb7cd
movzbl 0x2(%r15), %edx
shll $0x10, %edx
orl %ecx, %edx
movl %edx, 0x28(%rax)
movl $0xd, 0x24(%rax)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8912
jmp 0xb676
movl 0x4(%rsp), %ecx
movl %ecx, 0x28(%rax)
jmp 0xb7c1
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x8, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
jmp 0xb34b
movq (%r14), %rdi
callq 0x8912
jmp 0xb67d
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x17, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0xc, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x16, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rax
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %edx
decl %edx
cmpw $0x1, %dx
ja 0xb7d9
movzbl 0x2(%r15), %edx
shll $0x10, %edx
orl %ecx, %edx
movl %edx, 0x28(%rax)
movl $0x6, 0x24(%rax)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0xb, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rax
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %edx
decl %edx
cmpw $0x1, %dx
ja 0xb7e5
movzbl 0x2(%r15), %edx
shll $0x10, %edx
orl %ecx, %edx
movl %edx, 0x28(%rax)
jmp 0xb7c1
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x1, 0x24(%rcx)
cmpb $0x18, %bpl
je 0xb3df
movl 0x4(%rsp), %ecx
cmpl $0x38, %ecx
je 0xb5e2
cmpl $0xc2, %ecx
je 0xb809
cmpl $0xe2, %ecx
jne 0xb604
andl $0x30, %eax
movq 0x8(%rsp), %rcx
orl (%rcx), %eax
jmp 0xb812
movq (%r14), %rdi
callq 0x8966
cmpb $-0x5, %al
jne 0xb5f9
movq 0x8(%rsp), %rax
orl $0x130, (%rax)          # imm = 0x130
movq (%r14), %rdi
pushq $-0x1
popq %rsi
callq 0x8a72
movq 0x8(%rsp), %r8
movl (%r8), %eax
movl %eax, %ecx
xorl %r12d, %ecx
movl %ecx, %edx
andl $0x100, %edx            # imm = 0x100
movl %eax, %esi
orl $0x2000, %esi           # imm = 0x2000
movl %esi, %edi
xorl %r12d, %edi
testl %edx, %edx
cmovel %ecx, %edi
cmovel %eax, %esi
movl %edi, %eax
andl $0x10, %eax
xorl %esi, %r12d
testl %eax, %eax
cmovel %edi, %r12d
andl $0x20, %r12d
orl %eax, %r12d
movl %r12d, %eax
shll $0x5, %eax
orl %esi, %eax
orl %edx, %r12d
je 0xb653
movl %eax, (%r8)
movq (%rbx), %rcx
movl %eax, 0x2c(%rcx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
shll $0x8, %eax
addl 0x4(%rsp), %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x3, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rax
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %edx
decl %edx
cmpw $0x1, %dx
ja 0xb7f1
movzbl 0x2(%r15), %edx
shll $0x10, %edx
orl %ecx, %edx
movl %edx, 0x28(%rax)
movl $0x9, 0x24(%rax)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8912
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x6, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0xf, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x14, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x5, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8944
movzwl %ax, %ecx
movq (%rbx), %rax
movl %ecx, 0x28(%rax)
movl 0x20(%rax), %edx
decl %edx
cmpw $0x1, %dx
ja 0xb7fd
movzbl 0x2(%r15), %edx
shll $0x10, %edx
orl %ecx, %edx
movl %edx, 0x28(%rax)
movl $0x10, 0x24(%rax)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x13, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movzbl %al, %eax
movq (%rbx), %rcx
movl %eax, 0x28(%rcx)
movl $0x12, 0x24(%rcx)
jmp 0xb3ca
movq (%r14), %rdi
callq 0x8966
movsbl %al, %eax
addl %eax, %r13d
movq (%rbx), %rax
movl %r13d, 0x28(%rax)
movl $0x3, 0x24(%rax)
jmp 0xb3ca
movl $0xe, 0x24(%rax)
jmp 0xb3ca
movl $0x7, 0x24(%rax)
jmp 0xb3ca
movl $0x4, 0x24(%rax)
jmp 0xb3ca
movl $0xa, 0x24(%rax)
jmp 0xb3ca
movl $0x11, 0x24(%rax)
jmp 0xb3ca
notl %eax
movq 0x8(%rsp), %rcx
andl (%rcx), %eax
movl %eax, (%rcx)
jmp 0xb604
jmp 0xb81d
jmp 0xb81d
movq %rax, %r14
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x61e4
movq %r14, %rdi
callq 0x53c0