// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::WriteStream(_IO_FILE*)
int R3SurfelDatabase::
WriteStream(FILE *fp)
{
  // Save current file offset info so that can restore afterwards
  unsigned int saved_file_blocks_count = file_blocks_count;
  unsigned long long saved_file_blocks_offset = file_blocks_offset;
  unsigned int *saved_file_surfels_counts = new unsigned int [ blocks.NEntries() + 1];
  unsigned long long *saved_file_surfels_offsets = new unsigned long long [ blocks.NEntries() + 1];
  for (int i = 0; i < blocks.NEntries(); i++) {
    saved_file_surfels_counts[i] = blocks[i]->file_surfels_count;
    saved_file_surfels_offsets[i] = blocks[i]->file_surfels_offset;
  }

  // Write file header
  RNBoolean swap_endian = FALSE;
  if (!WriteFileHeader(fp, swap_endian)) {
    delete [] saved_file_surfels_counts;
    delete [] saved_file_surfels_offsets;
    return 0;
  }

  // Write blocks
  for (int i = 0; i < blocks.NEntries(); i++) {
    R3SurfelBlock *block = blocks.Kth(i);
    if (block->nsurfels == 0) continue;
    ReadBlock(block);
    block->file_surfels_count = block->nsurfels;
    block->file_surfels_offset = RNFileTell(fp);
    if (!WriteSurfel(fp, block->surfels, block->nsurfels, swap_endian, current_major_version, current_minor_version)) {
      delete [] saved_file_surfels_counts;
      delete [] saved_file_surfels_offsets;
      ReleaseBlock(block);
      return 0;
    }
    ReleaseBlock(block);
  }

  // Update block header info
  file_blocks_offset = RNFileTell(fp);
  file_blocks_count = blocks.NEntries();

  // Write block header
  if (!WriteBlockHeader(fp, swap_endian)) {
    delete [] saved_file_surfels_counts;
    delete [] saved_file_surfels_offsets;
    return 0;
  }

  // Save end of file offset
  unsigned long long end_of_file_offset = RNFileTell(fp);
  
  // Write file header again (now that info has been filled in)
  if (!WriteFileHeader(fp, swap_endian)) {
    delete [] saved_file_surfels_counts;
    delete [] saved_file_surfels_offsets;
    return 0;
  }

  // Seek back to end of file
  RNFileSeek(fp, end_of_file_offset, RN_FILE_SEEK_SET);

  // Restore previous file offset info
  file_blocks_count = saved_file_blocks_count;
  file_blocks_offset = saved_file_blocks_offset;
  for (int i = 0; i < blocks.NEntries(); i++) {
    R3SurfelBlock *block = blocks.Kth(i);
    block->file_surfels_count = saved_file_surfels_counts[i];
    block->file_surfels_offset = saved_file_surfels_offsets[i];
  }

  // Delete temporary data
  delete [] saved_file_surfels_counts;
  delete [] saved_file_surfels_offsets;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r15
movl 0x38(%rdi), %ebp
movq 0x30(%rdi), %rax
movq %rax, 0x10(%rsp)
movslq 0x4c(%rdi), %r13
pushq $-0x1
popq %rax
cmpq %rax, %r13
leaq 0x4(,%r13,4), %rdi
cmovlq %rax, %rdi
leaq 0x8(,%r13,8), %rbx
cmovlq %rax, %rbx
callq 0x13030
movq %rax, %r14
movq %rbx, %rdi
callq 0x13030
movq %rax, %rbx
movq 0x40(%r15), %rax
xorl %ecx, %ecx
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
cmpq %rcx, %rdx
je 0x3e044
movq (%rax,%rcx,8), %rsi
movl 0xb0(%rsi), %edi
movl %edi, (%r14,%rcx,4)
movq 0xa8(%rsi), %rsi
movq %rsi, (%rbx,%rcx,8)
incq %rcx
jmp 0x3e021
movq (%r15), %rax
xorl %r13d, %r13d
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x3e17b
movl %ebp, 0xc(%rsp)
xorl %ebp, %ebp
movslq 0x4c(%r15), %rax
cmpq %rax, %rbp
jge 0x3e0dd
movq 0x40(%r15), %rax
movq (%rax,%rbp,8), %r13
cmpl $0x0, 0x8(%r13)
je 0x3e0d8
movq %r15, %rdi
movq %r13, %rsi
callq 0x20a64
movl 0x8(%r13), %eax
movl %eax, 0xb0(%r13)
movq %r12, %rdi
callq 0x9c473
movq %rax, 0xa8(%r13)
movq (%r13), %rdx
movl 0x8(%r13), %ecx
movq (%r15), %rax
andl $0x0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
pushq $0x6
popq %r9
callq *0xa8(%rax)
testl %eax, %eax
je 0x3e18d
movq %r15, %rdi
movq %r13, %rsi
callq 0x20aa0
incq %rbp
jmp 0x3e066
movq %r12, %rdi
callq 0x9c473
movq %rax, 0x30(%r15)
movl 0x4c(%r15), %eax
movl %eax, 0x38(%r15)
movq (%r15), %rax
xorl %r13d, %r13d
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc8(%rax)
testl %eax, %eax
je 0x3e17b
movq %r12, %rdi
callq 0x9c473
movq %rax, %rbp
movq (%r15), %rax
xorl %r13d, %r13d
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x3e17b
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x9c462
movl 0xc(%rsp), %eax
movl %eax, 0x38(%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%r15)
movl 0x4c(%r15), %eax
movq 0x40(%r15), %rcx
testl %eax, %eax
cmovlel %r13d, %eax
cmpq %r13, %rax
je 0x3e1ad
movq (%rcx,%r13,8), %rdx
movl (%r14,%r13,4), %esi
movl %esi, 0xb0(%rdx)
movq (%rbx,%r13,8), %rsi
movq %rsi, 0xa8(%rdx)
incq %r13
jmp 0x3e158
movq %r14, %rdi
callq 0x136a0
movq %rbx, %rdi
callq 0x136a0
jmp 0x3e1c1
movq %r14, %rdi
callq 0x136a0
movq %rbx, %rdi
callq 0x136a0
movq %r15, %rdi
movq %r13, %rsi
callq 0x20aa0
xorl %r13d, %r13d
jmp 0x3e1c1
movq %r14, %rdi
callq 0x136a0
movq %rbx, %rdi
callq 0x136a0
pushq $0x1
popq %r13
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop