// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %r8d, %ebp
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %rcx, %rsi
callq 0x2180
movq %r14, %rdi
movq %r12, %rsi
callq 0x3940
movq %rax, %r13
movq %rdx, %r12
leaq 0x48(%rsp), %rdi
callq 0x20a0
movq (%r14), %rcx
subq %rcx, %r13
pushq $0x28
popq %rdi
movq %r13, %rax
cqto
idivq %rdi
movq %rax, %rsi
movl %esi, 0x1c(%rsp)
subq %rcx, %r12
movq %r12, %rax
cqto
idivq %rdi
cmpl %eax, %esi
jne 0x3086
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
jmp 0x31bd
movq %r14, 0x40(%rsp)
decl %eax
movl %eax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movl %eax, %edx
callq 0x4632
leaq 0x4(%rsp), %r8
movq %rax, -0x4(%r8)
leaq 0x20(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rsp, %rcx
callq 0x3204
movl %ebp, %r14d
leaq 0x20(%rsp), %r13
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %r14, %rax
jae 0x31b3
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0x31b3
movl (%rax), %ebp
movl 0x4(%rax), %r12d
movl 0xc(%rax), %r15d
movq %r13, %rdi
callq 0x321a
imulq $0x28, %r15, %rsi
movq 0x40(%rsp), %rax
addq (%rax), %rsi
movq %rbx, %rdi
callq 0x3232
movl %ebp, 0xc(%rsp)
movl %r15d, %edx
subl $0x1, %edx
setb %al
movl %edx, 0x8(%rsp)
cmpl %edx, %ebp
seta %cl
orb %al, %cl
jne 0x316d
movq 0x10(%rsp), %rdi
movl %ebp, %esi
callq 0x4632
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
callq 0x3204
leal 0x1(%r15), %esi
movl %esi, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
cmpl %r12d, %r15d
jae 0x30db
movq 0x10(%rsp), %rdi
movl %r12d, %edx
callq 0x4632
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
callq 0x3204
jmp 0x30db
leaq 0x20(%rsp), %rdi
callq 0x325e
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31e6
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x20a0
jmp 0x31fb
jmp 0x31e6
jmp 0x31e6
jmp 0x31e6
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x325e
movq %rbx, %rdi
callq 0x299e
movq %r14, %rdi
callq 0x2200
nop