// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %rbx
movl 0xa8(%rdi), %edi
callq 0x1050
testl %eax, %eax
je 0x253d
leaq 0x3c73(%rip), %rdx      # 0x61ae
jmp 0x2578
movl 0x90(%rbx), %edi
callq 0x1050
testl %eax, %eax
je 0x2555
leaq 0x3c70(%rip), %rdx      # 0x61c3
jmp 0x2578
movq 0x98(%rbx), %rdi
movq 0xa0(%rbx), %rsi
movl $0x4, %edx
callq 0x1090
testl %eax, %eax
je 0x2599
leaq 0x3c60(%rip), %rdx      # 0x61d8
movq %rbx, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x1b19
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd0(%rbx), %rdi
movq 0x138(%rbx), %r12
leaq (%rbx,%r12,8), %rax
addq $0x144, %rax            # imm = 0x144
xorl %r15d, %r15d
movq 0xd0(%rbx,%r15,8), %rcx
cmpl %ecx, 0x130(%rbx)
jbe 0x25ca
movl %ecx, 0x130(%rbx)
movq %rcx, %rdx
shrq $0x20, %rdx
leal 0xff(%rdx), %esi
shrl $0x8, %esi
addl %ecx, %esi
cmpl 0x134(%rbx), %esi
jbe 0x25ea
movl %esi, 0x134(%rbx)
leaq (%r12,%r15), %rsi
incq %rsi
movq %rsi, 0x138(%rbx)
movl %ecx, -0x4(%rax,%r15,8)
movl %edx, (%rax,%r15,8)
incq %r15
cmpq $0x8, %r15
jne 0x25b4
movq %rdi, 0x28(%rsp)
movl 0x130(%rbx), %ecx
movl 0x134(%rbx), %eax
movq %rax, 0x10(%rsp)
movq %rcx, 0x30(%rsp)
subl %ecx, %eax
addl $0x7, %eax
movl %eax, 0x24(%rsp)
movl %eax, %ebp
shrl $0x3, %ebp
addl $0x7, %ebp
movl %ebp, %r13d
andl $-0x8, %r13d
leaq 0x8(%r13), %rdi
callq 0x1150
testq %rax, %rax
je 0x28f8
movq %rax, %r14
shrl $0x3, %ebp
movq 0x30(%rsp), %rcx
movl %ecx, %eax
andl $-0x40, %eax
movl %eax, 0xc(%rsp)
movl %eax, (%r14)
movl %ebp, 0x4(%r14)
leaq 0x8(%r14), %rdi
movq %rdi, 0x18(%rsp)
movl $0xff, %esi
movq %r13, %rdx
movq %rcx, %r13
callq 0x10f0
movq 0x18(%rsp), %r9
andq $0x3f, %r13
je 0x26a1
negb %r13b
movq $-0x1, %rax
movl %r13d, %ecx
shrq %cl, %rax
orq %rax, (%r9)
leaq (%r12,%r15), %rax
movq 0x10(%rsp), %rcx
andq $0x3f, %rcx
movq 0x28(%rsp), %r13
je 0x26c4
movq $-0x1, %rdx
shlq %cl, %rdx
orq %rdx, -0x8(%r9,%rbp,8)
testq %rax, %rax
movl 0xc(%rsp), %edi
je 0x2797
addq %r12, %r15
decq %r15
movq %r14, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
movq $-0x1, %rbp
movq 0x148(%rbx,%rbp,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
je 0x278b
addl $0xff, %eax
shrl $0x8, %eax
subl %edi, %ecx
leaq (%rax,%rcx), %r13
decq %r13
movl %ecx, %edx
shrl $0x6, %edx
movq %r13, %r12
shrq $0x6, %r12
andb $0x3f, %cl
negb %cl
movq $-0x1, %rax
shrq %cl, %rax
cmpq %r12, %rdx
jne 0x2745
movq $-0x2, %rsi
movl %r13d, %ecx
shlq %cl, %rsi
orq %rsi, %rax
movq %rdx, %r12
jmp 0x2782
andq %rax, (%r9,%rdx,8)
leaq 0x1(%rdx), %rax
cmpq %r12, %rax
jae 0x2775
movq 0x10(%rsp), %rax
leaq (%rax,%rdx,8), %rdi
notq %rdx
addq %r12, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x10f0
movl 0xc(%rsp), %edi
movq 0x18(%rsp), %r9
movq $-0x2, %rax
movl %r13d, %ecx
shlq %cl, %rax
andq %rax, (%r9,%r12,8)
movq 0x28(%rsp), %r13
incq %rbp
cmpq %rbp, %r15
jne 0x26ea
leaq 0xc0(%rbx), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
cmpl $0x8, 0x24(%rsp)
jb 0x28b5
leaq 0xd8(%rbx), %rdx
xorl %r8d, %r8d
xorl %esi, %esi
movq %r13, %rdi
xorl %r10d, %r10d
movq (%r9,%rsi,8), %r9
cmpq $-0x1, %r9
je 0x288c
testq %r9, %r9
je 0x2888
bsfq %r9, %rcx
cmpq $0x3f, %rcx
je 0x288c
incl %ecx
movl %esi, %r11d
shll $0x6, %r11d
movl %ecx, %r15d
testl %r15d, %r15d
je 0x2885
addl %r10d, %r15d
shll $0x8, %r15d
addl $0xffffff00, %r15d      # imm = 0xFFFFFF00
cmpl 0x4(%rdi), %r15d
jbe 0x284d
movl %r15d, 0x4(%rdi)
addl (%r14), %r8d
movl %r8d, (%rdi)
movl $0x7, %eax
movq %rdx, %r8
movq %r13, %r10
movl 0x4(%r8), %ebp
movq %r8, %rdi
cmpl 0x4(%r10), %ebp
jb 0x2841
movq %r10, %rdi
addq $0x8, %r8
movq %rdi, %r10
decq %rax
jne 0x2831
movq %r9, %rax
shrq %cl, %rax
notq %rax
bsfq %rax, %rax
addl %ecx, %eax
movq %r9, %r8
movl %eax, %ecx
shrq %cl, %r8
bsfq %r8, %r15
incl %r15d
xorl %r10d, %r10d
testq %r8, %r8
cmovel %r10d, %r15d
leal (%rax,%r11), %r8d
addl %r15d, %eax
movl %eax, %ecx
cmpl $0x40, %eax
jl 0x2803
jmp 0x288c
subl %ecx, %r10d
addl $0x40, %r10d
incq %rsi
movl 0x4(%r14), %eax
cmpq %rax, %rsi
movq 0x18(%rsp), %r9
jb 0x27d2
shll $0x8, %r10d
cmpl 0x4(%rdi), %r10d
jbe 0x28b5
movl %r10d, 0x4(%rdi)
addl (%r14), %r8d
movl %r8d, (%rdi)
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
movq %rax, 0x138(%rbx)
movq %rax, 0x980(%rbx)
movl 0x90(%rbx), %edi
movl $0x70, %edx
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1180
cmpq $0x70, %rax
jae 0x2587
leaq 0x3913(%rip), %rdx      # 0x6206
jmp 0x2578
leaq 0x38ee(%rip), %rdx      # 0x61ed
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x2580