// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0xc0(%rsp), %rdi
movq %rsi, 0x8(%rsp)
callq 0x6608
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0xad5a
movq 0x30(%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x38(%rbx), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0xcaf5
movq 0x144e1(%rip), %rcx     # 0x20fc8
cmpb $0x0, (%rcx)
je 0xcaf1
incl 0x8(%rax)
jmp 0xcaf5
lock
incl 0x8(%rax)
leaq 0x108(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x136dc
movq %rbx, 0x38(%rsp)
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0xcb2b
callq 0x6752
leaq 0xd8(%rsp), %rdi
callq 0x67c2
leaq 0xc0(%rsp), %rdi
callq 0x66a0
movq (%r14), %r15
movq 0x8(%r14), %rbx
cmpq %rbx, %r15
je 0xcc43
leaq 0x108(%rsp), %r12
leaq 0x70(%rsp), %r13
movq 0x14487(%rip), %rbp     # 0x20ff0
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl $0x18, %edi
callq 0x5220
leaq 0x6fa(%rip), %rcx       # 0xd284
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x70(%rsp)
leaq 0x2aa6(%rip), %rax      # 0xf650
movq %rax, 0x88(%rsp)
leaq 0x2ab3(%rip), %rax      # 0xf66c
movq %rax, 0x80(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x137b8
movl %eax, %r14d
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0xcbec
movq %r13, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq *%rax
testb %r14b, %r14b
jne 0xcc36
movl $0x1e, %edx
movq %rbp, %rdi
leaq 0xa920(%rip), %rsi      # 0x17520
callq 0x5260
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq %rbp, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5190
testb %r14b, %r14b
je 0xcfd5
addq $0xc, %r15
cmpq %rbx, %r15
jne 0xcb69
leaq 0x108(%rsp), %rdi
callq 0x14142
testb %al, %al
je 0xcf8f
movq 0x8(%rsp), %rax
movq (%rax), %r15
movq 0x8(%rax), %rax
movq %rax, 0xb8(%rsp)
cmpq %rax, %r15
sete %bpl
je 0xcfd7
movq 0x14340(%rip), %rax     # 0x20fc0
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x208(%rsp), %r13
movswl 0x1c(%r15), %ebx
movl %ebx, %r12d
negl %r12d
cmovsl %ebx, %r12d
movl $0x1, %r14d
cmpl $0xa, %r12d
jb 0xcce0
movl $0x2, %r14d
cmpl $0x64, %r12d
jb 0xcce0
movl $0x3, %r14d
cmpl $0x3e8, %r12d           # imm = 0x3E8
jb 0xcce0
cmpl $0x2710, %r12d          # imm = 0x2710
movl $0x5, %r14d
sbbl $0x0, %r14d
shrl $0x1f, %ebx
leal (%r14,%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 %r14d, %esi
movl %r12d, %edx
callq 0xf2d7
movl $0x3, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xa833(%rip), %rcx      # 0x17560
callq 0x5250
leaq 0x20(%rsp), %r14
movq %r14, 0x10(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
movq 0x38(%rsp), %rbx
je 0xcd5f
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0xcd66
movups (%rcx), %xmm0
movups %xmm0, (%r14)
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 0xcda3
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 0xd04e
movl $0x8, %edx
movq %r13, %rdi
leaq 0xa7aa(%rip), %rsi      # 0x17583
callq 0x5260
movswl 0x1c(%r15), %edx
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
movl $0x2, %ecx
callq 0xd890
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq %r13, %rdi
callq 0x5260
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb07b(%rip), %rsi      # 0x17e9d
callq 0x5260
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
movq %r14, %rdi
callq 0x5260
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5190
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xce86
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
movl 0x8(%r15), %r14d
movl 0x60(%r15), %r12d
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x18, %edi
callq 0x5220
addl %r12d, %r14d
leaq 0xc13(%rip), %rcx       # 0xdac2
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, 0x40(%rsp)
leaq 0x28b0(%rip), %rax      # 0xf77a
movq %rax, 0x58(%rsp)
leaq 0x28bc(%rip), %rax      # 0xf792
movq %rax, 0x50(%rsp)
leaq 0x108(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
movl %r14d, %ecx
leaq 0x40(%rsp), %r8
callq 0x14758
movl %eax, %r14d
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0xcf12
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
testb %r14b, %r14b
je 0xd0a9
movq %r13, %rdi
leaq 0x20(%rsp), %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 0xcf77
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
addq $0x68, %r15
cmpq 0xb8(%rsp), %r15
sete %bpl
jne 0xcc99
jmp 0xcfd7
movq 0x1405a(%rip), %rbx     # 0x20ff0
leaq 0xa5a2(%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 0x14028(%rip), %rdi     # 0x20ff0
callq 0x5040
movq %rax, %rdi
callq 0x5190
xorl %ebp, %ebp
leaq 0x1d8(%rsp), %rdi
callq 0xf38c
leaq 0x1c0(%rsp), %rdi
callq 0xf344
leaq 0x190(%rsp), %rdi
callq 0xf3fa
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0xf3fa
movq -0x8(%rbx), %rdi
testq %rdi, %rdi
je 0xd01c
callq 0x6752
leaq 0x138(%rsp), %rdi
callq 0x66a0
leaq 0x108(%rsp), %rdi
callq 0x67c2
andb $0x1, %bpl
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x13f9b(%rip), %rdi     # 0x20ff0
leaq 0xa508(%rip), %rsi      # 0x17564
movl $0x10, %edx
callq 0x5260
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x13f79(%rip), %rdi     # 0x20ff0
callq 0x5260
movq %rax, %rbx
leaq 0xa4ef(%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 0xd0dd
movq 0x13f40(%rip), %rbx     # 0x20ff0
leaq 0xa4d5(%rip), %rsi      # 0x1758c
movl $0x12, %edx
movq %rbx, %rdi
leaq 0x20(%rsp), %r14
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
leaq 0x210(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, -0x8(%rdi)
movq -0x18(%rax), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x208(%rsp,%rax)
callq 0x51e0
leaq 0x300(%rsp), %rdi
callq 0x5110
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0xcfd7
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0xcfd7
jmp 0xd1e7
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0xd15b
callq 0x6752
leaq 0xd8(%rsp), %rdi
callq 0x67c2
jmp 0xd16d
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x66a0
jmp 0xd27c
jmp 0xd211
jmp 0xd26c
jmp 0xd1e7
movq %rax, %rbx
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0xd241
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xd241
jmp 0xd211
jmp 0xd1e7
jmp 0xd26c
movq %rax, %rbx
jmp 0xd24e
movq %rax, %rbx
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0xd26f
movq 0x218(%rsp), %rsi
jmp 0xd262
movq %rax, %rbx
jmp 0xd241
jmp 0xd211
jmp 0xd26c
movq %rax, %rbx
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0xd26f
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xd26f
movq %rax, %rdi
callq 0x6747
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xd241
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
leaq 0x208(%rsp), %rdi
callq 0x5330
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xd26f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0xd26f
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0xf154
movq %rbx, %rdi
callq 0x53e0