// File: 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:
subq $0x458, %rsp            # imm = 0x458
movq %rdi, 0x448(%rsp)
movq %rsi, 0x440(%rsp)
movq %rdx, 0x438(%rsp)
movq 0x448(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x440(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x145360
movq 0x440(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x1453c0
cmpl $0x0, %eax
jne 0x74a3d
movl $0x0, 0x454(%rsp)
jmp 0x74e18
leaq 0x30(%rsp), %rdi
leaq 0x13118d(%rip), %rsi    # 0x1a5bd6
callq 0x137e0
cmpl $0x0, %eax
je 0x74a7f
movq 0x18(%rsp), %rax
leaq 0x30(%rsp), %rsi
movq 0x10(%rax), %rdx
leaq 0x13117f(%rip), %rdi    # 0x1a5be7
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x440(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x145710
cmpl $0x0, %eax
jne 0x74aad
movl $0x0, 0x454(%rsp)
jmp 0x74e18
cmpl $0x1, 0x2c(%rsp)
je 0x74ac0
movq 0x18(%rsp), %rax
movl $0x1, 0x28(%rax)
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movl 0x28(%rax), %ecx
leaq 0x28(%rsp), %rsi
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74af4
movl $0x0, 0x454(%rsp)
jmp 0x74e18
cmpl $0x1, 0x28(%rsp)
je 0x74b26
movq 0x18(%rsp), %rax
movl 0x2c(%rsp), %esi
movq 0x10(%rax), %rdx
leaq 0x131103(%rip), %rdi    # 0x1a5c12
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74b5c
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0x24, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74b92
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
cmpl $0x2, 0x20(%rax)
jb 0x74bad
movq 0x18(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x17e9dd(%rip), %eax    # 0x1f3588
jbe 0x74bda
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
movl 0x24(%rax), %edx
movq 0x10(%rax), %rcx
leaq 0x13107a(%rip), %rdi    # 0x1a5c3d
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0x30, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145a10
cmpl $0x0, %eax
jne 0x74c10
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0x38, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74c46
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq 0x438(%rsp), %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74c7d
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
cmpl $0x4, 0x20(%rax)
jae 0x74ccc
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movl 0x28(%rax), %ecx
leaq 0x24(%rsp), %rsi
movl $0x1, %edx
callq 0x145610
cmpl $0x0, %eax
jne 0x74cbc
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movslq 0x24(%rsp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x74d04
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0x50, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145990
cmpl $0x0, %eax
jne 0x74d02
movl $0x0, 0x454(%rsp)
jmp 0x74e18
jmp 0x74d04
movq 0x18(%rsp), %rdi
movq 0x440(%rsp), %rax
movq %rax, 0x10(%rsp)
addq $0x58, %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, %rdi
xorl %esi, %esi
callq 0x1bde0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rax
movl 0x28(%rax), %ecx
movl $0x6, %edx
callq 0x145810
cmpl $0x0, %eax
jne 0x74d5a
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rdi
movq 0x440(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x90, %rdi
xorl %esi, %esi
callq 0x1be00
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rax
movl 0x28(%rax), %ecx
movl $0x2, %edx
callq 0x145810
cmpl $0x0, %eax
jne 0x74da6
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
movq %rax, %rsi
addq $0xa0, %rsi
movl 0x28(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x74ddc
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movq 0x18(%rsp), %rax
movq 0x440(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl 0x28(%rax), %ecx
movl $0x3ec, %edx            # imm = 0x3EC
callq 0x1453c0
cmpl $0x0, %eax
jne 0x74e0d
movl $0x0, 0x454(%rsp)
jmp 0x74e18
movl $0x1, 0x454(%rsp)
movl 0x454(%rsp), %eax
addq $0x458, %rsp            # imm = 0x458
retq
nopw (%rax,%rax)