// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::WriteFileHeader(_IO_FILE*, int)
int R3SurfelDatabase::
WriteFileHeader(FILE *fp, int swap_endian)
{
  // Get convenient variables
  unsigned int endian_test = 1;
  unsigned int nblocks = blocks.NEntries();
  char magic[32] = { '\0' };
  strncpy(magic, "R3SurfelDatabase", 32);
  char buffer[1024] = { '\0' };

  // Seek to start of file
  RNFileSeek(fp, 0, RN_FILE_SEEK_SET);

  // Write first part of header
  if (!RNWriteChar(fp, magic, 32, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &endian_test, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &endian_test, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &major_version, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &minor_version, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedLongLong(fp, &file_blocks_offset, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &file_blocks_count, 1, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &nblocks, 1, swap_endian)) return 0;

  // Write number of surfels
  if (major_version < 4) {
    // nsurfels used to be an int
    int nsurfels32 = nsurfels;
    if (!RNWriteInt(fp, &nsurfels32, 1, swap_endian)) return 0;
  }
  else {
    // nsurfels now is an RNInt64
    if (!RNWriteLongLong(fp, &nsurfels, 1, swap_endian)) return 0;
  }

  // Write rest of header
  if (!RNWriteDouble(fp, &bbox[0][0], 6, swap_endian)) return 0;
  if (!RNWriteDouble(fp, &timestamp_range[0], 2, swap_endian)) return 0;
  if (!RNWriteUnsignedInt(fp, &max_identifier, 1, swap_endian)) return 0;
  if (!RNWriteChar(fp, buffer, 1004, swap_endian)) return 0;

  // Return success
  return 1;
}

// Assembly:
subq $0x468, %rsp            # imm = 0x468
movq %rdi, 0x458(%rsp)
movq %rsi, 0x450(%rsp)
movl %edx, 0x44c(%rsp)
movq 0x458(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x1, 0x448(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, 0x444(%rsp)
leaq 0x420(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x132c0
leaq 0x420(%rsp), %rdi
leaq 0x13158d(%rip), %rsi    # 0x1a5bd6
movl $0x20, %edx
callq 0x132a0
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x132c0
movq 0x450(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x145360
movq 0x450(%rsp), %rdi
leaq 0x420(%rsp), %rsi
movl 0x44c(%rsp), %ecx
movl $0x20, %edx
callq 0x145a90
cmpl $0x0, %eax
jne 0x746ad
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x450(%rsp), %rdi
movl 0x44c(%rsp), %ecx
leaq 0x448(%rsp), %rsi
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x746e3
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x450(%rsp), %rdi
movl 0x44c(%rsp), %ecx
leaq 0x448(%rsp), %rsi
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x74719
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0x20, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x74750
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0x24, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x74787
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0x30, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145f60
cmpl $0x0, %eax
jne 0x747be
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0x38, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x747f5
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x450(%rsp), %rdi
movl 0x44c(%rsp), %ecx
leaq 0x444(%rsp), %rsi
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x7482b
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rax
cmpl $0x4, 0x20(%rax)
jae 0x74878
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movl %eax, 0x1c(%rsp)
movq 0x450(%rsp), %rdi
movl 0x44c(%rsp), %ecx
leaq 0x1c(%rsp), %rsi
movl $0x1, %edx
callq 0x145c90
cmpl $0x0, %eax
jne 0x74876
movl $0x0, 0x464(%rsp)
jmp 0x749c1
jmp 0x748b1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0x50, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145ed0
cmpl $0x0, %eax
jne 0x748af
movl $0x0, 0x464(%rsp)
jmp 0x749c1
jmp 0x748b1
movq 0x10(%rsp), %rdi
movq 0x450(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x58, %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, %rdi
xorl %esi, %esi
callq 0x1bde0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movl 0x44c(%rsp), %ecx
movl $0x6, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x74906
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rdi
movq 0x450(%rsp), %rax
movq %rax, (%rsp)
addq $0x90, %rdi
xorl %esi, %esi
callq 0x1be00
movq (%rsp), %rdi
movq %rax, %rsi
movl 0x44c(%rsp), %ecx
movl $0x2, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x7494f
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x10(%rsp), %rsi
movq 0x450(%rsp), %rdi
addq $0xa0, %rsi
movl 0x44c(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x74986
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movq 0x450(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl 0x44c(%rsp), %ecx
movl $0x3ec, %edx            # imm = 0x3EC
callq 0x145a90
cmpl $0x0, %eax
jne 0x749b6
movl $0x0, 0x464(%rsp)
jmp 0x749c1
movl $0x1, 0x464(%rsp)
movl 0x464(%rsp), %eax
addq $0x468, %rsp            # imm = 0x468
retq