// File: /workspace/github_c_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
movq %rdx, 0x90(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
callq 0x11e0c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1220a
movl (%rbx), %ebp
leaq 0x58(%r15), %rdi
movq %rdi, 0x60(%rsp)
movq %rbx, %rsi
callq 0x11e28
movl %ebp, (%rax)
leaq 0xd0(%r15), %rax
movq %rax, 0x98(%rsp)
leaq 0x60(%r15), %rax
movq %rax, 0x80(%rsp)
leaq 0x88(%r15), %rax
movq %rax, 0x70(%rsp)
leaq 0x40(%rsp), %r14
leaq 0xb8(%rsp), %r13
movq %r15, 0x68(%rsp)
movq 0xe8(%rsp), %rax
movq 0x108(%rsp), %rcx
movq %rax, 0x78(%rsp)
cmpq %rax, %rcx
je 0x11032
movq %rcx, %rax
movq %rcx, 0x88(%rsp)
cmpq 0x110(%rsp), %rcx
jne 0x10c79
movq 0x120(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x1f8, %ecx            # imm = 0x1F8
addq %rcx, %rax
movl -0x4(%rax), %ecx
movl %ecx, 0x28(%rsp)
movq -0xc(%rax), %rax
movq %rax, 0x20(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x12694
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0x20(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x110dc
movq 0x30(%rsp), %rax
testq %rax, %rax
movq %rax, 0xa0(%rsp)
je 0x11007
movl 0x20(%rsp), %eax
movl %eax, 0x1c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
callq 0x126ea
leaq 0x48(%rsp), %rdi
callq 0x61e4
movq 0x48(%r15), %rsi
testq %rsi, %rsi
je 0x10e79
movq %r14, %rdi
callq 0xecce
movq 0x30(%rsp), %rdi
callq 0x88c4
movq %rax, %rbx
movb $0x1, %r15b
xorl %ebp, %ebp
movq 0x40(%rsp), %r12
movq 0x30(%rsp), %rdi
callq 0x8966
movb %al, 0xb8(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x11e76
movq %r14, %rdi
movq %rax, %rsi
callq 0xece2
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0x10db8
cmpq $0x0, 0x40(%rsi)
je 0x10da3
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x10d85
movq %r13, %rdi
callq 0xc736
movq %rsp, %rdi
movq %r13, %rsi
callq 0x126ea
leaq 0xc0(%rsp), %rdi
callq 0x61e4
movq (%rsp), %rdi
movw $0x5, 0x20(%rdi)
movq 0x40(%rsp), %rsi
addq $0x38, %rsi
callq 0x5170
movsbl %r15b, %eax
movq (%rsp), %rcx
movw %ax, 0x22(%rcx)
movq 0x40(%rsp), %rax
movzbl 0x30(%rax), %ebp
movq 0x30(%rsp), %rdi
callq 0x88b4
incb %r15b
testb %al, %al
je 0x10d10
movq (%rsp), %rax
testq %rax, %rax
je 0x10dcc
movzwl 0x22(%rax), %eax
addq %rax, %rbx
addl %eax, 0x20(%rsp)
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x8a68
movq 0x68(%rsp), %r15
leaq 0x48(%rsp), %rdi
callq 0x61e4
movq (%rsp), %rdi
testw %bp, %bp
je 0x10e7d
testq %rdi, %rdi
je 0x10e82
leaq 0x407a(%rip), %rsi      # 0x14e7f
callq 0x5410
movzwl %bp, %ebx
xorl %r15d, %r15d
cmpl %r15d, %ebx
je 0x10ed3
testl %r15d, %r15d
je 0x10e2e
movq (%rsp), %rdi
leaq 0x37a0(%rip), %rsi      # 0x145c9
callq 0x5410
movq 0x30(%rsp), %rdi
callq 0x8966
movzbl %al, %edx
movq %r13, %rdi
pushq $0x2
popq %rcx
callq 0x11136
movq %r14, %rdi
leaq 0x3dd5(%rip), %rsi      # 0x14c25
movq %r13, %rdx
callq 0xc609
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x5440
movq %r14, %rdi
callq 0x5140
movq %r13, %rdi
callq 0x5140
incl %r15d
jmp 0x10e10
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x10ef4
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x67fc
movq %r14, %rdi
movq 0x90(%rsp), %rsi
movq %rbx, %rdx
leaq 0x20(%rsp), %rcx
callq 0x11ec4
movq %rsp, %rdi
movq %r14, %rsi
callq 0x126ea
leaq 0x48(%rsp), %rdi
callq 0x61e4
leaq 0xb0(%rsp), %rdi
callq 0x61e4
jmp 0x10ef4
movq (%rsp), %rdi
leaq 0x3fa4(%rip), %rsi      # 0x14e82
callq 0x5410
movq 0x68(%rsp), %r15
movq (%rsp), %rax
addw %bp, 0x22(%rax)
addl %ebx, 0x20(%rsp)
movq 0x98(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x11eee
movq %rax, %rdi
movq %rsp, %rsi
callq 0x11f9e
movq (%rsp), %rbx
movzwl 0x20(%rbx), %eax
cmpl $0x4, %eax
ja 0x10f9a
pushq $0x16
popq %rcx
btl %eax, %ecx
jae 0x10f9a
cmpl $0x3, 0x24(%rbx)
jne 0x10f9a
movl 0x28(%rbx), %esi
movq %r15, %rdi
callq 0x111ea
testb %al, %al
je 0x10f9a
addq $0x28, %rbx
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x12a2c
movq (%rsp), %rbx
cmpq 0x80(%rsp), %rax
jne 0x10f9a
movl 0x28(%rbx), %eax
movl %eax, 0x40(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
andw $0x0, 0x48(%rsp)
leaq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0x12a7e
movq (%rsp), %rsi
movl 0x28(%rsi), %ebx
addq $0x28, %rsi
movq 0x60(%rsp), %rdi
callq 0x11e28
movl %ebx, (%rax)
movq (%rsp), %rbx
movl 0x20(%rbx), %eax
movl %eax, %ecx
addl $-0x2, %ecx
cmpw $0x2, %cx
ja 0x10fc5
movl 0x1c(%rsp), %ebx
movq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x11e28
movl %ebx, (%rax)
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
cmpw $0xff, %ax
jne 0x10fe0
movl 0x1c(%rsp), %ebx
movq 0x70(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x11e28
movl %ebx, (%rax)
leaq 0x38(%rsp), %rdi
callq 0x61e4
movq (%rsp), %rax
movzwl 0x20(%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jb 0x11011
cmpl $0xff, %eax
jne 0x10c9d
jmp 0x11011
leaq 0x38(%rsp), %rdi
callq 0x61e4
leaq 0x8(%rsp), %rdi
callq 0x61e4
cmpq $0x0, 0xa0(%rsp)
movq 0x88(%rsp), %rcx
jne 0x10c32
cmpq 0x78(%rsp), %rcx
sete %bl
leaq 0xd8(%rsp), %rdi
callq 0x11fba
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x110c7
jmp 0x1109f
jmp 0x1109f
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x110ae
movq %rax, %rbx
jmp 0x110bd
jmp 0x110a6
jmp 0x110a6
jmp 0x1109f
jmp 0x1109f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5140
jmp 0x11090
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x5140
jmp 0x110b3
movq %rax, %rbx
jmp 0x110b3
jmp 0x110a6
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x61e4
leaq 0x38(%rsp), %rdi
callq 0x61e4
leaq 0x8(%rsp), %rdi
callq 0x61e4
leaq 0xd8(%rsp), %rdi
callq 0x11fba
movq %rbx, %rdi
callq 0x53c0