// File: /workspace/github_cpp_makefile_star2_O1/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, %ebx
movl %edx, %r14d
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movl 0xac(%r8), %ecx
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0x120(%rsp)
movups %xmm0, 0x158(%rsp)
xorl %r12d, %r12d
cmpl $0x0, 0x1cc(%r8)
movl $0x0, %ebp
je 0x12fce
movl %ecx, %r12d
movq 0x1d8(%r8), %rsi
movl 0x1e0(%r8), %edx
leaq 0x60(%rsp), %rdi
movq %r8, %r15
callq 0x12165
testl %eax, %eax
je 0x12f73
movl $0x1, %r14d
xorl %r12d, %r12d
jmp 0x13066
movq %r15, %r8
movl 0x60(%rsp), %ebp
testl %ebp, %ebp
je 0x12fa4
cmpl $0x2, %ebp
movl %r12d, %ecx
jne 0x12fac
xorl %ebp, %ebp
cmpl $0x0, 0xb4(%rsp)
movl $0x0, %r12d
je 0x12fce
cmpl $0x0, 0xa4(%rsp)
jne 0x12fbc
jmp 0x12fce
movl %r12d, %ecx
xorl %r12d, %r12d
jmp 0x12fce
cmpl $0x0, 0xa4(%rsp)
movl $0x0, %r12d
je 0x12fcc
xorl %ebp, %ebp
cmpl $0x0, 0xdc(%rsp)
setne %bpl
jmp 0x12fce
xorl %ebp, %ebp
movq %r13, 0x30(%rsp)
movl %r14d, %eax
movl %ebx, %r15d
imulq %rax, %r15
movq %r15, %rax
shrq $0x3c, %rax
movl %r14d, 0xc(%rsp)
movl $0x5c, %r14d
cmovel %r12d, %r14d
jne 0x13066
leaq 0xd0(%r8), %r14
cmpl $0x9, %ecx
movl %ecx, 0x2c(%rsp)
setae %cl
movq %r15, %rdi
shlq $0x4, %rdi
orb $0x2, %cl
movq %r15, %r13
shlq %cl, %r13
movq %r8, 0x18(%rsp)
callq 0x22a0
movq %rax, %r12
subq $0x8, %rsp
leaq 0x68(%rsp), %rax
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movl 0x14(%rsp), %edx
movl %ebx, 0x28(%rsp)
movl %ebx, %ecx
movq %r14, %rbx
movq %r14, %r8
movl %ebp, %r9d
movl 0x1c8(%rsp), %r10d
pushq %r10
pushq 0x20(%rsp)
pushq %rax
callq 0x1327c
addq $0x20, %rsp
testl %eax, %eax
je 0x130b4
movl %eax, %r14d
xorl %ebx, %ebx
movq 0xe8(%rsp), %rdi
callq 0x2290
movq 0x120(%rsp), %rdi
callq 0x2290
movq 0x158(%rsp), %rdi
callq 0x2290
movq %r12, %rdi
callq 0x2290
movq %rbx, %rdi
callq 0x2290
movl %r14d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x60(%rsp), %r9
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl 0x20(%rsp), %r12d
movl %r12d, %edx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x134c1
testl %eax, %eax
je 0x130e8
movl %eax, %r14d
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
jmp 0x13068
movl 0xc(%rsp), %r14d
movl %r12d, %ebp
addq $0xa8, 0x18(%rsp)
movq %r13, %rdi
callq 0x22a0
movq %rax, %rbx
cmpl $0x9, 0x2c(%rsp)
jb 0x131bd
leaq 0x38(%rsp), %rdi
movl $0x6, %esi
movl $0x10, %edx
callq 0x536a
testq %r15, %r15
movq 0x10(%rsp), %r12
movl %r14d, %r8d
je 0x1325a
movq %rbx, %rax
incq %rax
xorl %ecx, %ecx
movss 0x62c20(%rip), %xmm0    # 0x75d64
xorps %xmm1, %xmm1
movss 0x62c49(%rip), %xmm2    # 0x75d98
movss 0x62c3d(%rip), %xmm3    # 0x75d94
movq %r12, %rdx
xorl %esi, %esi
movss (%rdx,%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 0x13182
movaps %xmm4, %xmm6
ucomiss %xmm5, %xmm1
ja 0x1318a
movaps %xmm6, %xmm4
mulss %xmm2, %xmm4
addss %xmm3, %xmm4
cvttss2si %xmm4, %edi
rolw $0x8, %di
movw %di, -0x1(%rax,%rsi,2)
incq %rsi
cmpq $0x4, %rsi
jne 0x1315c
incq %rcx
addq $0x10, %rdx
addq $0x8, %rax
cmpq %r15, %rcx
jne 0x1315a
jmp 0x1325a
leaq 0x38(%rsp), %rdi
movl $0x6, %esi
movl $0x8, %edx
callq 0x536a
testq %r15, %r15
movq 0x10(%rsp), %r12
movl %r14d, %r8d
je 0x1325a
xorl %eax, %eax
movss 0x62b7c(%rip), %xmm0    # 0x75d64
xorps %xmm1, %xmm1
movss 0x62b9d(%rip), %xmm2    # 0x75d90
movss 0x62b99(%rip), %xmm3    # 0x75d94
movq %r12, %rcx
movq %rbx, %rdx
xorl %esi, %esi
movss (%rcx,%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 0x13229
movaps %xmm4, %xmm6
ucomiss %xmm5, %xmm1
ja 0x13231
movaps %xmm6, %xmm4
mulss %xmm2, %xmm4
addss %xmm3, %xmm4
cvttss2si %xmm4, %edi
movb %dil, (%rdx,%rsi)
incq %rsi
cmpq $0x4, %rsi
jne 0x13203
incq %rax
addq $0x4, %rdx
addq $0x10, %rcx
cmpq %r15, %rax
jne 0x13201
leaq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movl %ebp, %r9d
callq 0x5e9a
movl %eax, %r14d
jmp 0x13068