// File: /workspace/github_cpp_makefile_star2_O3/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 0x2edc
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3d64
movq %rax, %r13
movq %rdx, %r12
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x3365
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
cmpl %edx, %esi
jne 0x33a4
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
jmp 0x3511
decl %edx
movl %edx, 0x20(%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 0x5170
leaq 0xc(%rsp), %r8
movq %rax, -0x4(%r8)
leaq 0x30(%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x3572
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 0x34fa
leaq 0x30(%rsp), %r13
movq 0x30(%rsp), %rdi
cmpq 0x38(%rsp), %rdi
je 0x34ff
movl (%rdi), %r12d
movl 0x4(%rdi), %ebp
movl 0xc(%rdi), %r15d
movq %r13, %rdi
callq 0x35aa
leaq (%r15,%r15,4), %rsi
shlq $0x3, %rsi
movq 0x28(%rsp), %rax
addq (%rax), %rsi
movq %rbx, %rdi
callq 0x35f0
movl %r12d, 0x14(%rsp)
movl %r15d, %edx
subl $0x1, %edx
setb %al
movl %edx, 0x10(%rsp)
cmpl %edx, %r12d
seta %cl
orb %al, %cl
jne 0x349a
movq 0x18(%rsp), %rdi
movl %r12d, %esi
callq 0x5170
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x3572
leal 0x1(%r15), %esi
movl %esi, 0x14(%rsp)
movl %ebp, 0x10(%rsp)
cmpl %ebp, %r15d
jae 0x34d8
movq 0x18(%rsp), %rdi
movl %ebp, %edx
callq 0x5170
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x3572
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %r14, %rax
jb 0x3416
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3511
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 0x3547
movq %rax, %r14
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x3569
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x3569
jmp 0x3547
jmp 0x3547
jmp 0x3547
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3561
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq %rbx, %rdi
callq 0x2d56
movq %r14, %rdi
callq 0x2210
nop