// File: /workspace/github_c_makefile_star2_O2/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 $0x858, %rsp            # imm = 0x858
movq %rdi, %rbx
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 0x1ddb
leaq -0x470(%rbp), %r15
movq %r15, %rdi
callq 0x28d7
movq 0x30(%r15), %rax
movq %rax, 0x30(%r14)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%r14)
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
pushq $0x1
popq %r12
jmp 0x1dde
xorl %r12d, %r12d
movq %r14, %rcx
leaq 0xc0(%rbx), %rax
movq %rax, -0x48(%rbp)
movq %rbx, %r14
movq 0x128(%rbx), %r13
imulq $0x38, %r12, %rbx
movq %rcx, -0x30(%rbp)
addq %rcx, %rbx
testq %r13, %r13
je 0x1e62
movq 0x98(%r14), %r15
leaq (%r15,%r13), %rsi
incq %r12
leaq -0x470(%rbp), %rdi
callq 0x28d7
movq -0x440(%rbp), %rax
movq %rax, 0x30(%rbx)
movups -0x470(%rbp), %xmm0
movups -0x460(%rbp), %xmm1
movups -0x450(%rbp), %xmm2
movups %xmm2, 0x20(%rbx)
movups %xmm1, 0x10(%rbx)
movups %xmm0, (%rbx)
movq (%r15,%r13), %r13
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r13
addq $0x38, %rbx
jmp 0x1e01
testq %r12, %r12
movq %r14, %rbx
je 0x2018
movq 0x988(%rbx), %r14
movq -0x30(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x3734
testq %rax, %rax
je 0x1fde
movq %rax, %r15
movq %r14, -0x38(%rbp)
movl $0xff, %esi
addq 0x118(%rbx), %rsi
shrq $0x8, %rsi
pushq $0x1
popq %rdi
xorl %edx, %edx
callq 0x2b1b
testq %rax, %rax
je 0x1fe7
movq %rax, %r13
leaq 0xd0(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq -0x470(%rbp), %r14
movq %r14, %rdi
movq %r15, -0x30(%rbp)
movq %r15, %rsi
callq 0x28d7
leaq -0x60(%rbp), %r12
movq $-0x5, (%r12)
movw $0x1, 0x8(%r12)
leaq 0x998(%rbx), %rax
movq %rax, 0x10(%r12)
movq %r14, %rdi
movq %r14, %r15
movq %r12, %rsi
callq 0x3312
leaq -0x880(%rbp), %r14
movq %r15, (%r14)
movq %r12, 0x8(%r14)
movzwl 0x8(%r12), %eax
movw %ax, 0x10(%r14)
movl $0x1, 0x14(%r14)
andq $0x0, 0x18(%r14)
pushq $-0x1
popq %r15
movq %r14, %rdi
callq 0x2fb0
testq %rax, %rax
je 0x1fbf
movq -0x468(%rbp), %rdi
callq 0x2d28
testl %eax, %eax
je 0x1f35
movq (%rdx), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x1f35
addl $0xff, %eax
subl (%r13), %ecx
shrl $0x8, %eax
addq %rcx, %rax
decq %rax
movl %ecx, %edx
shrl $0x6, %edx
movq %rax, %rsi
shrq $0x6, %rsi
movq %r15, %rdi
shlq %cl, %rdi
cmpq %rsi, %rdx
jne 0x1f93
notb %al
movl %eax, %ecx
shlq %cl, %rdi
shrq %cl, %rdi
jmp 0x1fb5
orq %rdi, 0x8(%r13,%rdx,8)
incq %rdx
cmpq %rsi, %rdx
jae 0x1fa8
orq $-0x1, 0x8(%r13,%rdx,8)
jmp 0x1f98
notb %al
movq %r15, %rdi
movl %eax, %ecx
shrq %cl, %rdi
movq %rsi, %rdx
orq %rdi, 0x8(%r13,%rdx,8)
jmp 0x1f35
movq %r13, %rdi
movq -0x40(%rbp), %rsi
callq 0x2bba
movq %r13, %rdi
callq 0x1030
testq %rbx, %rbx
movq -0x30(%rbp), %r15
jne 0x1ffb
jmp 0x2004
leaq 0x3137(%rip), %rdx      # 0x511c
jmp 0x200b
leaq 0x337c(%rip), %rdx      # 0x536a
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1af6
cmpl $0x0, 0x88(%rbx)
je 0x202a
leaq 0x3217(%rip), %rdx      # 0x5222
pushq $0x6
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1af6
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x23f2
movq (%rbx), %rcx
leaq 0x307e(%rip), %rdx      # 0x50bd
leaq -0x880(%rbp), %rdi
movl $0x406, %esi            # imm = 0x406
xorl %eax, %eax
callq 0x10d0
movq (%rbx), %rcx
leaq 0x31df(%rip), %rdx      # 0x523b
leaq -0x470(%rbp), %r14
movl $0x40a, %esi            # imm = 0x40A
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b8d
movl %eax, 0x90(%rbx)
cmpl $0x0, 0x88(%rbx)
jne 0x2018
movl 0x20(%r15), %ecx
bsrl %ecx, %edx
xorl $0x1f, %edx
movb $0x20, %sil
subb %dl, %sil
testl %ecx, %ecx
movzbl %sil, %edx
cmovel %ecx, %edx
movb %dl, 0xc8(%rbx)
movq -0x38(%rbp), %r14
movq 0x10(%r14), %rcx
addq $0x71, %rcx
movq %rcx, 0x110(%rbx)
movq $0x1, 0x120(%rbx)
movq $0x70, 0x128(%rbx)
pushq $0x70
popq %rdx
movl %eax, %edi
movq -0x48(%rbp), %rsi
xorl %ecx, %ecx
callq 0x1180
cmpq $0x6f, %rax
ja 0x20f6
leaq 0x3156(%rip), %rdx      # 0x5247
jmp 0x217b
movl 0x90(%rbx), %edi
movq 0x10(%r14), %rdx
movq 0x18(%r14), %rsi
pushq $0x70
popq %rcx
callq 0x1180
movq 0x10(%r14), %rcx
cmpq %rcx, %rax
jae 0x211e
leaq 0x314f(%rip), %rdx      # 0x526b
jmp 0x217b
movl 0x90(%rbx), %edi
addq $0x70, %rcx
leaq 0x2ef5(%rip), %rsi      # 0x5024
pushq $0x1
popq %rdx
callq 0x1180
testq %rax, %rax
jle 0x2154
movl 0x90(%rbx), %edi
callq 0x1050
testl %eax, %eax
je 0x215d
leaq 0x3136(%rip), %rdx      # 0x5288
jmp 0x217b
leaq 0x3007(%rip), %rdx      # 0x5162
jmp 0x217b
leaq -0x470(%rbp), %rdi
leaq -0x880(%rbp), %rsi
callq 0x11b0
testl %eax, %eax
je 0x2182
leaq 0x312a(%rip), %rdx      # 0x52a5
pushq $0x4
jmp 0x200d
movq 0x110(%rbx), %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movl %ecx, %esi
callq 0x1c70
cmpl $0x0, 0x88(%rbx)
jne 0x2018
andq $0x0, 0x138(%rbx)
andq $0x0, 0x980(%rbx)
jmp 0x2018