// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/src/db.c
// Function: JFDB_write
JFDB *JFDB_write(JFDB *db, const JFT *tx, int flags) {
  // bump the level, merge all the levels up to this one
  // if we are at or beyond the file level: compact everything into a new file
  JFDB_CheckPoint *cp = &db->tip.cp;
  if (++cp->levels >= (1LLU << db->tip.level))
    return JFDB_compact(db, tx);

  // the caller can force a flush using the flags
  if (flags & JFDB_FLUSH)
    db->oughtFlush = True;
  db->isModified = True;

  // walk back N levels from prior - merge tries into a new trie
  JFT_Offset prior = cp->offset;
  JFT *trie = JFDB_get_trie(db, prior);
  JFT_Amount N = ffsll(cp->levels) - 1;
  JFT_Cursor cursors[N + 1];
  cursors[0] = JFT_cursor((JFT *)tx);
  for (int i = 1; i <= N; i++) {
    cursors[i] = JFT_cursor(trie);
    trie = JFDB_get_trie(db, JFT_parent_offset(trie));
  }

  // make the new trie
  JFT_Buffer *buf = db->scratch[0];
  JFT *next = JFT_cursor_merge_new(cursors, N + 1, buf, 0);
  if (!next)
    return JFDB_set_error(db, JFDB_ETRIE, "failed to merge keys");

  // set the parent: if we merged than that of the last trie, otherwise the prior
  JFT_set_parent_offset(next, N ? JFT_parent_offset(cursors[N].root) : prior);

  // as soon as we change the file size, our last checkpoint becomes stale
  if (ftruncate(db->kmap.fd, cp->lengthKeys + buf->mark + 1))
    return JFDB_set_error(db, JFDB_EFILE, "failed to truncate keys");

  if (pwrite(db->kmap.fd, buf->data, buf->mark, cp->lengthKeys) < buf->mark)
    return JFDB_set_error(db, JFDB_EFILE, "failed to append to keys");
  if (pwrite(db->kmap.fd, "\n", 1, cp->lengthKeys + buf->mark) < 1)
    return JFDB_set_error(db, JFDB_EFILE, "failed to append keys separator");

  cp->offset = cp->lengthKeys;
  cp->lengthKeys += buf->mark + 1;

  // remap to cover the newly appended trie
  JFDB_mmap_file(db, JFDB_KEYS, cp->lengthKeys, 2);
  if (JFDB_has_error(db))
    return db;

  // now mark everything that was changed as dirty
  JFDB_mark_dirty(db, tx, prior);
  if (JFDB_has_error(db))
    return db;

  // flush if we need to flush
  if (db->oughtFlush)
    JFDB_flush(db);
  return db;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %rbx
movq 0x120(%rdi), %rax
incq %rax
movq %rax, 0x120(%rdi)
movb 0xc8(%rdi), %cl
movq %rax, %rdi
shrq %cl, %rdi
testq %rdi, %rdi
je 0x24ad
movq %rbx, %rdi
callq 0x1d6d
jmp 0x289e
testb $0x1, %dl
je 0x24bc
movl $0x1, 0x984(%rbx)
movq %rsp, -0x60(%rbp)
movl $0x1, 0x980(%rbx)
movq 0x128(%rbx), %rcx
movq 0x98(%rbx), %r12
movq %rcx, -0x38(%rbp)
addq %rcx, %r12
bsfq %rax, %rax
pushq $-0x1
popq %r13
cmovneq %rax, %r13
leaq 0x1(%r13), %rax
movq %rax, -0x58(%rbp)
imulq $0x38, %r13, %rcx
movq %rcx, -0x48(%rbp)
leaq 0x47(%rcx), %r15
andq $-0x10, %r15
movq %rsp, %r14
movq %r14, -0x30(%rbp)
subq %r15, %r14
negq %r15
movq %r14, %rsp
leaq -0xd0(%rbp), %rdi
movq %rsi, -0x40(%rbp)
callq 0x28d7
leaq -0xd0(%rbp), %rcx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movq -0x30(%rbp), %rax
movups %xmm0, (%rax,%r15)
movq 0x30(%rcx), %rax
movq %rax, 0x30(%r14)
movups %xmm2, 0x20(%r14)
movups %xmm1, 0x10(%r14)
movq %r14, -0x30(%rbp)
leaq 0x38(%r14), %r15
movq %rcx, %r14
movq %r13, -0x50(%rbp)
subq $0x1, %r13
jb 0x25bc
movq %r14, %rdi
movq %r12, %rsi
callq 0x28d7
movq -0xa0(%rbp), %rax
movq %rax, 0x30(%r15)
movups -0xd0(%rbp), %xmm0
movups -0xc0(%rbp), %xmm1
movups -0xb0(%rbp), %xmm2
movups %xmm2, 0x20(%r15)
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movq (%r12), %r12
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r12
addq 0x98(%rbx), %r12
addq $0x38, %r15
jmp 0x255f
movq 0x988(%rbx), %r12
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq -0x58(%rbp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x3734
testq %rax, %rax
je 0x2641
movq -0x38(%rbp), %rcx
cmpq $0x0, -0x50(%rbp)
je 0x25f3
movq -0x48(%rbp), %rcx
movq (%r15,%rcx), %rcx
movq (%rcx), %rcx
movabsq $-0x10000000000, %rdx   # imm = 0xFFFFFF0000000000
andq (%rax), %rdx
movabsq $0xffffffffff, %rsi     # imm = 0xFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, (%rax)
movl 0x90(%rbx), %edi
movq 0x110(%rbx), %rax
movq 0x10(%r12), %rcx
leaq (%rax,%rcx), %rsi
incq %rsi
callq 0x10e0
testl %eax, %eax
je 0x264f
leaq 0x2af5(%rip), %rdx      # 0x5131
jmp 0x288d
leaq 0x2ad4(%rip), %rdx      # 0x511c
pushq $0x6
jmp 0x288f
movl 0x90(%rbx), %edi
movq 0x10(%r12), %rdx
movq 0x18(%r12), %rsi
movq 0x110(%rbx), %rcx
callq 0x1180
movq 0x10(%r12), %rcx
cmpq %rcx, %rax
jae 0x2681
leaq 0x2acd(%rip), %rdx      # 0x5149
jmp 0x288d
movl 0x90(%rbx), %edi
addq 0x110(%rbx), %rcx
leaq 0x298f(%rip), %rsi      # 0x5024
pushq $0x1
popq %rdx
callq 0x1180
testq %rax, %rax
jle 0x2886
movq 0x110(%rbx), %rax
movq %rax, 0x128(%rbx)
movq 0x10(%r12), %rcx
leaq (%rax,%rcx), %rdx
incq %rdx
movq %rdx, 0x110(%rbx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rcx
movq %rbx, %rdi
callq 0x1c70
cmpl $0x0, 0x88(%rbx)
jne 0x289a
movabsq $0x1000000000000000, %r15 # imm = 0x1000000000000000
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x28d7
leaq -0x140(%rbp), %r12
movq $-0x5, (%r12)
movw $0x1, 0x8(%r12)
leaq 0x998(%rbx), %rax
movq %rax, 0x10(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3312
leaq -0xf0(%rbp), %rcx
movq %r14, (%rcx)
movq %r12, 0x8(%rcx)
movzwl 0x8(%r12), %eax
movw %ax, 0x10(%rcx)
movl $0x1, 0x14(%rcx)
andq $0x0, 0x18(%rcx)
leaq -0x98(%rbp), %r14
leaq -0xf0(%rbp), %rdi
callq 0x2fb0
testq %rax, %rax
je 0x28b0
movq -0xe8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %r12
testq %r12, %r12
je 0x2751
addq 0x98(%rbx), %r12
leaq -0x128(%rbp), %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
callq 0x30f8
movq %r13, %rdi
movq -0x30(%rbp), %rsi
callq 0x3312
cmpq $-0x2, %rax
je 0x27c7
movq -0x120(%rbp), %rdi
movq (%rdi), %rax
movq %rax, %rcx
shrq $0x3e, %rcx
cmpl $0x3, %ecx
jb 0x27da
testq %r15, %rax
je 0x2863
movq (%r12), %r12
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r12
jmp 0x2775
andq $0x0, -0x78(%rbp)
andw $0x0, -0x70(%rbp)
movq 0x988(%rbx), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq %r13, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3312
movq %r13, -0x98(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
movzwl -0x70(%rbp), %eax
movw %ax, -0x88(%rbp)
movl $0x1, -0x84(%rbp)
andq $0x0, -0x80(%rbp)
movq %r14, %rdi
callq 0x2fb0
testq %rax, %rax
je 0x27c7
movq -0x120(%rbp), %rdi
movq (%rdi), %rax
testq %r15, %rax
jne 0x282b
orq %r15, %rax
movq %rax, (%rdi)
callq 0x2d28
testl %eax, %eax
je 0x282b
movq (%rdx), %rsi
movq %rbx, %rdi
callq 0x3071
jmp 0x282b
orq %r15, %rax
movq %rax, (%rdi)
callq 0x2d28
testl %eax, %eax
je 0x2751
movq (%rdx), %rsi
movq %rbx, %rdi
callq 0x3071
jmp 0x2751
leaq 0x28d5(%rip), %rdx      # 0x5162
pushq $0x4
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1af6
movq -0x60(%rbp), %rsp
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x88(%rbx)
jne 0x289a
cmpl $0x0, 0x984(%rbx)
je 0x289a
movq %rbx, %rdi
callq 0x224b
jmp 0x289a