// File: /workspace/github_cpp_makefile_star2_O2/lvandeve[P]lodepng/lodepng_util.cpp
// Function: lodepng::convertFromXYZ(unsigned char*, float const*, unsigned int, unsigned int, LodePNGState const*, float const*, unsigned int)
unsigned convertFromXYZ(unsigned char* out, const float* in, unsigned w, unsigned h,
                        const LodePNGState* state,
                        const float whitepoint[3], unsigned rendering_intent) {
  unsigned error = 0;
  size_t i, c, n, bytes_im, bytes_data;
  const LodePNGColorMode* mode_out = &state->info_raw;
  const LodePNGInfo* info = &state->info_png;
  int bit16 = mode_out->bitdepth > 8;
  float* im = 0;
  unsigned char* data = 0;

  /* parse ICC if present */
  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, 4 * sizeof(float), &bytes_im)) error = 92;
  if(error) goto cleanup;
  if(lodepng_mulofl(n, bit16 ? 8 : 4, &bytes_data)) error = 92;
  if(error) goto cleanup;

  /* Handle gamut */
  im = (float*)lodepng_malloc(bytes_im);
  error = convertFromXYZ_chrm(im, in, w, h, info, use_icc, &icc, whitepoint, rendering_intent);
  if(error) goto cleanup;

  /* Handle transfer function */
  /* Input is floating point, so lookup table cannot be used, but it's ensured to use float pow, not the slower double pow. */
  error = convertFromXYZ_gamma(im, w, h, info, use_icc, &icc);
  if(error) goto cleanup;

  /* Convert to integer output */
  data = (unsigned char*)lodepng_malloc(bytes_data);
  /* TODO: check if also 1/2/4 bit case needed: rounding is at different fine-grainedness for 8 and 16 bits below. */
  if(bit16) {
    LodePNGColorMode mode16 = lodepng_color_mode_make(LCT_RGBA, 16);
    for(i = 0; i < n; i++) {
      for(c = 0; c < 4; c++) {
        size_t j = i * 8 + c * 2;
        int i16 = (int)(0.5f + 65535.0f * LODEPNG_MIN(LODEPNG_MAX(0.0f, im[i * 4 + c]), 1.0f));
        data[j + 0] = (unsigned char)(i16 >> 8);
        data[j + 1] = (unsigned char)(i16 & 255);
      }
    }
    error = lodepng_convert(out, data, mode_out, &mode16, w, h);
    if(error) goto cleanup;
  } else {
    LodePNGColorMode mode8 = lodepng_color_mode_make(LCT_RGBA, 8);
    for(i = 0; i < n; i++) {
      for(c = 0; c < 4; c++) {
        data[i * 4 + c] = (unsigned char)(0.5f + 255.0f * LODEPNG_MIN(LODEPNG_MAX(0.0f, im[i * 4 + c]), 1.0f));
      }
    }
    error = lodepng_convert(out, data, mode_out, &mode8, w, h);
    if(error) goto cleanup;
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r9, 0x10(%rsp)
movl %ecx, %r15d
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r13
movl 0xac(%r8), %ecx
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0x120(%rsp)
movups %xmm0, 0x158(%rsp)
cmpl $0x0, 0x1cc(%r8)
je 0x1058f
movl %ecx, %ebp
movq 0x1d8(%r8), %rsi
movl 0x1e0(%r8), %edx
leaq 0x60(%rsp), %rdi
movq %r8, %r14
callq 0xf9f5
testl %eax, %eax
je 0x10593
pushq $0x1
jmp 0x10647
xorl %eax, %eax
jmp 0x105a2
leaq 0x60(%rsp), %rdi
callq 0x100c4
movl %ebp, %ecx
movq %r14, %r8
movl %ebx, %edx
movl %ebx, 0xc(%rsp)
movl %r15d, %esi
imulq %rdx, %rsi
movq %rsi, %rdx
shrq $0x3c, %rdx
jne 0x10645
movq %r13, 0x28(%rsp)
leaq 0xd0(%r8), %r13
cmpl $0x9, %ecx
movl %ecx, 0x20(%rsp)
setae %cl
movq %rsi, %rdi
shlq $0x4, %rdi
orb $0x2, %cl
movq %rsi, %rbp
shlq %cl, %rbp
movl %eax, %r14d
movq %r8, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
callq 0x2280
movq %rax, %rbx
subq $0x8, %rsp
leaq 0x68(%rsp), %rax
movq %rbx, %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movl %r15d, 0x2c(%rsp)
movl %r15d, %ecx
movq %r13, %r15
movq %r13, %r8
movl %r14d, %r13d
movl %r14d, %r9d
movl 0x1c8(%rsp), %r10d
pushq %r10
pushq 0x20(%rsp)
pushq %rax
callq 0x1083c
addq $0x20, %rsp
testl %eax, %eax
je 0x1067e
movl %eax, %r12d
xorl %r13d, %r13d
movq %rbx, %r15
jmp 0x1064f
pushq $0x5c
popq %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
leaq 0x60(%rsp), %rdi
callq 0xf66b
movq %r15, %rdi
callq 0x2260
movq %r13, %rdi
callq 0x2260
movl %r12d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x30(%rsp), %r14
leaq 0x60(%rsp), %r9
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
movq %r15, %rcx
movl %r13d, %r8d
callq 0x109a1
testl %eax, %eax
je 0x106b6
movl %eax, %r12d
xorl %r13d, %r13d
movq 0x10(%rsp), %r15
jmp 0x1064f
movl 0xc(%rsp), %r12d
addq $0xa8, 0x18(%rsp)
movq %rbp, %rdi
callq 0x2280
movq %rax, %r13
cmpl $0x9, 0x20(%rsp)
jb 0x10781
leaq 0x38(%rsp), %rdi
pushq $0x6
popq %rsi
pushq $0x10
popq %rdx
callq 0x4a1b
movq %r13, %rax
incq %rax
xorl %ecx, %ecx
movss 0x4f696(%rip), %xmm0    # 0x5fd90
xorps %xmm1, %xmm1
movss 0x4f6af(%rip), %xmm2    # 0x5fdb4
movss 0x4f6a3(%rip), %xmm3    # 0x5fdb0
movq 0x10(%rsp), %r15
movq %r15, %r8
movq 0x18(%rsp), %rdx
cmpq %r14, %rcx
je 0x1081c
xorl %esi, %esi
cmpq $0x4, %rsi
je 0x10774
movss (%r8,%rsi,4), %xmm5
ucomiss %xmm5, %xmm0
movaps %xmm5, %xmm4
cmpltss %xmm1, %xmm4
movaps %xmm5, %xmm6
cmpltss %xmm0, %xmm6
andnps %xmm0, %xmm6
andnps %xmm6, %xmm4
movaps %xmm5, %xmm6
ja 0x10752
movaps %xmm4, %xmm6
ucomiss %xmm5, %xmm1
ja 0x1075a
movaps %xmm6, %xmm4
mulss %xmm2, %xmm4
addss %xmm3, %xmm4
cvttss2si %xmm4, %edi
rolw $0x8, %di
movw %di, -0x1(%rax,%rsi,2)
incq %rsi
jmp 0x10725
incq %rcx
addq $0x10, %r8
addq $0x8, %rax
jmp 0x1071a
leaq 0x38(%rsp), %rdi
pushq $0x6
popq %rsi
pushq $0x8
popq %rdx
callq 0x4a1b
xorl %eax, %eax
movss 0x4f5f5(%rip), %xmm0    # 0x5fd90
xorps %xmm1, %xmm1
movss 0x4f606(%rip), %xmm2    # 0x5fdac
movss 0x4f602(%rip), %xmm3    # 0x5fdb0
movq %r13, %rcx
movq 0x10(%rsp), %r15
movq %r15, %r8
movq 0x18(%rsp), %rdx
cmpq %r14, %rax
je 0x1081c
xorl %esi, %esi
cmpq $0x4, %rsi
je 0x1080f
movss (%r8,%rsi,4), %xmm5
ucomiss %xmm5, %xmm0
movaps %xmm5, %xmm4
cmpltss %xmm1, %xmm4
movaps %xmm5, %xmm6
cmpltss %xmm0, %xmm6
andnps %xmm0, %xmm6
andnps %xmm6, %xmm4
movaps %xmm5, %xmm6
ja 0x107f2
movaps %xmm4, %xmm6
ucomiss %xmm5, %xmm1
ja 0x107fa
movaps %xmm6, %xmm4
mulss %xmm2, %xmm4
addss %xmm3, %xmm4
cvttss2si %xmm4, %edi
movb %dil, (%rcx,%rsi)
incq %rsi
jmp 0x107c5
incq %rax
addq $0x10, %r8
addq $0x4, %rcx
jmp 0x107be
leaq 0x38(%rsp), %rcx
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %r8d
movl %ebx, %r9d
callq 0x5390
movl %eax, %r12d
jmp 0x1064f