// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, %r8
movq %rdi, %rbx
movq 0x120(%rdi), %rax
incq %rax
movq %rax, 0x120(%rdi)
movb 0xc8(%rdi), %cl
movq %rax, %rsi
shrq %cl, %rsi
testq %rsi, %rsi
je 0x2d65
movq %rbx, %rdi
movq %r8, %rsi
callq 0x2133
jmp 0x304a
testb $0x1, %dl
je 0x2d74
movl $0x1, 0x984(%rbx)
movq %rsp, -0x60(%rbp)
movl $0x1, 0x980(%rbx)
movq 0x98(%rbx), %r15
movq 0x128(%rbx), %rcx
movq %rcx, -0x38(%rbp)
bsfq %rax, %rax
movq $-0x1, %rcx
cmovneq %rax, %rcx
movq %rcx, -0x40(%rbp)
leaq 0x1(%rcx), %rax
movq %rax, -0x58(%rbp)
imulq $0x38, %rcx, %rcx
movq %rcx, -0x50(%rbp)
leaq 0x47(%rcx), %r14
andq $-0x10, %r14
movq %rsp, %r12
movq %r12, -0x30(%rbp)
subq %r14, %r12
movq %r12, %rsp
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x10(%r13)
movq $0x0, 0x30(%r13)
movq %r8, (%r13)
movq %r13, %rdi
movl $0x1, %esi
movq %r8, -0x48(%rbp)
movq %r8, %rdx
callq 0x39a0
negq %r14
movq 0x30(%r13), %rax
movq -0x30(%rbp), %rcx
movq %rax, 0x30(%rcx,%r14)
movaps (%r13), %xmm0
movaps 0x10(%r13), %xmm1
movaps 0x20(%r13), %xmm2
movups %xmm0, (%r12)
movups %xmm1, 0x10(%r12)
movq %r12, -0x30(%rbp)
movups %xmm2, 0x20(%r12)
cmpq $0x0, -0x40(%rbp)
je 0x2ed4
addq -0x38(%rbp), %r15
movq -0x30(%rbp), %rax
leaq 0x38(%rax), %r12
movq -0x40(%rbp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x70(%rbp)
movq %r15, -0xa0(%rbp)
movq %r13, %rdi
movl $0x1, %esi
movq %r15, %rdx
callq 0x39a0
movaps -0xa0(%rbp), %xmm0
movaps -0x90(%rbp), %xmm1
movaps -0x80(%rbp), %xmm2
movups %xmm0, (%r12)
movups %xmm1, 0x10(%r12)
movups %xmm2, 0x20(%r12)
movq -0x70(%rbp), %rax
movq %rax, 0x30(%r12)
movq (%r15), %r15
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r15
addq 0x98(%rbx), %r15
addq $0x38, %r12
decq %r14
jne 0x2e54
movabsq $0xffffffffff, %r14     # imm = 0xFFFFFFFFFF
movq 0x988(%rbx), %r15
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x45bf
testq %rax, %rax
je 0x2f59
movq -0x38(%rbp), %rcx
cmpq $0x0, -0x40(%rbp)
je 0x2f16
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx,%rcx), %rcx
movq (%rcx), %rcx
movabsq $-0x10000000000, %rdx   # imm = 0xFFFFFF0000000000
andq (%rax), %rdx
andq %r14, %rcx
orq %rdx, %rcx
movq %rcx, (%rax)
movl 0x90(%rbx), %edi
movq 0x110(%rbx), %rax
movq 0x10(%r15), %rcx
leaq (%rax,%rcx), %rsi
incq %rsi
callq 0x10e0
testl %eax, %eax
je 0x2f6d
leaq 0x41dd(%rip), %rdx      # 0x7131
jmp 0x3037
leaq 0x41bc(%rip), %rdx      # 0x711c
movq %rbx, %rdi
movl $0x6, %esi
jmp 0x303f
movl 0x90(%rbx), %edi
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rsi
movq 0x110(%rbx), %rcx
callq 0x1180
movq 0x10(%r15), %rcx
cmpq %rcx, %rax
jae 0x2f9c
leaq 0x41b2(%rip), %rdx      # 0x7149
jmp 0x3037
movl 0x90(%rbx), %edi
addq 0x110(%rbx), %rcx
leaq 0x4074(%rip), %rsi      # 0x7024
movl $0x1, %edx
callq 0x1180
testq %rax, %rax
jle 0x3030
movq 0x110(%rbx), %rax
movq %rax, 0x128(%rbx)
movq 0x10(%r15), %rcx
leaq (%rax,%rcx), %rdx
incq %rdx
movq %rdx, 0x110(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x2, %ecx
callq 0x202f
testq %rbx, %rbx
je 0x3046
cmpl $0x0, 0x88(%rbx)
jne 0x3046
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x3067
testq %rbx, %rbx
je 0x3046
cmpl $0x0, 0x88(%rbx)
jne 0x3046
cmpl $0x0, 0x984(%rbx)
je 0x3046
movq %rbx, %rdi
callq 0x28e5
jmp 0x3046
leaq 0x412b(%rip), %rdx      # 0x7162
movq %rbx, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x1eb0
movq -0x60(%rbp), %rsp
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq