// File: /workspace/github_c_makefile_star2_O2/jflatow[P]jfdb/src/db.c
// Function: JFDB_flush
JFDB *JFDB_flush(JFDB *db) {
  // we only ever write a checkpoint if we are sure everything in it made it to disk
  // so make sure everything gets to disk, and then write a checkpoint
  // we can only reclaim space when we are sure that it is not being used anymore
  // now that we are flushing, it is safe to recompute the gap list we're using
  if (JFDB_fsync(db->vmap.fd))
    return JFDB_set_error(db, JFDB_EFILE, "failed to fsync vals");
  if (JFDB_fsync(db->kmap.fd))
    return JFDB_set_error(db, JFDB_EFILE, "failed to fsync keys");
  if (JFDB_msync(db->kmap.map, db->kmap.size, MS_SYNC))
    return JFDB_set_error(db, JFDB_EFILE, "failed to msync keys");

  // put the current gaps back in the running
  for (int i = 0; i < JFDB_NUM_GAPS; i++)
    JFDB_add_to_free_list(db, db->tip.cp.gaps[i]);

  // create a region map to cover the whole interval, default to not free
  JFDB_RegionMap *map = JFDB_region_map_alloc(db->freeList.lower, db->freeList.upper, 1);
  if (!map)
    return JFDB_set_error(db, JFDB_ENOMEM, "failed to create gap map");

  // add all the free regions
  for (int i = 0; i < db->freeList.size; i++)
    JFDB_region_map_clear(map, db->freeList.data[i]);

  // determine gaps
  JFDB_region_map_gaps(map, db->tip.cp.gaps);
  JFDB_region_map_free(map);

  db->freeList.size = 0;
  db->isModified = False;
  db->oughtFlush = False;

  if (pwrite(db->kmap.fd, &db->tip, sizeof(JFDB_Header), 0) < sizeof(JFDB_Header))
    return JFDB_set_error(db, JFDB_EFILE, "failed to write keys header");
  return db;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0xa8(%rdi), %edi
callq 0x1050
testl %eax, %eax
je 0x226e
leaq 0x2f42(%rip), %rdx      # 0x51ae
jmp 0x22a7
movl 0x90(%rbx), %edi
callq 0x1050
testl %eax, %eax
je 0x2286
leaq 0x2f3f(%rip), %rdx      # 0x51c3
jmp 0x22a7
movq 0x98(%rbx), %rdi
movq 0xa0(%rbx), %rsi
pushq $0x4
popq %rdx
callq 0x1090
testl %eax, %eax
je 0x22c3
leaq 0x2f31(%rip), %rdx      # 0x51d8
pushq $0x4
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1af6
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xd0(%rbx), %r14
pushq $0x1a
popq %r15
cmpq $0x22, %r15
je 0x22e5
movq (%rbx,%r15,8), %rsi
movq %rbx, %rdi
callq 0x2acc
incq %r15
jmp 0x22ce
movl 0x130(%rbx), %edi
movl 0x134(%rbx), %esi
pushq $0x1
popq %rdx
callq 0x2b1b
testq %rax, %rax
je 0x23e4
movq %rax, %r15
movq 0x138(%rbx), %rdx
xorl %esi, %esi
pushq $-0x1
popq %rdi
pushq $-0x2
popq %r8
cmpq %rdx, %rsi
je 0x2391
movq 0x140(%rbx,%rsi,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x238c
addl $0xff, %eax
shrl $0x8, %eax
subl (%r15), %ecx
addq %rcx, %rax
decq %rax
movl %ecx, %r9d
shrl $0x6, %r9d
movq %rax, %r10
shrq $0x6, %r10
andb $0x3f, %cl
negb %cl
movq %rdi, %r11
shrq %cl, %r11
cmpq %r10, %r9
jne 0x2367
movq %r8, %r10
movl %eax, %ecx
shlq %cl, %r10
orq %r10, %r11
jmp 0x2387
andq %r11, 0x8(%r15,%r9,8)
incq %r9
cmpq %r10, %r9
jae 0x237c
andq $0x0, 0x8(%r15,%r9,8)
jmp 0x236c
movq %r8, %r11
movl %eax, %ecx
shlq %cl, %r11
movq %r10, %r9
andq %r11, 0x8(%r15,%r9,8)
incq %rsi
jmp 0x2315
leaq 0xc0(%rbx), %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x2bba
movq %r15, %rdi
callq 0x1030
andq $0x0, 0x138(%rbx)
andq $0x0, 0x980(%rbx)
movl 0x90(%rbx), %edi
pushq $0x70
popq %rdx
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1180
cmpq $0x70, %rax
jae 0x22b4
leaq 0x2e27(%rip), %rdx      # 0x5206
jmp 0x22a7
leaq 0x2e02(%rip), %rdx      # 0x51ed
pushq $0x2
jmp 0x22a9