// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %r13
movq %rdi, %r15
movl 0x38(%rdi), %eax
movl %eax, 0x14(%rsp)
movq 0x30(%rdi), %rax
movq %rax, 0x20(%rsp)
movslq 0x4c(%rdi), %r12
movq $-0x1, %rax
cmpq %rax, %r12
leaq 0x4(,%r12,4), %rdi
cmovlq %rax, %rdi
leaq 0x8(,%r12,8), %r14
cmovlq %rax, %r14
callq 0x13030
movq %rax, %rbx
movq %r14, %rdi
callq 0x13030
movq %rax, %r14
testq %r12, %r12
jle 0x43878
movq 0x40(%r15), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl 0xb0(%rdx), %esi
movl %esi, (%rbx,%rcx,4)
movq 0xa8(%rdx), %rdx
movq %rdx, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x43858
movq (%r15), %rax
xorl %ebp, %ebp
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x43aab
movq %r13, 0x18(%rsp)
cmpl $0x0, 0x4c(%r15)
jle 0x43a07
xorl %ebp, %ebp
movq 0x40(%r15), %rax
movq (%rax,%rbp,8), %r13
cmpl $0x0, 0x8(%r13)
je 0x43964
cmpl $0x0, 0xb4(%r13)
jne 0x438dd
movq (%r15), %rax
movq 0x8(%r15), %rdx
movl 0x28(%r15), %ecx
movq %r15, %rdi
movq %r13, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x438e4
incl 0xb4(%r13)
movl 0x8(%r13), %eax
movl %eax, 0xb0(%r13)
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0xad15f
movq %rax, 0xa8(%r13)
movq (%r13), %rdx
movl 0x8(%r13), %ecx
movq (%r15), %rax
movl $0x0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
movl $0x6, %r9d
callq *0xa8(%rax)
testl %eax, %eax
je 0x4396c
cmpl $0x1, 0xb4(%r13)
jne 0x43953
movq (%r15), %rax
movq 0x8(%r15), %rdx
movl 0x28(%r15), %ecx
movq %r15, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x4395c
decl 0xb4(%r13)
je 0x439b2
xorl %r12d, %r12d
jmp 0x439e8
movl $0x7, %r12d
jmp 0x439e8
movq %rbx, %rdi
callq 0x13680
movq %r14, %rdi
callq 0x13680
cmpl $0x1, 0xb4(%r13)
jne 0x439a1
movq (%r15), %rax
movq 0x8(%r15), %rdx
movl 0x28(%r15), %ecx
movq %r15, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x439e2
decl 0xb4(%r13)
movl $0x1, %r12d
jne 0x439e8
jmp 0x439b5
xorl %r12d, %r12d
testb $0x2, 0x89(%r13)
je 0x439e8
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x439e8
movl $0x1, %r12d
cmpl $0x7, %r12d
je 0x439f7
testl %r12d, %r12d
jne 0x43acc
incq %rbp
movslq 0x4c(%r15), %rax
cmpq %rax, %rbp
jl 0x438a5
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0xad15f
movq %rax, 0x30(%r15)
movl 0x4c(%r15), %eax
movl %eax, 0x38(%r15)
movq (%r15), %rax
xorl %ebp, %ebp
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc8(%rax)
testl %eax, %eax
je 0x43aab
movq %r12, %rdi
callq 0xad15f
movq %rax, %r13
movq (%r15), %rax
xorl %ebp, %ebp
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0xc0(%rax)
testl %eax, %eax
je 0x43aab
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xad14e
movl 0x14(%rsp), %eax
movl %eax, 0x38(%r15)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%r15)
movslq 0x4c(%r15), %rax
movl $0x1, %ebp
testq %rax, %rax
jle 0x43aab
movq 0x40(%r15), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl (%rbx,%rdx,4), %edi
movl %edi, 0xb0(%rsi)
movq (%r14,%rdx,8), %rdi
movq %rdi, 0xa8(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x43a8b
movq %rbx, %rdi
callq 0x13680
movq %r14, %rdi
callq 0x13680
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x43abb