// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0xa8(%rdi), %edi
callq 0x1050
testl %eax, %eax
je 0x290e
leaq 0x48a2(%rip), %rdx      # 0x71ae
jmp 0x2949
movl 0x90(%rbx), %edi
callq 0x1050
testl %eax, %eax
je 0x2926
leaq 0x489f(%rip), %rdx      # 0x71c3
jmp 0x2949
movq 0x98(%rbx), %rdi
movq 0xa0(%rbx), %rsi
movl $0x4, %edx
callq 0x1090
testl %eax, %eax
je 0x296a
leaq 0x488f(%rip), %rdx      # 0x71d8
movq %rbx, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x1eb0
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x138(%rbx), %rax
leaq (%rbx,%rax,8), %rcx
addq $0x144, %rcx            # imm = 0x144
incq %rax
xorl %edx, %edx
movq 0xd0(%rbx,%rdx,8), %rsi
cmpl %esi, 0x130(%rbx)
jbe 0x2997
movl %esi, 0x130(%rbx)
movq %rsi, %rdi
shrq $0x20, %rdi
leal 0xff(%rdi), %r8d
shrl $0x8, %r8d
addl %esi, %r8d
cmpl 0x134(%rbx), %r8d
jbe 0x29bc
movl %r8d, 0x134(%rbx)
leaq (%rax,%rdx), %r8
movq %r8, 0x138(%rbx)
movl %esi, -0x4(%rcx,%rdx,8)
movl %edi, (%rcx,%rdx,8)
incq %rdx
cmpq $0x8, %rdx
jne 0x2981
movl 0x130(%rbx), %r13d
movl 0x134(%rbx), %r15d
movl %r15d, %ebp
subl %r13d, %ebp
addl $0x7, %ebp
shrl $0x3, %ebp
addl $0x7, %ebp
movl %ebp, %r12d
andl $-0x8, %r12d
leaq 0x8(%r12), %rdi
callq 0x1150
testq %rax, %rax
je 0x2c5c
movq %rax, %r14
movq %r15, 0x8(%rsp)
shrl $0x3, %ebp
movl %r13d, %eax
andl $-0x40, %eax
movl %eax, (%r14)
movl %ebp, 0x4(%r14)
leaq 0x8(%r14), %r15
movq %r15, %rdi
movl $0xff, %esi
movq %r12, %rdx
callq 0x10f0
movq %r15, %rdi
andq $0x3f, %r13
je 0x2a56
negb %r13b
movq $-0x1, %rax
movl %r13d, %ecx
shrq %cl, %rax
orq %rax, (%rdi)
movq 0x8(%rsp), %rcx
andq $0x3f, %rcx
je 0x2a70
movq $-0x1, %rax
shlq %cl, %rax
orq %rax, -0x8(%rdi,%rbp,8)
testq %r14, %r14
je 0x2c5c
movq 0x138(%rbx), %r13
testq %r13, %r13
je 0x2b45
movq %r14, %rax
addq $0x10, %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movq %rdi, 0x10(%rsp)
movq 0x140(%rbx,%rbp,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x2b39
addl $0xff, %eax
shrl $0x8, %eax
subl (%r14), %ecx
leaq (%rax,%rcx), %r15
decq %r15
movl %ecx, %edx
shrl $0x6, %edx
movq %r15, %r12
shrq $0x6, %r12
andb $0x3f, %cl
negb %cl
movq $-0x1, %rax
shrq %cl, %rax
cmpq %r12, %rdx
jne 0x2af9
movq $-0x2, %rsi
movl %r15d, %ecx
shlq %cl, %rsi
orq %rsi, %rax
leaq (%rdi,%rdx,8), %rcx
jmp 0x2b36
andq %rax, (%rdi,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %r12, %rax
jae 0x2b25
movq 0x8(%rsp), %rax
leaq (%rax,%rdx,8), %rdi
notq %rdx
addq %r12, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x10f0
movq 0x10(%rsp), %rdi
movq $-0x2, %rax
movl %r15d, %ecx
shlq %cl, %rax
leaq (%rdi,%r12,8), %rcx
andq %rax, (%rcx)
incq %rbp
cmpq %rbp, %r13
jne 0x2a9c
leaq 0xd0(%rbx), %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x100(%rbx)
movups %xmm0, 0xf0(%rbx)
movups %xmm0, 0xe0(%rbx)
movups %xmm0, 0xd0(%rbx)
cmpl $0x0, 0x4(%r14)
je 0x2c70
leaq 0xd8(%rbx), %rsi
xorl %r9d, %r9d
xorl %edi, %edi
movq %rdx, %r8
xorl %r11d, %r11d
movq 0x8(%r14,%rdi,8), %r10
cmpq $-0x1, %r10
je 0x2c43
testq %r10, %r10
je 0x2c3f
bsfq %r10, %rcx
cmpq $0x3f, %rcx
je 0x2c43
incl %ecx
movl %edi, %r15d
shll $0x6, %r15d
movl %ecx, %r12d
testl %r12d, %r12d
je 0x2c3c
addl %r11d, %r12d
shll $0x8, %r12d
addl $0xffffff00, %r12d      # imm = 0xFFFFFF00
cmpl 0x4(%r8), %r12d
jbe 0x2c04
movl %r12d, 0x4(%r8)
addl (%r14), %r9d
movl %r9d, (%r8)
movl $0x7, %eax
movq %rsi, %r9
movq %rdx, %r11
movl 0x4(%r9), %ebp
movq %r9, %r8
cmpl 0x4(%r11), %ebp
jb 0x2bf8
movq %r11, %r8
addq $0x8, %r9
movq %r8, %r11
decq %rax
jne 0x2be8
movq %r10, %rax
shrq %cl, %rax
notq %rax
bsfq %rax, %rax
addl %ecx, %eax
movq %r10, %r9
movl %eax, %ecx
shrq %cl, %r9
bsfq %r9, %r12
incl %r12d
xorl %r11d, %r11d
testq %r9, %r9
cmovel %r11d, %r12d
leal (%rax,%r15), %r9d
addl %r12d, %eax
movl %eax, %ecx
cmpl $0x40, %eax
jl 0x2bba
jmp 0x2c43
subl %ecx, %r11d
addl $0x40, %r11d
incq %rdi
movl 0x4(%r14), %eax
cmpq %rax, %rdi
jb 0x2b88
shll $0x8, %r11d
movq %r8, %rdx
jmp 0x2c76
leaq 0x458a(%rip), %rdx      # 0x71ed
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x2951
xorl %r11d, %r11d
xorl %r9d, %r9d
leaq 0xc0(%rbx), %r15
cmpl 0x4(%rdx), %r11d
jbe 0x2c8d
movl %r11d, 0x4(%rdx)
addl (%r14), %r9d
movl %r9d, (%rdx)
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
movq %rax, 0x138(%rbx)
movq %rax, 0x980(%rbx)
movl 0x90(%rbx), %edi
movl $0x70, %edx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x1180
cmpq $0x70, %rax
jae 0x2958
leaq 0x453b(%rip), %rdx      # 0x7206
jmp 0x2949