// File: /workspace/github_cpp_makefile_star2_O1/onfe[P]Completionist/src/suggest.cpp
// Function: suggest(PhraseList&, SegmentTree&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, unsigned int)
pvc_t suggest (
  PhraseList& pl,
  SegmentTree& st,
  std::string& prefix,
  uint32_t numResults = 16
) {
  // Get the pointers [first, last] for the candidate phrases.
  pvcip_t candidates = pl.query(prefix);

  // transform the pointers to indexes.
  uint32_t first = candidates.first - pl.pl.begin();
  uint32_t last = candidates.second - pl.pl.begin();

  // if there are no results, return an empty phrase vector.
  if (first == last) { return pvc_t(); }

  // the indexes are from first match to next non-match
  // decrement the last index to instead use first - last match.
  --last;
  pvc_t results;

  // The queue automatically orders elements so that .top() returns the largest element.
  // in this case, the largest element is the phrase range with the greatest weight.
  std::priority_queue<PhraseRange> queue;
  uip_t best = st.getMax(first, last);
  queue.emplace(first, last, best.first, best.second);

  while (results.size() < numResults && !queue.empty()) {
    PhraseRange curr = queue.top();
    queue.pop();

    // The best result is added to the result
    results.push_back(pl.pl[curr.index]);

    uint32_t lo = curr.first;
    uint32_t hi = curr.index - 1;

    // Check for underflows in indexing.
    if (curr.index -1 < curr.index && lo <= hi) {
      // index is ok, so add to queue.
      best = st.getMax(lo, hi);
      queue.emplace(lo, hi, best.first, best.second);
    }

    lo = curr.index + 1;
    hi = curr.last;

    // Check for overflows in indexing.
    if (curr.index + 1 > curr.index && lo <= hi) {
      // index is ok, so add to queue.
      best = st.getMax(lo, hi);
      queue.emplace(lo, hi, best.first, best.second);
    }
  }

  return results;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, %ebp
movq %rdx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rcx), %rsi
movq 0x8(%rcx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x2ed2
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3da0
movq %rax, %r13
movq %rdx, %r12
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x338d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2150
movq 0x28(%rsp), %rax
movq (%rax), %rax
subq %rax, %r13
shrq $0x3, %r13
imull $0xcccccccd, %r13d, %esi # imm = 0xCCCCCCCD
movl %esi, 0x24(%rsp)
subq %rax, %r12
shrq $0x3, %r12
imull $0xcccccccd, %r12d, %edx # imm = 0xCCCCCCCD
movl %edx, 0x14(%rsp)
cmpl %edx, %esi
jne 0x33d0
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
jmp 0x3535
decl %edx
movl %edx, 0x14(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq %rax, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
callq 0x51dc
leaq 0x4(%rsp), %r8
movq %rax, -0x4(%r8)
leaq 0x30(%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rsp, %rcx
callq 0x3596
movl %ebp, %r14d
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %r14, %rax
jae 0x351e
leaq 0x30(%rsp), %r13
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x351e
movl (%rax), %r12d
movl 0x4(%rax), %ebp
movl 0xc(%rax), %r15d
movq %r13, %rdi
callq 0x35ce
leaq (%r15,%r15,4), %rsi
shlq $0x3, %rsi
movq 0x28(%rsp), %rax
addq (%rax), %rsi
movq %rbx, %rdi
callq 0x360e
movl %r12d, 0x10(%rsp)
movl %r15d, %edx
subl $0x1, %edx
setb %al
movl %edx, 0xc(%rsp)
cmpl %edx, %r12d
seta %cl
orb %al, %cl
jne 0x34c1
movq 0x18(%rsp), %rdi
movl %r12d, %esi
callq 0x51dc
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
callq 0x3596
leal 0x1(%r15), %esi
movl %esi, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
cmpl %ebp, %r15d
jae 0x34fc
movq 0x18(%rsp), %rdi
movl %ebp, %edx
callq 0x51dc
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
callq 0x3596
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %r14, %rax
jb 0x3440
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3535
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x356b
movq %rax, %r14
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x358d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x358d
jmp 0x356b
jmp 0x356b
jmp 0x356b
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3585
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq %rbx, %rdi
callq 0x2d50
movq %r14, %rdi
callq 0x2210
nop