// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
xorl %ebx, %ebx
movq %rsi, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x9c462
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x9c478
testl %eax, %eax
je 0x3d681
leaq 0x8c348(%rip), %rsi     # 0xc999f
leaq 0x10(%rsp), %rdi
pushq $0x11
popq %rdx
callq 0x13520
testl %eax, %eax
je 0x3d695
movq 0x10(%r14), %rdx
leaq 0x8c33d(%rip), %rdi     # 0xc99b0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x9ce26
movl %ebx, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4(%rsp), %rsi
pushq $0x1
popq %rbp
xorl %ebx, %ebx
movq %r15, %rdi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x9c61d
testl %eax, %eax
je 0x3d681
cmpl $0x1, 0x4(%rsp)
jne 0x3d6bc
movl 0x28(%r14), %ebp
jmp 0x3d6c4
movl $0x1, 0x28(%r14)
leaq 0xc(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movl %ebp, %ecx
callq 0x9c61d
testl %eax, %eax
je 0x3d681
cmpl $0x1, 0xc(%rsp)
jne 0x3d73f
leaq 0x20(%r14), %r13
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x9c61d
testl %eax, %eax
je 0x3d681
leaq 0x24(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c61d
testl %eax, %eax
je 0x3d681
movl (%r13), %esi
leal -0x7(%rsi), %eax
cmpl $-0x6, %eax
ja 0x3d75c
movl 0x24(%r14), %edx
movq 0x10(%r14), %rcx
leaq 0x8c2d5(%rip), %rdi     # 0xc9a06
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x9ce26
jmp 0x3d681
movl 0x4(%rsp), %esi
movq 0x10(%r14), %rdx
leaq 0x8c28d(%rip), %rdi     # 0xc99db
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x9ce26
jmp 0x3d681
leaq 0x30(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c79a
testl %eax, %eax
je 0x3d681
leaq 0x38(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c61d
testl %eax, %eax
je 0x3d681
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x9c61d
testl %eax, %eax
je 0x3d681
cmpl $0x3, (%r13)
ja 0x3d7da
movl 0x28(%r14), %ecx
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c5ab
testl %eax, %eax
je 0x3d681
movslq 0x8(%rsp), %rax
movq %rax, 0x50(%r14)
jmp 0x3d7f5
leaq 0x50(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c742
testl %eax, %eax
je 0x3d681
leaq 0x60(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x6
popq %rdx
movq %r15, %rdi
callq 0x9c6cd
testl %eax, %eax
je 0x3d681
leaq 0x90(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x2
popq %rdx
movq %r15, %rdi
callq 0x9c6cd
testl %eax, %eax
je 0x3d681
leaq 0xa0(%r14), %rsi
movl 0x28(%r14), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x9c61d
testl %eax, %eax
je 0x3d681
movl 0x28(%r14), %ecx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movl $0x3ec, %edx            # imm = 0x3EC
callq 0x9c478
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
jmp 0x3d681