// File: /workspace/github_cpp_makefile_star2_O3/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, (%rsp)
movq %r8, %r14
movl %ecx, %r15d
movl %edx, %r13d
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movl 0xac(%r8), %edx
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0x120(%rsp)
movups %xmm0, 0x158(%rsp)
cmpl $0x0, 0x1cc(%r8)
je 0x12d0b
movl %edx, %ebp
movq 0x1d8(%r14), %rsi
movl 0x1e0(%r14), %edx
leaq 0x60(%rsp), %rdi
callq 0x11e1d
testl %eax, %eax
je 0x12e0d
movl $0x1, %r15d
jmp 0x12dbc
movq %r14, %rcx
movq %rbx, 0x10(%rsp)
xorl %r12d, %r12d
movl %r13d, %eax
movl %r15d, %r14d
imulq %rax, %r14
movq %r14, %rax
shrq $0x3c, %rax
jne 0x12db6
movq %rcx, 0x28(%rsp)
leaq 0xd0(%rcx), %rax
movq %rax, 0x30(%rsp)
cmpl $0x9, %edx
setae %cl
movq %r14, %rdi
shlq $0x4, %rdi
orb $0x2, %cl
movq %r14, %rbp
shlq %cl, %rbp
movl %edx, 0x1c(%rsp)
callq 0x22b0
movq %rax, %rbx
subq $0x8, %rsp
leaq 0x68(%rsp), %rax
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movl %r13d, 0x10(%rsp)
movl %r13d, %edx
movq 0x38(%rsp), %r13
movl %r15d, 0x2c(%rsp)
movl %r15d, %ecx
movq %r13, %r8
movl %r12d, 0x28(%rsp)
movl %r12d, %r9d
movl 0x1c8(%rsp), %r10d
pushq %r10
pushq 0x10(%rsp)
pushq %rax
callq 0x13039
addq $0x20, %rsp
testl %eax, %eax
je 0x12e47
movl %eax, %r15d
jmp 0x12dbe
movl $0x5c, %r15d
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0xe8(%rsp), %rdi
callq 0x22a0
movq 0x120(%rsp), %rdi
callq 0x22a0
movq 0x158(%rsp), %rdi
callq 0x22a0
movq %rbx, %rdi
callq 0x22a0
movq %r13, %rdi
callq 0x22a0
movl %r15d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rcx
movq %rbx, 0x10(%rsp)
movl 0x60(%rsp), %r12d
testl %r12d, %r12d
je 0x12e82
cmpl $0x2, %r12d
movl %ebp, %edx
jne 0x12e89
xorl %r12d, %r12d
cmpl $0x0, 0xb4(%rsp)
je 0x12d16
cmpl $0x0, 0xa4(%rsp)
jne 0x12e97
jmp 0x12d16
movq 0x28(%rsp), %r12
leaq 0x60(%rsp), %r9
movq %rbx, (%rsp)
movq %rbx, %rdi
movl 0x8(%rsp), %esi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
movq %r13, %rcx
movl 0x20(%rsp), %r8d
callq 0x13261
testl %eax, %eax
je 0x12eab
movl %eax, %r15d
xorl %r13d, %r13d
movq (%rsp), %rbx
jmp 0x12dc1
movl %ebp, %edx
jmp 0x12d16
cmpl $0x0, 0xa4(%rsp)
je 0x12d13
xorl %r12d, %r12d
cmpl $0x0, 0xdc(%rsp)
setne %r12b
jmp 0x12d16
movl 0x8(%rsp), %r15d
addq $0xa8, %r12
movq %rbp, %rdi
callq 0x22b0
movq %rax, %r13
cmpl $0x9, 0x1c(%rsp)
jb 0x12f8d
leaq 0x38(%rsp), %rdi
movl $0x6, %esi
movl $0x10, %edx
callq 0x5944
testq %r14, %r14
movl %ebx, %r9d
movq (%rsp), %rbx
je 0x13019
xorl %eax, %eax
xorps %xmm0, %xmm0
movaps 0x62e23(%rip), %xmm1    # 0x75d20
movaps 0x62e4c(%rip), %xmm2    # 0x75d50
movaps 0x62e35(%rip), %xmm3    # 0x75d40
movdqa 0x62e4d(%rip), %xmm4    # 0x75d60
movq %rbx, %rcx
movups (%rcx), %xmm5
movaps %xmm5, %xmm6
cmpltps %xmm0, %xmm6
movaps %xmm5, %xmm7
cmpnltps %xmm1, %xmm7
movaps %xmm5, %xmm8
cmpltps %xmm1, %xmm8
orps %xmm6, %xmm8
orps %xmm6, %xmm7
andnps %xmm1, %xmm8
andps %xmm7, %xmm8
andnps %xmm5, %xmm7
orps %xmm8, %xmm7
mulps %xmm2, %xmm7
addps %xmm3, %xmm7
cvttps2dq %xmm7, %xmm5
movdqa %xmm5, %xmm6
psrld $0x8, %xmm6
movdqa %xmm6, %xmm7
punpckldq %xmm5, %xmm7    # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
punpckhdq %xmm5, %xmm6    # xmm6 = xmm6[2],xmm5[2],xmm6[3],xmm5[3]
pand %xmm4, %xmm6
pand %xmm4, %xmm7
packuswb %xmm6, %xmm7
packuswb %xmm7, %xmm7
movq %xmm7, (%r13,%rax,8)
incq %rax
addq $0x10, %rcx
cmpq %rax, %r14
jne 0x12f16
jmp 0x13019
leaq 0x38(%rsp), %rdi
movl $0x6, %esi
movl $0x8, %edx
callq 0x5944
testq %r14, %r14
movl %ebx, %r9d
movq (%rsp), %rbx
je 0x13019
xorl %eax, %eax
xorps %xmm0, %xmm0
movaps 0x62d67(%rip), %xmm1    # 0x75d20
movaps 0x62d70(%rip), %xmm2    # 0x75d30
movaps 0x62d79(%rip), %xmm3    # 0x75d40
movq %rbx, %rcx
movups (%rcx), %xmm4
movaps %xmm4, %xmm5
cmpltps %xmm0, %xmm5
movaps %xmm4, %xmm6
cmpnltps %xmm1, %xmm6
movaps %xmm4, %xmm7
cmpltps %xmm1, %xmm7
orps %xmm5, %xmm7
orps %xmm5, %xmm6
andnps %xmm1, %xmm7
andps %xmm6, %xmm7
andnps %xmm4, %xmm6
orps %xmm7, %xmm6
mulps %xmm2, %xmm6
addps %xmm3, %xmm6
cvttps2dq %xmm6, %xmm4
packuswb %xmm4, %xmm4
packuswb %xmm4, %xmm4
movd %xmm4, (%r13,%rax,4)
incq %rax
addq $0x10, %rcx
cmpq %rax, %r14
jne 0x12fca
leaq 0x38(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl %r15d, %r8d
callq 0x642f
movl %eax, %r15d
jmp 0x12dc1