// File: /workspace/github_c_makefile_star2_O1/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, 0xc0(%rsp)
movq %rsi, %rbx
movq %rdi, 0x50(%rsp)
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 0x1544c
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0xc, %rcx
cmpq %rcx, %rax
je 0x13833
movl 0x8(%rbx), %ecx
movl %ecx, 0x8(%rax)
movq (%rbx), %rcx
movq %rcx, (%rax)
addq $0xc, 0x110(%rsp)
movq 0x50(%rsp), %r14
jmp 0x13848
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x155e8
movq 0x50(%rsp), %r14
movl (%rbx), %ebp
leaq 0x58(%r14), %rdi
movq %rdi, 0xa0(%rsp)
movq %rbx, %rsi
callq 0x15290
movl %ebp, (%rax)
leaq 0xd0(%r14), %rax
movq %rax, 0xc8(%rsp)
leaq 0x60(%r14), %rax
movq %rax, 0xb0(%rsp)
leaq 0x88(%r14), %rax
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %r12
leaq 0x3a9e(%rip), %r13      # 0x17331
movq %r14, %rbp
movq 0xf0(%rsp), %rax
movq 0x110(%rsp), %rdx
movq %rax, 0xa8(%rsp)
cmpq %rax, %rdx
je 0x13f04
movq 0x118(%rsp), %rdi
cmpq %rdi, %rdx
movq %rdx, 0xb8(%rsp)
je 0x138e2
leaq -0xc(%rdx), %rax
movl -0x4(%rdx), %ecx
movl %ecx, 0x48(%rsp)
movq -0xc(%rdx), %rcx
movq %rcx, 0x40(%rsp)
jmp 0x13945
movq 0x128(%rsp), %rax
movq -0x8(%rax), %rax
movq 0x1ec(%rax), %rcx
movq %rcx, 0x40(%rsp)
movl 0x1f4(%rax), %eax
movl %eax, 0x48(%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 0x40(%rsp), %edx
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x13ff0
movq 0x58(%rsp), %rax
movq %rax, 0xd0(%rsp)
testq %rax, %rax
je 0x13ecf
movl 0x40(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
testq %rdi, %rdi
je 0x13999
callq 0x6752
movq 0x48(%rbp), %rax
testq %rax, %rax
je 0x139c9
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x139d5
movq 0xd609(%rip), %rcx      # 0x20fc8
cmpb $0x0, (%rcx)
je 0x139d1
incl 0x8(%rax)
jmp 0x139d5
xorl %r15d, %r15d
jmp 0x13b34
lock
incl 0x8(%rax)
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x98fa
movq %rax, 0xd8(%rsp)
movb $0x1, %bpl
xorl %r14d, %r14d
movq 0x20(%rsp), %r15
movq %rbx, %rdi
callq 0x999c
movb %al, 0x70(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x152f4
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
addq $0x8, %rax
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x7f60
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x13add
cmpq $0x0, 0x40(%rax)
je 0x13add
cmpq $0x0, (%rsp)
jne 0x13ab5
movl $0x48, %edi
callq 0x5220
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0xd0cc(%rip), %rcx      # 0x20b30
movq %rcx, (%rax)
leaq 0x10(%rax), %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
xorl %esi, %esi
movq %rsi, 0x40(%rax)
movq %rax, %rdx
addq $0x20, %rdx
movq %rdx, 0x10(%rax)
movq %rsi, 0x18(%rax)
movb $0x0, 0x20(%rax)
movq %rcx, (%rsp)
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rsp)
testq %rdi, %rdi
je 0x13aab
callq 0x6752
movq (%rsp), %rax
movw $0x5, 0x20(%rax)
movq 0x20(%rsp), %rsi
addq $0x38, %rsi
movq (%rsp), %rdi
callq 0x5170
movsbl %bpl, %eax
movq (%rsp), %rcx
movw %ax, 0x22(%rcx)
movq 0x20(%rsp), %rax
movzbl 0x30(%rax), %r14d
cmpq $0x0, 0x20(%rsp)
je 0x13af8
movq %rbx, %rdi
callq 0x98ea
incb %bpl
testb %al, %al
je 0x139f0
movq (%rsp), %rax
testq %rax, %rax
movq 0xd8(%rsp), %rsi
je 0x13b14
movzwl 0x22(%rax), %eax
addq %rax, %rsi
addl %eax, 0x40(%rsp)
movq %rbx, %rdi
callq 0x9aca
movq 0x50(%rsp), %rbp
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x13b30
callq 0x6752
movzwl %r14w, %r15d
testl %r15d, %r15d
je 0x13ca4
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13ca4
leaq 0x432e(%rip), %rsi      # 0x17e7f
callq 0x5430
xorl %ebp, %ebp
movq 0x58(%rsp), %rbx
testl %ebp, %ebp
je 0x13b71
movq (%rsp), %rdi
leaq 0x3a5d(%rip), %rsi      # 0x175c9
callq 0x5430
movq %rbx, %rdi
callq 0x999c
movl %eax, %r14d
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x2, %esi
movq %r12, %rdi
movl $0x30, %edx
callq 0x5270
movzbl %r14b, %eax
movl $0x4, %ecx
xorl %edx, %edx
movl %eax, %esi
shrl %cl, %esi
andl $0xf, %esi
movb (%rsi,%r13), %sil
movq 0x70(%rsp), %rdi
movb %sil, (%rdi,%rdx)
incq %rdx
addl $-0x4, %ecx
cmpq $0x2, %rdx
jne 0x13ba6
movl $0x1, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x404b(%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 0x13c07
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x13c0d
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 0x13c54
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x13c76
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5230
incl %ebp
cmpl %r15d, %ebp
jne 0x13b5d
movq (%rsp), %rdi
leaq 0x41f6(%rip), %rsi      # 0x17e82
callq 0x5430
movq 0x50(%rsp), %rbp
movq (%rsp), %rax
addw %r15w, 0x22(%rax)
addl %r15d, 0x40(%rsp)
cmpq $0x0, (%rsp)
jne 0x13d54
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x90(%rsp)
movq %rax, 0x98(%rsp)
testq %rax, %rax
je 0x13ce3
movq 0xd2f3(%rip), %rcx      # 0x20fc8
cmpb $0x0, (%rcx)
je 0x13cdf
incl 0x8(%rax)
jmp 0x13ce3
lock
incl 0x8(%rax)
leaq 0x40(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xc0(%rsp), %rsi
cmpq $0x0, 0x10(%rsi)
je 0x13f30
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movq %r12, %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 0x13d33
callq 0x6752
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x13d42
callq 0x6752
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x13d54
callq 0x6752
movq 0xc8(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x15358
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x7f60
movq (%rsp), %rax
movzwl 0x20(%rax), %ecx
cmpl $0x4, %ecx
ja 0x13e5d
movl $0x16, %edx
btl %ecx, %edx
jae 0x13e5d
cmpl $0x3, 0x24(%rax)
jne 0x13e5d
movl 0x28(%rax), %eax
movq 0x30(%rbp), %rcx
movq 0x38(%rbp), %rdx
cmpq %rdx, %rcx
setne %sil
je 0x13dcf
movl 0x60(%rcx), %edi
cmpl %eax, %edi
ja 0x13dc9
addl 0x8(%rcx), %edi
cmpl %eax, %edi
ja 0x13dcf
addq $0x68, %rcx
jmp 0x13db2
testb $0x1, %sil
je 0x13e5d
movq 0x68(%rbp), %rdx
movq 0xb0(%rsp), %rdi
movq %rdi, %rcx
testq %rdx, %rdx
je 0x13e07
movq %rdi, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x13df0
movq %rdi, %rdx
cmpq %rdi, %rcx
je 0x13e19
cmpl 0x20(%rcx), %eax
cmovbq %rdi, %rcx
movq %rcx, %rdx
cmpq %rdi, %rdx
jne 0x13e5d
movl %eax, 0x20(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x24(%rsp)
movw $0x0, 0x28(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x15cc4
movq (%rsp), %rsi
movl 0x28(%rsi), %ebx
addq $0x28, %rsi
movq 0xa0(%rsp), %rdi
callq 0x15290
movl %ebx, (%rax)
movq (%rsp), %rax
movl 0x20(%rax), %eax
addl $-0x2, %eax
cmpw $0x2, %ax
ja 0x13e82
movl 0x1c(%rsp), %ebx
movq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x15290
movl %ebx, (%rax)
movq (%rsp), %rax
cmpw $0xff, 0x20(%rax)
jne 0x13ea3
movl 0x1c(%rsp), %ebx
movq 0x68(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x15290
movl %ebx, (%rax)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13eb2
callq 0x6752
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jb 0x13ede
cmpl $0xff, %eax
jne 0x13954
jmp 0x13ede
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13ede
callq 0x6752
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x13eed
callq 0x6752
cmpq $0x0, 0xd0(%rsp)
movq 0xb8(%rsp), %rdx
jne 0x13896
cmpq 0xa8(%rsp), %rdx
sete %bl
leaq 0xe0(%rsp), %rdi
callq 0x153fc
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 0x13fda
jmp 0x13fb9
jmp 0x13f41
movq %rax, %rbx
movq 0x98(%rsp), %rdi
jmp 0x13fad
movq %rax, %rbx
jmp 0x13fcb
jmp 0x13fa5
jmp 0x13fa5
jmp 0x13fb9
jmp 0x13fb9
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x13f7f
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x13f7f
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x13fbc
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5230
jmp 0x13fbc
jmp 0x13fa5
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x13fbc
callq 0x6752
jmp 0x13fbc
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x13fcb
callq 0x6752
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x13fda
callq 0x6752
leaq 0xe0(%rsp), %rdi
callq 0x153fc
movq %rbx, %rdi
callq 0x53e0
nop