// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movl 0x38(%rdi), %eax
movl %eax, 0xc(%rsp)
movq 0x30(%rdi), %rax
movq %rax, 0x10(%rsp)
movslq 0x4c(%rdi), %r13
movq $-0x1, %rax
cmpq %rax, %r13
leaq 0x4(,%r13,4), %rdi
cmovlq %rax, %rdi
leaq 0x8(,%r13,8), %r14
cmovlq %rax, %r14
callq 0x13030
movq %rax, %r15
movq %r14, %rdi
callq 0x13030
movq %rax, %r14
testq %r13, %r13
jle 0x434df
movq 0x40(%rbx), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl 0xb0(%rdx), %esi
movl %esi, (%r15,%rcx,4)
movq 0xa8(%rdx), %rdx
movq %rdx, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %r13
jne 0x434be
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x436bd
cmpl $0x0, 0x4c(%rbx)
jle 0x4360a
xorl %ebp, %ebp
movq 0x40(%rbx), %rax
movq (%rax,%rbp,8), %r13
cmpl $0x0, 0x8(%r13)
je 0x435fa
movl 0xb4(%r13), %eax
testl %eax, %eax
jne 0x43545
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x4354e
movl 0xb4(%r13), %eax
incl %eax
movl %eax, 0xb4(%r13)
movl 0x8(%r13), %eax
movl %eax, 0xb0(%r13)
movq %r12, %rdi
callq 0xabc6b
movq %rax, 0xa8(%r13)
movq (%r13), %rdx
movl 0x8(%r13), %ecx
movq (%rbx), %rax
movl $0x0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
movl $0x6, %r9d
callq *0xa8(%rax)
testl %eax, %eax
je 0x436de
movl 0xb4(%r13), %eax
cmpl $0x1, %eax
jne 0x435c4
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x435fa
movl 0xb4(%r13), %eax
decl %eax
movl %eax, 0xb4(%r13)
jne 0x435fa
testb $0x2, 0x89(%r13)
je 0x435fa
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
incq %rbp
movslq 0x4c(%rbx), %rax
cmpq %rax, %rbp
jl 0x43506
movq %r12, %rdi
callq 0xabc6b
movq %rax, 0x30(%rbx)
movl 0x4c(%rbx), %eax
movl %eax, 0x38(%rbx)
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc8(%rax)
testl %eax, %eax
je 0x436bd
movq %r12, %rdi
callq 0xabc6b
movq %rax, %r13
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x436bd
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xabc5a
movl 0xc(%rsp), %eax
movl %eax, 0x38(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rbx)
movslq 0x4c(%rbx), %rax
testq %rax, %rax
jle 0x436a6
movq 0x40(%rbx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl (%r15,%rdx,4), %edi
movl %edi, 0xb0(%rsi)
movq (%r14,%rdx,8), %rdi
movq %rdi, 0xa8(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x43685
movq %r15, %rdi
callq 0x13680
movq %r14, %rdi
callq 0x13680
movl $0x1, %ebp
jmp 0x436cd
movq %r15, %rdi
callq 0x13680
movq %r14, %rdi
callq 0x13680
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x13680
movq %r14, %rdi
callq 0x13680
movl 0xb4(%r13), %eax
cmpl $0x1, %eax
jne 0x4371b
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x43758
movl 0xb4(%r13), %eax
xorl %ebp, %ebp
decl %eax
movl %eax, 0xb4(%r13)
jne 0x436cd
testb $0x2, 0x89(%r13)
je 0x436cd
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x436cd
xorl %ebp, %ebp
jmp 0x436cd
nop