// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/src/trie.c
// Function: JFT_iter_trans_next
JFT_Boolean JFT_iter_trans_next(JFT_Iter *iter) {
  // translate the next batch of offsets in the leaf
  // we are done once the position reaches the leaf size
  JFT_Leaf *leaf = &iter->sub.trans.leaf;
  JFT_Count left = leaf->size - iter->sub.trans.position;
  if (!left)
    return False;

  JFT_Word *words = iter->batch.data = iter->words;
  JFT_Word last = leaf->data[leaf->size - 1];
  JFT_Count lower = iter->sub.trans.lower;
  JFT_Count upper = MIN(iter->sub.trans.upper, iter->sub.trans.TLBSize / 2);
  for (iter->batch.size = 0; iter->batch.size < JFT_ITER_WORDS; ) {
    // try to make an informed initial guess
    // if that fails, fall back to bisection
    // NB: we could keep making 'informed' guesses, but it requires more work
    //    (because once we guess too high we no longer know how many are in range)
    JFT_Word word = leaf->data[iter->sub.trans.position++], at;
    JFT_Count lo = lower, hi = upper, mid = lo + (hi - lo) / (left + 1);
    while (lo < hi) {
      at = iter->sub.trans.TLB[2 * mid];
      if (at < word) {
        lo = mid + 1;
      } else if (at > word) {
        hi = mid;
        if (at > last)
          upper = mid;
      } else {
        lower = mid;
        break;
      }
      mid = lo + (hi - lo) / 2;
    }

    // we either found the word, or the bounds collapsed
    // if we found it, add the translated value
    // if the bounds collapsed, we won't be finding any more
    if (lo < hi)
      words[iter->batch.size++] = iter->sub.trans.TLB[2 * mid + 1];
    else
      iter->sub.trans.position = leaf->size;

    // once we are out of leaf words, we are done
    if (!(left = leaf->size - iter->sub.trans.position))
      break;
  }

  // remember the bounds for next time
  iter->sub.trans.lower = lower;
  iter->sub.trans.upper = upper;

  return True;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl 0xa0(%rdi), %ecx
movl 0xbc(%rdi), %edx
movl %ecx, %ebp
subl %edx, %ebp
je 0x4396
leaq 0x18(%rdi), %rax
movq %rax, 0x10(%rdi)
movq 0xa8(%rdi), %r8
leal -0x1(%rcx), %eax
movq (%r8,%rax,8), %r9
movl 0xc0(%rdi), %r11d
movl 0xc4(%rdi), %eax
movl 0xb8(%rdi), %esi
shrl %esi
cmpl %esi, %eax
cmovbl %eax, %esi
andl $0x0, 0xc(%rdi)
xorl %r10d, %r10d
cmpl $0xf, %r10d
ja 0x439a
leal 0x1(%rdx), %eax
movl %eax, 0xbc(%rdi)
movl %edx, %eax
movq (%r8,%rax,8), %rbx
movl %esi, %eax
subl %r11d, %eax
incl %ebp
xorl %edx, %edx
divl %ebp
addl %r11d, %eax
movl %r11d, %edx
movl %esi, %ebp
cmpl %ebp, %edx
jae 0x435d
movq 0xb0(%rdi), %r14
leal (%rax,%rax), %r15d
movq (%r14,%r15,8), %r12
cmpq %rbx, %r12
jae 0x4349
incl %eax
movl %eax, %edx
jmp 0x4353
jbe 0x436a
cmpq %r9, %r12
cmoval %eax, %esi
movl %eax, %ebp
movl %ebp, %eax
subl %edx, %eax
shrl %eax
addl %edx, %eax
jmp 0x432b
movl %ecx, 0xbc(%rdi)
movl %ecx, %edx
movl %r11d, %eax
jmp 0x4387
movl %r15d, %edx
movq 0x8(%r14,%rdx,8), %rdx
movl %r10d, %r11d
incl %r10d
movl %r10d, 0xc(%rdi)
movq %rdx, 0x18(%rdi,%r11,8)
movl 0xbc(%rdi), %edx
movl %ecx, %ebp
movl %eax, %r11d
subl %edx, %ebp
jne 0x42ff
jmp 0x439d
xorl %eax, %eax
jmp 0x43ae
movl %r11d, %eax
movl %eax, 0xc0(%rdi)
movl %esi, 0xc4(%rdi)
movl $0x1, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq