// File: /workspace/github_c_makefile_star2_O1/jflatow[P]jfdb/src/trie.c
// Function: JFT_cursor_merge
JFT_Status JFT_cursor_merge(JFT_Cursor *cursors,
                            JFT_Amount num,
                            JFT_SpliceFun splice,
                            void *restrict acc,
                            int flags) {
  // cursors are assumed to be at the same initial location (i.e. root)
  // we currently support merging up to 64 cursors at a time
  // JFDB cannot have more than 64 levels, so internally we don't need more
  // more than that and caller can merge in batches
  if (num < 1 || num > JFT_MASK_CAPACITY)
    return ENumCursors;

  // determine the max key size and ensure it is within bounds
  JFT_KeySize maxKeySize = max_key_size(cursors, num);
  if (maxKeySize > JFT_KEY_LIMIT)
    return EKeySize;

  // allocate all the stack space we will need up front
  JFT_MergeFrame *stack = calloc(maxKeySize, sizeof(JFT_MergeFrame)), *frame = stack;
  if (stack == NULL)
    return ENoMem;

  // initialize stack, etc.
  JFT_Status result = Ok;
  JFT_Direction direction = (flags & JFT_FLAGS_REVERSE) ? Reverse : Forward;
  *frame = (JFT_MergeFrame) {
    .bound = JFT_SYMBOL_ANY,
    .active = JFT_MASK_ACTIVE(num),
    .done = 0
  };

  JFT_MergeContext ctx = (JFT_MergeContext) {
    .stack = stack,
    .frame = frame,
    .maxKeySize = maxKeySize,
    .numCursors = num,
    .acc = acc,
    .flags = flags
  };

  // walk all the cursors in parallel, keeping them in sync
  // the point is we visit each path in the merged-trie exactly once
  while (1) {
    // are there any active cursors with discrete symbols?
    while (frame->active && frame->bound != JFT_SYMBOL_NIL) {
      // merge the active set
      switch (result = splice(cursors, &ctx, In)) {
        case Step:
          // push a new frame, get subsequent symbol from all active cursors
          // copy the currently active cursors and offset into the new frame
          (frame + 1)->active = frame->active;
          (frame + 1)->offset = frame->offset;
          frame = ctx.frame = move_into_frame(cursors,
                                              JFT_cursor_step,
                                              frame->active,
                                              frame + 1,
                                              direction);
          break;
        case Next:
          // shift the frame by recomputing active cursors
          // i.e. from those that stepped into this frame that are not done
          // if at the root, there can be no siblings, so we are done
          // reset the offset to that of the parent (in case prev changed it)
          if (frame == stack)
            goto out;
          frame->offset = (frame - 1)->offset;
          frame = ctx.frame = move_into_frame(cursors,
                                              JFT_cursor_next,
                                              (frame - 1)->active & ~frame->done,
                                              frame,
                                              direction);
          break;
        default:
          // early exit, pass the result through
          goto done;
      }
    }

    // we hit a wall, start backing out: pop the frame, call second splice
    // its not possible we are at the root, we would have exited already
    frame = ctx.frame = back_into_frame(cursors, frame - 1, direction);
  out:
    result = splice(cursors, &ctx, Out);

    // we could be at the root now, if so pass the result through
    // otherwise, move on to the next sibling
    if (frame == stack)
      goto done;
    frame->offset = (frame - 1)->offset;
    frame = ctx.frame = move_into_frame(cursors,
                                        JFT_cursor_next,
                                        (frame - 1)->active & ~frame->done,
                                        frame,
                                        direction);
  }

 done:
  free(stack);
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x10(%rsp)
leaq -0x41(%rsi), %rax
movl $0x4, %r12d
cmpq $-0x40, %rax
jb 0x4470
movl %r8d, %ebp
movq %rcx, %r13
movq %rsi, %r15
movq %rdi, %r14
testq %rsi, %rsi
je 0x4293
movq %r13, (%rsp)
leaq 0x8(%r14), %r12
xorl %ebx, %ebx
movq %r15, %r13
movq (%r12), %rdi
callq 0x5aba
movl 0x2c(%rax), %eax
cmpw %bx, %ax
cmovbel %ebx, %eax
movl %eax, %ebx
addq $0x38, %r12
decq %r13
jne 0x426e
incl %ebx
movq (%rsp), %r13
jmp 0x4297
movw $0x1, %bx
movzwl %bx, %edi
movl $0x3, %r12d
cmpl $0x400, %edi            # imm = 0x400
ja 0x4470
movl $0x20, %esi
callq 0x1120
testq %rax, %rax
je 0x445d
movl %ebp, %ecx
andl $0x1, %ecx
negl %ecx
orl $0x1, %ecx
movl %ecx, (%rsp)
movl %r15d, %ecx
negb %cl
movq $-0x1, %rdx
shrq %cl, %rdx
movq %r13, %rsi
movq $-0x1, %r13
movq %r13, (%rax)
movq %rdx, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %rax, 0x8(%rcx)
movw %bx, 0x10(%rcx)
movq %r15, 0x18(%rcx)
movq %rsi, 0x20(%rcx)
movl %ebp, 0x28(%rcx)
leaq -0x38(%r14), %rbx
movq %rax, %r15
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%r15)
je 0x43cb
leaq 0x8(%r15), %rbp
cmpq $-0x2, (%r15)
je 0x43cb
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
movl $0x1, %edx
callq *0x10(%rsp)
cmpl $0x2, %eax
je 0x4384
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x4456
movq (%rbp), %rdx
leaq 0x20(%r15), %r12
movq %rdx, 0x28(%r15)
movq 0x18(%r15), %rax
movq %rax, 0x38(%r15)
movq %r14, %rdi
leaq -0x8cd(%rip), %rsi      # 0x3aa6
movq %r12, %rcx
movl (%rsp), %r8d
callq 0x4482
movq %r12, %r15
jmp 0x43b7
movq 0x8(%rsp), %rbp
cmpq %rbp, %r15
je 0x440d
movq -0x8(%r15), %rax
movq 0x10(%r15), %rdx
movq %rax, 0x18(%r15)
notq %rdx
andq -0x18(%r15), %rdx
movq %r14, %rdi
leaq -0x526(%rip), %rsi      # 0x3e85
movq %r15, %rcx
movl (%rsp), %r8d
callq 0x4482
movq %r15, 0x20(%rsp)
leaq 0x8(%r15), %rbp
cmpq $0x0, 0x8(%r15)
jne 0x4329
movq -0x18(%r15), %r12
testq %r12, %r12
movq 0x8(%rsp), %rbp
je 0x4404
bsfq %r12, %rax
incq %rax
imulq $0x38, %rax, %rdi
addq %rbx, %rdi
callq 0x3a4a
leaq -0x1(%r12), %rax
andq %rax, %r12
bsfq %r12, %rax
cmoveq %r13, %rax
incq %rax
testq %r12, %r12
jne 0x43e0
addq $-0x20, %r15
movq %r15, 0x20(%rsp)
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq *0x10(%rsp)
cmpq %rbp, %r15
je 0x4465
movq -0x8(%r15), %rax
movq 0x10(%r15), %rdx
movq %rax, 0x18(%r15)
notq %rdx
andq -0x18(%r15), %rdx
movq %r14, %rdi
leaq -0x5bb(%rip), %rsi      # 0x3e85
movq %r15, %rcx
movl (%rsp), %r8d
callq 0x4482
movq %r15, 0x20(%rsp)
jmp 0x431a
movq 0x8(%rsp), %rbp
jmp 0x4468
movl $0x5, %r12d
jmp 0x4470
movl %eax, %r12d
movq %rbp, %rdi
callq 0x1030
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq