// File: /workspace/github_cpp_makefile_star2_O2/lvandeve[P]lodepng/lodepng_util.cpp
// Function: lodepng::parseICC(lodepng::LodePNGICC*, unsigned char const*, unsigned long)
static unsigned parseICC(LodePNGICC* icc, const unsigned char* data, size_t size) {
  size_t i, j;
  size_t pos = 0;
  unsigned version;
  unsigned inputspace;
  size_t numtags;

  if(size < 132) return 1; /* Too small to be a valid icc profile. */

  icc->has_chromaticity = 0;
  icc->has_whitepoint = 0;
  icc->has_trc = 0;
  icc->has_chad = 0;

  icc->trc[0].type = icc->trc[1].type = icc->trc[2].type = 0;
  icc->white[0] = icc->white[1] = icc->white[2] = 0;
  icc->red[0] = icc->red[1] = icc->red[2] = 0;
  icc->green[0] = icc->green[1] = icc->green[2] = 0;
  icc->blue[0] = icc->blue[1] = icc->blue[2] = 0;

  pos = 8;
  version = decodeICCUint32(data, size, &pos);
  if(pos >= size) return 1;
  icc->version_major = (int)((version >> 24) & 255);
  icc->version_minor = (int)((version >> 20) & 15);
  icc->version_bugfix = (int)((version >> 16) & 15);

  pos = 16;
  inputspace = decodeICCUint32(data, size, &pos);
  if(pos >= size) return 1;
  if(inputspace == 0x47524159) {
    /* The string  "GRAY" as unsigned 32-bit int. */
    icc->inputspace = 1;
  } else if(inputspace == 0x52474220) {
    /* The string  "RGB " as unsigned 32-bit int. */
    icc->inputspace = 2;
  } else {
    /* unsupported by PNG (CMYK, YCbCr, Lab, HSV, ...) */
    icc->inputspace = 0;
  }

  /* Should always be 0.9642, 1.0, 0.8249 */
  pos = 68;
  icc->illuminant[0] = decodeICC15Fixed16(data, size, &pos);
  icc->illuminant[1] = decodeICC15Fixed16(data, size, &pos);
  icc->illuminant[2] = decodeICC15Fixed16(data, size, &pos);

  pos = 128;
  numtags = decodeICCUint32(data, size, &pos);
  if(pos >= size) return 1;
  /* scan for tags we want to handle */
  for(i = 0; i < numtags; i++) {
    size_t offset;
    unsigned tagsize;
    size_t namepos = pos;
    pos += 4;
    offset = decodeICCUint32(data, size, &pos);
    tagsize = decodeICCUint32(data, size, &pos);
    if(pos >= size || offset >= size) return 1;
    if(offset + tagsize > size) return 1;
    if(tagsize < 8) return 1;

    if(isICCword(data, size, namepos, "wtpt")) {
      offset += 8; /* skip tag and reserved */
      icc->white[0] = decodeICC15Fixed16(data, size, &offset);
      icc->white[1] = decodeICC15Fixed16(data, size, &offset);
      icc->white[2] = decodeICC15Fixed16(data, size, &offset);
      icc->has_whitepoint = 1;
    } else if(isICCword(data, size, namepos, "rXYZ")) {
      offset += 8; /* skip tag and reserved */
      icc->red[0] = decodeICC15Fixed16(data, size, &offset);
      icc->red[1] = decodeICC15Fixed16(data, size, &offset);
      icc->red[2] = decodeICC15Fixed16(data, size, &offset);
      icc->has_chromaticity = 1;
    } else if(isICCword(data, size, namepos, "gXYZ")) {
      offset += 8; /* skip tag and reserved */
      icc->green[0] = decodeICC15Fixed16(data, size, &offset);
      icc->green[1] = decodeICC15Fixed16(data, size, &offset);
      icc->green[2] = decodeICC15Fixed16(data, size, &offset);
      icc->has_chromaticity = 1;
    } else if(isICCword(data, size, namepos, "bXYZ")) {
      offset += 8; /* skip tag and reserved */
      icc->blue[0] = decodeICC15Fixed16(data, size, &offset);
      icc->blue[1] = decodeICC15Fixed16(data, size, &offset);
      icc->blue[2] = decodeICC15Fixed16(data, size, &offset);
      icc->has_chromaticity = 1;
    } else if(isICCword(data, size, namepos, "chad")) {
      offset += 8; /* skip datatype keyword "sf32" and reserved */
      for(j = 0; j < 9; j++) {
        icc->chad[j] = decodeICC15Fixed16(data, size, &offset);
      }
      icc->has_chad = 1;
    } else if(isICCword(data, size, namepos, "rTRC") ||
              isICCword(data, size, namepos, "gTRC") ||
              isICCword(data, size, namepos, "bTRC") ||
              isICCword(data, size, namepos, "kTRC")) {
      char c = (char)data[namepos];
      /* both 'k' and 'r' are stored in channel 0 */
      int channel = (c == 'b') ? 2 : (c == 'g' ? 1 : 0);
      /* "curv": linear, gamma power or LUT */
      if(isICCword(data, size, offset, "curv")) {
        size_t count;
        LodePNGICCCurve* trc = &icc->trc[channel];
        icc->has_trc = 1;
        offset += 8; /* skip tag "curv" and reserved */
        count = decodeICCUint32(data, size, &offset);
        if(count == 0) {
          trc->type = 0; /* linear */
        } else if(count == 1) {
          trc->type = 2; /* gamma */
          trc->gamma = decodeICCUint16(data, size, &offset) / 256.0f;
        } else {
          trc->type = 1; /* LUT */
          if(offset + count * 2 > size || count > 16777216) return 1; /* also avoid crazy count */
          trc->lut_size = count;
          trc->lut = (float*)lodepng_malloc(count * sizeof(float));
          for(j = 0; j < count; j++) {
            trc->lut[j] = decodeICCUint16(data, size, &offset) * (1.0f / 65535.0f);
          }
        }
      }
      /* "para": parametric formula with gamma power, multipliers, biases and comparison point */
      /* TODO: test this on a realistic sample */
      if(isICCword(data, size, offset, "para")) {
        unsigned type;
        LodePNGICCCurve* trc = &icc->trc[channel];
        icc->has_trc = 1;
        offset += 8; /* skip tag "para" and reserved */
        type = decodeICCUint16(data, size, &offset);
        offset += 2;
        if(type > 4) return 1; /* unknown parametric curve type */
        trc->type = type + 2;
        trc->gamma = decodeICC15Fixed16(data, size, &offset);
        if(type >= 1) {
          trc->a = decodeICC15Fixed16(data, size, &offset);
          trc->b = decodeICC15Fixed16(data, size, &offset);
        }
        if(type >= 2) {
          trc->c = decodeICC15Fixed16(data, size, &offset);
        }
        if(type >= 3) {
          trc->d = decodeICC15Fixed16(data, size, &offset);
        }
        if(type == 4) {
          trc->e = decodeICC15Fixed16(data, size, &offset);
          trc->f = decodeICC15Fixed16(data, size, &offset);
        }
      }
      /* TODO: verify: does the "chrm" tag participate in computation so should be parsed? */
    }
    /* Return error if any parse went beyond the filesize. Note that the
    parsing itself was always safe since it bound-checks inside. */
    if(offset > size) return 1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
pushq $0x1
popq %rbx
cmpq $0x84, %rdx
jb 0x100b3
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
andl $0x0, 0x1c(%rdi)
andl $0x0, 0xf0(%rdi)
andl $0x0, 0xb8(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x44(%rdi)
movups %xmm0, 0x54(%rdi)
movups %xmm0, 0x64(%rdi)
movups %xmm0, 0x74(%rdi)
leaq 0x8(%rsp), %r13
movq $0x8, (%r13)
movq %rsi, %rdi
movq %rdx, %rsi
movq %r13, %rdx
callq 0x110df
cmpq %r14, (%r13)
jae 0x100b3
movl %eax, %ecx
shrl $0x18, %ecx
movl %ecx, 0x4(%r12)
movl %eax, %ecx
shrl $0x14, %ecx
andl $0xf, %ecx
movl %ecx, 0x8(%r12)
shrl $0x10, %eax
andl $0xf, %eax
movl %eax, 0xc(%r12)
leaq 0x8(%rsp), %r13
movq $0x10, (%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110df
cmpq %r14, (%r13)
jae 0x100b3
xorl %ecx, %ecx
cmpl $0x52474220, %eax       # imm = 0x52474220
sete %cl
addl %ecx, %ecx
cmpl $0x47524159, %eax       # imm = 0x47524159
pushq $0x1
popq %rbx
cmovel %ebx, %ecx
movl %ecx, (%r12)
leaq 0x8(%rsp), %r13
movq $0x44, (%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x10(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x14(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x18(%r12)
movq $0x80, (%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110df
movq (%r13), %rcx
cmpq %r14, %rcx
jae 0x100b3
movq %rcx, %rdx
leaq 0x80(%r12), %rcx
movq %rcx, 0x18(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movq %rdx, %rbx
cmpq 0x28(%rsp), %rax
je 0x100b1
movq %rbx, %rbp
movq %rax, 0x30(%rsp)
leaq 0x4(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x110df
movl %eax, %r13d
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x110df
movq 0x8(%rsp), %rbx
cmpq %r14, %rbx
jae 0x100ac
cmpl $0x8, %eax
jb 0x100ac
cmpq %r14, %r13
jae 0x100ac
movl %eax, %eax
addq %r13, %rax
cmpq %r14, %rax
ja 0x100ac
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50333(%rip), %rcx     # 0x5ff08
callq 0x1111a
testl %eax, %eax
je 0xfc36
addq $0x8, %r13
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %r13
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x48(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x4c(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x50(%r12)
movl $0x1, 0x44(%r12)
jmp 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x502c7(%rip), %rcx     # 0x5ff0d
callq 0x1111a
testl %eax, %eax
je 0xfc9e
addq $0x8, %r13
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %r13
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x58(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x5c(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x60(%r12)
jmp 0xfd66
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50264(%rip), %rcx     # 0x5ff12
callq 0x1111a
testl %eax, %eax
je 0xfd03
addq $0x8, %r13
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %r13
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x64(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x68(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x6c(%r12)
jmp 0xfd66
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50204(%rip), %rcx     # 0x5ff17
callq 0x1111a
testl %eax, %eax
je 0xfd86
addq $0x8, %r13
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %r13
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x70(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x74(%r12)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x110f6
movss %xmm0, 0x78(%r12)
movl $0x1, 0x54(%r12)
movq 0x30(%rsp), %rax
incq %rax
cmpq %r14, (%rsp)
jbe 0xfb53
jmp 0x100ac
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50186(%rip), %rcx     # 0x5ff1c
callq 0x1111a
testl %eax, %eax
je 0xfdd5
addq $0x8, %r13
movq %r13, (%rsp)
xorl %r13d, %r13d
cmpq $0x9, %r13
je 0xfdca
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x20(%r12,%r13,4)
incq %r13
jmp 0xfdaa
movl $0x1, 0x1c(%r12)
jmp 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x5013c(%rip), %rcx     # 0x5ff21
callq 0x1111a
testl %eax, %eax
jne 0xfe3d
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50128(%rip), %rcx     # 0x5ff26
callq 0x1111a
testl %eax, %eax
jne 0xfe3d
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50114(%rip), %rcx     # 0x5ff2b
callq 0x1111a
testl %eax, %eax
jne 0xfe3d
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
leaq 0x50100(%rip), %rcx     # 0x5ff30
callq 0x1111a
testl %eax, %eax
je 0xfd6f
movb (%r15,%rbp), %al
xorl %ebp, %ebp
cmpb $0x67, %al
sete %bpl
cmpb $0x62, %al
pushq $0x2
popq %rax
cmoveq %rax, %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x500d3(%rip), %rcx     # 0x5ff35
callq 0x1111a
testl %eax, %eax
je 0xff88
imulq $0x38, %rbp, %rax
addq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x1, 0x7c(%r12)
addq $0x8, %r13
movq %r13, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110df
cmpl $0x1, %eax
je 0xfeb6
testl %eax, %eax
jne 0xfef7
movq 0x10(%rsp), %rax
andl $0x0, (%rax)
movq (%rsp), %r13
jmp 0xff88
movq 0x10(%rsp), %rcx
movl $0x2, (%rcx)
movq (%rsp), %rax
leaq 0x2(%rax), %r13
movq %r13, (%rsp)
xorps %xmm0, %xmm0
cmpq %r14, %r13
ja 0xfeed
movzwl (%r15,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cvtsi2ss %eax, %xmm0
mulss 0x4fe9f(%rip), %xmm0    # 0x5fd8c
movss %xmm0, 0x18(%rcx)
jmp 0xff88
movq %rbp, 0x20(%rsp)
movl %eax, %ebp
movq 0x10(%rsp), %rcx
movl $0x1, (%rcx)
cmpl $0x1000000, %ebp        # imm = 0x1000000
ja 0x100ac
movq (%rsp), %r13
leaq (,%rbp,2), %rax
addq %r13, %rax
cmpq %r14, %rax
ja 0x100ac
movq %rbp, 0x10(%rcx)
leaq (,%rbp,4), %rdi
callq 0x2280
movq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0xff7f
leaq 0x2(%r13), %rdx
xorps %xmm0, %xmm0
cmpq %r14, %rdx
ja 0xff72
movzwl (%r15,%r13), %esi
rolw $0x8, %si
movzwl %si, %esi
cvtsi2ss %esi, %xmm0
mulss 0x4fe16(%rip), %xmm0    # 0x5fd88
movss %xmm0, (%rax,%rcx,4)
incq %rcx
movq %rdx, %r13
jmp 0xff49
movq %r13, (%rsp)
movq 0x20(%rsp), %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x4ffa2(%rip), %rcx     # 0x5ff3a
callq 0x1111a
testl %eax, %eax
je 0xfd6f
movq %rbp, %rcx
movl $0x1, 0x7c(%r12)
leaq 0xa(%r13), %rax
cmpq %r14, %rax
jbe 0xffc6
addq $0xc, %r13
movq %r13, (%rsp)
xorl %ebp, %ebp
jmp 0xffe5
movzwl 0x8(%r15,%r13), %eax
rolw $0x8, %ax
addq $0xc, %r13
movq %r13, (%rsp)
cmpw $0x4, %ax
ja 0x100ac
movzwl %ax, %ebp
imulq $0x38, %rcx, %r13
addq 0x18(%rsp), %r13
leal 0x2(%rbp), %eax
movl %eax, (%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x18(%r13)
testl %ebp, %ebp
je 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x1c(%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x20(%r13)
cmpl $0x1, %ebp
je 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x24(%r13)
cmpl $0x3, %ebp
jb 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x110f6
movss %xmm0, 0x28(%r13)
cmpl $0x4, %ebp
jne 0xfd6f
movq %r15, %rdi
movq %r14, %rsi
movq %rsp, %rbp
movq %rbp, %rdx
callq 0x110f6
movss %xmm0, 0x2c(%r13)
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x110f6
movss %xmm0, 0x30(%r13)
jmp 0xfd6f
pushq $0x1
popq %rbx
jmp 0x100b3
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq