// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2ImageFileFormat(char const*)
static int
R2ImageFileFormat(const char *filename)
{
  // Initialize result
  int format = 0;
  
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) return 0;
  
  // Read header/magic/unique identifier
  unsigned char buffer[16] = { '\0' };
  int nbytes = fread(buffer, sizeof(unsigned char), 8, fp);
  if (nbytes == 0) return 0;
  else if ((nbytes >= 2) &&
    (buffer[0] == 0xFF) && (buffer[1] == 0xD8)) {
    format = R2_IMAGE_JPEG_FORMAT;
  }
  else if ((nbytes >= 8) &&
    (buffer[0] == 0x89) && (buffer[1] == 0x50) &&
    (buffer[2] == 0x4E) && (buffer[3] == 0x47) &&
    (buffer[4] == 0x0D) && (buffer[5] == 0x0A) &&
    (buffer[6] == 0x1A) && (buffer[7] == 0x0A)) {
    format = R2_IMAGE_PNG_FORMAT; 
  }
  else if ((nbytes >= 4) &&
    (buffer[0] == 0x49) && (buffer[1] == 0x49) &&
    (buffer[2] == 0x2A) && (buffer[3] == 0x00)) {
    format = R2_IMAGE_TIFF_FORMAT; 
  }
  else if ((nbytes >= 2) &&
    (buffer[0] == 0x42) && (buffer[1] == 0x4D)) {
    format = R2_IMAGE_BMP_FORMAT;
  }
  else if ((nbytes >= 2) &&
    (buffer[0] == 0xD4) && (buffer[1] == 0x31)) {
    format = R2_IMAGE_RAW_FORMAT;
  }
  
  // Close file
  fclose(fp);
  
  // Return format
  return format;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x6ceec(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0xc670c
movl $0x0, 0x44(%rsp)
jmp 0xc687f
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xf280
leaq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0xf780
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jne 0xc674e
movl $0x0, 0x44(%rsp)
jmp 0xc687f
cmpl $0x2, 0xc(%rsp)
jl 0xc677a
movzbl 0x10(%rsp), %eax
cmpl $0xff, %eax
jne 0xc677a
movzbl 0x11(%rsp), %eax
cmpl $0xd8, %eax
jne 0xc677a
movl $0x1, 0x34(%rsp)
jmp 0xc686b
cmpl $0x8, 0xc(%rsp)
jl 0xc67e0
movzbl 0x10(%rsp), %eax
cmpl $0x89, %eax
jne 0xc67e0
movzbl 0x11(%rsp), %eax
cmpl $0x50, %eax
jne 0xc67e0
movzbl 0x12(%rsp), %eax
cmpl $0x4e, %eax
jne 0xc67e0
movzbl 0x13(%rsp), %eax
cmpl $0x47, %eax
jne 0xc67e0
movzbl 0x14(%rsp), %eax
cmpl $0xd, %eax
jne 0xc67e0
movzbl 0x15(%rsp), %eax
cmpl $0xa, %eax
jne 0xc67e0
movzbl 0x16(%rsp), %eax
cmpl $0x1a, %eax
jne 0xc67e0
movzbl 0x17(%rsp), %eax
cmpl $0xa, %eax
jne 0xc67e0
movl $0x2, 0x34(%rsp)
jmp 0xc6869
cmpl $0x4, 0xc(%rsp)
jl 0xc6819
movzbl 0x10(%rsp), %eax
cmpl $0x49, %eax
jne 0xc6819
movzbl 0x11(%rsp), %eax
cmpl $0x49, %eax
jne 0xc6819
movzbl 0x12(%rsp), %eax
cmpl $0x2a, %eax
jne 0xc6819
movzbl 0x13(%rsp), %eax
cmpl $0x0, %eax
jne 0xc6819
movl $0x4, 0x34(%rsp)
jmp 0xc6867
cmpl $0x2, 0xc(%rsp)
jl 0xc683e
movzbl 0x10(%rsp), %eax
cmpl $0x42, %eax
jne 0xc683e
movzbl 0x11(%rsp), %eax
cmpl $0x4d, %eax
jne 0xc683e
movl $0x3, 0x34(%rsp)
jmp 0xc6865
cmpl $0x2, 0xc(%rsp)
jl 0xc6863
movzbl 0x10(%rsp), %eax
cmpl $0xd4, %eax
jne 0xc6863
movzbl 0x11(%rsp), %eax
cmpl $0x31, %eax
jne 0xc6863
movl $0x5, 0x34(%rsp)
jmp 0xc6865
jmp 0xc6867
jmp 0xc6869
jmp 0xc686b
jmp 0xc686d
movq 0x28(%rsp), %rdi
callq 0xf4b0
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)