// File: /workspace/github_c_makefile_star2_O3/mrkite[P]regs/src/disasm.cc
// Function: Disassembler::disassemble(std::vector<Segment, std::allocator<Segment>>, std::vector<Entry, std::allocator<Entry>>)
bool Disassembler::disassemble(std::vector<Segment> segments,
                          std::vector<Entry> entries) {
  Scanner scanner(segments, symbols, fingerprints);
  // trace all entry points
  for (auto &entry : entries) {
    if (!scanner.trace(entry, std::bind(&Disassembler::decodeInst, this,
                                        ph::_1, ph::_2))) {
      std::cerr << "Failed to trace execution flow" << std::endl;
      return false;
    }
  }
  // build the basic blocks
  if (!scanner.basicBlocks()) {
    std::cerr << "Failed to calculate basic blocks" << std::endl;
    return false;
  }
  // disassemble each segment
  for (auto &segment : segments) {
    std::string fname = "seg" + std::to_string(segment.segnum);
    std::ofstream f(fname, std::ios::out | std::ios::binary | std::ios::trunc);
    if (!f.is_open()) {
      std::cerr << "Failed to open '" << fname << "' for writing" << std::endl;
      return false;
    }
    f << "Segment " << hex(segment.segnum, 2) << " "
      << segment.name << std::endl;
    if (!scanner.disassemble(f, segment.mapped,
                             segment.mapped + segment.length,
                             std::bind(&Disassembler::printInst, this,
                                       ph::_1))) {
      std::cerr << "Disassembly failed" << std::endl;
      return false;
    }
    f.close();
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rdx, %r15
movq %rdi, %rbp
leaq 0xc0(%rsp), %rdi
movq %rsi, 0x8(%rsp)
callq 0x65d0
leaq 0xd8(%rsp), %rdi
movq %rbp, %rsi
callq 0xab58
movq 0x30(%rbp), %rax
movq %rax, 0x60(%rsp)
movq 0x38(%rbp), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0xc885
movq 0x14751(%rip), %rcx     # 0x20fc8
cmpb $0x0, (%rcx)
je 0xc881
incl 0x8(%rax)
jmp 0xc885
lock
incl 0x8(%rax)
leaq 0x108(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x13470
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0xc8b6
callq 0x671a
leaq 0xd8(%rsp), %rdi
callq 0x678a
leaq 0xc0(%rsp), %rdi
callq 0x6668
movq (%r15), %r12
movq 0x8(%r15), %r14
cmpq %r14, %r12
je 0xc986
leaq 0x108(%rsp), %r15
leaq 0x70(%rsp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl $0x18, %edi
callq 0x5220
leaq 0x706(%rip), %rcx       # 0xd014
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movq %rbp, %rbx
movq %rbp, 0x10(%rax)
movq %rax, 0x70(%rsp)
leaq 0x2b36(%rip), %rax      # 0xf462
movq %rax, 0x88(%rsp)
leaq 0x2b43(%rip), %rax      # 0xf47e
movq %rax, 0x80(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1354c
movl %eax, %ebp
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0xc96d
movq %r13, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq *%rax
testb %bpl, %bpl
je 0xcd27
addq $0xc, %r12
cmpq %r14, %r12
movq %rbx, %rbp
jne 0xc8ed
leaq 0x108(%rsp), %rdi
callq 0x13ece
testb %al, %al
je 0xccdc
movq %rbp, 0x38(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rbp
movq 0x8(%rax), %rax
movb $0x1, %r14b
movq %rax, 0xb8(%rsp)
cmpq %rax, %rbp
je 0xce64
leaq 0x20(%rsp), %rbx
movq 0x145f4(%rip), %rax     # 0x20fc0
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x208(%rsp), %r13
movq %rbx, %r14
movswl 0x1c(%rbp), %ebx
movl %ebx, %r15d
negl %r15d
cmovsl %ebx, %r15d
movl $0x1, %r12d
cmpl $0xa, %r15d
jb 0xca2e
movl $0x2, %r12d
cmpl $0x64, %r15d
jb 0xca2e
movl $0x3, %r12d
cmpl $0x3e8, %r15d           # imm = 0x3E8
jb 0xca2e
cmpl $0x2710, %r15d          # imm = 0x2710
movl $0x5, %r12d
sbbl $0x0, %r12d
shrl $0x1f, %ebx
leal (%r12,%rbx), %esi
leaq 0x218(%rsp), %rax
movq %rax, 0x208(%rsp)
movq %r13, %rdi
movl $0x2d, %edx
callq 0x5270
movl %ebx, %edi
addq 0x208(%rsp), %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0xf0e5
movl $0x3, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xaae5(%rip), %rcx      # 0x17560
callq 0x5250
movq %r14, %r15
movq %r14, 0x10(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xcaa6
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0xcaad
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0xcaea
movq 0x218(%rsp), %rsi
incq %rsi
callq 0x5230
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl $0x34, %edx
callq 0x5440
leaq 0x278(%rsp), %rdi
callq 0x50b0
testb %al, %al
je 0xcd72
movl $0x8, %edx
movq %r13, %rdi
leaq 0xaa63(%rip), %rsi      # 0x17583
callq 0x5260
movswl 0x1c(%rbp), %edx
leaq 0x98(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x2, %ecx
callq 0xd6b0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq %r13, %rdi
callq 0x5260
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb333(%rip), %rsi      # 0x17e9d
callq 0x5260
movq 0x28(%rbp), %rsi
movq 0x30(%rbp), %rdx
movq %r15, %rdi
callq 0x5260
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq %r15, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5190
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xcbce
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
movl 0x8(%rbp), %r15d
movl 0x60(%rbp), %r12d
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x18, %edi
callq 0x5220
addl %r12d, %r15d
leaq 0xcd7(%rip), %rcx       # 0xd8ce
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x40(%rsp)
leaq 0x2963(%rip), %rax      # 0xf57a
movq %rax, 0x58(%rsp)
leaq 0x296f(%rip), %rax      # 0xf592
movq %rax, 0x50(%rsp)
leaq 0x108(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
movl %r15d, %ecx
leaq 0x40(%rsp), %r8
callq 0x14500
movl %eax, %r15d
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0xcc5f
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
testb %r15b, %r15b
je 0xcdcd
movq %r13, %rdi
movq %r14, %rbx
callq 0x5300
movq 0x8(%rsp), %rax
movq %rax, 0x208(%rsp)
movq -0x18(%rax), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x208(%rsp,%rax)
leaq 0x210(%rsp), %rdi
callq 0x51e0
leaq 0x300(%rsp), %rdi
callq 0x5110
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xccc2
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
addq $0x68, %rbp
cmpq 0xb8(%rsp), %rbp
jne 0xc9e5
movb $0x1, %r14b
jmp 0xce64
movq 0x1430d(%rip), %rbx     # 0x20ff0
leaq 0xa855(%rip), %rsi      # 0x1753f
movl $0x20, %edx
movq %rbx, %rdi
callq 0x5260
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq 0x142db(%rip), %rdi     # 0x20ff0
callq 0x5040
movq %rax, %rdi
callq 0x5190
jmp 0xce61
movq 0x142c2(%rip), %rbx     # 0x20ff0
leaq 0xa7eb(%rip), %rsi      # 0x17520
movl $0x1e, %edx
movq %rbx, %rdi
callq 0x5260
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq 0x14290(%rip), %rdi     # 0x20ff0
callq 0x5040
movq %rax, %rdi
callq 0x5190
jmp 0xce61
movq 0x14277(%rip), %rdi     # 0x20ff0
leaq 0xa7e4(%rip), %rsi      # 0x17564
movl $0x10, %edx
callq 0x5260
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x14255(%rip), %rdi     # 0x20ff0
callq 0x5260
movq %rax, %rbx
leaq 0xa7cb(%rip), %rsi      # 0x17575
movl $0xd, %edx
movq %rax, %rdi
callq 0x5260
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5210
jmp 0xcdff
movq 0x1421c(%rip), %rbx     # 0x20ff0
leaq 0xa7b1(%rip), %rsi      # 0x1758c
movl $0x12, %edx
movq %rbx, %rdi
movq %r14, %r15
callq 0x5260
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5190
movq 0x8(%rsp), %rax
movq %rax, 0x208(%rsp)
movq -0x18(%rax), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x208(%rsp,%rax)
leaq 0x210(%rsp), %rdi
callq 0x51e0
leaq 0x300(%rsp), %rdi
callq 0x5110
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0xce61
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
xorl %r14d, %r14d
leaq 0x1d8(%rsp), %rdi
callq 0xf1a0
leaq 0x1c0(%rsp), %rdi
callq 0xf152
leaq 0x190(%rsp), %rdi
callq 0xf20e
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0xf20e
movq -0x8(%rbx), %rdi
testq %rdi, %rdi
je 0xcea9
callq 0x671a
leaq 0x138(%rsp), %rdi
callq 0x6668
leaq 0x108(%rsp), %rdi
callq 0x678a
movl %r14d, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xcf7d
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0xceef
callq 0x671a
leaq 0xd8(%rsp), %rdi
callq 0x678a
jmp 0xcf01
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x6668
jmp 0xd00b
jmp 0xcfad
jmp 0xcfa5
jmp 0xcfad
jmp 0xcf7d
movq %rax, %rbx
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0xcfda
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xcfda
jmp 0xcfa5
jmp 0xcf7d
jmp 0xcfad
movq %rax, %rbx
jmp 0xcfe7
movq %rax, %rbx
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0xcffe
movq 0x218(%rsp), %rsi
jmp 0xcff6
movq %rax, %rbx
jmp 0xcfda
jmp 0xcfa5
movq %rax, %rbx
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0xcffe
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xcffe
movq %rax, %rdi
callq 0x670f
movq %rax, %rbx
jmp 0xcffe
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xcfda
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
leaq 0x208(%rsp), %rdi
callq 0x5330
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0xcffe
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
leaq 0x108(%rsp), %rdi
callq 0xef62
movq %rbx, %rdi
callq 0x53e0
nop