// File: /workspace/github_c_makefile_star2_O1/jflatow[P]jfdb/src/trie.c
// Function: JFT_iter_but_next
JFT_Boolean JFT_iter_but_next(JFT_Iter *iter) {
  // a - b - c - d - ...
  // if the first iter is exhausted, we are done
  if (ffsll(iter->sub.many.exists) != ffsll(iter->sub.many.active))
    return False;

  JFT_Word *words = iter->batch.data = iter->words;
  for (iter->batch.size = 0; iter->batch.size < JFT_ITER_WORDS; ) {
    // scour the active iters for the next min element, until we fill a batch
    JFT_Mask hasMin = 0;
    JFT_Word min = -1, elem;
    JFT_Mask active = iter->sub.many.active;
    for (int b = ffsll(active); b; b = ffsll(active &= active - 1)) {
      JFT_Amount k = b - 1;
      JFT_Iter *i = iter->sub.many.iters + k;
      // as long as the position is out of bounds on the iter, reload it
      // if the iter is exhausted, remove it from active and skip to next
      while (i->batch.position >= i->batch.size)
        if (!JFT_iter_next(i)) {
          iter->sub.many.active &= ~(1LLU << k);
          goto nextActive;
        }
      if ((elem = i->batch.data[i->batch.position]) == min) {
        hasMin |= (1LLU << k);
      } else if (elem < min) {
        min = elem;
        hasMin = (1LLU << k);
      }
    nextActive:
      /* done with the active iter, move on to next */;
    }
    // if we can't find another min, all iters are exhausted and we are done
    // add the min to the batch iff only the first has it (main difference b/w but & all)
    if (!hasMin)
      break;
    else if (hasMin == (1LLU << (ffsll(iter->sub.many.exists) - 1)))
      words[iter->batch.size++] = min;
    // advance any iters we found the min on
    for (int b = ffsll(hasMin); b; b = ffsll(hasMin &= hasMin - 1))
      iter->sub.many.iters[b - 1].batch.position++;
  }
  return True;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0xa8(%rdi), %rax
movq 0xb0(%rdi), %rcx
bsfq %rax, %rdx
xorl %esi, %esi
bsfq %rcx, %rcx
sete %sil
setne %dil
cmpq %rdx, %rcx
sete %cl
andb %dil, %cl
testq %rax, %rax
movzbl %cl, %eax
cmovnel %eax, %esi
movl $0x0, 0x4(%rsp)
cmpb $0x1, %sil
jne 0x5a1c
leaq 0x18(%rbx), %rax
movq %rax, 0x10(%rbx)
movl $0x0, 0xc(%rbx)
movq $-0x1, %rsi
cmpl $0xf, 0xc(%rbx)
ja 0x5a14
movl $0x1, 0x4(%rsp)
movq 0xb0(%rbx), %r12
testq %r12, %r12
je 0x59a1
bsfq %r12, %r14
incq %r14
movq $-0x1, %rbp
xorl %r13d, %r13d
decl %r14d
imulq $0xc8, %r14, %r15
addq 0xa0(%rbx), %r15
movl 0x8(%r15), %eax
cmpl 0xc(%r15), %eax
jb 0x5953
movl $0x0, 0x8(%r15)
movq %r15, %rdi
callq *(%r15)
testl %eax, %eax
jne 0x591a
movq $-0x2, %rax
movl %r14d, %ecx
rolq %cl, %rax
andq %rax, 0xb0(%rbx)
movq $-0x1, %rsi
jmp 0x5983
movq 0x10(%r15), %rcx
movq (%rcx,%rax,8), %rax
cmpq %rbp, %rax
movq $-0x1, %rsi
jne 0x596d
btsq %r14, %r13
jmp 0x5983
movl $0x1, %edx
movl %r14d, %ecx
shlq %cl, %rdx
cmpq %rbp, %rax
cmovbq %rax, %rbp
cmovbq %rdx, %r13
leaq -0x1(%r12), %rax
andq %rax, %r12
bsfq %r12, %r14
cmoveq %rsi, %r14
incq %r14
testq %r12, %r12
jne 0x5909
jmp 0x59ab
movq $-0x1, %rbp
xorl %r13d, %r13d
testq %r13, %r13
je 0x5a1c
movq 0xa8(%rbx), %rax
movq %rax, %rcx
negq %rcx
andq %rax, %rcx
cmpq %rcx, %r13
jne 0x59d3
movl 0xc(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0xc(%rbx)
movq %rbp, 0x18(%rbx,%rax,8)
bsfq %r13, %rcx
incq %rcx
movq %r13, %rax
movq 0xa0(%rbx), %rdx
imulq $0xc8, %rcx, %rcx
incl -0xc0(%rdx,%rcx)
leaq -0x1(%rax), %rcx
andq %rcx, %rax
bsfq %rax, %rcx
cmoveq %rsi, %rcx
incq %rcx
testq %rax, %rax
jne 0x59dd
testq %r13, %r13
jne 0x58d6
jmp 0x5a1c
movl $0x1, 0x4(%rsp)
movl 0x4(%rsp), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq