// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq 0x120(%rdi), %rax
incq %rax
movq %rax, 0x120(%rdi)
movb 0xc8(%rdi), %cl
movq %rax, %rsi
shrq %cl, %rsi
testq %rsi, %rsi
je 0x29a1
movq %r15, %rdi
movq %r8, %rsi
callq 0x1d9f
jmp 0x2cb1
testb $0x1, %dl
je 0x29b1
movl $0x1, 0x984(%r15)
movq %rsp, -0x68(%rbp)
movl $0x1, 0x980(%r15)
movq 0x98(%r15), %r12
movq 0x128(%r15), %rcx
movq %rcx, -0x38(%rbp)
bsfq %rax, %rax
movq $-0x1, %r14
cmovneq %rax, %r14
leaq 0x1(%r14), %rax
movq %rax, -0x48(%rbp)
imulq $0x38, %r14, %rcx
movq %rcx, -0x60(%rbp)
leaq 0x47(%rcx), %rbx
andq $-0x10, %rbx
movq %rsp, %r13
movq %r13, -0x30(%rbp)
subq %rbx, %r13
movq %r13, %rsp
xorps %xmm0, %xmm0
movq %r15, -0x40(%rbp)
leaq -0xa0(%rbp), %r15
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x8(%r15)
movq %r8, (%r15)
movq %r15, %rdi
movl $0x1, %esi
movq %r8, -0x58(%rbp)
movq %r8, %rdx
callq 0x35c4
negq %rbx
movq 0x30(%r15), %rax
movq -0x30(%rbp), %rcx
movq %rax, 0x30(%rcx,%rbx)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm0, (%r13)
movups %xmm1, 0x10(%r13)
movups %xmm2, 0x20(%r13)
testq %r14, %r14
je 0x2b48
movq %r14, %rbx
addq -0x38(%rbp), %r12
leaq -0x98(%rbp), %r14
movq -0x40(%rbp), %r15
movq %r13, -0x30(%rbp)
addq $0x38, %r13
movq %rbx, -0x50(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r14)
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
movq %r12, -0xa0(%rbp)
leaq -0xa0(%rbp), %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0x35c4
movups -0xa0(%rbp), %xmm0
movups -0x90(%rbp), %xmm1
movups -0x80(%rbp), %xmm2
movups %xmm0, (%r13)
movups %xmm1, 0x10(%r13)
movups %xmm2, 0x20(%r13)
movq -0x70(%rbp), %rax
movq %rax, 0x30(%r13)
movq (%r12), %r12
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %r12
addq 0x98(%r15), %r12
addq $0x38, %r13
decq %rbx
jne 0x2a8e
movq 0x988(%r15), %r12
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x4149
testq %rax, %rax
movq -0x38(%rbp), %rbx
movabsq $0xffffffffff, %rsi     # imm = 0xFFFFFFFFFF
je 0x2b7a
cmpq $0x0, -0x50(%rbp)
je 0x2b8e
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx,%rcx), %rcx
movq (%rcx), %rcx
jmp 0x2b91
movq -0x40(%rbp), %r15
movq 0x988(%r15), %r12
movq %r13, %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x4149
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
testq %rax, %rax
movabsq $0xffffffffff, %rsi     # imm = 0xFFFFFFFFFF
jne 0x2b91
leaq 0x359b(%rip), %rdx      # 0x611c
movq %r15, %rdi
movl $0x6, %esi
jmp 0x2ca6
movq %rbx, %rcx
movabsq $-0x10000000000, %rdx   # imm = 0xFFFFFF0000000000
andq (%rax), %rdx
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, (%rax)
movl 0x90(%r15), %edi
movq 0x110(%r15), %rax
movq 0x10(%r12), %rcx
leaq (%rax,%rcx), %rsi
incq %rsi
callq 0x10e0
testl %eax, %eax
je 0x2bd6
leaq 0x3560(%rip), %rdx      # 0x6131
jmp 0x2c9e
movl 0x90(%r15), %edi
movq 0x10(%r12), %rdx
movq 0x18(%r12), %rsi
movq 0x110(%r15), %rcx
callq 0x1180
movq 0x10(%r12), %rcx
cmpq %rcx, %rax
jae 0x2c09
leaq 0x3545(%rip), %rdx      # 0x6149
jmp 0x2c9e
movl 0x90(%r15), %edi
addq 0x110(%r15), %rcx
leaq 0x3406(%rip), %rsi      # 0x6024
movl $0x1, %edx
callq 0x1180
testq %rax, %rax
jle 0x2c97
movq 0x110(%r15), %rax
movq %rax, 0x128(%r15)
movq 0x10(%r12), %rcx
leaq (%rax,%rcx), %rdx
incq %rdx
movq %rdx, 0x110(%r15)
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %ecx
callq 0x1c9b
cmpl $0x0, 0x88(%r15)
jne 0x2cad
movq %r15, %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
callq 0x2cce
cmpl $0x0, 0x88(%r15)
jne 0x2cad
cmpl $0x0, 0x984(%r15)
je 0x2cad
movq %r15, %rdi
callq 0x2514
jmp 0x2cad
leaq 0x34c4(%rip), %rdx      # 0x6162
movq %r15, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x1b19
movq -0x68(%rbp), %rsp
movq %r15, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq