// File: logicx24[P]RayTracer/lodepng.cpp
// Function: lodepng_inspect(unsigned int*, unsigned int*, LodePNGState*, unsigned char const*, unsigned long)
unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state,
                         const unsigned char* in, size_t insize)
{
  LodePNGInfo* info = &state->info_png;
  if(insize == 0 || in == 0)
  {
    CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/
  }
  if(insize < 33)
  {
    CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/
  }

  /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/
  lodepng_info_cleanup(info);
  lodepng_info_init(info);

  if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71
     || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10)
  {
    CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/
  }
  if(in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R')
  {
    CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/
  }

  /*read the values given in the header*/
  *w = lodepng_read32bitInt(&in[16]);
  *h = lodepng_read32bitInt(&in[20]);
  info->color.bitdepth = in[24];
  info->color.colortype = (LodePNGColorType)in[25];
  info->compression_method = in[26];
  info->filter_method = in[27];
  info->interlace_method = in[28];

  if(*w == 0 || *h == 0)
  {
    CERROR_RETURN_ERROR(state->error, 93);
  }

  if(!state->decoder.ignore_crc)
  {
    unsigned CRC = lodepng_read32bitInt(&in[29]);
    unsigned checksum = lodepng_crc32(&in[12], 17);
    if(CRC != checksum)
    {
      CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/
    }
  }

  /*error: only compression method 0 is allowed in the specification*/
  if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32);
  /*error: only filter method 0 is allowed in the specification*/
  if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33);
  /*error: only interlace methods 0 and 1 exist in the specification*/
  if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34);

  state->error = checkColorValidity(info->color.colortype, info->color.bitdepth);
  return state->error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rax
addq $0xb8, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x17478
cmpq $0x0, -0x28(%rbp)
jne 0x17492
movq -0x20(%rbp), %rax
movl $0x30, 0x1a0(%rax)
movl $0x30, -0x4(%rbp)
jmp 0x17717
cmpq $0x21, -0x30(%rbp)
jae 0x174b3
movq -0x20(%rbp), %rax
movl $0x1b, 0x1a0(%rax)
movl $0x1b, -0x4(%rbp)
jmp 0x17717
movq -0x38(%rbp), %rdi
callq 0x14330
movq -0x38(%rbp), %rdi
callq 0x141a0
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x89, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x50, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x4e, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x47, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0xd, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0xa, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x1a, %eax
jne 0x1752e
movq -0x28(%rbp), %rax
movzbl 0x7(%rax), %eax
cmpl $0xa, %eax
je 0x17548
movq -0x20(%rbp), %rax
movl $0x1c, 0x1a0(%rax)
movl $0x1c, -0x4(%rbp)
jmp 0x17717
movq -0x28(%rbp), %rax
movzbl 0xc(%rax), %eax
cmpl $0x49, %eax
jne 0x1757c
movq -0x28(%rbp), %rax
movzbl 0xd(%rax), %eax
cmpl $0x48, %eax
jne 0x1757c
movq -0x28(%rbp), %rax
movzbl 0xe(%rax), %eax
cmpl $0x44, %eax
jne 0x1757c
movq -0x28(%rbp), %rax
movzbl 0xf(%rax), %eax
cmpl $0x52, %eax
je 0x17596
movq -0x20(%rbp), %rax
movl $0x1d, 0x1a0(%rax)
movl $0x1d, -0x4(%rbp)
jmp 0x17717
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0x11e40
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
addq $0x14, %rdi
callq 0x11e40
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movzbl 0x18(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movzbl 0x19(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movzbl 0x1a(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movzbl 0x1b(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movzbl 0x1c(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1761c
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x17636
movq -0x20(%rbp), %rax
movl $0x5d, 0x1a0(%rax)
movl $0x5d, -0x4(%rbp)
jmp 0x17717
movq -0x20(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x17689
movq -0x28(%rbp), %rdi
addq $0x1d, %rdi
callq 0x11e40
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
movl $0x11, %esi
callq 0x13080
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
je 0x17687
movq -0x20(%rbp), %rax
movl $0x39, 0x1a0(%rax)
movl $0x39, -0x4(%rbp)
jmp 0x17717
jmp 0x17689
movq -0x38(%rbp), %rax
cmpl $0x0, (%rax)
je 0x176a9
movq -0x20(%rbp), %rax
movl $0x20, 0x1a0(%rax)
movl $0x20, -0x4(%rbp)
jmp 0x17717
movq -0x38(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x176ca
movq -0x20(%rbp), %rax
movl $0x21, 0x1a0(%rax)
movl $0x21, -0x4(%rbp)
jmp 0x17717
movq -0x38(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jbe 0x176eb
movq -0x20(%rbp), %rax
movl $0x22, 0x1a0(%rax)
movl $0x22, -0x4(%rbp)
jmp 0x17717
movq -0x38(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x38(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x17720
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1a0(%rax)
movq -0x20(%rbp), %rax
movl 0x1a0(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq