// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::ReadFileHeader(_IO_FILE*, unsigned int&)
int R3SurfelDatabase::
ReadFileHeader(FILE *fp, unsigned int& nblocks)
{
  // Seek to start of file
  RNFileSeek(fp, 0, RN_FILE_SEEK_SET);

  // Read unique string
  char buffer[1024]; 
  if (!RNReadChar(fp, buffer, 32, 0)) return 0;
  if (strcmp(buffer, "R3SurfelDatabase")) {
    RNFail("Incorrect header (%s) in database file %s\n", buffer, filename);
    return 0;
  }

  // Read endian test
  unsigned int endian_test1, endian_test2;
  if (!RNReadUnsignedInt(fp, &endian_test1, 1, 0)) return 0;
  if (endian_test1 != 1) swap_endian = 1;
  if (!RNReadUnsignedInt(fp, &endian_test2, 1, swap_endian)) return 0;
  if (endian_test2 != 1) {
    RNFail("Incorrect endian (%x) in database file %s\n", endian_test1, filename);
    return 0;
  }

  // Read version
  if (!RNReadUnsignedInt(fp, &major_version, 1, swap_endian)) return 0;
  if (!RNReadUnsignedInt(fp, &minor_version, 1, swap_endian)) return 0;
  if ((major_version < 2) || (major_version > current_major_version)) {
    RNFail("Incorrect version (%d.%d) in database file %s\n", major_version, minor_version, filename);
    return 0;
  }
  
  // Read block info
  if (!RNReadUnsignedLongLong(fp, &file_blocks_offset, 1, swap_endian)) return 0;
  if (!RNReadUnsignedInt(fp, &file_blocks_count, 1, swap_endian)) return 0;
  if (!RNReadUnsignedInt(fp, &nblocks, 1, swap_endian)) return 0;

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

  // Read bounding box
  if (!RNReadDouble(fp, &bbox[0][0], 6, swap_endian)) return 0;

  // Read timestamp range
  if (!RNReadDouble(fp, &timestamp_range[0], 2, swap_endian)) return 0;

  // Read max identifier
  if (!RNReadUnsignedInt(fp, &max_identifier, 1, swap_endian)) return 0;

  // Read extra at end of header
  if (!RNReadChar(fp, buffer, 1004, swap_endian)) return 0;
  
  // Return success
  return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x410, %rsp            # imm = 0x410
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
xorl %ebx, %ebx
movq %rsi, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xabc5a
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0xabc70
testl %eax, %eax
je 0x42a27
movabsq $0x6573616261746144, %rax # imm = 0x6573616261746144
xorq 0x18(%rsp), %rax
movabsq $0x6c65667275533352, %rcx # imm = 0x6C65667275533352
xorq 0x10(%rsp), %rcx
movzbl 0x20(%rsp), %edx
orq %rcx, %rdx
orq %rax, %rdx
je 0x42a3a
movq 0x10(%r14), %rdx
leaq 0x9bfd7(%rip), %rdi     # 0xde9f0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0xac81a
movl %ebx, %eax
addq $0x410, %rsp            # imm = 0x410
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x4(%rsp), %rsi
xorl %ebx, %ebx
movq %r15, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0xabe71
testl %eax, %eax
je 0x42a27
cmpl $0x1, 0x4(%rsp)
jne 0x42a61
movl 0x28(%r14), %ecx
jmp 0x42a6e
movl $0x1, 0x28(%r14)
movl $0x1, %ecx
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
cmpl $0x1, 0xc(%rsp)
jne 0x42aed
leaq 0x20(%r14), %r13
movl 0x28(%r14), %ecx
movq %r15, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
leaq 0x24(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
movl (%r13), %esi
leal -0x7(%rsi), %eax
cmpl $-0x6, %eax
ja 0x42b0a
movl 0x24(%r14), %edx
movq 0x10(%r14), %rcx
leaq 0x9bf67(%rip), %rdi     # 0xdea46
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xac81a
jmp 0x42a27
movl 0x4(%rsp), %esi
movq 0x10(%r14), %rdx
leaq 0x9bf1f(%rip), %rdi     # 0xdea1b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xac81a
jmp 0x42a27
leaq 0x30(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0xac05b
testl %eax, %eax
je 0x42a27
leaq 0x38(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
movl 0x28(%r14), %ecx
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
cmpl $0x3, (%r13)
ja 0x42b90
movl 0x28(%r14), %ecx
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0xabde6
testl %eax, %eax
je 0x42a27
movslq 0x8(%rsp), %rax
movq %rax, 0x50(%r14)
jmp 0x42bad
leaq 0x50(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0xabfe6
testl %eax, %eax
je 0x42a27
leaq 0x60(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x6, %edx
callq 0xabf55
testl %eax, %eax
je 0x42a27
leaq 0x90(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x2, %edx
callq 0xabf55
testl %eax, %eax
je 0x42a27
leaq 0xa0(%r14), %rsi
movl 0x28(%r14), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0xabe71
testl %eax, %eax
je 0x42a27
movl 0x28(%r14), %ecx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movl $0x3ec, %edx            # imm = 0x3EC
callq 0xabc70
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
jmp 0x42a27