// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
movq %rdi, 0x78(%rsp)
movl 0x38(%rdi), %eax
movl %eax, 0xcc(%rsp)
movq 0x30(%rdi), %rax
movq %rax, 0xc0(%rsp)
addq $0x40, %rdi
callq 0x16d50
addl $0x1, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x13030
movq 0x78(%rsp), %rdi
movq %rax, 0xb8(%rsp)
addq $0x40, %rdi
callq 0x16d50
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x13030
movq %rax, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
movq 0x78(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x74(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x74(%rsp), %eax
cmpl %ecx, %eax
jge 0x75e65
movq 0x78(%rsp), %rdi
addq $0x40, %rdi
movl 0xac(%rsp), %esi
callq 0x76450
movq 0x78(%rsp), %rdi
movq (%rax), %rax
movl 0xb0(%rax), %edx
movq 0xb8(%rsp), %rax
movslq 0xac(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
addq $0x40, %rdi
movl 0xac(%rsp), %esi
callq 0x76450
movq (%rax), %rax
movq 0xa8(%rax), %rdx
movq 0xb0(%rsp), %rax
movslq 0xac(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x75dc8
movq 0x78(%rsp), %rdi
movl $0x0, 0xa8(%rsp)
movq 0xd0(%rsp), %rsi
movl 0xa8(%rsp), %edx
movq (%rdi), %rax
callq *0xc0(%rax)
cmpl $0x0, %eax
jne 0x75edc
movq 0xb8(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x75eaf
movq 0x68(%rsp), %rdi
callq 0x13750
movq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, %rax
je 0x75ecc
movq 0x60(%rsp), %rdi
callq 0x13750
movl $0x0, 0xe4(%rsp)
jmp 0x76282
movl $0x0, 0xa4(%rsp)
movq 0x78(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x5c(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x76057
movq 0x78(%rsp), %rdi
addq $0x40, %rdi
movl 0xa4(%rsp), %esi
callq 0x436f0
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x75f3e
jmp 0x76041
movq 0x78(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x31140
movq 0x98(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0xb0(%rax)
movq 0xd0(%rsp), %rdi
callq 0x1453b0
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movq 0x98(%rsp), %rax
movq %rcx, 0xa8(%rax)
movq 0xd0(%rsp), %rsi
movq 0x98(%rsp), %rax
movq (%rax), %rdx
movq 0x98(%rsp), %rax
movl 0x8(%rax), %ecx
movl 0xa8(%rsp), %r8d
movl 0x17d5ce(%rip), %r9d    # 0x1f3588
movl 0x186623(%rip), %r10d   # 0x1fc5e4
movq (%rdi), %rax
movl %r10d, (%rsp)
callq *0xa8(%rax)
cmpl $0x0, %eax
jne 0x7602f
movq 0xb8(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x75ff0
movq 0x50(%rsp), %rdi
callq 0x13750
movq 0xb0(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x7600d
movq 0x48(%rsp), %rdi
callq 0x13750
movq 0x78(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x311c0
movl $0x0, 0xe4(%rsp)
jmp 0x76282
movq 0x78(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x311c0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x75ee7
movq 0xd0(%rsp), %rdi
callq 0x1453b0
movq 0x78(%rsp), %rdi
movq %rax, 0x30(%rdi)
addq $0x40, %rdi
callq 0x16d50
movq 0x78(%rsp), %rdi
movl %eax, 0x38(%rdi)
movq 0xd0(%rsp), %rsi
movl 0xa8(%rsp), %edx
movq (%rdi), %rax
callq *0xc8(%rax)
cmpl $0x0, %eax
jne 0x760e5
movq 0xb8(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x760b8
movq 0x40(%rsp), %rdi
callq 0x13750
movq 0xb0(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x760d5
movq 0x38(%rsp), %rdi
callq 0x13750
movl $0x0, 0xe4(%rsp)
jmp 0x76282
movq 0xd0(%rsp), %rdi
callq 0x1453b0
movq 0x78(%rsp), %rdi
movq %rax, 0x90(%rsp)
movq 0xd0(%rsp), %rsi
movl 0xa8(%rsp), %edx
movq (%rdi), %rax
callq *0xc0(%rax)
cmpl $0x0, %eax
jne 0x76166
movq 0xb8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x76139
movq 0x30(%rsp), %rdi
callq 0x13750
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x76156
movq 0x28(%rsp), %rdi
callq 0x13750
movl $0x0, 0xe4(%rsp)
jmp 0x76282
movq 0xd0(%rsp), %rdi
movq 0x90(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
movq 0x78(%rsp), %rax
movl 0xcc(%rsp), %ecx
movl %ecx, 0x38(%rax)
movq 0xc0(%rsp), %rcx
movq %rcx, 0x30(%rax)
movl $0x0, 0x8c(%rsp)
movq 0x78(%rsp), %rdi
movl 0x8c(%rsp), %eax
movl %eax, 0x24(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x7623d
movq 0x78(%rsp), %rdi
addq $0x40, %rdi
movl 0x8c(%rsp), %esi
callq 0x436f0
movq %rax, 0x80(%rsp)
movq 0xb8(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0xb0(%rax)
movq 0xb0(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0xa8(%rax)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x761a3
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x7625a
movq 0x18(%rsp), %rdi
callq 0x13750
movq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x76277
movq 0x10(%rsp), %rdi
callq 0x13750
movl $0x1, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)