// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rcx, %r14
movq %rdx, %rbx
testq %r8, %r8
setne %al
testq %rcx, %rcx
setne %cl
testb %al, %cl
jne 0x6a384
movl $0x30, 0x1f0(%rbx)
movl $0x30, %eax
jmp 0x6a558
cmpq $0x20, %r8
ja 0x6a39e
movl $0x1b, 0x1f0(%rbx)
movl $0x1b, %eax
jmp 0x6a558
movq %rsi, %r15
movq %rdi, %r12
leaq 0xb8(%rbx), %rdi
callq 0x6840a
xorps %xmm0, %xmm0
movups %xmm0, 0xe0(%rbx)
movabsq $0x800000006, %rax      # imm = 0x800000006
movq %rax, 0xc8(%rbx)
movups %xmm0, 0xd0(%rbx)
xorl %eax, %eax
movl %eax, 0xc0(%rbx)
movq $0x0, 0xb8(%rbx)
movl %eax, 0x15c(%rbx)
movl %eax, 0x16c(%rbx)
movl %eax, 0x174(%rbx)
movl %eax, 0x198(%rbx)
movl %eax, 0x1a0(%rbx)
movups %xmm0, 0x1c0(%rbx)
movups %xmm0, 0x1d0(%rbx)
movups %xmm0, 0x1e0(%rbx)
movups %xmm0, 0xf0(%rbx)
movups %xmm0, 0x100(%rbx)
movups %xmm0, 0x110(%rbx)
movups %xmm0, 0x120(%rbx)
movups %xmm0, 0x130(%rbx)
movl %eax, 0x140(%rbx)
movups %xmm0, 0x1a8(%rbx)
cmpb $-0x77, (%r14)
jne 0x6a549
cmpb $0x50, 0x1(%r14)
jne 0x6a549
cmpb $0x4e, 0x2(%r14)
jne 0x6a549
cmpb $0x47, 0x3(%r14)
jne 0x6a549
cmpb $0xd, 0x4(%r14)
jne 0x6a549
cmpb $0xa, 0x5(%r14)
jne 0x6a549
cmpb $0x1a, 0x6(%r14)
jne 0x6a549
cmpb $0xa, 0x7(%r14)
jne 0x6a549
movl 0x8(%r14), %eax
bswapl %eax
cmpl $0xd, %eax
jne 0x6a561
leaq 0x8(%r14), %rdi
leaq 0x1ea0b(%rip), %rsi     # 0x88ec4
callq 0x6780c
testb %al, %al
je 0x6a572
movl 0x10(%r14), %ecx
movl 0x14(%r14), %eax
bswapl %ecx
testq %r12, %r12
je 0x6a4d9
movl %ecx, (%r12)
bswapl %eax
testq %r15, %r15
je 0x6a4e3
movl %eax, (%r15)
movzbl 0x18(%r14), %esi
movl %esi, 0xcc(%rbx)
movzbl 0x19(%r14), %edi
movl %edi, 0xc8(%rbx)
movzbl 0x1a(%r14), %r12d
movl %r12d, 0xb8(%rbx)
movzbl 0x1b(%r14), %r15d
movl %r15d, 0xbc(%rbx)
movzbl 0x1c(%r14), %ebp
movl %ebp, 0xc0(%rbx)
testl %ecx, %ecx
je 0x6a583
testl %eax, %eax
je 0x6a583
callq 0x6a639
movl %eax, 0x1f0(%rbx)
testl %eax, %eax
jne 0x6a558
testb %r12b, %r12b
je 0x6a594
movl $0x20, 0x1f0(%rbx)
movl $0x20, %eax
jmp 0x6a558
movl $0x1c, 0x1f0(%rbx)
movl $0x1c, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x5e, 0x1f0(%rbx)
movl $0x5e, %eax
jmp 0x6a558
movl $0x1d, 0x1f0(%rbx)
movl $0x1d, %eax
jmp 0x6a558
movl $0x5d, 0x1f0(%rbx)
movl $0x5d, %eax
jmp 0x6a558
testb %r15b, %r15b
je 0x6a5aa
movl $0x21, 0x1f0(%rbx)
movl $0x21, %eax
jmp 0x6a558
cmpb $0x2, %bpl
jb 0x6a5c1
movl $0x22, 0x1f0(%rbx)
movl $0x22, %eax
jmp 0x6a558
xorl %eax, %eax
cmpl $0x0, 0x20(%rbx)
jne 0x6a558
movzbl 0x1d(%r14), %edx
movzbl 0x1e(%r14), %ecx
shll $0x10, %ecx
movzbl 0x1f(%r14), %edi
shll $0x8, %edi
movzbl 0x20(%r14), %r8d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xc, %r9d
leaq 0x1e00b(%rip), %r10     # 0x88600
movb (%r14,%r9), %r11b
xorb %sil, %r11b
movzbl %r11b, %r11d
shrl $0x8, %esi
xorl (%r10,%r11,4), %esi
incq %r9
cmpq $0x1d, %r9
jne 0x6a5f5
shll $0x18, %edx
orl %edx, %ecx
orl %edi, %ecx
orl %r8d, %ecx
xorl %ecx, %esi
cmpl $-0x1, %esi
je 0x6a558
movl $0x39, 0x1f0(%rbx)
movl $0x39, %eax
jmp 0x6a558