// File: 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
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x28(%rbp), %rax
addq $0xa8, %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x8, 0x4(%rax)
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %edx
movl $0x100, %eax            # imm = 0x100
movl $0x10000, %ecx          # imm = 0x10000
cmpl $0x0, %edx
cmovnel %ecx, %eax
cltq
movq %rax, -0x78(%rbp)
movl -0x6c(%rbp), %ecx
movl $0x8, %edx
movl $0x10, %eax
cmpl $0x0, %ecx
cmovnel %eax, %edx
leaq -0xa0(%rbp), %rdi
movl $0x6, %esi
callq 0x5870
movl $0x0, -0xa4(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x1df30
movq -0x58(%rbp), %rax
cmpl $0x0, 0xfc(%rax)
je 0x1e0db
movq -0x58(%rbp), %rax
movq 0x108(%rax), %rsi
movq -0x58(%rbp), %rax
movl 0x110(%rax), %eax
movl %eax, %edx
leaq -0x1d0(%rbp), %rdi
callq 0x1e550
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1e0c9
jmp 0x1e522
leaq -0x1d0(%rbp), %rdi
callq 0x1eef0
movl %eax, -0xa4(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %edi
movl -0x20(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdx
callq 0x1ef60
cmpl $0x0, %eax
je 0x1e0fa
movl $0x5c, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1e105
jmp 0x1e522
movq -0x40(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl $0x4, %eax
movl $0x8, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movslq %eax, %rsi
leaq -0x48(%rbp), %rdx
callq 0x1ef60
cmpl $0x0, %eax
je 0x1e134
movl $0x5c, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1e13f
jmp 0x1e522
movq -0x48(%rbp), %rdi
callq 0x1efb0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x50(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
leaq -0xa0(%rbp), %rdx
callq 0x6d60
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1e17a
jmp 0x1e522
cmpl $0x0, -0xa4(%rbp)
je 0x1e266
cmpl $0x2, -0x1d0(%rbp)
jne 0x1e266
imulq $0x3, -0x78(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1efb0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
imulq $0x0, -0x78(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x1d8(%rbp)
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x0, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x1e0(%rbp)
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x1e8(%rbp)
movq -0x1d8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x58(%rbp), %rcx
movl -0xa4(%rbp), %r8d
xorl %eax, %eax
movl %eax, %edx
leaq -0x1d0(%rbp), %r9
callq 0x1efd0
movq -0x1e0(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x58(%rbp), %rcx
movl -0xa4(%rbp), %r8d
movl $0x1, %edx
leaq -0x1d0(%rbp), %r9
callq 0x1efd0
movq -0x1e8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x58(%rbp), %rcx
movl -0xa4(%rbp), %r8d
movl $0x2, %edx
leaq -0x1d0(%rbp), %r9
callq 0x1efd0
jmp 0x1e2b3
movq -0x78(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1efb0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq %rax, -0x1e0(%rbp)
movq %rax, -0x1d8(%rbp)
movq -0x68(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x58(%rbp), %rcx
movl -0xa4(%rbp), %r8d
xorl %eax, %eax
movl %eax, %edx
leaq -0x1d0(%rbp), %r9
callq 0x1efd0
cmpl $0x0, -0x6c(%rbp)
je 0x1e405
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x1e400
movq -0x1d8(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x3, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rsi
shlq $0x3, %rsi
movzbl 0x1(%rdx,%rsi), %edx
addl %edx, %ecx
movl %ecx, %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x1e0(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x3, %rdx
movzbl 0x2(%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rsi
shlq $0x3, %rsi
movzbl 0x3(%rdx,%rsi), %edx
addl %edx, %ecx
movl %ecx, %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0x4(%rax,%rcx,4)
movq -0x1e8(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x3, %rdx
movzbl 0x4(%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rsi
shlq $0x3, %rsi
movzbl 0x5(%rdx,%rsi), %edx
addl %edx, %ecx
movl %ecx, %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0x8(%rax,%rcx,4)
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x3, %rcx
movzbl 0x6(%rax,%rcx), %eax
shll $0x8, %eax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x3, %rdx
movzbl 0x7(%rcx,%rdx), %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movss 0x94613(%rip), %xmm1    # 0xb29ec
mulss %xmm1, %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0xc(%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1e2c5
jmp 0x1e4ec
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x1e4ea
movq -0x1d8(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x2, %rdx
movzbl (%rcx,%rdx), %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x1e0(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x2, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0x4(%rax,%rcx,4)
movq -0x1e8(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
shlq $0x2, %rdx
movzbl 0x2(%rcx,%rdx), %ecx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0x8(%rax,%rcx,4)
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movzbl 0x3(%rax,%rcx), %eax
cvtsi2ss %eax, %xmm0
movss 0x94525(%rip), %xmm1    # 0xb29e8
mulss %xmm1, %xmm0
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
movss %xmm0, 0xc(%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1e40d
jmp 0x1e4ec
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
movq -0x58(%rbp), %rcx
movl -0xa4(%rbp), %r8d
movq -0x10(%rbp), %rax
leaq -0x1d0(%rbp), %r9
movq %rax, (%rsp)
callq 0x1f320
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1e520
jmp 0x1e522
jmp 0x1e522
leaq -0x1d0(%rbp), %rdi
callq 0x1df80
movq -0x60(%rbp), %rdi
callq 0x1df10
movq -0x68(%rbp), %rdi
callq 0x1df10
movl -0x2c(%rbp), %eax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
nopl (%rax)