// File: Lut99[P]RayTracer/src/lib/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) {
  unsigned width, height;
  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*/
  /* TODO: remove this. One should use a new LodePNGState for new sessions */
  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(lodepng_chunk_length(in + 8) != 13) {
    CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/
  }
  if(!lodepng_chunk_type_equals(in + 8, "IHDR")) {
    CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/
  }

  /*read the values given in the header*/
  width = lodepng_read32bitInt(&in[16]);
  height = lodepng_read32bitInt(&in[20]);
  /*TODO: remove the undocumented feature that allows to give null pointers to width or height*/
  if(w) *w = width;
  if(h) *h = height;
  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];

  /*errors returned only after the parsing so other values are still output*/

  /*error: invalid image size*/
  if(width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93);
  /*error: invalid colortype or bitdepth combination*/
  state->error = checkColorValidity(info->color.colortype, info->color.bitdepth);
  if(state->error) return state->error;
  /*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);

  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*/
    }
  }

  return state->error;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq 0x28(%rsp), %rax
addq $0xb8, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x14453d
cmpq $0x0, 0x20(%rsp)
jne 0x144559
movq 0x28(%rsp), %rax
movl $0x30, 0x1f0(%rax)
movl $0x30, 0x44(%rsp)
jmp 0x144865
cmpq $0x21, 0x18(%rsp)
jae 0x14457d
movq 0x28(%rsp), %rax
movl $0x1b, 0x1f0(%rax)
movl $0x1b, 0x44(%rsp)
jmp 0x144865
movq 0x8(%rsp), %rdi
callq 0x1406f0
movq 0x8(%rsp), %rdi
callq 0x140510
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x89, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x50, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x4e, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x47, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x4(%rax), %eax
cmpl $0xd, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x5(%rax), %eax
cmpl $0xa, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x1a, %eax
jne 0x144602
movq 0x20(%rsp), %rax
movzbl 0x7(%rax), %eax
cmpl $0xa, %eax
je 0x14461e
movq 0x28(%rsp), %rax
movl $0x1c, 0x1f0(%rax)
movl $0x1c, 0x44(%rsp)
jmp 0x144865
movq 0x20(%rsp), %rdi
addq $0x8, %rdi
callq 0x13ee80
cmpl $0xd, %eax
je 0x14464d
movq 0x28(%rsp), %rax
movl $0x5e, 0x1f0(%rax)
movl $0x5e, 0x44(%rsp)
jmp 0x144865
movq 0x20(%rsp), %rdi
addq $0x8, %rdi
leaq 0x3206b(%rip), %rsi     # 0x1766c8
callq 0x13eee0
cmpb $0x0, %al
jne 0x144682
movq 0x28(%rsp), %rax
movl $0x1d, 0x1f0(%rax)
movl $0x1d, 0x44(%rsp)
jmp 0x144865
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x13ea10
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rdi
addq $0x14, %rdi
callq 0x13ea10
movl %eax, 0x10(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0x1446b9
movl 0x14(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x30(%rsp)
je 0x1446cc
movl 0x10(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rax
movzbl 0x18(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x20(%rsp), %rax
movzbl 0x19(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x20(%rsp), %rax
movzbl 0x1a(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rax
movzbl 0x1b(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x20(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x14(%rsp)
je 0x14472e
cmpl $0x0, 0x10(%rsp)
jne 0x14474a
movq 0x28(%rsp), %rax
movl $0x5d, 0x1f0(%rax)
movl $0x5d, 0x44(%rsp)
jmp 0x144865
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edi
movq 0x8(%rsp), %rax
movl 0x14(%rax), %esi
callq 0x144870
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x1f0(%rax)
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1f0(%rax)
je 0x14478e
movq 0x28(%rsp), %rax
movl 0x1f0(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0x144865
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax)
je 0x1447b4
movq 0x28(%rsp), %rax
movl $0x20, 0x1f0(%rax)
movl $0x20, 0x44(%rsp)
jmp 0x144865
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x1447db
movq 0x28(%rsp), %rax
movl $0x21, 0x1f0(%rax)
movl $0x21, 0x44(%rsp)
jmp 0x144865
movq 0x8(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jbe 0x1447ff
movq 0x28(%rsp), %rax
movl $0x22, 0x1f0(%rax)
movl $0x22, 0x44(%rsp)
jmp 0x144865
movq 0x28(%rsp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x144856
movq 0x20(%rsp), %rdi
addq $0x1d, %rdi
callq 0x13ea10
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %rdi
addq $0xc, %rdi
movl $0x11, %esi
callq 0x13ee00
movl %eax, (%rsp)
movl 0x4(%rsp), %eax
cmpl (%rsp), %eax
je 0x144854
movq 0x28(%rsp), %rax
movl $0x39, 0x1f0(%rax)
movl $0x39, 0x44(%rsp)
jmp 0x144865
jmp 0x144856
movq 0x28(%rsp), %rax
movl 0x1f0(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop