// File: /workspace/github_cpp_makefile_star2_O1/donfreiday[P]gb/main.cpp
// Function: disassemble(CPU&, unsigned short&)
void disassemble(CPU& cpu, u16& pc) {
  g_disasm.address = pc;
  g_disasm.opcode = cpu.mmu.memory[pc];
  g_disasm.operandSize = 0;
  g_disasm.operand = 0;

  if (g_disasm.opcode == 0xCB) {
    g_disasm.operandSize = 1;
    g_disasm.operand = cpu.mmu.memory[++pc];
    g_disasm.str = cpu.instructions_CB[g_disasm.operand].disassembly;
  } else if (cpu.instructions[g_disasm.opcode].operandLength == 1) {
    g_disasm.operandSize = 1;
    g_disasm.operand = cpu.mmu.memory[++pc];
    g_disasm.str = cpu.instructions[g_disasm.opcode].disassembly;
  } else if (cpu.instructions[g_disasm.opcode].operandLength == 2) {
    g_disasm.operandSize = 2;
    g_disasm.operand = cpu.mmu.read16(++pc);
    g_disasm.str = cpu.instructions[g_disasm.opcode].disassembly;
    pc++;
  } else {
    ++pc;
    g_disasm.operandSize = 0;
    g_disasm.str = cpu.instructions[g_disasm.opcode].disassembly;
  }

  // Make sure we don't skip over any code entry points
  bool skippedEntry = false;
  for (u16 address : g_disasm.knownEntryPoints) {
    if (pc < address && pc + g_disasm.operandSize > address) {
      skippedEntry = true;
      pc = address;
    }
  }

  // Data sections are mixed in with code. So we need to make sure
  // that we don't skip right over the PC by blindly disassembling
  if (pc < g_cpu.reg.pc && pc + g_disasm.operandSize > g_cpu.reg.pc) {
    pc = g_cpu.reg.pc;
    g_disasm.knownEntryPoints.insert(pc);
    skippedEntry = true;
  }

  if (!skippedEntry) {
    pc += g_disasm.operandSize;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movzwl (%rsi), %eax
movw %ax, 0x36a3e(%rip)      # 0x72ef8
movzwl (%rsi), %eax
movq 0x20(%rdi), %rcx
movzbl (%rcx,%rax), %eax
movb %al, 0x36a2f(%rip)      # 0x72efa
movb $0x0, 0x36a2c(%rip)     # 0x72efe
movw $0x0, 0x36a21(%rip)     # 0x72efc
cmpq $0xcb, %rax
jne 0x3c517
movb $0x1, 0x36a14(%rip)     # 0x72efe
leaq 0x369e7(%rip), %r14     # 0x72ed8
movzwl (%rbx), %eax
incl %eax
movw %ax, (%rbx)
movzwl %ax, %eax
movzbl (%rcx,%rax), %eax
movw %ax, 0x369f5(%rip)      # 0x72efc
shll $0x4, %eax
movq 0x12a8(%rdi,%rax), %r15
jmp 0x3c5d5
leaq 0x2a8(%rdi), %r15
movl %eax, %edx
shll $0x4, %edx
movzbl 0x2b0(%rdi,%rdx), %edx
cmpl $0x2, %edx
je 0x3c55f
cmpl $0x1, %edx
jne 0x3c5bd
movb $0x1, 0x369be(%rip)     # 0x72efe
leaq 0x36991(%rip), %r14     # 0x72ed8
movzwl (%rbx), %edx
incl %edx
movw %dx, (%rbx)
movzwl %dx, %edx
movzbl (%rcx,%rdx), %ecx
movw %cx, 0x3699f(%rip)      # 0x72efc
jmp 0x3c5ce
addq $0x18, %rdi
movb $0x2, 0x36994(%rip)     # 0x72efe
leaq 0x36967(%rip), %r14     # 0x72ed8
movzwl (%rbx), %eax
incl %eax
movw %ax, (%rbx)
movzwl %ax, %esi
callq 0x3837c
movw %ax, 0x36974(%rip)      # 0x72efc
movzbl 0x3696b(%rip), %eax     # 0x72efa
shll $0x4, %eax
movq (%r15,%rax), %r15
movq 0x36943(%rip), %r12     # 0x72ee0
movq %r15, %rdi
callq 0x72f0
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x73c0
incw (%rbx)
jmp 0x3c5f7
incw (%rbx)
movb $0x0, 0x36937(%rip)     # 0x72efe
leaq 0x3690a(%rip), %r14     # 0x72ed8
shll $0x4, %eax
movq (%r15,%rax), %r15
movq 0x36904(%rip), %r12     # 0x72ee0
movq %r15, %rdi
callq 0x72f0
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x73c0
movq 0x3691a(%rip), %rax     # 0x72f18
leaq 0x36903(%rip), %r15     # 0x72f08
cmpq %r15, %rax
je 0x3c649
movzwl (%rbx), %r13d
movzbl 0x368e8(%rip), %r12d    # 0x72efe
xorl %r14d, %r14d
movzwl 0x20(%rax), %ecx
cmpw %cx, %r13w
jae 0x3c63a
movzwl %cx, %edx
movzwl %r13w, %esi
addl %r12d, %esi
cmpl %edx, %esi
jbe 0x3c63a
movw %cx, (%rbx)
movb $0x1, %r14b
movl %ecx, %r13d
movq %rax, %rdi
callq 0x76c0
cmpq %r15, %rax
jne 0x3c619
jmp 0x3c64c
xorl %r14d, %r14d
movzwl (%rbx), %ecx
movzwl 0x237ba(%rip), %eax     # 0x5fe10
cmpw %ax, %cx
jae 0x3c680
movzwl %ax, %edx
movzbl 0x36899(%rip), %esi     # 0x72efe
addl %ecx, %esi
cmpl %edx, %esi
jbe 0x3c680
movw %ax, (%rbx)
leaq 0x3688b(%rip), %rdi     # 0x72f00
movq %rbx, %rsi
callq 0x3c794
movb $0x1, %r14b
testb $0x1, %r14b
jne 0x3c690
movzbl 0x36871(%rip), %eax     # 0x72efe
addw %ax, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq