// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: readChunk_iCCP(LodePNGInfo*, LodePNGDecompressSettings const*, unsigned char const*, unsigned long)
static unsigned readChunk_iCCP(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings,
                               const unsigned char* data, size_t chunkLength) {
  unsigned error = 0;
  unsigned i;

  unsigned length, string2_begin;
  ucvector decoded;

  info->iccp_defined = 1;
  if(info->iccp_name) lodepng_clear_icc(info);

  for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
  if(length + 2 >= chunkLength) return 75; /*no null termination, corrupt?*/
  if(length < 1 || length > 79) return 89; /*keyword too short or long*/

  info->iccp_name = (char*)lodepng_malloc(length + 1);
  if(!info->iccp_name) return 83; /*alloc fail*/

  info->iccp_name[length] = 0;
  for(i = 0; i != length; ++i) info->iccp_name[i] = (char)data[i];

  if(data[length + 1] != 0) return 72; /*the 0 byte indicating compression must be 0*/

  string2_begin = length + 2;
  if(string2_begin > chunkLength) return 75; /*no null termination, corrupt?*/

  length = (unsigned)chunkLength - string2_begin;
  ucvector_init(&decoded);
  error = zlib_decompress(&decoded.data, &decoded.size,
                          &data[string2_begin],
                          length, zlibsettings);
  if(!error) {
    if(decoded.size) {
      info->iccp_profile_size = (unsigned int) decoded.size;
      info->iccp_profile = (unsigned char*)lodepng_malloc(decoded.size);
      if(info->iccp_profile) {
        lodepng_memcpy(info->iccp_profile, decoded.data, decoded.size);
      } else {
        error = 83; /* alloc fail */
      }
    } else {
      error = 100; /*invalid ICC profile size*/
    }
  }
  ucvector_cleanup(&decoded);
  return error;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %rax
movl $0x1, 0xe8(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, 0xf0(%rax)
je 0x145f88
movq 0x48(%rsp), %rdi
callq 0x1403c0
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x30(%rsp), %rcx
movb %al, 0x7(%rsp)
jae 0x145fba
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x145fc4
jmp 0x145fd3
jmp 0x145fc6
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x145f90
movl 0x24(%rsp), %eax
addl $0x2, %eax
movl %eax, %eax
cmpq 0x30(%rsp), %rax
jb 0x145ff0
movl $0x4b, 0x54(%rsp)
jmp 0x1461b0
cmpl $0x1, 0x24(%rsp)
jb 0x145ffe
cmpl $0x4f, 0x24(%rsp)
jbe 0x14600b
movl $0x59, 0x54(%rsp)
jmp 0x1461b0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x13ce80
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0xf0(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, 0xf0(%rax)
jne 0x146046
movl $0x53, 0x54(%rsp)
jmp 0x1461b0
movq 0x48(%rsp), %rax
movq 0xf0(%rax), %rax
movl 0x24(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x24(%rsp), %eax
je 0x146098
movq 0x38(%rsp), %rax
movl 0x28(%rsp), %ecx
movb (%rax,%rcx), %dl
movq 0x48(%rsp), %rax
movq 0xf0(%rax), %rax
movl 0x28(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x146062
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1460bc
movl $0x48, 0x54(%rsp)
jmp 0x1461b0
movl 0x24(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpq 0x30(%rsp), %rax
jbe 0x1460df
movl $0x4b, 0x54(%rsp)
jmp 0x1461b0
movq 0x30(%rsp), %rax
subl 0x20(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x148440
leaq 0x8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
addq $0x8, %rsi
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %eax
addq %rax, %rdx
movl 0x24(%rsp), %eax
movl %eax, %ecx
movq 0x40(%rsp), %r8
callq 0x14aac0
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jne 0x14619e
cmpq $0x0, 0x10(%rsp)
je 0x146194
movq 0x10(%rsp), %rax
movl %eax, %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x100(%rax)
movq 0x10(%rsp), %rdi
callq 0x13ce80
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0xf8(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, 0xf8(%rax)
je 0x14618a
movq 0x48(%rsp), %rax
movq 0xf8(%rax), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x14b830
jmp 0x146192
movl $0x53, 0x2c(%rsp)
jmp 0x14619c
movl $0x64, 0x2c(%rsp)
jmp 0x14619e
leaq 0x8(%rsp), %rdi
callq 0x14ff20
movl 0x2c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)