// File: /workspace/github_c_makefile_star2_O1/jflatow[P]jfdb/src/db.c
// Function: JFDB_compact
JFDB *JFDB_compact(JFDB *db, const JFT *tx) {
  JFDB_CheckPoint *cp = &db->tip.cp;

  // merge all tries into a new trie - at this level there should be at most N
  // allow one additional trie (as argument), in case we are performing a write
  JFT_Amount N = db->tip.level, i = 0;
  JFT_Cursor cursors[N + 1];
  if (tx)
    cursors[i++] = JFT_cursor((JFT *)tx);

  // walk backwards from the latest checkpoint for as far as we can, one cursor per trie
  JFT *trie;
  for (JFT_Offset pos = db->tip.cp.offset; pos; pos = JFT_parent_offset(trie)) {
    trie = JFDB_get_trie(db, pos);
    cursors[i++] = JFT_cursor(trie);
  }

  // if there's nothing to merge (empty db), we are done
  if (i == 0)
    return db;

  JFT_Buffer *buf = db->scratch[0];
  JFT *next = JFT_cursor_merge_new(cursors, i, buf, 0);

  if (!next)
    return JFDB_set_error(db, JFDB_ETRIE, "failed to merge keys");

  JFDB_calculate_gaps(db, next);
  if (JFDB_has_error(db))
    return JFDB_set_error(db, JFDB_ETRIE, "failed to calculate gaps");

  // release the old file
  JFDB_mmap_release(db, JFDB_KEYS);

  // produce the new file
  char fp[JFDB_MAX_PATH + 6];
  char tp[JFDB_MAX_PATH + 10];
  snprintf(fp, sizeof(fp), "%s.keys", db->path);
  snprintf(tp, sizeof(tp), "%s.keys.tmp", db->path);
  db->kmap.fd = JFDB_file_open(db, tp);
  if (JFDB_has_error(db))
    return db;

  // write the new header: base the level on the # primary keys
  db->tip.level = flsll(((JFT_Root *)next)->numPrimary);
  cp->lengthKeys = sizeof(JFDB_Header) + buf->mark + 1;
  cp->levels = 1;
  cp->offset = sizeof(JFDB_Header);
  if (pwrite(db->kmap.fd, &db->tip, sizeof(JFDB_Header), 0) < sizeof(JFDB_Header))
    return JFDB_set_error(db, JFDB_EFILE, "failed to write compact keys header");

  // write the trie, sync to disk, rename the file
  if (pwrite(db->kmap.fd, buf->data, buf->mark, sizeof(JFDB_Header)) < buf->mark)
    return JFDB_set_error(db, JFDB_EFILE, "failed to write compact keys");
  if (pwrite(db->kmap.fd, "\n", 1, sizeof(JFDB_Header) + buf->mark) < 1)
    return JFDB_set_error(db, JFDB_EFILE, "failed to append keys separator");
  if (JFDB_fsync(db->kmap.fd))
    return JFDB_set_error(db, JFDB_EFILE, "failed to fsync compact keys");
  if (rename(tp, fp))
    return JFDB_set_error(db, JFDB_EFILE, "failed to rename compact keys");

  // map the new file
  JFDB_mmap_file(db, JFDB_KEYS, cp->lengthKeys, 1);
  if (JFDB_has_error(db))
    return db;

  // clear the between flush state
  db->freeList.size = 0;
  db->isModified = False;
  db->oughtFlush = False;

  // XXX: if we wanted to release some memory we could shrink buf (flag on open?)

  return db;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
movq %rdi, %r12
movzbl 0xc8(%rdi), %eax
imull $0x38, %eax, %eax
movq %rsp, %r14
addl $0x47, %eax
andl $-0x10, %eax
subq %rax, %r14
movq %r14, %rsp
testq %rsi, %rsi
je 0x21c7
movq %rsi, %rdx
xorps %xmm0, %xmm0
leaq -0x480(%rbp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movq $0x0, 0x30(%r15)
movq %rsi, (%r15)
movq %r15, %rdi
movl $0x1, %esi
callq 0x39a0
movaps (%r15), %xmm0
movaps 0x10(%r15), %xmm1
movaps 0x20(%r15), %xmm2
movups %xmm0, (%r14)
movups %xmm1, 0x10(%r14)
movups %xmm2, 0x20(%r14)
movq 0x30(%r15), %rax
movq %rax, 0x30(%r14)
movl $0x1, %r15d
jmp 0x21ca
xorl %r15d, %r15d
movq 0x128(%r12), %r13
testq %r13, %r13
movq %r14, -0x30(%rbp)
je 0x227c
imulq $0x38, %r15, %rbx
addq %r14, %rbx
movq %r12, %r14
movq 0x98(%r12), %r12
leaq (%r12,%r13), %rdx
incq %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x480(%rbp)
movaps %xmm0, -0x460(%rbp)
movaps %xmm0, -0x470(%rbp)
movq $0x0, -0x450(%rbp)
movq %rdx, -0x480(%rbp)
leaq -0x480(%rbp), %rdi
movl $0x1, %esi
callq 0x39a0
movaps -0x480(%rbp), %xmm0
movaps -0x470(%rbp), %xmm1
movaps -0x460(%rbp), %xmm2
movups %xmm0, (%rbx)
movups %xmm1, 0x10(%rbx)
movups %xmm2, 0x20(%rbx)
movq -0x450(%rbp), %rax
movq %rax, 0x30(%rbx)
addq $0x38, %rbx
movq (%r12,%r13), %r13
movq %r14, %r12
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r13
jne 0x21e6
testq %r15, %r15
je 0x264a
movq 0x988(%r12), %rbx
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x45bf
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x25dd
movq %rbx, -0x48(%rbp)
movl $0xff, %r14d
addq 0x118(%r12), %r14
shrq $0x8, %r14
leal 0x6(%r14), %ebx
shrl $0x3, %ebx
addl $0x7, %ebx
movl %ebx, %r15d
andl $-0x8, %r15d
leaq 0x8(%r15), %rdi
callq 0x1150
movq %r12, %r13
testq %rax, %rax
je 0x25e6
movq %rax, %r12
movq %r13, -0x38(%rbp)
shrl $0x3, %ebx
movl $0x0, (%rax)
movl %ebx, 0x4(%rax)
leaq 0x8(%rax), %r13
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x10f0
orb $0x1, 0x8(%r12)
testb $0x3f, %r14b
je 0x2329
movq $-0x1, %rax
movl %r14d, %ecx
shlq %cl, %rax
orq %rax, -0x8(%r13,%rbx,8)
movq %r13, -0x30(%rbp)
movq -0x38(%rbp), %rbx
leaq 0xd0(%rbx), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
leaq -0x480(%rbp), %r14
movaps %xmm0, (%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
xorl %r13d, %r13d
movq %r13, 0x30(%r14)
movq -0x40(%rbp), %rdx
movq %rdx, (%r14)
movq %r14, %rdi
movl $0x1, %esi
callq 0x39a0
leaq -0x70(%rbp), %r15
movq $-0x5, (%r15)
movw $0x1, 0x8(%r15)
leaq 0x998(%rbx), %rax
movq %rax, 0x10(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3f07
leaq -0x890(%rbp), %rdi
movq %r14, (%rdi)
movq %r15, 0x8(%rdi)
movzwl 0x8(%r15), %eax
movw %ax, 0x10(%rdi)
movl $0x1, 0x14(%rdi)
movq %r13, 0x18(%rdi)
callq 0x37f8
testq %rax, %rax
je 0x24c3
movq %r12, %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movl $0xff, %r14d
leaq -0x890(%rbp), %rbx
movq -0x478(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, %rax
shrq $0x28, %rax
andl %r14d, %eax
shrq $0x30, %rdx
andl $0xfff, %edx            # imm = 0xFFF
addq %rdx, %rcx
addq $0x8, %rcx
cmpl %r14d, %eax
jne 0x240f
movl (%rcx), %eax
addq $0x4, %rcx
testl %eax, %eax
je 0x24b2
movq (%rcx), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x24b2
addl $0xff, %eax
subl (%r12), %ecx
shrl $0x8, %eax
leaq (%rax,%rcx), %r15
decq %r15
movl %ecx, %edx
shrl $0x6, %edx
movq %r15, %rbx
shrq $0x6, %rbx
movq $-0x1, %rax
shlq %cl, %rax
cmpq %rbx, %rdx
jne 0x2466
notb %r15b
movl %r15d, %ecx
shlq %cl, %rax
shrq %cl, %rax
movq %rdx, %rbx
jmp 0x24a3
movq -0x30(%rbp), %rcx
orq %rax, (%rcx,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %rbx, %rax
jae 0x2493
movq -0x58(%rbp), %rax
leaq (%rax,%rdx,8), %rdi
notq %rdx
addq %rbx, %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x10f0
notb %r15b
movq $-0x1, %rax
movl %r15d, %ecx
shrq %cl, %rax
movq -0x30(%rbp), %rcx
orq %rax, (%rcx,%rbx,8)
leaq -0x890(%rbp), %rbx
movq %rbx, %rdi
callq 0x37f8
testq %rax, %rax
jne 0x23df
movq -0x50(%rbp), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
cmpl $0x0, 0x4(%r12)
je 0x2601
movq -0x38(%rbp), %rax
leaq 0xd8(%rax), %rsi
xorl %r8d, %r8d
xorl %edi, %edi
movq %r15, %rdx
xorl %r13d, %r13d
movq -0x30(%rbp), %r14
movq (%r14,%rdi,8), %r9
cmpq $-0x1, %r9
je 0x25c6
testq %r9, %r9
je 0x25c2
bsfq %r9, %rcx
cmpq $0x3f, %rcx
je 0x25c6
incl %ecx
movl %edi, %r10d
shll $0x6, %r10d
movl %ecx, %r11d
testl %r11d, %r11d
je 0x25bf
addl %r13d, %r11d
shll $0x8, %r11d
addl $0xffffff00, %r11d      # imm = 0xFFFFFF00
cmpl 0x4(%rdx), %r11d
jbe 0x2583
movl %r11d, 0x4(%rdx)
addl (%r12), %r8d
movl %r8d, (%rdx)
movl $0x7, %eax
movq %rsi, %r8
movq %r15, %r11
movl 0x4(%r8), %ebx
movq %r8, %rdx
cmpl 0x4(%r11), %ebx
jb 0x2577
movq %r11, %rdx
addq $0x8, %r8
movq %rdx, %r11
decq %rax
jne 0x2567
movq %r9, %rax
shrq %cl, %rax
notq %rax
bsfq %rax, %rax
addl %ecx, %eax
movq %r9, %r8
movl %eax, %ecx
shrq %cl, %r8
bsfq %r8, %r11
incl %r11d
xorl %r13d, %r13d
testq %r8, %r8
cmovel %r13d, %r11d
leal (%rax,%r10), %r8d
addl %r11d, %eax
movl %eax, %ecx
cmpl $0x40, %eax
jl 0x2534
jmp 0x25c6
subl %ecx, %r13d
addl $0x40, %r13d
incq %rdi
movl 0x4(%r12), %eax
cmpq %rax, %rdi
jb 0x2503
shll $0x8, %r13d
jmp 0x2607
leaq 0x4b38(%rip), %rdx      # 0x711c
jmp 0x263b
leaq 0x4d7d(%rip), %rdx      # 0x736a
movq %r13, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1eb0
movq %r13, %r12
jmp 0x2624
movq %r15, %rdx
xorl %r8d, %r8d
cmpl 0x4(%rdx), %r13d
jbe 0x2618
movl %r13d, 0x4(%rdx)
addl (%r12), %r8d
movl %r8d, (%rdx)
movq %r12, %rdi
callq 0x1030
movq -0x38(%rbp), %r12
testq %r12, %r12
je 0x2634
cmpl $0x0, 0x88(%r12)
je 0x265c
leaq 0x4be7(%rip), %rdx      # 0x7222
movq %r12, %rdi
movl $0x6, %esi
xorl %eax, %eax
callq 0x1eb0
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movl $0x1, %esi
callq 0x2cd0
movq (%r12), %rcx
leaq 0x4a49(%rip), %rdx      # 0x70bd
leaq -0x890(%rbp), %rdi
movl $0x406, %esi            # imm = 0x406
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rcx
leaq 0x4ba9(%rip), %rdx      # 0x723b
leaq -0x480(%rbp), %r14
movl $0x40a, %esi            # imm = 0x40A
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
movq %r12, %rdi
movq %r14, %rsi
callq 0x1f47
movl %eax, 0x90(%r12)
testq %r12, %r12
je 0x264a
cmpl $0x0, 0x88(%r12)
jne 0x264a
leaq 0xc0(%r12), %rsi
movq -0x40(%rbp), %rcx
movl 0x20(%rcx), %ecx
bsrl %ecx, %edx
xorl $0x1f, %edx
movb $0x20, %dil
subb %dl, %dil
testl %ecx, %ecx
movzbl %dil, %edx
cmovel %ecx, %edx
movb %dl, 0xc8(%r12)
movq -0x48(%rbp), %rbx
movq 0x10(%rbx), %rcx
addq $0x71, %rcx
movq %rcx, 0x110(%r12)
movq $0x1, 0x120(%r12)
movq $0x70, 0x128(%r12)
movl $0x70, %edx
movl %eax, %edi
xorl %ecx, %ecx
callq 0x1180
cmpq $0x6f, %rax
ja 0x2747
leaq 0x4b05(%rip), %rdx      # 0x7247
jmp 0x27d6
movl 0x90(%r12), %edi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
movl $0x70, %ecx
callq 0x1180
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
jae 0x2773
leaq 0x4afa(%rip), %rdx      # 0x726b
jmp 0x27d6
movl 0x90(%r12), %edi
addq $0x70, %rcx
leaq 0x489e(%rip), %rsi      # 0x7024
movl $0x1, %edx
callq 0x1180
testq %rax, %rax
jle 0x27af
movl 0x90(%r12), %edi
callq 0x1050
testl %eax, %eax
je 0x27b8
leaq 0x4adb(%rip), %rdx      # 0x7288
jmp 0x27d6
leaq 0x49ac(%rip), %rdx      # 0x7162
jmp 0x27d6
leaq -0x480(%rbp), %rdi
leaq -0x890(%rbp), %rsi
callq 0x11b0
testl %eax, %eax
je 0x27e3
leaq 0x4acf(%rip), %rdx      # 0x72a5
movq %r12, %rdi
movl $0x4, %esi
jmp 0x2643
movq 0x110(%r12), %rdx
movq %r12, %rdi
movl $0x1, %esi
movl $0x1, %ecx
callq 0x202f
testq %r12, %r12
je 0x264a
cmpl $0x0, 0x88(%r12)
jne 0x264a
xorl %eax, %eax
movq %rax, 0x138(%r12)
movq %rax, 0x980(%r12)
jmp 0x264a