// File: /workspace/github_c_makefile_star2_O3/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 $0x878, %rsp            # imm = 0x878
movq %rdi, %r12
movzbl 0xc8(%rdi), %eax
imull $0x38, %eax, %eax
movq %rsp, %rbx
addl $0x47, %eax
andl $-0x10, %eax
subq %rax, %rbx
movq %rbx, %rsp
testq %rsi, %rsi
je 0x1e29
movq %rsi, %rdx
xorps %xmm0, %xmm0
leaq -0x490(%rbp), %r15
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x8(%r15)
movq %rsi, (%r15)
movq %r15, %rdi
movl $0x1, %esi
callq 0x35c4
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm0, (%rbx)
movups %xmm1, 0x10(%rbx)
movups %xmm2, 0x20(%rbx)
movq 0x30(%r15), %rax
movq %rax, 0x30(%rbx)
movl $0x1, %r15d
jmp 0x1e2c
xorl %r15d, %r15d
movq 0x128(%r12), %r13
testq %r13, %r13
movq %rbx, -0x30(%rbp)
je 0x1ed3
imulq $0x38, %r15, %r14
addq %rbx, %r14
movq %r12, %rbx
movq 0x98(%r12), %r12
leaq (%r12,%r13), %rdx
incq %r15
leaq -0x488(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rdx, -0x490(%rbp)
leaq -0x490(%rbp), %rdi
movl $0x1, %esi
callq 0x35c4
movups -0x490(%rbp), %xmm0
movups -0x480(%rbp), %xmm1
movups -0x470(%rbp), %xmm2
movups %xmm0, (%r14)
movups %xmm1, 0x10(%r14)
movups %xmm2, 0x20(%r14)
movq -0x460(%rbp), %rax
movq %rax, 0x30(%r14)
addq $0x38, %r14
movq (%r12,%r13), %r13
movq %rbx, %r12
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r13
jne 0x1e48
testq %r15, %r15
je 0x2294
movq 0x988(%r12), %rbx
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x4149
movq %rax, -0x48(%rbp)
testq %rax, %rax
je 0x2145
movq %rbx, -0x50(%rbp)
movl $0xff, %r14d
addq 0x118(%r12), %r14
shrq $0x8, %r14
leal 0x6(%r14), %eax
movl %eax, -0x3c(%rbp)
movl %eax, %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 0x2151
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 0x1f85
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, -0x58(%rbp)
xorps %xmm0, %xmm0
leaq -0x490(%rbp), %r14
movups %xmm0, 0x28(%r14)
movups %xmm0, 0x18(%r14)
movups %xmm0, 0x8(%r14)
movq -0x48(%rbp), %rdx
movq %rdx, (%r14)
movq %r14, %rdi
movl $0x1, %esi
callq 0x35c4
leaq -0x78(%rbp), %r15
movq $-0x5, (%r15)
movw $0x1, 0x8(%r15)
leaq 0x998(%rbx), %rax
movq %rax, 0x10(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3af3
leaq -0x8a0(%rbp), %rdi
movq %r14, (%rdi)
movq %r15, 0x8(%rdi)
movzwl 0x8(%r15), %eax
movw %ax, 0x10(%rdi)
movl $0x1, 0x14(%rdi)
movq $0x0, 0x18(%rdi)
callq 0x3421
testq %rax, %rax
je 0x2114
movq %r12, %rax
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movl $0xff, %r14d
leaq -0x8a0(%rbp), %r13
movq -0x488(%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 0x2069
movl (%rcx), %eax
addq $0x4, %rcx
testl %eax, %eax
je 0x2103
movq (%rcx), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x2103
addl $0xff, %eax
shrl $0x8, %eax
movl %ecx, %edx
leaq (%rdx,%rax), %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 0x20be
notb %r15b
movl %r15d, %ecx
shlq %cl, %rax
shrq %cl, %rax
movq %rdx, %rbx
jmp 0x20fb
movq -0x30(%rbp), %rcx
orq %rax, (%rcx,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %rbx, %rax
jae 0x20eb
movq -0x60(%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)
movq %r13, %rdi
callq 0x3421
testq %rax, %rax
jne 0x2039
xorps %xmm0, %xmm0
movq -0x58(%rbp), %r15
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
cmpl $0x8, -0x3c(%rbp)
jae 0x216f
movq %r12, %rdi
callq 0x1030
movq -0x38(%rbp), %r12
jmp 0x2273
leaq 0x3fd0(%rip), %rdx      # 0x611c
jmp 0x2285
leaq 0x4212(%rip), %rdx      # 0x636a
movq %r13, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1b19
movq %r13, %r12
jmp 0x2273
movq -0x38(%rbp), %rax
leaq 0xd8(%rax), %rdx
xorl %r8d, %r8d
xorl %esi, %esi
movq %r15, %rdi
xorl %r10d, %r10d
movq -0x30(%rbp), %r14
movq (%r14,%rsi,8), %r9
cmpq $-0x1, %r9
je 0x223c
testq %r9, %r9
je 0x2238
bsfq %r9, %rcx
cmpq $0x3f, %rcx
je 0x223c
incl %ecx
movl %esi, %r11d
shll $0x6, %r11d
movl %ecx, %ebx
testl %ebx, %ebx
je 0x2235
addl %r10d, %ebx
shll $0x8, %ebx
addl $0xffffff00, %ebx       # imm = 0xFFFFFF00
cmpl 0x4(%rdi), %ebx
jbe 0x21ff
movl %ebx, 0x4(%rdi)
addl (%r12), %r8d
movl %r8d, (%rdi)
movl $0x7, %eax
movq %rdx, %r8
movq %r15, %r10
movl 0x4(%r8), %ebx
movq %r8, %rdi
cmpl 0x4(%r10), %ebx
jb 0x21f3
movq %r10, %rdi
addq $0x8, %r8
movq %rdi, %r10
decq %rax
jne 0x21e3
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, %rbx
incl %ebx
xorl %r10d, %r10d
testq %r8, %r8
cmovel %r10d, %ebx
leal (%rax,%r11), %r8d
addl %ebx, %eax
movl %eax, %ecx
cmpl $0x40, %eax
jl 0x21b9
jmp 0x223c
subl %ecx, %r10d
addl $0x40, %r10d
incq %rsi
movl 0x4(%r12), %eax
cmpq %rax, %rsi
jb 0x2189
shll $0x8, %r10d
cmpl 0x4(%rdi), %r10d
jbe 0x2262
movl %r10d, 0x4(%rdi)
addl (%r12), %r8d
movl %r8d, (%rdi)
movq %r12, %rdi
callq 0x1030
movq -0x38(%rbp), %r12
testq %r12, %r12
je 0x227e
cmpl $0x0, 0x88(%r12)
je 0x22a6
leaq 0x3f9d(%rip), %rdx      # 0x6222
movq %r12, %rdi
movl $0x6, %esi
xorl %eax, %eax
callq 0x1b19
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 0x290c
movq (%r12), %rcx
leaq 0x3dff(%rip), %rdx      # 0x60bd
leaq -0x8a0(%rbp), %rdi
movl $0x406, %esi            # imm = 0x406
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rcx
leaq 0x3f5f(%rip), %rdx      # 0x623b
leaq -0x490(%rbp), %r14
movl $0x40a, %esi            # imm = 0x40A
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
movq %r12, %rdi
movq %r14, %rsi
callq 0x1bb0
movl %eax, 0x90(%r12)
cmpl $0x0, 0x88(%r12)
jne 0x2294
leaq 0xc0(%r12), %rsi
movq -0x48(%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 -0x50(%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 0x2388
leaq 0x3ec4(%rip), %rdx      # 0x6247
jmp 0x2417
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 0x23b4
leaq 0x3eb9(%rip), %rdx      # 0x626b
jmp 0x2417
movl 0x90(%r12), %edi
addq $0x70, %rcx
leaq 0x3c5d(%rip), %rsi      # 0x6024
movl $0x1, %edx
callq 0x1180
testq %rax, %rax
jle 0x23f0
movl 0x90(%r12), %edi
callq 0x1050
testl %eax, %eax
je 0x23f9
leaq 0x3e9a(%rip), %rdx      # 0x6288
jmp 0x2417
leaq 0x3d6b(%rip), %rdx      # 0x6162
jmp 0x2417
leaq -0x490(%rbp), %rdi
leaq -0x8a0(%rbp), %rsi
callq 0x11b0
testl %eax, %eax
je 0x2424
leaq 0x3e8e(%rip), %rdx      # 0x62a5
movq %r12, %rdi
movl $0x4, %esi
jmp 0x228d
movq 0x110(%r12), %rdx
movq %r12, %rdi
movl $0x1, %esi
movl $0x1, %ecx
callq 0x1c9b
cmpl $0x0, 0x88(%r12)
jne 0x2294
xorl %eax, %eax
movq %rax, 0x138(%r12)
movq %rax, 0x980(%r12)
jmp 0x2294