// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/src/trie.c
// Function: JFT_atom
JFT *JFT_atom(const JFT_Stem *restrict primary,
              const JFT_Leaf *restrict value,
              const JFT_Stem *restrict indices,
              JFT_Count numIndices,
              JFT_Buffer *restrict scratch,
              JFT_Buffer *restrict output) {
  // create an atomic trie (i.e. a single operation)
  // using these atoms we can make bigger and bigger tries
  // first, manually create a trie for each individual key
  JFT *atom;
  JFT_Count num = numIndices + 1, k = 0, batch = MIN(num, JFT_MASK_CAPACITY);
  JFT_Cursor cursors[batch];
  JFT_Offset rootOffsets[batch];
  JFT_Root *root;
  JFT_Stem stem = (JFT_Stem) {
    .pre = JFT_SYMBOL_PRIMARY,
    .size = primary->size + !primary->pre,
    .data = primary->data
  };

  do {
    if (!(JFT_buffer_reset(scratch)))
      return NULL;

    if (k == 0) {
      if (!(make_root(scratch)))
        return NULL;
      if (!(make_leaf(scratch, 0, &stem, value)))
        return NULL;
      root = (JFT_Root *)(scratch->data);
      root->extent = scratch->mark;
      root->numPrimary = 1;
      root->maxKeySize = stem.size;
      rootOffsets[0] = 0;
      num--;
    } else {
      root = (JFT_Root *)(output->data);
      if (!JFT_buffer_write(scratch, output->data, output->mark))
        return NULL;
    }

    JFT_Leaf index = JFTV(root->primaryOffset);
    for (JFT_Count i = 1; i < batch; i++, k++) {
      rootOffsets[i] = scratch->mark;
      stem.pre = JFT_SYMBOL_INDICES;
      stem.size = indices[k].size + !indices[k].pre;
      stem.data = indices[k].data;
      if (!(make_root(scratch)))
        return NULL;
      if (!(make_leaf(scratch, rootOffsets[i], &stem, &index)))
        return NULL;
      root = (JFT_Root *)(scratch->data + rootOffsets[i]);
      root->extent = scratch->mark - rootOffsets[i];
      root->numIndices = 1;
      root->maxIndices = 1;
      root->maxKeySize = stem.size;
      num--;
    }

    for (JFT_Amount i = 0; i < batch; i++)
      cursors[i] = JFT_cursor((JFT *)(scratch->data + rootOffsets[i]));
    atom = JFT_cursor_merge_new(cursors, batch, output, JFT_FLAGS_ATOM);
    batch = MIN(num + 1, JFT_MASK_CAPACITY);
  } while (atom && num);
  return atom;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r14
movq %rdx, -0x88(%rbp)
movq %rsi, -0x78(%rbp)
incl %ecx
cmpl $0x40, %ecx
pushq $0x40
popq %rax
movl %eax, %r13d
movl %ecx, -0x2c(%rbp)
cmovbl %ecx, %r13d
imull $0x38, %r13d, %eax
addl $0xf, %eax
andl $0x3ff0, %eax           # imm = 0x3FF0
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
leal 0xf(,%r13,8), %eax
andl $0x7f0, %eax            # imm = 0x7F0
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
leaq -0xa0(%rbp), %rcx
movq $-0x5, (%rcx)
movzwl 0x8(%rdi), %ebx
cmpq $0x1, (%rdi)
adcw $0x0, %bx
movw %bx, 0x8(%rcx)
movq 0x10(%rdi), %rax
movq %rax, 0x10(%rcx)
leaq -0x50(%rbp), %r12
movq $0x0, -0x40(%rbp)
movq %r9, -0x70(%rbp)
andq $0x0, 0x10(%r14)
cmpl $0x0, -0x40(%rbp)
movq %r13, -0x38(%rbp)
je 0x3fec
movq 0x10(%r9), %r13
movq 0x18(%r9), %r15
movq %r14, %rdi
movq %r13, %rsi
callq *0x20(%r14)
testl %eax, %eax
jne 0x4218
movq 0x18(%r14), %rdi
addq 0x10(%r14), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1140
addq 0x10(%r14), %r13
movq %r13, 0x10(%r14)
leaq -0x50(%rbp), %r12
jmp 0x4047
movq %r14, %rdi
callq 0x4229
testq %rax, %rax
je 0x4218
movq %r14, %rdi
xorl %esi, %esi
leaq -0xa0(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x4267
movq %rax, %rcx
movl $0x0, %eax
testq %rcx, %rcx
je 0x421a
movq 0x10(%r14), %r13
movq 0x18(%r14), %r15
movq %r13, 0x18(%r15)
movl $0x1, 0x20(%r15)
movw %bx, 0x2c(%r15)
movq -0x48(%rbp), %rax
andq $0x0, (%rax)
decl -0x2c(%rbp)
movl $0x1, -0xb0(%rbp)
movq 0x8(%r15), %rax
movq %rax, -0x50(%rbp)
movq %r12, -0xa8(%rbp)
movl -0x38(%rbp), %eax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %eax
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
leaq 0x1(%r12), %r15
cmpq -0x38(%rbp), %r15
jae 0x4132
movq -0x40(%rbp), %rax
addq %r12, %rax
movq -0x48(%rbp), %rcx
movq %r13, 0x8(%rcx,%r12,8)
movl %eax, %eax
imulq $0x18, %rax, %rax
movq -0x88(%rbp), %rcx
movzwl 0x8(%rcx,%rax), %ebx
cmpq $0x1, (%rcx,%rax)
adcw $0x0, %bx
movq $-0x4, -0xa0(%rbp)
movw %bx, -0x98(%rbp)
movq 0x10(%rcx,%rax), %rax
movq %rax, -0x90(%rbp)
movq %r14, %rdi
callq 0x4229
testq %rax, %rax
je 0x4218
movq %r14, %rdi
movq %r13, %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
callq 0x4267
testq %rax, %rax
je 0x4218
movq 0x10(%r14), %rax
movq 0x18(%r14), %rcx
movq %rax, %rdx
subq %r13, %rdx
movq %rdx, 0x18(%rcx,%r13)
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x24(%rcx,%r13)
movw %bx, 0x2c(%rcx,%r13)
movq %r15, %r12
movq %rax, %r13
jmp 0x4074
addq %r12, -0x40(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x68(%rbp)
subq %r12, %rax
movq %rax, -0x60(%rbp)
imulq $0x7, -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %r13
xorl %r15d, %r15d
cmpq %r15, -0x80(%rbp)
je 0x41cd
movq 0x18(%r14), %rdx
addq (%r13), %rdx
leaq -0xe0(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rdx, -0xe8(%rbp)
leaq -0xe8(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x30f8
movups -0xe8(%rbp), %xmm0
movups -0xd8(%rbp), %xmm1
movups -0xc8(%rbp), %xmm2
movq -0x58(%rbp), %rcx
movups %xmm0, (%rcx,%r15,8)
movups %xmm1, 0x10(%rcx,%r15,8)
movups %xmm2, 0x20(%rcx,%r15,8)
movq -0xb8(%rbp), %rax
movq %rax, 0x30(%rcx,%r15,8)
addq $0x7, %r15
addq $0x8, %r13
jmp 0x4154
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %rdx, %r15
pushq $0x4
popq %rcx
callq 0x3734
movl -0x2c(%rbp), %ecx
subl %r12d, %ecx
incl %ecx
cmpl $0x40, %ecx
movl %ecx, %r13d
pushq $0x40
popq %rcx
cmovael %ecx, %r13d
testq %rax, %rax
je 0x421a
movq -0x60(%rbp), %rcx
movl %ecx, -0x2c(%rbp)
cmpl %r12d, -0x68(%rbp)
leaq -0x50(%rbp), %r12
movq %r15, %r9
jne 0x3fa2
jmp 0x421a
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq