// File: /workspace/github_c_makefile_star2_O3/mrkite[P]regs/src/scanner.cc
// Function: Scanner::trace(Entry const&, std::function<std::shared_ptr<Inst> (std::shared_ptr<TheHandle>, Entry*)>)
bool Scanner::trace(const Entry &start,
                    std::function<std::shared_ptr<Inst>(Handle, Entry*)>
                    decode) {
  std::stack<Entry> workList;

  workList.push(start);
  labels[start.org] = start.org;
  while (!workList.empty()) {
    auto state = workList.top();
    workList.pop();
    std::shared_ptr<Inst> inst = nullptr;
    do {
      auto ptr = getAddress(state.org);
      if (ptr == nullptr) {
        return false;
      }
      auto addr = state.org;
      inst = nullptr;
      int16_t numDB = 0;
      if (fingerprints) {
        auto node = fingerprints->root;
        int8_t len = 0;
        auto fstart = ptr->tell();
        do {
          node = node->map[ptr->r8()];
          len++;
          if (node != nullptr && !node->name.empty()) {
            if (inst == nullptr) {
              inst = std::make_shared<Inst>();
              inst->type = Special;
            }
            inst->name = node->name;
            inst->length = len;
            numDB = node->numDB;
          }
        } while (node != nullptr && !ptr->eof());
        if (inst) {
          fstart += inst->length;
          state.org += inst->length;
        }
        ptr->seek(fstart);
      }
      if (numDB > 0 && inst) {
        inst->name += " {";
        for (int i = 0; i < numDB; i++) {
          if (i) {
            inst->name += ", ";
          }
          inst->name += "$" + hex(ptr->r8(), 2);
        }
        inst->name += "}";
        inst->length += numDB;
        state.org += numDB;
      }
      if (!inst) {
        inst = decode(ptr, &state);
      }
      map[addr] = inst;
      if (inst->type == Jump || inst->type == Branch || inst->type == Call) {
        if (inst->operType == Opr::Abs) {
          if (valid(inst->oper) && labels.find(inst->oper) == labels.end()) {
            workList.push({inst->oper, state.flags});
            labels[inst->oper] = inst->oper;
          }
        }
      }
      if (inst->type == Jump || inst->type == Branch || inst->type == Return) {
        // we want branches included in the block
        branches[state.org] = addr;
      }
      if (inst->type == Invalid) {
        branches[addr] = addr;
      }
    } while (inst->type != Return && inst->type != Jump &&
             inst->type != Invalid);
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, 0xd0(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %r14
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x15214
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0xc, %rcx
cmpq %rcx, %rax
je 0x135c0
movl 0x8(%rbx), %ecx
movl %ecx, 0x8(%rax)
movq (%rbx), %rcx
movq %rcx, (%rax)
addq $0xc, 0x110(%rsp)
jmp 0x135d0
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x153b0
movl (%rbx), %ebp
leaq 0x58(%r12), %rdi
movq %rdi, 0xb8(%rsp)
movq %rbx, %rsi
callq 0x15044
movl %ebp, (%rax)
leaq 0xd0(%r12), %rax
movq %rax, 0xd8(%rsp)
leaq 0x60(%r12), %rax
movq %rax, 0x90(%rsp)
leaq 0x88(%r12), %rax
movq %rax, 0xa0(%rsp)
leaq 0x3d14(%rip), %rbp      # 0x17331
movq %r12, 0x98(%rsp)
movq 0xf0(%rsp), %rax
movq 0x110(%rsp), %rcx
movq %rax, 0xc0(%rsp)
cmpq %rax, %rcx
je 0x13c95
movq 0x118(%rsp), %rdi
cmpq %rdi, %rcx
movq %rcx, 0xc8(%rsp)
je 0x13674
leaq -0xc(%rcx), %rax
movq %rcx, %rdx
movl -0x4(%rcx), %ecx
movl %ecx, 0x50(%rsp)
movq -0xc(%rdx), %rcx
movq %rcx, 0x48(%rsp)
jmp 0x136d7
movq 0x128(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1ec(%rax), %rcx
movq %rcx, 0x48(%rsp)
movl 0x1f4(%rax), %eax
movl %eax, 0x50(%rsp)
movl $0x1f8, %esi            # imm = 0x1F8
callq 0x5230
movq 0x128(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x128(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x118(%rsp)
leaq 0x1f8(%rax), %rcx
movq %rcx, 0x120(%rsp)
movl $0x1ec, %ecx            # imm = 0x1EC
addq %rcx, %rax
movq %rax, 0x110(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0x48(%rsp), %edx
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x13d80
movq 0x58(%rsp), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0x13c63
movl 0x48(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
testq %rdi, %rdi
je 0x13728
callq 0x671a
movq 0x48(%r12), %rax
testq %rax, %rax
je 0x13763
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rbx
testq %rax, %rax
je 0x13775
movq 0xd874(%rip), %rcx      # 0x20fc8
cmpb $0x0, (%rcx)
je 0x1376c
incl 0x8(%rax)
movq 0x68(%rsp), %rbx
jmp 0x13775
movq (%rsp), %rdi
jmp 0x13a3b
lock
incl 0x8(%rax)
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x96de
movq %rax, %r15
movb $0x1, %r12b
xorl %r13d, %r13d
movq 0x20(%rsp), %r14
movq %rbx, %rdi
callq 0x9780
movb %al, 0x70(%rsp)
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
callq 0x150aa
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
addq $0x8, %rax
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x7dd0
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x13881
cmpq $0x0, 0x40(%rsi)
je 0x1386e
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x1384f
movl $0x48, %edi
callq 0x5220
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0xd332(%rip), %rcx      # 0x20b30
movq %rcx, (%rax)
leaq 0x10(%rax), %rdi
movq %rax, %rcx
addq $0x20, %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
xorl %edx, %edx
movq %rdx, 0x40(%rax)
movq %rcx, 0x10(%rax)
movq %rdx, 0x18(%rax)
movq %rdi, (%rsp)
movq 0x8(%rsp), %rcx
movq %rax, 0x8(%rsp)
testq %rcx, %rcx
je 0x13844
movq %rcx, %rdi
callq 0x671a
movq (%rsp), %rdi
movw $0x5, 0x20(%rdi)
movq 0x20(%rsp), %rsi
addq $0x38, %rsi
callq 0x5170
movsbl %r12b, %eax
movq (%rsp), %rcx
movw %ax, 0x22(%rcx)
movq 0x20(%rsp), %rax
movzbl 0x30(%rax), %r13d
movq %rbx, %rdi
callq 0x96ce
incb %r12b
testb %al, %al
je 0x13786
movq (%rsp), %rax
testq %rax, %rax
je 0x13895
movzwl 0x22(%rax), %eax
addq %rax, %r15
addl %eax, 0x48(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x98ae
movq 0x98(%rsp), %r12
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x138b7
callq 0x671a
movq (%rsp), %rdi
testw %r13w, %r13w
je 0x13a3b
testq %rdi, %rdi
je 0x13a44
leaq 0x45aa(%rip), %rsi      # 0x17e7f
callq 0x5430
movzwl %r13w, %r14d
xorl %r12d, %r12d
movq 0x58(%rsp), %rbx
testl %r12d, %r12d
je 0x138fb
movq (%rsp), %rdi
leaq 0x3cd3(%rip), %rsi      # 0x175c9
callq 0x5430
movq %rbx, %rdi
callq 0x9780
movl %eax, %r15d
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x2, %esi
leaq 0x70(%rsp), %rdi
movl $0x30, %edx
callq 0x5270
movzbl %r15b, %eax
movl $0x4, %ecx
xorl %edx, %edx
movl %eax, %esi
shrl %cl, %esi
andl $0xf, %esi
movb (%rsi,%rbp), %sil
movq 0x70(%rsp), %rdi
movb %sil, (%rdi,%rdx)
incq %rdx
addl $-0x4, %ecx
cmpq $0x2, %rdx
jne 0x13932
movl $0x1, %r8d
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x42bd(%rip), %rcx      # 0x17c25
callq 0x5250
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x13995
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x1399b
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq (%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x5100
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x139e2
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x13a04
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5230
incl %r12d
cmpl %r14d, %r12d
jne 0x138e6
movq (%rsp), %rdi
leaq 0x4467(%rip), %rsi      # 0x17e82
callq 0x5430
movq 0x98(%rsp), %r12
movq (%rsp), %rax
addw %r13w, 0x22(%rax)
addl %r14d, 0x48(%rsp)
jmp 0x13aeb
testq %rdi, %rdi
jne 0x13aeb
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
testq %rax, %rax
movq 0xd0(%rsp), %rsi
je 0x13a80
movq 0xd556(%rip), %rcx      # 0x20fc8
cmpb $0x0, (%rcx)
je 0x13a7c
incl 0x8(%rax)
jmp 0x13a80
lock
incl 0x8(%rax)
leaq 0x48(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x10(%rsi)
je 0x13cc1
leaq 0x20(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq *0x18(%rsi)
movaps 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movaps %xmm0, (%rsp)
testq %rdi, %rdi
je 0x13ad9
callq 0x671a
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x13ad9
callq 0x671a
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x13aeb
callq 0x671a
movq 0xd8(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x15110
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x7dd0
movq (%rsp), %rcx
movzwl 0x20(%rcx), %eax
cmpl $0x4, %eax
ja 0x13bec
movl $0x16, %edx
btl %eax, %edx
jae 0x13bec
cmpl $0x3, 0x24(%rcx)
jne 0x13bec
movq 0x30(%r12), %rdx
movq 0x38(%r12), %rsi
cmpq %rsi, %rdx
je 0x13bec
movl 0x28(%rcx), %ecx
movl 0x60(%rdx), %edi
cmpl %ecx, %edi
ja 0x13b62
addl 0x8(%rdx), %edi
cmpl %ecx, %edi
ja 0x13b6d
addq $0x68, %rdx
cmpq %rsi, %rdx
jne 0x13b54
jmp 0x13bec
movq 0x68(%r12), %rdx
testq %rdx, %rdx
je 0x13ba5
movq 0x90(%rsp), %rsi
xorl %edi, %edi
cmpl %ecx, 0x20(%rdx)
setb %dil
cmovaeq %rdx, %rsi
movq 0x10(%rdx,%rdi,8), %rdx
testq %rdx, %rdx
jne 0x13b7f
cmpq 0x90(%rsp), %rsi
je 0x13ba5
cmpl 0x20(%rsi), %ecx
jae 0x13bec
movl %ecx, 0x20(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x24(%rsp)
movw $0x0, 0x28(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x15a88
movq (%rsp), %rsi
movl 0x28(%rsi), %ebx
addq $0x28, %rsi
movq 0xb8(%rsp), %rdi
callq 0x15044
movl %ebx, (%rax)
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
leal -0x2(%rax), %ecx
cmpw $0x2, %cx
ja 0x13c15
movl 0x1c(%rsp), %ebx
movq 0xa0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x15044
movl %ebx, (%rax)
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
movzwl %ax, %eax
cmpl $0xff, %eax
jne 0x13c37
movl 0x1c(%rsp), %ebx
movq 0xa0(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x15044
movl %ebx, (%rax)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13c46
callq 0x671a
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jb 0x13c72
cmpl $0xff, %eax
jne 0x136e6
jmp 0x13c72
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13c72
callq 0x671a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x13c81
callq 0x671a
cmpq $0x0, 0x68(%rsp)
movq 0xc8(%rsp), %rcx
jne 0x13625
cmpq 0xc0(%rsp), %rcx
sete %bl
leaq 0xe0(%rsp), %rdi
callq 0x151bc
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5130
movq %rax, %rbx
jmp 0x13d6b
jmp 0x13d34
jmp 0x13cd2
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
jmp 0x13d43
movq %rax, %rbx
jmp 0x13d5c
jmp 0x13d3b
jmp 0x13d3b
jmp 0x13d34
jmp 0x13d34
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x13d10
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x13d10
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x13d4d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x13d4d
movq %rax, %rbx
jmp 0x13d4d
jmp 0x13d3b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x13d4d
callq 0x671a
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13d5c
callq 0x671a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x13d6b
callq 0x671a
leaq 0xe0(%rsp), %rdi
callq 0x151bc
movq %rbx, %rdi
callq 0x53e0