// File: /workspace/github_c_makefile_star2_O0/jflatow[P]jfdb/src/trie.c
// Function: splice_new
static JFT_Status splice_new(JFT_Cursor *cursors,
                             JFT_MergeContext *ctx,
                             JFT_Phase phase) {
  // determine what to do based on the most recent / highest precedence cursor
  // (cursors should be given in order of recency / precedence)
  JFT_MergeFrame *frame = ctx->frame;
  JFT_Mask active = frame->active;
  JFT_Amount b = ffsll(active);
  JFT_Cursor *cursor = cursors + b - 1;
  JFT_Cons *cons = (JFT_Cons *)ctx->acc;
  JFT_Buffer *buf = cons->buffer;
  JFT_NodeType type = JFT_node_type(cursor->node);

  if (phase == Out) {
    // do nothing during phase out, except finishing touches
    // copy the changed root data back into the buffer
    if (type == Root) {
      JFT_Root *root = (JFT_Root *)buf->data;
      root->extent = buf->mark;
      root->numPrimary = cons->root.numPrimary;
      root->numIndices = cons->root.numIndices;
      root->maxIndices = cons->root.maxIndices;
      root->maxKeySize = ctx->maxKeySize;
    }
    return Ok;
  }

  if (type == Root) {
    // on the root: make space for the new root in buffer
    // root is a parent, mark it's offset before writing
    frame->offset = buf->mark;
    if (!make_root(buf))
      return ENoMem;

    // keep a root in cons and update it later (NB: cannot rely on buf pointers)
    // allocate memory to store the compressed stem before we write it
    cons->root.head = JFT_head(Root, False, 0, 0, 0);
    cons->stem = (JFT_Stem) {.data = malloc(ctx->maxKeySize)};
    if (cons->stem.data == NULL)
      return ENoMem;

    // allocate the iterators for indices
    cons->iters = malloc(ctx->numCursors * sizeof(JFT_Iter));

    // allocate the offset translation tables for indices (if we are translating)
    if (!(ctx->flags & JFT_FLAGS_ATOM)) {
      cons->TLBs = malloc(ctx->numCursors * sizeof(JFT_Offset *));
      cons->TLBSizes = calloc(ctx->numCursors, sizeof(JFT_Count));
      for (JFT_Amount k = 0; k < ctx->numCursors; k++)
        cons->TLBs[k] = malloc(2 * ((JFT_Root *)cursors[k].root)->numPrimary * sizeof(JFT_Offset));
    }

    // step into the tree
    return Step;
  }

  // add the symbol to the budding stem
  // if its a keyspace, mark the keyspace in the cons
  if (cursor->symbol == JFT_SYMBOL_PRIMARY ||
      cursor->symbol == JFT_SYMBOL_INDICES) {
      cons->stem.pre = cons->keySpace = cursor->symbol;
      cons->stem.size++;
  } else {
      cons->stem.data[cons->stem.size - (cons->stem.pre ? 1 : 0)] = cursor->symbol;
      cons->stem.size++;
  }

  // if cursor is on a terminal node: overwrite
  if (JFT_cursor_at_terminal(cursor)) {
    int doTranslate = !(ctx->flags & JFT_FLAGS_ATOM);
    JFT_Leaf leaf = JFT_leaf(cursor->node);
    // simply skip empty leaves: the offset in parent will remain zero and thus will not branch
    // eventually (next merge), the node will get phased out of the trie completely
    if ((ctx->flags & JFT_FLAGS_PRUNE) && leaf.size == 0)
      return Next;

    if (cons->keySpace == JFT_SYMBOL_PRIMARY) {
      // at value node
      // if we are translating, store the translation of old value offset to new
      if (doTranslate) {
        cons->TLBs[b - 1][cons->TLBSizes[b - 1]++] = cursor->node - cursor->root;
        cons->TLBs[b - 1][cons->TLBSizes[b - 1]++] = buf->mark;
      }

      // emit the leaf node
      frame->offset = buf->mark;
      if (!make_leaf(buf, (frame - 1)->offset, &cons->stem, &leaf))
        return ENoMem;

      cons->root.numPrimary++;
    } else {
      // at index node
      // wrap each cursors' words in an iterator
      // which yields batches of sorted (maybe translated) words
      JFT_Count maxWords = 0, numWords = 0;
      do {
        cursor = cursors + b - 1;
        leaf = JFT_leaf(cursor->node);
        if (!(JFT_cursor_at_terminal(cursor)))
          break;
        if (doTranslate) {
          // use a translation iterator, translates a static size batch at a time
          cons->iters[b - 1] = JFT_iter_trans(leaf, cons->TLBs[b - 1], cons->TLBSizes[b - 1]);
        } else {
          // use an iterator that just returns the raw leaf words in one batch
          cons->iters[b - 1] = JFT_iter_leaf(leaf);
        }
        maxWords += leaf.size;
      } while ((b = ffsll(active &= active - 1)));

      // emit the leaf node using an upper bound on the number of words
      frame->offset = buf->mark;
      if (!(prep_leaf(buf, (frame - 1)->offset, &cons->stem, maxWords)))
        return ENoMem;

      // write the indices, count the actual words
      JFT_Iter i = JFT_iter_any(cons->iters, frame->active ^ active);
      do {
        JFT_buffer_paste(buf, i.batch.data, i.batch.size * sizeof(JFT_Word));
        numWords += i.batch.size;
      } while (JFT_iter_next(&i));

      // correct the leaf node using the actual number of words
      edit_leaf(buf, frame->offset, &cons->stem, maxWords, numWords);

      if (numWords > cons->root.maxIndices)
        cons->root.maxIndices = numWords;
      cons->root.numIndices++;
    }

    // reset the bud
    cons->stem.pre = 0;
    cons->stem.size = 0;

    // done with this subtree
    return Next;
  }

  // cursor is non-terminal, maybe emit a branch
  // look at the other cursors and count # unique subsequent symbols
  JFT_Range range = {.min = 255, .max = 0};
  JFT_SymbolSet set = {};
  int count;
  do {
    if (cursor->symbol != JFT_SYMBOL_ANY)
      JFT_cursor_mark_subsequent(cursor, &range, &set);
    b = ffsll(active &= active - 1);
  } while (b && !JFT_cursor_at_terminal(cursor = cursors + b - 1));

  if ((count = JFT_symbol_set_count(&set)) > 1) {
    // more than one possible symbol: emit a branch node
    frame->offset = buf->mark;
    if (!make_branch(buf, (frame - 1)->offset, &cons->stem, &range, &set, count))
      return ENoMem;

    // reset the bud
    cons->stem.pre = 0;
    cons->stem.size = 0;
  }
  return Step;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdx
tzcntq %rdx, %rax
addq $0x1, %rax
xorl %ecx, %ecx
cmpq $0x0, %rdx
cmoveq %rcx, %rax
cltq
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
imulq $0x38, -0x38(%rbp), %rcx
addq %rcx, %rax
addq $-0x38, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4d00
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x5a54
cmpl $0x0, -0x54(%rbp)
jne 0x5a48
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x48(%rbp), %rax
movl 0x34(%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x48(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movw 0x10(%rax), %cx
movq -0x60(%rbp), %rax
movw %cx, 0x2c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x61a5
cmpl $0x0, -0x54(%rbp)
jne 0x5bd9
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rdi
callq 0x6680
cmpq $0x0, %rax
jne 0x5a89
movl $0x5, -0x4(%rbp)
jmp 0x61a5
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
movl %ecx, %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x7a00
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x350(%rbp)
movq $0x0, -0x78(%rbp)
movw $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x10(%rax), %eax
movl %eax, %edi
callq 0x1150
movq %rax, %rcx
movq -0x350(%rbp), %rax
movq %rcx, -0x68(%rbp)
movq -0x78(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x50(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x5b0b
movl $0x5, -0x4(%rbp)
jmp 0x61a5
movq -0x18(%rbp), %rax
imulq $0xc8, 0x18(%rax), %rdi
callq 0x1150
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x5bcd
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
shlq $0x3, %rdi
callq 0x1150
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x1120
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x68(%rax)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x5bcb
movq -0x10(%rbp), %rax
imulq $0x38, -0x80(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movl 0x20(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1150
movq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x80(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x5b7b
jmp 0x5bcd
movl $0x1, -0x4(%rbp)
jmp 0x61a5
movq -0x40(%rbp), %rax
cmpq $-0x5, 0x30(%rax)
je 0x5bef
movq -0x40(%rbp), %rax
cmpq $-0x4, 0x30(%rax)
jne 0x5c19
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x48(%rbp), %rax
movw 0x48(%rax), %cx
addw $0x1, %cx
movw %cx, 0x48(%rax)
jmp 0x5c61
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movb %al, %dl
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x48(%rbp), %rcx
movzwl 0x48(%rcx), %ecx
movq -0x48(%rbp), %rsi
movq 0x40(%rsi), %r8
xorl %esi, %esi
movl $0x1, %edi
cmpq $0x0, %r8
cmovnel %edi, %esi
subl %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x48(%rbp), %rax
movw 0x48(%rax), %cx
addw $0x1, %cx
movw %cx, 0x48(%rax)
movq -0x40(%rbp), %rdi
callq 0x7a70
cmpl $0x0, %eax
je 0x6066
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x84(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7ad0
movl %eax, -0x98(%rbp)
movq %rdx, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x5cce
cmpl $0x0, -0x98(%rbp)
jne 0x5cce
movl $0x2, -0x4(%rbp)
jmp 0x61a5
movq -0x48(%rbp), %rax
cmpq $-0x5, 0x8(%rax)
jne 0x5dc8
cmpl $0x0, -0x84(%rbp)
je 0x5d74
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %rax
subq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq 0x68(%rcx), %rsi
movq -0x38(%rbp), %rdi
subq $0x1, %rdi
movl (%rsi,%rdi,4), %ecx
movl %ecx, %r8d
addl $0x1, %r8d
movl %r8d, (%rsi,%rdi,4)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq 0x68(%rcx), %rsi
movq -0x38(%rbp), %rdi
subq $0x1, %rdi
movl (%rsi,%rdi,4), %ecx
movl %ecx, %r8d
addl $0x1, %r8d
movl %r8d, (%rsi,%rdi,4)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x8(%rax), %rsi
movq -0x48(%rbp), %rdx
addq $0x40, %rdx
leaq -0x98(%rbp), %rcx
callq 0x6720
cmpq $0x0, %rax
jne 0x5db6
movl $0x5, -0x4(%rbp)
jmp 0x61a5
movq -0x48(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
jmp 0x6044
movl $0x0, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movq -0x10(%rbp), %rax
imulq $0x38, -0x38(%rbp), %rcx
addq %rcx, %rax
addq $-0x38, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7ad0
movl %eax, -0xb0(%rbp)
movq %rdx, -0xa8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x40(%rbp), %rdi
callq 0x7a70
cmpl $0x0, %eax
jne 0x5e39
jmp 0x5f55
cmpl $0x0, -0x84(%rbp)
je 0x5ebe
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
imulq $0xc8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x358(%rbp)
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rcx
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x38(%rbp), %rdx
subq $0x1, %rdx
movl (%rax,%rdx,4), %r8d
movl -0x98(%rbp), %esi
movq -0x90(%rbp), %rdx
leaq -0x178(%rbp), %rdi
callq 0x7b30
movq -0x358(%rbp), %rdi
leaq -0x178(%rbp), %rsi
movl $0xc8, %edx
callq 0x1140
jmp 0x5f10
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
imulq $0xc8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x360(%rbp)
movl -0x98(%rbp), %esi
movq -0x90(%rbp), %rdx
leaq -0x240(%rbp), %rdi
callq 0x7bb0
movq -0x360(%rbp), %rdi
leaq -0x240(%rbp), %rsi
movl $0xc8, %edx
callq 0x1140
movl -0x98(%rbp), %eax
addl -0x9c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
andq -0x30(%rbp), %rdx
movq %rdx, -0x30(%rbp)
tzcntq %rdx, %rax
addq $0x1, %rax
xorl %ecx, %ecx
cmpq $0x0, %rdx
cmoveq %rcx, %rax
cltq
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x5ddc
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x8(%rax), %rsi
movq -0x48(%rbp), %rdx
addq $0x40, %rdx
movl -0x9c(%rbp), %ecx
callq 0x7c00
cmpq $0x0, %rax
jne 0x5f96
movl $0x5, -0x4(%rbp)
jmp 0x61a5
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
xorq -0x30(%rbp), %rdx
leaq -0x308(%rbp), %rdi
callq 0x7ce0
movq -0x50(%rbp), %rdi
movq -0x2f8(%rbp), %rsi
movl -0x2fc(%rbp), %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0x7d40
movl -0x2fc(%rbp), %eax
addl -0xa0(%rbp), %eax
movl %eax, -0xa0(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x6ce0
cmpl $0x0, %eax
jne 0x5fb6
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rdx
addq $0x40, %rdx
movl -0x9c(%rbp), %ecx
movl -0xa0(%rbp), %r8d
callq 0x7d90
movl -0xa0(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x38(%rcx), %eax
jbe 0x6037
movl -0xa0(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x48(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
movq -0x48(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x48(%rbp), %rax
movw $0x0, 0x48(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x61a5
movq 0x33f3(%rip), %rax      # 0x9460
movq %rax, -0x318(%rbp)
movq 0x33ed(%rip), %rax      # 0x9468
movq %rax, -0x310(%rbp)
leaq -0x340(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x10f0
movq -0x40(%rbp), %rax
cmpq $-0x1, 0x30(%rax)
je 0x60b7
movq -0x40(%rbp), %rdi
leaq -0x318(%rbp), %rsi
leaq -0x340(%rbp), %rdx
callq 0x7df0
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
andq -0x30(%rbp), %rdx
movq %rdx, -0x30(%rbp)
tzcntq %rdx, %rax
addq $0x1, %rax
xorl %ecx, %ecx
cmpq $0x0, %rdx
cmoveq %rcx, %rax
cltq
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x38(%rbp)
movb %al, -0x361(%rbp)
je 0x6116
movq -0x10(%rbp), %rdi
imulq $0x38, -0x38(%rbp), %rax
addq %rax, %rdi
addq $-0x38, %rdi
movq %rdi, -0x40(%rbp)
callq 0x7a70
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x361(%rbp)
movb -0x361(%rbp), %al
testb $0x1, %al
jne 0x6095
leaq -0x340(%rbp), %rdi
callq 0x7e80
movl %eax, -0x344(%rbp)
cmpl $0x1, %eax
jle 0x619e
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movq -0x8(%rax), %rsi
movq -0x48(%rbp), %rdx
addq $0x40, %rdx
movl -0x344(%rbp), %r9d
leaq -0x318(%rbp), %rcx
leaq -0x340(%rbp), %r8
callq 0x7ef0
cmpq $0x0, %rax
jne 0x6188
movl $0x5, -0x4(%rbp)
jmp 0x61a5
movq -0x48(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x48(%rbp), %rax
movw $0x0, 0x48(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
nopw %cs:(%rax,%rax)