// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
movq %rsi, -0x4e8(%rbp)
movq %rdx, -0x4e0(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x4d8(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x8210
movq -0x4d8(%rbp), %rsi
leaq -0x168(%rbp), %rdi
callq 0x14870
jmp 0x1a551
movq -0x4d8(%rbp), %rsi
addq $0x30, %rsi
leaq -0x188(%rbp), %rdi
movq %rdi, -0x4f0(%rbp)
callq 0x81a0
movq -0x4f0(%rbp), %rcx
leaq -0x120(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x168(%rbp), %rdx
callq 0x304c0
jmp 0x1a592
leaq -0x188(%rbp), %rdi
callq 0x81f0
leaq -0x168(%rbp), %rdi
callq 0x81d0
leaq -0x138(%rbp), %rdi
callq 0x8360
movq -0x4e0(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rdi
callq 0x14140
movq %rax, -0x198(%rbp)
movq -0x190(%rbp), %rdi
callq 0x14170
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x141a0
testb $0x1, %al
jne 0x1a606
jmp 0x1a7bb
leaq -0x198(%rbp), %rdi
callq 0x141e0
movq %rax, %rcx
movq -0x4d8(%rbp), %rax
movq %rcx, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rcx
movq %rcx, -0x4f8(%rbp)
leaq 0x688(%rip), %rcx       # 0x1acc0
movq %rcx, -0x1f0(%rbp)
movq $0x0, -0x1e8(%rbp)
movq %rax, -0x1f8(%rbp)
movq 0x3c960(%rip), %rcx     # 0x56fb8
movq 0x3c991(%rip), %r8      # 0x56ff0
leaq -0x1e0(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
callq 0x1d7b0
jmp 0x1a67b
leaq -0x1c8(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x1d800
jmp 0x1a690
movq -0x4f8(%rbp), %rsi
leaq -0x120(%rbp), %rdi
leaq -0x1c8(%rbp), %rdx
callq 0x30590
movb %al, -0x4f9(%rbp)
jmp 0x1a6b2
movb -0x4f9(%rbp), %al
xorb $-0x1, %al
movb %al, -0x4fa(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x1d8a0
movb -0x4fa(%rbp), %al
testb $0x1, %al
jne 0x1a6db
jmp 0x1a7a8
movq 0x3c906(%rip), %rdi     # 0x56fe8
leaq 0x1fdf7(%rip), %rsi     # 0x3a4e0
callq 0x62f0
movq %rax, -0x508(%rbp)
jmp 0x1a6f7
movq -0x508(%rbp), %rdi
movq 0x3c8a3(%rip), %rsi     # 0x56fa8
callq 0x6330
jmp 0x1a70c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x1fc(%rbp)
jmp 0x1ac81
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x1a75d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x81f0
leaq -0x168(%rbp), %rdi
callq 0x81d0
leaq -0x138(%rbp), %rdi
callq 0x8360
jmp 0x1aca7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x1ac9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x1d8a0
jmp 0x1ac9b
jmp 0x1a7aa
leaq -0x198(%rbp), %rdi
callq 0x14200
jmp 0x1a5ea
leaq -0x120(%rbp), %rdi
callq 0x31430
movb %al, -0x509(%rbp)
jmp 0x1a7cf
movb -0x509(%rbp), %al
testb $0x1, %al
jne 0x1a81f
jmp 0x1a7db
movq 0x3c806(%rip), %rdi     # 0x56fe8
leaq 0x1fd16(%rip), %rsi     # 0x3a4ff
callq 0x62f0
movq %rax, -0x518(%rbp)
jmp 0x1a7f7
movq -0x518(%rbp), %rdi
movq 0x3c7a3(%rip), %rsi     # 0x56fa8
callq 0x6330
jmp 0x1a80c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x1fc(%rbp)
jmp 0x1ac81
movq -0x4e8(%rbp), %rax
movq %rax, -0x208(%rbp)
movq -0x208(%rbp), %rdi
callq 0x7cb0
movq %rax, -0x210(%rbp)
movq -0x208(%rbp), %rdi
callq 0x7ce0
movq %rax, -0x218(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x7d10
testb $0x1, %al
jne 0x1a86f
jmp 0x1ac73
leaq -0x210(%rbp), %rdi
callq 0x7d50
movq %rax, -0x220(%rbp)
movq -0x220(%rbp), %rax
movswl 0x1c(%rax), %esi
leaq -0x260(%rbp), %rdi
callq 0x1d910
jmp 0x1a89b
leaq 0x1fc7e(%rip), %rsi     # 0x3a520
leaq -0x240(%rbp), %rdi
leaq -0x260(%rbp), %rdx
callq 0x1d8c0
jmp 0x1a8b7
leaq -0x260(%rbp), %rdi
callq 0x61e0
movl $0x10, %edi
movl $0x4, %esi
callq 0x122b0
movl %eax, -0x51c(%rbp)
jmp 0x1a8da
movl -0x51c(%rbp), %edi
movl $0x20, %esi
callq 0x122b0
movl %eax, -0x520(%rbp)
jmp 0x1a8f2
movl -0x520(%rbp), %edx
leaq -0x460(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x65c0
jmp 0x1a90d
leaq -0x460(%rbp), %rdi
callq 0x6240
movb %al, -0x521(%rbp)
jmp 0x1a921
movb -0x521(%rbp), %al
testb $0x1, %al
jne 0x1a9fe
jmp 0x1a931
movq 0x3c6b0(%rip), %rdi     # 0x56fe8
leaq 0x1fbe5(%rip), %rsi     # 0x3a524
callq 0x62f0
movq %rax, -0x530(%rbp)
jmp 0x1a94d
movq -0x530(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x62b0
movq %rax, -0x538(%rbp)
jmp 0x1a969
movq -0x538(%rbp), %rdi
leaq 0x1fbbe(%rip), %rsi     # 0x3a535
callq 0x62f0
movq %rax, -0x540(%rbp)
jmp 0x1a985
movq -0x540(%rbp), %rdi
movq 0x3c615(%rip), %rsi     # 0x56fa8
callq 0x6330
jmp 0x1a99a
movb $0x0, -0x1(%rbp)
movl $0x1, -0x1fc(%rbp)
jmp 0x1ac22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x61e0
jmp 0x1ac9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x1ac65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x1ac59
leaq 0x1fb3e(%rip), %rsi     # 0x3a543
leaq -0x460(%rbp), %rdi
callq 0x62f0
movq %rax, -0x548(%rbp)
jmp 0x1aa1a
movq -0x4d8(%rbp), %rsi
movq -0x220(%rbp), %rax
movswl 0x1c(%rax), %edx
leaq -0x480(%rbp), %rdi
movl $0x2, %ecx
callq 0x1bbc0
jmp 0x1aa3f
movq -0x548(%rbp), %rdi
leaq -0x480(%rbp), %rsi
callq 0x62b0
movq %rax, -0x550(%rbp)
jmp 0x1aa5b
movq -0x550(%rbp), %rdi
leaq 0x2041f(%rip), %rsi     # 0x3ae88
callq 0x62f0
movq %rax, -0x558(%rbp)
jmp 0x1aa77
movq -0x558(%rbp), %rdi
movq -0x220(%rbp), %rsi
addq $0x28, %rsi
callq 0x62b0
movq %rax, -0x560(%rbp)
jmp 0x1aa97
movq -0x560(%rbp), %rdi
movq 0x3c503(%rip), %rsi     # 0x56fa8
callq 0x6330
jmp 0x1aaac
leaq -0x480(%rbp), %rdi
callq 0x61e0
movq -0x4d8(%rbp), %rax
movq -0x220(%rbp), %rcx
movl 0x8(%rcx), %edx
movl 0x60(%rcx), %ecx
movl %ecx, -0x568(%rbp)
addl %edx, %ecx
movl %ecx, -0x564(%rbp)
leaq 0x13bf(%rip), %rcx      # 0x1bea0
movq %rcx, -0x4c8(%rbp)
movq $0x0, -0x4c0(%rbp)
movq %rax, -0x4d0(%rbp)
movq 0x3c4b7(%rip), %rcx     # 0x56fb8
leaq -0x4b8(%rbp), %rdi
leaq -0x4c8(%rbp), %rsi
leaq -0x4d0(%rbp), %rdx
callq 0x1da30
jmp 0x1ab1d
leaq -0x4a0(%rbp), %rdi
leaq -0x4b8(%rbp), %rsi
callq 0x1da70
jmp 0x1ab32
movl -0x564(%rbp), %ecx
movl -0x568(%rbp), %edx
leaq -0x120(%rbp), %rdi
leaq -0x460(%rbp), %rsi
leaq -0x4a0(%rbp), %r8
callq 0x31dc0
movb %al, -0x569(%rbp)
jmp 0x1ab60
movb -0x569(%rbp), %al
xorb $-0x1, %al
movb %al, -0x56a(%rbp)
leaq -0x4a0(%rbp), %rdi
callq 0x1db10
movb -0x56a(%rbp), %al
testb $0x1, %al
jne 0x1ab89
jmp 0x1ac0a
movq 0x3c458(%rip), %rdi     # 0x56fe8
leaq 0x1f9b5(%rip), %rsi     # 0x3a54c
callq 0x62f0
movq %rax, -0x578(%rbp)
jmp 0x1aba5
movq -0x578(%rbp), %rdi
movq 0x3c3f5(%rip), %rsi     # 0x56fa8
callq 0x6330
jmp 0x1abba
movb $0x0, -0x1(%rbp)
movl $0x1, -0x1fc(%rbp)
jmp 0x1ac22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x61e0
jmp 0x1ac59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x4a0(%rbp), %rdi
callq 0x1db10
jmp 0x1ac59
leaq -0x460(%rbp), %rdi
callq 0x6440
jmp 0x1ac18
movl $0x0, -0x1fc(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x6470
leaq -0x240(%rbp), %rdi
callq 0x61e0
movl -0x1fc(%rbp), %eax
testl %eax, %eax
jne 0x1ac81
jmp 0x1ac46
jmp 0x1ac48
leaq -0x210(%rbp), %rdi
callq 0x7d70
jmp 0x1a853
leaq -0x460(%rbp), %rdi
callq 0x6470
leaq -0x240(%rbp), %rdi
callq 0x61e0
jmp 0x1ac9b
movb $0x1, -0x1(%rbp)
movl $0x1, -0x1fc(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x1db30
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
leaq -0x120(%rbp), %rdi
callq 0x1db30
movq -0x170(%rbp), %rdi
callq 0x6540
nopw %cs:(%rax,%rax)