// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: lodepng_inspect_chunk(LodePNGState*, unsigned long, unsigned char const*, unsigned long)
unsigned lodepng_inspect_chunk(LodePNGState* state, size_t pos,
                               const unsigned char* in, size_t insize) {
  const unsigned char* chunk = in + pos;
  unsigned chunkLength;
  const unsigned char* data;
  unsigned unhandled = 0;
  unsigned error = 0;

  if (pos + 4 > insize) return 30;
  chunkLength = lodepng_chunk_length(chunk);
  if(chunkLength > 2147483647) return 63;
  data = lodepng_chunk_data_const(chunk);
  if(data + chunkLength + 4 > in + insize) return 30;

  if(lodepng_chunk_type_equals(chunk, "PLTE")) {
    error = readChunk_PLTE(&state->info_png.color, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "tRNS")) {
    error = readChunk_tRNS(&state->info_png.color, data, chunkLength);
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
  } else if(lodepng_chunk_type_equals(chunk, "bKGD")) {
    error = readChunk_bKGD(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "tEXt")) {
    error = readChunk_tEXt(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "zTXt")) {
    error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "iTXt")) {
    error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "tIME")) {
    error = readChunk_tIME(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "pHYs")) {
    error = readChunk_pHYs(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "gAMA")) {
    error = readChunk_gAMA(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "cHRM")) {
    error = readChunk_cHRM(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "sRGB")) {
    error = readChunk_sRGB(&state->info_png, data, chunkLength);
  } else if(lodepng_chunk_type_equals(chunk, "iCCP")) {
    error = readChunk_iCCP(&state->info_png, &state->decoder.zlibsettings, data, chunkLength);
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
  } else {
    /* unhandled chunk is ok (is not an error) */
    unhandled = 1;
  }

  if(!error && !unhandled && !state->decoder.ignore_crc) {
    if(lodepng_chunk_check_crc(chunk)) return 57; /*invalid CRC*/
  }

  return error;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x28(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movq 0x30(%rsp), %rax
addq $0x4, %rax
cmpq 0x20(%rsp), %rax
jbe 0x1449c3
movl $0x1e, 0x44(%rsp)
jmp 0x144d35
movq 0x18(%rsp), %rdi
callq 0x13ee80
movl %eax, 0x14(%rsp)
cmpl $0x7fffffff, 0x14(%rsp) # imm = 0x7FFFFFFF
jbe 0x1449e8
movl $0x3f, 0x44(%rsp)
jmp 0x144d35
movq 0x18(%rsp), %rdi
callq 0x13f040
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
addq %rcx, %rax
addq $0x4, %rax
movq 0x28(%rsp), %rcx
addq 0x20(%rsp), %rcx
cmpq %rcx, %rax
jbe 0x144a23
movl $0x1e, 0x44(%rsp)
jmp 0x144d35
movq 0x18(%rsp), %rdi
leaq 0x31c9e(%rip), %rsi     # 0x1766cd
callq 0x13eee0
cmpb $0x0, %al
je 0x144a60
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
addq $0x10, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x144d40
movl %eax, (%rsp)
jmp 0x144cfb
movq 0x18(%rsp), %rdi
leaq 0x31c66(%rip), %rsi     # 0x1766d2
callq 0x13eee0
cmpb $0x0, %al
je 0x144a9d
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
addq $0x10, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x144ec0
movl %eax, (%rsp)
jmp 0x144cf9
movq 0x18(%rsp), %rdi
leaq 0x31c2e(%rip), %rsi     # 0x1766d7
callq 0x13eee0
cmpb $0x0, %al
je 0x144ad6
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145050
movl %eax, (%rsp)
jmp 0x144cf7
movq 0x18(%rsp), %rdi
leaq 0x31bfa(%rip), %rsi     # 0x1766dc
callq 0x13eee0
cmpb $0x0, %al
je 0x144b0f
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x1451e0
movl %eax, (%rsp)
jmp 0x144cf5
movq 0x18(%rsp), %rdi
leaq 0x31bc6(%rip), %rsi     # 0x1766e1
callq 0x13eee0
cmpb $0x0, %al
je 0x144b4d
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl 0x14(%rsp), %eax
movl %eax, %ecx
callq 0x1453e0
movl %eax, (%rsp)
jmp 0x144cf3
movq 0x18(%rsp), %rdi
leaq 0x31b8d(%rip), %rsi     # 0x1766e6
callq 0x13eee0
cmpb $0x0, %al
je 0x144b8b
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl 0x14(%rsp), %eax
movl %eax, %ecx
callq 0x1455e0
movl %eax, (%rsp)
jmp 0x144cf1
movq 0x18(%rsp), %rdi
leaq 0x31b54(%rip), %rsi     # 0x1766eb
callq 0x13eee0
cmpb $0x0, %al
je 0x144bc4
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145aa0
movl %eax, (%rsp)
jmp 0x144cef
movq 0x18(%rsp), %rdi
leaq 0x31b20(%rip), %rsi     # 0x1766f0
callq 0x13eee0
cmpb $0x0, %al
je 0x144bfd
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145b70
movl %eax, (%rsp)
jmp 0x144ced
movq 0x18(%rsp), %rdi
leaq 0x31aec(%rip), %rsi     # 0x1766f5
callq 0x13eee0
cmpb $0x0, %al
je 0x144c36
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145c40
movl %eax, (%rsp)
jmp 0x144ceb
movq 0x18(%rsp), %rdi
leaq 0x31ab8(%rip), %rsi     # 0x1766fa
callq 0x13eee0
cmpb $0x0, %al
je 0x144c6c
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145cc0
movl %eax, (%rsp)
jmp 0x144ce9
movq 0x18(%rsp), %rdi
leaq 0x31a87(%rip), %rsi     # 0x1766ff
callq 0x13eee0
cmpb $0x0, %al
je 0x144ca2
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
movl %eax, %edx
callq 0x145ef0
movl %eax, (%rsp)
jmp 0x144ce7
movq 0x18(%rsp), %rdi
leaq 0x31a56(%rip), %rsi     # 0x176704
callq 0x13eee0
cmpb $0x0, %al
je 0x144cdd
movq 0x38(%rsp), %rdi
addq $0xb8, %rdi
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl 0x14(%rsp), %eax
movl %eax, %ecx
callq 0x145f40
movl %eax, (%rsp)
jmp 0x144ce5
movl $0x1, 0x4(%rsp)
jmp 0x144ce7
jmp 0x144ce9
jmp 0x144ceb
jmp 0x144ced
jmp 0x144cef
jmp 0x144cf1
jmp 0x144cf3
jmp 0x144cf5
jmp 0x144cf7
jmp 0x144cf9
jmp 0x144cfb
cmpl $0x0, (%rsp)
jne 0x144d2e
cmpl $0x0, 0x4(%rsp)
jne 0x144d2e
movq 0x38(%rsp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x144d2e
movq 0x18(%rsp), %rdi
callq 0x13f050
cmpl $0x0, %eax
je 0x144d2c
movl $0x39, 0x44(%rsp)
jmp 0x144d35
jmp 0x144d2e
movl (%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop