// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/test/trie.c
// Function: main
int main(int argc, char **argv) {
  size_t O = argc > 1 ? atoi(argv[1]) : 0;
  size_t B = argc > 2 ? atoi(argv[2]) : 1;
  JFT_Buffer
    *scratch = JFT_membuf(),
    *outputW = JFT_membuf(),
    *outputX = JFT_membuf(),
    *outputY = JFT_membuf();
  if (scratch == NULL || outputX == NULL || outputY == NULL)
    return ENoMem;

  JFT_Word *b = malloc(B * sizeof(JFT_Word));
  JFT_Leaf v = ((JFT_Leaf) {.size = B, .data = b});
  if (B > 0)
    b[0] = 42;
  const JFT_Stem k[] = {
    JFTK("hello world"),
    JFTK("i1"),
    JFTK("i2"),
    JFTK("index3"),
    JFTK("index3"),
    JFTK("aliens"),
    JFTK("attack!!"),
    JFTK("keyhole")
  };
  JFT *w = JFT_atom(k + 0, &v, k + 1, 7, scratch, outputW);
  JFT *x = JFT_atom(k + 1, &v, k + 1, 7, scratch, outputX);
  JFT *y = JFT_atom(k + 2, &v, k + 2, 6, scratch, outputY);

  JFT_Cursor cursors[] = {
    JFT_cursor(w),
    JFT_cursor(x),
    JFT_cursor(y)
  };
  JFT *z = JFT_cursor_merge_new(cursors, 3, scratch, 0);
  if (z) {
    printf("scratch: %zu\n", scratch->size);
    print_node(z + O);
    print_trie(z + O);

    JFT_Cursor c = JFT_cursor(z + O);
    JFT_Stem q = (JFT_Stem) {.data = malloc(JFT_KEY_LIMIT)};
    JFT_Keys ks = JFT_keys(&c, &q, Reverse);
    uint8_t keyData[JFT_KEY_LIMIT];
    print_stem("", JFT_key(c.node, keyData));
    while (JFT_keys_next(&ks))
      print_stem(" -> ", *ks.stem);
    free(q.data);
  } else {
    printf("Failed to splice atom\n");
  }

  free(b);
  JFT_membuf_free(scratch);
  JFT_membuf_free(outputW);
  JFT_membuf_free(outputX);
  JFT_membuf_free(outputY);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
pushq $0x1
popq %rbp
cmpl $0x2, %edi
jl 0x133a
movq %rsi, %rbx
movl %edi, %r14d
movq 0x8(%rsi), %rdi
callq 0x11d0
movslq %eax, %r15
cmpl $0x2, %r14d
je 0x133d
movq 0x10(%rbx), %rdi
callq 0x11d0
movl %eax, %ebp
jmp 0x133d
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x4b15
movq %rax, %rbx
xorl %eax, %eax
callq 0x4b15
movq %rax, %r13
xorl %eax, %eax
callq 0x4b15
movq %rax, %r14
xorl %eax, %eax
callq 0x4b15
testq %rbx, %rbx
sete %dl
movq %r14, 0x38(%rsp)
testq %r14, %r14
sete %cl
orb %dl, %cl
movq %rax, 0x30(%rsp)
testq %rax, %rax
sete %al
orb %cl, %al
je 0x138c
pushq $0x5
popq %rax
jmp 0x19ec
movq %r15, 0x58(%rsp)
movq %rbx, 0x28(%rsp)
movslq %ebp, %rbx
leaq (,%rbx,8), %rdi
callq 0x1160
movl %ebx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
testl %ebx, %ebx
je 0x13ba
movq $0x2a, (%rax)
movq %rax, 0x60(%rsp)
leaq 0x796a(%rip), %rsi      # 0x8d30
leaq 0x180(%rsp), %r14
movl $0xc0, %edx
movq %r14, %rdi
callq 0x1150
leaq 0x198(%rsp), %r15
leaq 0x70(%rsp), %r12
pushq $0x7
popq %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
movq %r13, 0x68(%rsp)
movq %r13, %r9
callq 0x424d
movq %rax, %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rbx, %r8
movq 0x38(%rsp), %r9
callq 0x424d
movq %rax, %r15
leaq 0x1b0(%rsp), %rdi
pushq $0x6
popq %rcx
movq %r12, %rsi
movq %rdi, %rdx
movq %rbx, %r8
movq 0x30(%rsp), %r9
callq 0x424d
movq %rax, %r12
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x1a43
leaq 0x108(%rsp), %rdi
movq %r15, %rsi
callq 0x1a43
leaq 0x140(%rsp), %rdi
movq %r12, %rsi
callq 0x1a43
pushq $0x3
popq %rsi
movq %r13, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x3a7c
testq %rax, %rax
je 0x14c7
movq %rax, %r15
movq 0x8(%rbx), %rsi
leaq 0x4c85(%rip), %rdi      # 0x6125
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x10e0
movq 0x58(%rsp), %rax
leaq (%r15,%rax), %rbp
movq (%r15,%rax), %r14
btq $0x3d, %r14
leaq 0x8(%r15,%rax), %r12
jb 0x14d8
movq %r12, %rax
jmp 0x14e7
leaq 0x4e64(%rip), %rdi      # 0x6332
callq 0x1080
jmp 0x19ba
movzbl 0x8(%rbp), %ebx
orq $-0x100, %rbx
leaq 0x9(%rbp), %rax
movabsq $0xffffffffff, %r13     # imm = 0xFFFFFFFFFF
shrq $0x30, %r14
andl $0xfff, %r14d           # imm = 0xFFF
movq %rbx, 0x240(%rsp)
movw %r14w, 0x248(%rsp)
movq %rax, 0x250(%rsp)
leaq 0x4e2c(%rip), %r15      # 0x6348
movq %r15, %rdi
callq 0x1080
movq (%rbp), %rax
movq %rax, %rcx
shrq $0x3e, %rcx
leaq 0x4b9a(%rip), %rdx      # 0x60d0
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
movq %rax, %rdx
shrq $0x3d, %rdx
andl $0x1, %edx
movq %rax, %rcx
shrq $0x3c, %rcx
andl $0x1, %ecx
movq %rax, %rdi
shrq $0x28, %rdi
movzbl %dil, %r9d
andq %r13, %rax
movq %rax, (%rsp)
leaq 0x4bce(%rip), %rdi      # 0x6138
movl %r14d, %r8d
xorl %eax, %eax
callq 0x10e0
movq 0x250(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x240(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x4d67(%rip), %rdi      # 0x62fb
callq 0x1a69
movq %r15, %rdi
callq 0x1080
movq (%rbp), %rax
movq %rax, %rcx
shrq $0x3e, %rcx
leaq 0x4a5d(%rip), %rdx      # 0x6010
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rcx
movl 0x20(%rbp), %r8d
movl 0x24(%rbp), %r9d
movl 0x28(%rbp), %eax
movzwl 0x2c(%rbp), %edi
movl %edi, 0x8(%rsp)
movl %eax, (%rsp)
leaq 0x4c16(%rip), %rdi      # 0x61fb
xorl %eax, %eax
callq 0x10e0
jmp 0x16de
movq %rax, %rcx
shrq $0x28, %rcx
movzbl %cl, %ecx
shrq $0x30, %rax
andl $0xfff, %eax            # imm = 0xFFF
addq %rax, %r12
shll $0x4, %ecx
leaq 0x4a0f(%rip), %rax      # 0x6020
movq (%rcx,%rax), %rbx
movq 0x8(%rcx,%rax), %r14
leaq 0x4d38(%rip), %rdi      # 0x6359
callq 0x1080
subq %rbx, %r14
xorl %r15d, %r15d
cmpq %r15, %r14
jb 0x16de
leal (%rbx,%r15), %eax
movq (%r12,%r15,8), %rsi
movzbl %al, %edi
callq 0x1ace
incq %r15
jmp 0x162c
movq %rax, %r14
shrq $0x28, %r14
movl $0xff, %ecx
andl %ecx, %r14d
shrq $0x30, %rax
andl $0xfff, %eax            # imm = 0xFFF
addq %rax, %r12
cmpl %ecx, %r14d
jne 0x1672
movl (%r12), %r14d
addq $0x4, %r12
leaq 0x4c50(%rip), %rdi      # 0x62c9
movl %r14d, %esi
xorl %eax, %eax
callq 0x10e0
leaq 0x4c4f(%rip), %rdi      # 0x62d9
xorl %eax, %eax
callq 0x10e0
movl $0x2710, %ebx           # imm = 0x2710
cmpl %ebx, %r14d
cmovbl %r14d, %ebx
leaq 0x4c41(%rip), %r15      # 0x62e5
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x16bf
movq (%r12,%r13,8), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e0
incq %r13
jmp 0x16a7
cmpl $0x2710, %r14d          # imm = 0x2710
jbe 0x16d6
leaq 0x4c1b(%rip), %rdi      # 0x62ea
xorl %eax, %eax
callq 0x10e0
pushq $0xa
popq %rdi
callq 0x1040
movabsq $0x2000000000000000, %rbx # imm = 0x2000000000000000
leaq 0x4c59(%rip), %rdi      # 0x6348
callq 0x1080
leaq 0x240(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1a43
pushq $0x1
popq %r15
pushq $0x20
popq %r12
xorl %r14d, %r14d
movq %rbp, 0x20(%rsp)
testl %r14d, %r14d
movl $0x0, %ebp
cmovgl %r14d, %ebp
subl $0x1, %ebp
jb 0x1732
movl %r12d, %edi
callq 0x1040
jmp 0x1723
movq 0x270(%rsp), %rbp
leaq 0x270(%rsp), %rdi
callq 0x1b76
cmpq $0x100, %rbp            # imm = 0x100
leaq 0x4ba7(%rip), %rdi      # 0x62fc
leaq 0x4b93(%rip), %rcx      # 0x62ef
cmovaeq %rcx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e0
incl %r14d
movq 0x20(%rsp), %rbp
je 0x1785
movq %r13, %rdi
movl %r15d, %esi
callq 0x3541
cmpq $-0x2, %rax
jb 0x1717
movq %r13, %rdi
movl %r15d, %esi
callq 0x35eb
cmpq $-0x2, %rax
jb 0x17af
subl $0x1, %r14d
jb 0x17b8
movq %r13, %rdi
movl %r15d, %esi
callq 0x34f7
cmpq $-0x4, %rax
jbe 0x1785
jmp 0x17b8
testl %r14d, %r14d
jne 0x1717
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1a43
leaq 0x40(%rsp), %r15
andq $0x0, (%r15)
andw $0x0, 0x8(%r15)
movl $0x400, %edi            # imm = 0x400
callq 0x1160
movq %rax, 0x10(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x365a
movzwl 0x8(%r15), %eax
movw %ax, 0x20(%rsp)
movq 0x8(%r14), %r13
leaq 0x640(%rsp), %r14
xorl %r12d, %r12d
xorl %r15d, %r15d
movq (%r13), %rbp
movq %rbp, %rax
shrq $0x3e, %rax
je 0x1873
testq %rbx, %rbp
jne 0x182a
leaq 0x8(%r13), %rsi
xorl %r15d, %r15d
jmp 0x183a
movzbl 0x8(%r13), %r15d
orq $-0x100, %r15
leaq 0x9(%r13), %rsi
movq %rbp, %rax
shrq $0x30, %rax
andl $0xfff, %eax            # imm = 0xFFF
cmpq $0x1, %r15
movl %eax, %ecx
adcw $-0x1, %cx
addl %eax, %r12d
movzwl %cx, %edx
subq %rdx, %r14
movq %r14, %rdi
callq 0x1150
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %rbp
subq %rbp, %r13
jmp 0x180f
movq %r15, 0x80(%rsp)
movw %r12w, 0x88(%rsp)
movq %r14, 0x90(%rsp)
movq %r14, 0x10(%rsp)
movups 0x80(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x4a57(%rip), %rdi      # 0x62fb
callq 0x1a69
xorl %ebx, %ebx
leaq 0x4881(%rip), %r14      # 0x6133
leaq 0x98(%rsp), %rbp
pushq $-0x1
popq %r13
leaq 0x40(%rsp), %r15
movq %rbx, %rax
incq %rbx
testq %rax, %rax
je 0x1918
movq %rbp, %rdi
movl %r13d, %esi
callq 0x35eb
movzwl 0x48(%rsp), %esi
cmpq $-0x2, %rax
jb 0x1909
subw $0x1, %si
movw %si, 0x48(%rsp)
jb 0x19ab
movq %rbp, %rdi
movl %r13d, %esi
callq 0x34f7
cmpq $-0x4, %rax
jbe 0x18ce
jmp 0x19ab
cmpw 0x20(%rsp), %si
jbe 0x19ab
decl %esi
jmp 0x1996
movq 0xa0(%rsp), %rax
movq (%rax), %rax
shrq $0x3e, %rax
cmpl $0x3, %eax
jb 0x1961
movzwl 0xb0(%rsp), %eax
decq %rax
cmpq %rax, 0xc0(%rsp)
jne 0x1961
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1a69
jmp 0x18c3
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3541
cmpq $-0x2, %rax
jb 0x1989
movq %rbp, %rdi
movl %r13d, %esi
callq 0x34f7
cmpq $-0x4, %rax
jbe 0x18c3
jmp 0x19ab
movzwl 0x48(%rsp), %esi
leal 0x1(%rsi), %eax
movw %ax, 0x48(%rsp)
movq 0xc8(%rsp), %rdx
movq %r15, %rdi
callq 0x1b92
jmp 0x1918
movq 0x50(%rsp), %rdi
callq 0x1030
movq 0x28(%rsp), %rbx
movq 0x60(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x4b39
movq 0x68(%rsp), %rdi
callq 0x4b39
movq 0x38(%rsp), %rdi
callq 0x4b39
movq 0x30(%rsp), %rdi
callq 0x4b39
xorl %eax, %eax
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rcx
shrq $0x28, %rcx
movzbl %cl, %ebx
shrq $0x30, %rax
andl $0xfff, %eax            # imm = 0xFFF
addq %rax, %r12
leaq (%r12,%rbx), %r14
leaq 0x492f(%rip), %rdi      # 0x634e
callq 0x1080
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x16de
movzbl (%r12,%r15), %edi
movq (%r14,%r15,8), %rsi
callq 0x1ace
incq %r15
jmp 0x1a27