// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::SyncFile()
int R3SurfelDatabase::
SyncFile(void)
{
  // Check if file is read-only
  if (!strcmp(rwaccess, "rb")) return 1;

  // Sync blocks
  for (int i = 0; i < blocks.NEntries(); i++) {
    R3SurfelBlock *block = blocks.Kth(i);
    if (!SyncBlock(block)) return 0;
  }

  // Update blocks offset
  unsigned int nblocks = blocks.NEntries();
  if (nblocks > file_blocks_count) {
    // Update block header size
    file_blocks_count = nblocks;

    // Update block header offset (after last block)
    file_blocks_offset = 0;
    for (int i = 0; i < blocks.NEntries(); i++) {
      R3SurfelBlock *block = blocks.Kth(i);
      unsigned long long offset = block->file_surfels_offset + block->file_surfels_count * NBytesPerSurfel();
      if (offset > file_blocks_offset) file_blocks_offset = offset;
    }

    // Double check block header offset
    if (file_blocks_offset <= 0) {
      RNFileSeek(fp, 0, RN_FILE_SEEK_END);
      file_blocks_offset = RNFileTell(fp);
    }
  }

  // Write blocks
  if (!WriteBlockHeader(fp, swap_endian)) return 0;

  // Write header again (now that the offset values have been filled in)
  if (!WriteFileHeader(fp, swap_endian)) return 0;

  // Return success
  return 1;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rax), %rdi
leaq 0x12e797(%rip), %rsi    # 0x1a41f5
callq 0x137e0
cmpl $0x0, %eax
jne 0x75a75
movl $0x1, 0x54(%rsp)
jmp 0x75c60
movl $0x0, 0x44(%rsp)
movq 0x18(%rsp), %rdi
movl 0x44(%rsp), %eax
movl %eax, 0x14(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x75ae4
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
movl 0x44(%rsp), %esi
callq 0x436f0
movq 0x18(%rsp), %rdi
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rsi
callq 0x763e0
cmpl $0x0, %eax
jne 0x75ad5
movl $0x0, 0x54(%rsp)
jmp 0x75c60
jmp 0x75ad7
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x75a7d
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
callq 0x16d50
movq 0x18(%rsp), %rcx
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x38(%rcx), %eax
jbe 0x75c10
movq 0x18(%rsp), %rax
movl 0x34(%rsp), %ecx
movl %ecx, 0x38(%rax)
movq $0x0, 0x30(%rax)
movl $0x0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movl 0x30(%rsp), %eax
movl %eax, 0x10(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x75bd1
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
movl 0x30(%rsp), %esi
callq 0x436f0
movq 0x18(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rax
movl 0xb0(%rax), %eax
movl %eax, 0x4(%rsp)
callq 0x72900
movl 0x4(%rsp), %edx
movq 0x18(%rsp), %rcx
movl %eax, %esi
movq 0x8(%rsp), %rax
imull %esi, %edx
movl %edx, %edx
addq %rdx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x30(%rcx), %rax
jbe 0x75bbf
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x30(%rax)
jmp 0x75bc1
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x75b24
movq 0x18(%rsp), %rax
cmpq $0x0, 0x30(%rax)
ja 0x75c0e
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x145360
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x1453b0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x30(%rax)
jmp 0x75c10
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl 0x28(%rdi), %edx
movq (%rdi), %rax
callq *0xc8(%rax)
cmpl $0x0, %eax
jne 0x75c34
movl $0x0, 0x54(%rsp)
jmp 0x75c60
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl 0x28(%rdi), %edx
movq (%rdi), %rax
callq *0xc0(%rax)
cmpl $0x0, %eax
jne 0x75c58
movl $0x0, 0x54(%rsp)
jmp 0x75c60
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)