// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %edi
callq 0x1050
cmpl $0x0, %eax
je 0x2d50
movq -0x10(%rbp), %rdi
movl $0x4, %esi
leaq 0x646e(%rip), %rdx      # 0x91ae
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2f19
movq -0x10(%rbp), %rax
movl 0x90(%rax), %edi
callq 0x1050
cmpl $0x0, %eax
je 0x2d84
movq -0x10(%rbp), %rdi
movl $0x4, %esi
leaq 0x644f(%rip), %rdx      # 0x91c3
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2f19
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
movl $0x4, %edx
callq 0x1090
cmpl $0x0, %eax
je 0x2dc9
movq -0x10(%rbp), %rdi
movl $0x4, %esi
leaq 0x641f(%rip), %rdx      # 0x91d8
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2f19
movl $0x0, -0x14(%rbp)
cmpl $0x8, -0x14(%rbp)
jge 0x2dfa
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0xd0(%rax,%rcx,8), %rsi
callq 0x3af0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2dd0
movq -0x10(%rbp), %rax
movl 0x130(%rax), %edi
movq -0x10(%rbp), %rax
movl 0x134(%rax), %esi
movl $0x1, %edx
callq 0x3b80
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x2e43
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x63ba(%rip), %rdx      # 0x91ed
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2f19
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x138(%rcx), %rax
jae 0x2e7f
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq 0x140(%rax,%rcx,8), %rsi
callq 0x3ca0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2e4a
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0xc0, %rsi
addq $0x10, %rsi
callq 0x3dd0
movq -0x20(%rbp), %rdi
callq 0x3fe0
movq -0x10(%rbp), %rax
movq $0x0, 0x138(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x980(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x984(%rax)
movq -0x10(%rbp), %rax
movl 0x90(%rax), %edi
movq -0x10(%rbp), %rsi
addq $0xc0, %rsi
movl $0x70, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1180
cmpq $0x70, %rax
jae 0x2f11
movq -0x10(%rbp), %rdi
movl $0x4, %esi
leaq 0x6302(%rip), %rdx      # 0x9206
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2f19
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)