// File: /workspace/github_c_makefile_star2_O1/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 %ecx, %ebx
subl 0xbc(%rdi), %ebx
je 0x554d
leaq 0x18(%rdi), %rax
movq %rax, 0x10(%rdi)
movq 0xa8(%rdi), %rsi
leal -0x1(%rcx), %eax
movq (%rsi,%rax,8), %r9
movl 0xc0(%rdi), %r8d
movl 0xc4(%rdi), %eax
movl 0xb8(%rdi), %r10d
shrl %r10d
cmpl %r10d, %eax
cmovbl %eax, %r10d
movl $0x0, 0xc(%rdi)
xorl %r11d, %r11d
cmpl $0xf, %r11d
ja 0x5538
movl 0xbc(%rdi), %r14d
leal 0x1(%r14), %ebp
incl %ebx
movl %r10d, %eax
subl %r8d, %eax
xorl %edx, %edx
divl %ebx
addl %r8d, %eax
movl %ebp, 0xbc(%rdi)
cmpl %r8d, %r10d
seta %bl
jbe 0x5500
movq (%rsi,%r14,8), %rdx
movq 0xb0(%rdi), %r14
movl %r8d, %r15d
movl %r10d, %ebp
leal (%rax,%rax), %r12d
movq (%r14,%r12,8), %r12
cmpq %rdx, %r12
jae 0x54d7
incl %eax
movl %eax, %r15d
jmp 0x54e2
jbe 0x54fa
cmpq %r9, %r12
cmoval %eax, %ebp
movl %eax, %r10d
movl %r10d, %eax
subl %r15d, %eax
shrl %eax
addl %r15d, %eax
cmpl %r15d, %r10d
seta %bl
ja 0x54c3
movl %ebp, %r10d
jmp 0x5500
movl %ebp, %r10d
movl %eax, %r8d
testb $0x1, %bl
je 0x5524
movq 0xb0(%rdi), %rdx
addl %eax, %eax
movq 0x8(%rdx,%rax,8), %rax
movl %r11d, %edx
incl %r11d
movl %r11d, 0xc(%rdi)
movq %rax, 0x18(%rdi,%rdx,8)
jmp 0x552a
movl %ecx, 0xbc(%rdi)
movl %ecx, %ebx
subl 0xbc(%rdi), %ebx
jne 0x5480
movl %r8d, 0xc0(%rdi)
movl %r10d, 0xc4(%rdi)
movl $0x1, %eax
jmp 0x554f
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq