// File: /workspace/github_cpp_makefile_star2_O2/lvandeve[P]lodepng/lodepng_util.cpp
// Function: lodepng::convertToXYZ(float*, float*, unsigned char const*, unsigned int, unsigned int, LodePNGState const*)
unsigned convertToXYZ(float* out, float whitepoint[3], const unsigned char* in,
                      unsigned w, unsigned h, const LodePNGState* state) {
  unsigned error = 0;
  size_t i, n, bytes;
  const LodePNGColorMode* mode_in = &state->info_raw;
  const LodePNGInfo* info = &state->info_png;
  unsigned char* data = 0;
  float* gammatable = 0;
  int bit16 = mode_in->bitdepth > 8;
  size_t num = bit16 ? 65536 : 256;
  LodePNGColorMode tempmode = lodepng_color_mode_make(LCT_RGBA, bit16 ? 16 : 8);

  unsigned use_icc = 0;
  LodePNGICC icc;
  lodepng_icc_init(&icc);
  if(info->iccp_defined) {
    error = parseICC(&icc, info->iccp_profile, info->iccp_profile_size);
    if(error) goto cleanup; /* corrupted ICC profile */
    use_icc = validateICC(&icc);
  }

  if(lodepng_mulofl((size_t)w, (size_t)h, &n)) error = 92;
  if(error) goto cleanup;
  if(lodepng_mulofl(n, bit16 ? 8 : 4, &bytes)) error = 92;
  if(error) goto cleanup;

  data = (unsigned char*)lodepng_malloc(bytes);
  error = lodepng_convert(data, in, &tempmode, mode_in, w, h);
  if(error) goto cleanup;

  /* Handle transfer function */
  {
    float* gammatable_r;
    float* gammatable_g;
    float* gammatable_b;

    /* RGB ICC, can have three different transfer functions */
    if(use_icc && icc.inputspace == 2) {
      gammatable = (float*)lodepng_malloc(num * 3 * sizeof(float));
      gammatable_r = &gammatable[num * 0];
      gammatable_g = &gammatable[num * 1];
      gammatable_b = &gammatable[num * 2];
      convertToXYZ_gamma_table(gammatable_r, num, 0, info, use_icc, &icc);
      convertToXYZ_gamma_table(gammatable_g, num, 1, info, use_icc, &icc);
      convertToXYZ_gamma_table(gammatable_b, num, 2, info, use_icc, &icc);
    } else {
      gammatable = (float*)lodepng_malloc(num * sizeof(float));
      gammatable_r = gammatable_g = gammatable_b = gammatable;
      convertToXYZ_gamma_table(gammatable, num, 0, info, use_icc, &icc);
    }

    if(bit16) {
      for(i = 0; i < n; i++) {
        out[i * 4 + 0] = gammatable_r[data[i * 8 + 0] * 256u + data[i * 8 + 1]];
        out[i * 4 + 1] = gammatable_g[data[i * 8 + 2] * 256u + data[i * 8 + 3]];
        out[i * 4 + 2] = gammatable_b[data[i * 8 + 4] * 256u + data[i * 8 + 5]];
        out[i * 4 + 3] = (data[i * 8 + 6] * 256 + data[i * 8 + 7]) * (1 / 65535.0f);
      }
    } else {
      for(i = 0; i < n; i++) {
        out[i * 4 + 0] = gammatable_r[data[i * 4 + 0]];
        out[i * 4 + 1] = gammatable_g[data[i * 4 + 1]];
        out[i * 4 + 2] = gammatable_b[data[i * 4 + 2]];
        out[i * 4 + 3] = data[i * 4 + 3] * (1 / 255.0f);
      }
    }
  }

  error = convertToXYZ_chrm(out, w, h, info, use_icc, &icc, whitepoint);
  if(error) goto cleanup;

cleanup:
  lodepng_icc_cleanup(&icc);
  lodepng_free(data);
  lodepng_free(gammatable);
  return error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r9, %r15
movl %r8d, %r13d
movl %ecx, %ebx
movq %rdx, 0x8(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
movl 0xac(%r9), %r14d
xorl %eax, %eax
cmpl $0x9, %r14d
setae %al
movl $0x10000, %ecx          # imm = 0x10000
movl $0x100, %ebp            # imm = 0x100
cmovaeq %rcx, %rbp
leal 0x8(,%rax,8), %edx
leaq 0x38(%rsp), %rdi
pushq $0x6
popq %rsi
callq 0x4a1b
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0x120(%rsp)
movups %xmm0, 0x158(%rsp)
cmpl $0x0, 0x1cc(%r15)
movl %ebx, 0x1c(%rsp)
je 0xf745
movl %r13d, %ebx
movl %r14d, %r13d
movq 0x1d8(%r15), %rsi
movl 0x1e0(%r15), %edx
leaq 0x60(%rsp), %rdi
callq 0xf9f5
testl %eax, %eax
je 0xf74e
movl %eax, %r14d
xorl %r13d, %r13d
jmp 0xf7d4
movq %r12, 0x20(%rsp)
xorl %esi, %esi
jmp 0xf769
movq %r12, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x100c4
movl %eax, %esi
movl %r13d, %r14d
movl %ebx, %r13d
movl 0x1c(%rsp), %ebx
xorl %eax, %eax
cmpl $0x9, %r14d
setae %al
movl %ebx, %ecx
movl %r13d, %r12d
imulq %rcx, %r12
leaq 0x4(,%rax,4), %rcx
movq %r12, %rax
mulq %rcx
jno 0xf794
xorl %r13d, %r13d
pushq $0x5c
popq %r14
jmp 0xf7d4
movl %r14d, 0x14(%rsp)
movl %esi, 0x18(%rsp)
leaq 0xa8(%r15), %r14
movq %rax, %rdi
callq 0x2280
movl %ebx, %r8d
movl %r13d, %ebx
movq %rax, %r13
leaq 0x38(%rsp), %rdx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rcx
movl %ebx, %r9d
callq 0x5390
testl %eax, %eax
je 0xf806
movl %eax, %r14d
xorl %r15d, %r15d
leaq 0x60(%rsp), %rdi
callq 0xf66b
movq %r13, %rdi
callq 0x2260
movq %r15, %rdi
callq 0x2260
movl %r14d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, 0x2c(%rsp)
addq $0xd0, %r15
movq %r15, 0x8(%rsp)
movl 0x18(%rsp), %ebx
testl %ebx, %ebx
je 0xf89d
cmpl $0x2, 0x60(%rsp)
jne 0xf89d
cmpl $0x9, 0x14(%rsp)
movl $0xc0000, %eax          # imm = 0xC0000
movl $0xc00, %edi            # imm = 0xC00
cmovaeq %rax, %rdi
callq 0x2280
movq %rax, %r15
leaq (%rax,%rbp,4), %r14
leaq (%rax,%rbp,8), %rbx
pushq $0x1
popq %r8
leaq 0x60(%rsp), %r9
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq 0x8(%rsp), %rcx
callq 0x100e8
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rcx
pushq $0x1
popq %r8
leaq 0x60(%rsp), %r9
callq 0x100e8
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rcx
pushq $0x1
popq %r8
leaq 0x60(%rsp), %r9
callq 0x100e8
jmp 0xf8cc
leal (,%rbp,4), %edi
callq 0x2280
movq %rax, %r15
leaq 0x60(%rsp), %r9
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq 0x8(%rsp), %rcx
movl %ebx, %r8d
callq 0x100e8
movq %r15, %r14
movq %r15, %rbx
movl 0x14(%rsp), %eax
cmpl $0x8, %eax
jbe 0xf95e
movq 0x20(%rsp), %rax
addq $0xc, %rax
xorl %ecx, %ecx
movss 0x5049c(%rip), %xmm0    # 0x5fd88
cmpq %rcx, %r12
je 0xf9c3
movzwl (%r13,%rcx,8), %edx
rolw $0x8, %dx
movzwl %dx, %edx
movss (%r15,%rdx,4), %xmm1
movss %xmm1, -0xc(%rax)
movzwl 0x2(%r13,%rcx,8), %edx
rolw $0x8, %dx
movzwl %dx, %edx
movss (%r14,%rdx,4), %xmm1
movss %xmm1, -0x8(%rax)
movzwl 0x4(%r13,%rcx,8), %edx
rolw $0x8, %dx
movzwl %dx, %edx
movss (%rbx,%rdx,4), %xmm1
movss %xmm1, -0x4(%rax)
movzwl 0x6(%r13,%rcx,8), %edx
rolw $0x8, %dx
movzwl %dx, %edx
cvtsi2ss %edx, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, (%rax)
incq %rcx
addq $0x10, %rax
jmp 0xf8ec
movq 0x20(%rsp), %rax
addq $0xc, %rax
xorl %ecx, %ecx
movss 0x50413(%rip), %xmm0    # 0x5fd84
cmpq %rcx, %r12
je 0xf9c3
movzbl (%r13,%rcx,4), %edx
movss (%r15,%rdx,4), %xmm1
movss %xmm1, -0xc(%rax)
movzbl 0x1(%r13,%rcx,4), %edx
movss (%r14,%rdx,4), %xmm1
movss %xmm1, -0x8(%rax)
movzbl 0x2(%r13,%rcx,4), %edx
movss (%rbx,%rdx,4), %xmm1
movss %xmm1, -0x4(%rax)
movzbl 0x3(%r13,%rcx,4), %edx
cvtsi2ss %edx, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, (%rax)
incq %rcx
addq $0x10, %rax
jmp 0xf971
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
leaq 0x60(%rsp), %r9
movq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl 0x2c(%rsp), %edx
movq 0x8(%rsp), %rcx
movl 0x18(%rsp), %r8d
callq 0x10232
movl %eax, %r14d
jmp 0xf7d7