// File: /workspace/github_c_makefile_star2_O3/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
leaq -0x41(%rsi), %rax
movl $0x4, %r12d
cmpq $-0x40, %rax
jb 0x400c
movl %r8d, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
xorl %r8d, %r8d
movabsq $0xffffffffff, %rdx     # imm = 0xFFFFFFFFFF
xorl %r13d, %r13d
imulq $0x38, %r8, %rsi
movq 0x8(%r14,%rsi), %rsi
movq (%rsi), %rdi
cmpq %rax, %rdi
jb 0x3e2d
andq %rdx, %rdi
subq %rdi, %rsi
jmp 0x3e1d
movl 0x2c(%rsi), %esi
cmpw %r13w, %si
cmovbel %r13d, %esi
movl %esi, %r13d
incq %r8
cmpq %r15, %r8
jne 0x3e14
incl %r13d
movzwl %r13w, %edi
movl $0x3, %r12d
cmpl $0x400, %edi            # imm = 0x400
ja 0x400c
movq %rcx, %r12
movl $0x20, %esi
callq 0x1120
testq %rax, %rax
je 0x3fe9
movl %ebp, %esi
andl $0x1, %ebp
negl %ebp
orl $0x1, %ebp
movl %r15d, %ecx
negb %cl
movq %rax, %rdx
movq $-0x1, %rax
movq %rax, (%rdx)
shrq %cl, %rax
movq %rax, 0x8(%rdx)
leaq 0x8(%rsp), %rax
movq %rdx, (%rax)
movq %rdx, 0x8(%rax)
movw %r13w, 0x10(%rax)
movq %r15, 0x18(%rax)
movq %r12, 0x20(%rax)
movl %esi, 0x28(%rax)
movl $0x1, %eax
movq %rdx, (%rsp)
movq %rdx, %r15
testq %rax, %rax
je 0x3f68
leaq 0x8(%r15), %r13
cmpq $-0x2, (%r15)
je 0x3f68
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq *%rbx
cmpl $0x2, %eax
je 0x3f22
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x4003
movq (%r13), %rdx
leaq 0x20(%r15), %r12
movq %rdx, 0x28(%r15)
movq 0x18(%r15), %rax
movq %rax, 0x38(%r15)
movq %r14, %rdi
leaq -0x837(%rip), %rsi      # 0x36db
movq %r12, %rcx
movl %ebp, %r8d
callq 0x4023
movq %r12, %r15
jmp 0x3f54
cmpq (%rsp), %r15
je 0x3ff1
movq -0x8(%r15), %rax
movq 0x10(%r15), %rdx
movq %rax, 0x18(%r15)
notq %rdx
andq -0x18(%r15), %rdx
movq %r14, %rdi
leaq -0x4d8(%rip), %rsi      # 0x3a71
movq %r15, %rcx
movl %ebp, %r8d
callq 0x4023
movq %r15, 0x10(%rsp)
leaq 0x8(%r15), %r13
cmpq $0x0, 0x8(%r15)
jne 0x3eca
movq -0x18(%r15), %r12
testq %r12, %r12
je 0x3f8f
bsfq %r12, %rax
imulq $0x38, %rax, %rdi
addq %r14, %rdi
callq 0x367f
leaq -0x1(%r12), %rax
andq %rax, %r12
bsfq %r12, %rax
jne 0x3f75
leaq -0x20(%r15), %r13
movq %r13, 0x10(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq *%rbx
movq (%rsp), %rdi
cmpq %rdi, %r13
je 0x401e
movq -0x28(%r15), %rax
movq -0x10(%r15), %rdx
movq %rax, -0x8(%r15)
notq %rdx
andq -0x38(%r15), %rdx
movq %r14, %rdi
leaq -0x55c(%rip), %rsi      # 0x3a71
movq %r13, %rcx
movl %ebp, %r8d
callq 0x4023
movq %r13, 0x10(%rsp)
movq -0x18(%r15), %rax
movq %r13, %r15
jmp 0x3ebd
movl $0x5, %r12d
jmp 0x400c
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq *%rbx
movl %eax, %r12d
movq (%rsp), %rdi
callq 0x1030
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r12d
jmp 0x4007