// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, (%rsp)
leaq -0x41(%rsi), %rax
cmpq $-0x40, %rax
jae 0x343c
pushq $0x4
jmp 0x35ff
movl %r8d, %r13d
movq %rcx, %rbp
movq %rsi, %r15
movq %rdi, %r14
xorl %r12d, %r12d
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
xorl %ecx, %ecx
cmpq %r15, %rcx
je 0x348c
imulq $0x38, %rcx, %rdx
movq 0x8(%r14,%rdx), %rdx
movq (%rdx), %rsi
movq %rsi, %rdi
shrq $0x3e, %rdi
je 0x3479
andq %rax, %rsi
subq %rsi, %rdx
jmp 0x3465
movl 0x2c(%rdx), %edx
cmpw %r12w, %dx
cmovbel %r12d, %edx
incq %rcx
movl %edx, %r12d
jmp 0x3457
incl %r12d
cmpw $0x400, %r12w           # imm = 0x400
jbe 0x349e
pushq $0x3
jmp 0x35ff
movzwl %r12w, %edi
pushq $0x20
popq %rsi
callq 0x1120
testq %rax, %rax
je 0x35fd
movq %rax, %rbx
movq %rbp, %rdx
movl %r13d, %ebp
andl $0x1, %ebp
negl %ebp
orl $0x1, %ebp
movl %r15d, %ecx
negb %cl
pushq $-0x1
popq %rax
shrq %cl, %rax
orq $-0x1, (%rbx)
movq %rax, 0x8(%rbx)
leaq 0x8(%rsp), %rax
movq %rbx, (%rax)
movq %rbx, 0x8(%rax)
movw %r12w, 0x10(%rax)
movq %r15, 0x18(%rax)
movq %rdx, 0x20(%rax)
movl %r13d, 0x28(%rax)
movq %rbx, %r13
cmpq $0x0, 0x8(%r13)
je 0x3505
cmpq $-0x2, (%r13)
jne 0x357f
movq -0x18(%r13), %r15
bsfq %r15, %rax
testq %r15, %r15
je 0x3527
imulq $0x38, %rax, %rdi
addq %r14, %rdi
callq 0x31af
leaq -0x1(%r15), %rax
andq %rax, %r15
jmp 0x3509
addq $-0x20, %r13
movq %r13, 0x10(%rsp)
movq %r13, %r15
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
pushq $-0x1
popq %rdx
callq *(%rsp)
cmpq %rbx, %r15
je 0x35f0
movq -0x8(%r15), %rax
movq 0x10(%r15), %rdx
movq %rax, 0x18(%r15)
notq %rdx
andq -0x18(%r15), %rdx
movq %r14, %rdi
leaq -0x2c4(%rip), %rsi      # 0x32a3
movq %r15, %rcx
movl %ebp, %r8d
callq 0x3613
movq %r15, %r13
movq %r13, 0x10(%rsp)
jmp 0x34f7
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq *(%rsp)
cmpl $0x2, %eax
je 0x35ba
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x35f3
leaq 0x20(%r13), %r15
movq 0x8(%r13), %rdx
movq 0x18(%r13), %rax
movq %rdx, 0x28(%r13)
movq %rax, 0x38(%r13)
movq %r14, %rdi
leaq -0x3bf(%rip), %rsi      # 0x31f9
jmp 0x3567
movq %rbx, %r15
cmpq %rbx, %r13
je 0x3533
movq -0x8(%r13), %rax
movq 0x10(%r13), %rdx
movq %rax, 0x18(%r13)
notq %rdx
andq -0x18(%r13), %rdx
movq %r14, %rdi
leaq -0x340(%rip), %rsi      # 0x32a3
movq %r13, %rcx
movl %ebp, %r8d
callq 0x3613
jmp 0x3575
movl %eax, %r12d
movq %rbx, %rdi
callq 0x1030
jmp 0x3601
pushq $0x5
popq %r12
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq