// File: /workspace/github_c_makefile_star2_O3/mrkite[P]regs/src/main.cc
// Function: main
int main(int argc, char **argv) {
  API api(iigs_dat, iigs_dat_len);

  struct arguments arguments;
  arguments.filename = "";
  arguments.keyword = nullptr;
  arguments.org = 0;
  arguments.flags = 0;
  argp_parse(&argp, argc, argv, 0, 0, &arguments);

  if (arguments.keyword) {
    api.search(arguments.keyword, arguments.org);
    return 0;
  }

  // load map if it exists
  Map map(arguments.filename, arguments.org);
  OMF omf;
  if (!omf.load(arguments.filename, map.org)) {
    std::cerr << "Failed to load " << arguments.filename << std::endl;
    return -1;
  }
  auto segments = omf.get();
  if (map.needsEntry()) {
    for (auto &s : segments) {
      if ((s.kind & 0x1f) == 0) {  // first code
        map.addEntry(s.mapped + s.entry, arguments.flags);
        break;
      }
    }
  }

  // note that we save the map here.. before we add a bunch of symbols
  // from the api
  map.save();


  auto prints = std::make_shared<Fingerprints>();
  for (auto s : api.symbols) {
    if (s.second->kind == symbol::isFunction) {
      auto f = std::static_pointer_cast<symbol::Function>(s.second);
      if (f->signature.size() >= 2) {
        if (f->signature[0] >= 0) {  // tool
          // ldx tool, jsl e1/0000
          std::vector<uint8_t> sig = { 0xa2, 0x00, 0x00,
            0x22, 0x00, 0x00, 0xe1 };
          sig[1] = f->signature[0];
          sig[2] = f->signature[1];
          prints->add(sig, f->name);
        } else if (f->signature[0] == -1) {  // p16/gsos
          // jsl e1/00a8
          std::vector<uint8_t> sig = { 0x22, 0xa8, 0x00, 0xe1, 0x00, 0x00 };
          sig[4] = f->signature[2] & 0xff;
          sig[5] = f->signature[2] >> 8;
          prints->add(sig, f->name, f->signature[1] & 0xff);
        } else if (f->signature[0] == -2) {  // p8
          // jsr bf00
          std::vector<uint8_t> sig = { 0x20, 0x00, 0xbf, 0x00 };
          sig[3] = f->signature[2];
          prints->add(sig, f->name, f->signature[1] & 0xff);
        } else if (f->signature[0] == -3) {  // smartport
          // jsr c50d
          std::vector<uint8_t> sig5 = { 0x20, 0x0d, 0xc5, 0x00 };
          std::vector<uint8_t> sig7 = { 0x20, 0x0d, 0xc7, 0x00 };
          sig5[3] = f->signature[2];
          sig7[3] = f->signature[2];
          prints->add(sig5, f->name, f->signature[1]);
          prints->add(sig7, f->name, f->signature[1]);
        } else if (f->signature[0] == -4) {  // symbol
          map.addSymbol(f->signature[1], f->name);
        }
      }
    }
  }

  Disassembler d(prints, map.getSymbols(), map.b);
  d.disassemble(segments, map.getEntries());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, %rbx
movl %edi, %ebp
movl 0x4ad3c(%rip), %edx     # 0x50514
leaq 0x1ba61(%rip), %rsi     # 0x21240
leaq 0x2b8(%rsp), %rdi
callq 0xf96a
leaq 0x11a7f(%rip), %rax     # 0x17272
leaq 0x50(%rsp), %r9
movq %rax, (%r9)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r9)
leaq 0x4ad1e(%rip), %rdi     # 0x50528
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x50c0
movq 0x60(%rsp), %rbx
testq %rbx, %rbx
je 0x5884
leaq 0x178(%rsp), %r15
movq %r15, -0x10(%r15)
movq %rbx, %rdi
callq 0x5150
leaq (%rax,%rbx), %rdx
leaq 0x168(%rsp), %rdi
movq %rbx, %rsi
callq 0x6920
movl 0x58(%rsp), %edx
leaq 0x2b8(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x1109e
movq 0x168(%rsp), %rdi
xorl %ebx, %ebx
cmpq %r15, %rdi
je 0x604b
movq 0x178(%rsp), %rsi
jmp 0x6043
movq 0x50(%rsp), %rsi
movl 0x58(%rsp), %edx
leaq 0x210(%rsp), %rdi
callq 0x9a40
leaq 0x1e8(%rsp), %rdi
callq 0x6c48
movq 0x50(%rsp), %rsi
movl 0x210(%rsp), %edx
leaq 0x1e8(%rsp), %rdi
callq 0x6cb2
testb %al, %al
je 0x5917
leaq 0x150(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x7d06
leaq 0x210(%rsp), %rdi
callq 0xa5e0
testb %al, %al
je 0x59bb
movq 0x150(%rsp), %rax
movq 0x158(%rsp), %rcx
cmpq %rcx, %rax
je 0x59bb
testb $0x1f, 0x10(%rax)
je 0x59a4
addq $0x68, %rax
jmp 0x58fe
movq 0x1b6d2(%rip), %rbx     # 0x20ff0
leaq 0x1170a(%rip), %rsi     # 0x1702f
movl $0xf, %edx
movq %rbx, %rdi
callq 0x5260
movq 0x50(%rsp), %r14
testq %r14, %r14
je 0x5958
movq %r14, %rdi
callq 0x5150
movq 0x1b6a5(%rip), %rdi     # 0x20ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x5260
jmp 0x596f
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x53c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5210
movsbl %al, %esi
movq 0x1b663(%rip), %rdi     # 0x20ff0
callq 0x5040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x5190
jmp 0x5fe3
movl 0x20(%rax), %esi
addl 0x60(%rax), %esi
movl 0x5c(%rsp), %edx
leaq 0x210(%rsp), %rdi
callq 0xa60a
leaq 0x210(%rsp), %rdi
callq 0x9f7a
leaq 0x28(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x20(%rsp), %rsi
leaq 0x188(%rsp), %rdx
callq 0x6a04
leaq 0x2c0(%rsp), %rax
movq 0x10(%rax), %r14
cmpq %rax, %r14
je 0x5eb4
leaq 0x30(%rsp), %r15
leaq 0x1c(%rsp), %rbp
leaq 0x118(%rsp), %rbx
leaq 0x20(%r14), %rsi
leaq 0x188(%rsp), %rdi
callq 0x650e
movq 0x1a8(%rsp), %r12
cmpl $0x6, 0x24(%r12)
jne 0x5b4d
movq 0x1b0(%rsp), %rax
testq %rax, %rax
leaq 0xf8(%rsp), %rdi
je 0x5a5d
movq 0x1b579(%rip), %rcx     # 0x20fc8
cmpb $0x0, (%rcx)
je 0x5a59
incl 0x8(%rax)
jmp 0x5a5d
lock
incl 0x8(%rax)
movq %rax, 0x48(%rsp)
movq 0x50(%r12), %rax
movq 0x58(%r12), %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jb 0x5b3e
movl (%rax), %ecx
testl %ecx, %ecx
js 0x5ba5
movl $0x220000a2, 0x30(%rsp) # imm = 0x220000A2
movw $0x0, 0x34(%rsp)
movb $-0x1f, 0x36(%rsp)
movl $0x7, %edx
movq %rsp, %rdi
movq %r15, %rsi
movq %rbp, %rcx
callq 0x6560
movq 0x50(%r12), %rax
movb (%rax), %al
movq (%rsp), %rcx
movb %al, 0x1(%rcx)
movq 0x50(%r12), %rax
movb 0x4(%rax), %al
movq (%rsp), %rcx
movb %al, 0x2(%rcx)
movq 0x20(%rsp), %r13
leaq 0x128(%rsp), %rax
movq %rax, 0x118(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %rbx, %r12
movq %rbx, %rdi
callq 0x688a
movq %r13, %rdi
movq %rsp, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xf79e
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x5b28
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x5230
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x5b3e
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x5230
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x5b4d
callq 0x671a
movq 0x1b0(%rsp), %rdi
testq %rdi, %rdi
je 0x5b5f
callq 0x671a
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x5b84
movq 0x198(%rsp), %rsi
incq %rsi
callq 0x5230
movq %r14, %rdi
callq 0x51a0
movq %rax, %r14
leaq 0x2c0(%rsp), %rax
cmpq %rax, %r14
jne 0x5a0e
jmp 0x5eb4
addl $0x4, %ecx
cmpl $0x3, %ecx
ja 0x5b3e
leaq 0x11450(%rip), %rdx     # 0x17004
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl 0x4(%rax), %r13d
leaq 0x108(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %rdi, %r12
callq 0x688a
leaq 0x210(%rsp), %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0xa64a
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x5b3e
movq 0x108(%rsp), %rsi
incq %rsi
jmp 0x5b39
movl $0xbf0020, 0x30(%rsp)   # imm = 0xBF0020
movl $0x4, %edx
movq %rsp, %rdi
movq %r15, %rsi
movq %rbp, %rcx
callq 0x6560
movq 0x50(%r12), %rax
movb 0x8(%rax), %al
movq (%rsp), %rcx
movb %al, 0x3(%rcx)
movq 0x20(%rsp), %r13
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x688a
movq 0x50(%r12), %rax
movzbl 0x4(%rax), %ecx
movq %r13, %rdi
movq %rsp, %rsi
leaq 0xb8(%rsp), %rdx
callq 0xf79e
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x5b28
movq 0xc8(%rsp), %rsi
jmp 0x5b20
movl $0xe100a822, 0x30(%rsp) # imm = 0xE100A822
movw $0x0, 0x34(%rsp)
movl $0x6, %edx
movq %rsp, %rdi
movq %r15, %rsi
movq %rbp, %rcx
callq 0x6560
movq 0x50(%r12), %rax
movb 0x8(%rax), %al
movq (%rsp), %rcx
movb %al, 0x4(%rcx)
movq 0x50(%r12), %rax
movb 0x9(%rax), %al
movq (%rsp), %rcx
movb %al, 0x5(%rcx)
movq 0x20(%rsp), %r13
leaq 0xe8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xd8(%rsp), %rdi
callq 0x688a
movq 0x50(%r12), %rax
movzbl 0x4(%rax), %ecx
movq %r13, %rdi
movq %rsp, %rsi
leaq 0xd8(%rsp), %rdx
callq 0xf79e
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x5b28
movq 0xe8(%rsp), %rsi
jmp 0x5b20
movl $0xc50d20, 0x30(%rsp)   # imm = 0xC50D20
movl $0x4, %edx
movq %rsp, %rdi
movq %r15, %r13
movq %r15, %rsi
movq %rbp, %rcx
callq 0x6560
movl $0xc70d20, 0x1c(%rsp)   # imm = 0xC70D20
movl $0x4, %edx
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x1b(%rsp), %rcx
callq 0x6560
movq 0x50(%r12), %rax
movb 0x8(%rax), %al
movq (%rsp), %rcx
movb %al, 0x3(%rcx)
movq 0x50(%r12), %rax
movb 0x8(%rax), %al
movq 0x30(%rsp), %rcx
movb %al, 0x3(%rcx)
movq 0x20(%rsp), %r13
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0x688a
movq 0x50(%r12), %rax
movzbl 0x4(%rax), %ecx
movq %r13, %rdi
movq %rsp, %rsi
leaq 0x98(%rsp), %rdx
callq 0xf79e
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x5e36
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
movq 0x20(%rsp), %r13
leaq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x688a
movq 0x50(%r12), %rax
movzbl 0x4(%rax), %ecx
movq %r13, %rdi
movq %r15, %rsi
leaq 0x78(%rsp), %rdx
callq 0xf79e
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x5e99
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x5230
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5b28
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
jmp 0x5b23
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq %rax, 0x70(%rsp)
testq %rax, %rax
je 0x5ee2
movq 0x1b0f4(%rip), %rcx     # 0x20fc8
cmpb $0x0, (%rcx)
je 0x5ede
incl 0x8(%rax)
jmp 0x5ee2
lock
incl 0x8(%rax)
leaq 0x288(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xa5f8
movzbl 0x214(%rsp), %ecx
leaq 0x188(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x288(%rsp), %rdx
callq 0xc780
leaq 0x288(%rsp), %rdi
callq 0x678a
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x5f35
callq 0x671a
leaq 0x1d0(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x65d0
leaq 0x138(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xa5e6
leaq 0x188(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
leaq 0x138(%rsp), %rdx
callq 0xc820
movq 0x138(%rsp), %rdi
testq %rdi, %rdi
je 0x5f99
movq 0x148(%rsp), %rsi
subq %rdi, %rsi
callq 0x5230
leaq 0x1d0(%rsp), %rdi
callq 0x6668
movq 0x1c0(%rsp), %rdi
testq %rdi, %rdi
je 0x5fb8
callq 0x671a
leaq 0x188(%rsp), %rdi
callq 0x678a
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x5fd4
callq 0x671a
leaq 0x150(%rsp), %rdi
callq 0x6668
xorl %ebx, %ebx
leaq 0x1f8(%rsp), %r14
movq %r14, %rdi
callq 0x6668
movq -0x8(%r14), %rdi
testq %rdi, %rdi
je 0x6001
callq 0x671a
leaq 0x258(%rsp), %r14
movq %r14, %rdi
callq 0x678a
movq -0x18(%r14), %rdi
testq %rdi, %rdi
je 0x602a
movq 0x250(%rsp), %rsi
subq %rdi, %rsi
callq 0x5230
leaq 0x228(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x604b
movq 0x228(%rsp), %rsi
incq %rsi
callq 0x5230
leaq 0x2e8(%rsp), %rdi
callq 0x678a
leaq 0x2b8(%rsp), %rdi
callq 0x6802
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x620b
movq %rax, %r14
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x6268
movq 0x108(%rsp), %rsi
incq %rsi
jmp 0x625e
jmp 0x6265
movq %rax, %r14
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x6173
movq 0x88(%rsp), %rsi
jmp 0x60f6
movq %rax, %r14
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x6173
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x6173
jmp 0x624a
jmp 0x6265
movq %rax, %r14
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x624d
movq 0xc8(%rsp), %rsi
jmp 0x6240
jmp 0x624a
jmp 0x6265
movq %rax, %r14
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x624d
movq 0xe8(%rsp), %rsi
jmp 0x6240
jmp 0x624a
jmp 0x6265
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x624d
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
jmp 0x6243
movq %rax, %r14
movq 0x138(%rsp), %rdi
testq %rdi, %rdi
je 0x61b3
movq 0x148(%rsp), %rsi
subq %rdi, %rsi
callq 0x5230
jmp 0x61b3
movq %rax, %r14
leaq 0x1d0(%rsp), %rdi
callq 0x6668
jmp 0x61c5
movq %rax, %r14
leaq 0x188(%rsp), %rdi
callq 0x6698
jmp 0x62ba
movq %rax, %r14
leaq 0x288(%rsp), %rdi
callq 0x678a
jmp 0x61ec
movq %rax, %r14
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x62ba
callq 0x671a
jmp 0x62ba
jmp 0x620b
jmp 0x62b2
movq %rax, %r14
jmp 0x62c9
movq %rax, %r14
jmp 0x62e3
jmp 0x62ad
movq %rax, %r14
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x624d
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x624d
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x6268
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x5230
jmp 0x6268
movq %rax, %r14
cmpq $0x0, 0x48(%rsp)
je 0x627a
movq 0x48(%rsp), %rdi
callq 0x671a
leaq 0x188(%rsp), %rdi
callq 0x65a4
jmp 0x62ba
movq %rax, %r14
movq 0x168(%rsp), %rdi
cmpq %r15, %rdi
je 0x62f0
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x62f0
jmp 0x62ad
movq %rax, %r14
jmp 0x62f0
movq %rax, %r14
jmp 0x62d6
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x62c9
callq 0x671a
leaq 0x150(%rsp), %rdi
callq 0x6668
leaq 0x1e8(%rsp), %rdi
callq 0x66b4
leaq 0x210(%rsp), %rdi
callq 0x66d2
leaq 0x2e8(%rsp), %rdi
callq 0x678a
leaq 0x2b8(%rsp), %rdi
callq 0x6802
movq %r14, %rdi
callq 0x53e0