// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x430, %rsp            # imm = 0x430
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, 0x4(%rsp)
movl 0x4c(%rdi), %eax
movl %eax, 0x8(%rsp)
movups 0x8c636(%rip), %xmm0    # 0xc9a99
leaq 0x10(%rsp), %r12
movaps %xmm0, 0x10(%r12)
movups 0x8c614(%rip), %xmm0    # 0xc9a89
movaps %xmm0, (%r12)
leaq 0x30(%rsp), %rdi
xorl %ebx, %ebx
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x132b0
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x9c462
pushq $0x20
popq %rdx
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %ecx
callq 0x9c7f2
testl %eax, %eax
je 0x3d5ff
leaq 0x4(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x4(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x20(%r15), %r12
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x24(%r15), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x30(%r15), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9cb44
testl %eax, %eax
je 0x3d5ff
leaq 0x38(%r15), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
cmpl $0x3, (%r12)
ja 0x3d587
movl 0x50(%r15), %eax
leaq 0xc(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9c932
jmp 0x3d598
leaq 0x50(%r15), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9cada
testl %eax, %eax
je 0x3d5ff
leaq 0x60(%r15), %rsi
pushq $0x6
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9ca70
testl %eax, %eax
je 0x3d5ff
leaq 0x90(%r15), %rsi
pushq $0x2
popq %rdx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x9ca70
testl %eax, %eax
je 0x3d5ff
addq $0xa0, %r15
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x9c99c
testl %eax, %eax
je 0x3d5ff
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
movl $0x3ec, %edx            # imm = 0x3EC
movl %ebp, %ecx
callq 0x9c7f2
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
movl %ebx, %eax
addq $0x430, %rsp            # imm = 0x430
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop