// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2PixelDatabase.cpp
// Function: gaps::R2PixelDatabase::ReadHeader(_IO_FILE*)
int R2PixelDatabase::
ReadHeader(FILE *fp)
{
  // Seek to beginning of file
  RNFileSeek(fp, 0, RN_FILE_SEEK_SET);

  // Read magic string
  char buffer[1024]; 
  if (!RNReadChar(fp, buffer, 32, 0)) return 0;
  if (strcmp(buffer, "R2PixelDatabase")) {
    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;
  
  // Read entry info
  if (!RNReadUnsignedLongLong(fp, &entries_seek, 1, swap_endian)) return 0;
  if (!RNReadUnsignedInt(fp, &entries_count, 1, swap_endian)) return 0;

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

// Assembly:
subq $0x428, %rsp            # imm = 0x428
movq %rdi, 0x418(%rsp)
movq %rsi, 0x410(%rsp)
movq 0x418(%rsp), %rax
movq %rax, (%rsp)
movq 0x410(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x145360
movq 0x410(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq 0x1453c0
cmpl $0x0, %eax
jne 0x125f84
movl $0x0, 0x424(%rsp)
jmp 0x126172
leaq 0x10(%rsp), %rdi
leaq 0x82c5e(%rip), %rsi     # 0x1a8bee
callq 0x137e0
cmpl $0x0, %eax
je 0x125fc5
movq (%rsp), %rax
leaq 0x10(%rsp), %rsi
movq 0x18(%rax), %rdx
leaq 0x7fc39(%rip), %rdi     # 0x1a5be7
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq 0x410(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x145710
cmpl $0x0, %eax
jne 0x125ff3
movl $0x0, 0x424(%rsp)
jmp 0x126172
cmpl $0x1, 0xc(%rsp)
je 0x126005
movq (%rsp), %rax
movl $0x1, 0x30(%rax)
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
movl 0x30(%rax), %ecx
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x126038
movl $0x0, 0x424(%rsp)
jmp 0x126172
cmpl $0x1, 0x8(%rsp)
je 0x126069
movq (%rsp), %rax
movl 0xc(%rsp), %esi
movq 0x18(%rax), %rdx
leaq 0x7fbc0(%rip), %rdi     # 0x1a5c12
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
movl 0x30(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x12609e
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
movq %rax, %rsi
addq $0x2c, %rsi
movl 0x30(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x1260d3
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
movq %rax, %rsi
addq $0x38, %rsi
movl 0x30(%rax), %ecx
movl $0x1, %edx
callq 0x145a10
cmpl $0x0, %eax
jne 0x126105
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
movq %rax, %rsi
addq $0x34, %rsi
movl 0x30(%rax), %ecx
movl $0x1, %edx
callq 0x145710
cmpl $0x0, %eax
jne 0x126137
movl $0x0, 0x424(%rsp)
jmp 0x126172
movq (%rsp), %rax
movq 0x410(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl 0x30(%rax), %ecx
movl $0x400, %edx            # imm = 0x400
callq 0x1453c0
cmpl $0x0, %eax
jne 0x126167
movl $0x0, 0x424(%rsp)
jmp 0x126172
movl $0x1, 0x424(%rsp)
movl 0x424(%rsp), %eax
addq $0x428, %rsp            # imm = 0x428
retq
nopw %cs:(%rax,%rax)