// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
movl 0xa0(%rdi), %ecx
movl 0xbc(%rdi), %r14d
movl %ecx, %ebp
subl %r14d, %ebp
je 0x4feb
leaq 0x18(%rdi), %rax
movq %rax, 0x10(%rdi)
movq 0xa8(%rdi), %rsi
leal -0x1(%rcx), %eax
movq (%rsi,%rax,8), %r8
movl 0xc0(%rdi), %r10d
movl 0xc4(%rdi), %eax
movl 0xb8(%rdi), %r11d
shrl %r11d
cmpl %r11d, %eax
cmovbl %eax, %r11d
movl $0x0, 0xc(%rdi)
xorl %r9d, %r9d
leal 0x1(%r14), %ebx
movl %ebx, 0xbc(%rdi)
movl %r11d, %eax
subl %r10d, %eax
jbe 0x4f96
movl %r14d, %edx
movq (%rsi,%rdx,8), %r14
incl %ebp
xorl %edx, %edx
divl %ebp
addl %r10d, %eax
movq 0xb0(%rdi), %r15
movl %r10d, %ebp
movl %r11d, %edx
leal (%rax,%rax), %r12d
movq (%r15,%r12,8), %r13
cmpq %r14, %r13
jae 0x4f7b
incl %eax
movl %eax, %ebp
jmp 0x4f86
jbe 0x4fb4
cmpq %r8, %r13
cmoval %eax, %edx
movl %eax, %r11d
movl %r11d, %eax
subl %ebp, %eax
shrl %eax
addl %ebp, %eax
cmpl %ebp, %r11d
ja 0x4f68
jmp 0x4f99
movl %r11d, %edx
movl %ecx, 0xbc(%rdi)
movl %ecx, %r14d
movl %edx, %r11d
movl %ecx, %ebp
subl %r14d, %ebp
je 0x4fd6
cmpl $0x10, %r9d
jb 0x4f39
jmp 0x4fd6
movl %r12d, %r10d
movq 0x8(%r15,%r10,8), %r10
movl %r9d, %r11d
incl %r9d
movl %r9d, 0xc(%rdi)
movq %r10, 0x18(%rdi,%r11,8)
movl %ebx, %r14d
movl %edx, %r11d
movl %eax, %r10d
jmp 0x4fa5
movl %r10d, 0xc0(%rdi)
movl %r11d, 0xc4(%rdi)
movl $0x1, %eax
jmp 0x4fed
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq