// File: 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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
addq $0xa8, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpl $0x8, 0x4(%rax)
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x6c(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x1df30
movq -0x68(%rbp), %rax
cmpl $0x0, 0xfc(%rax)
je 0x1f960
movq -0x68(%rbp), %rax
movq 0x108(%rax), %rsi
movq -0x68(%rbp), %rax
movl 0x110(%rax), %eax
movl %eax, %edx
leaq -0x1b0(%rbp), %rdi
callq 0x1e550
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1f94e
jmp 0x1fdde
leaq -0x1b0(%rbp), %rdi
callq 0x1eef0
movl %eax, -0x84(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %edi
movl -0x18(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdx
callq 0x1ef60
cmpl $0x0, %eax
je 0x1f97f
movl $0x5c, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1f98a
jmp 0x1fdde
movq -0x48(%rbp), %rdi
movl $0x10, %esi
leaq -0x50(%rbp), %rdx
callq 0x1ef60
cmpl $0x0, %eax
je 0x1f9a8
movl $0x5c, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1f9b3
jmp 0x1fdde
movq -0x48(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl $0x4, %eax
movl $0x8, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movslq %eax, %rsi
leaq -0x58(%rbp), %rdx
callq 0x1ef60
cmpl $0x0, %eax
je 0x1f9e2
movl $0x5c, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1f9ed
jmp 0x1fdde
movq -0x50(%rbp), %rdi
callq 0x1efb0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movq -0x68(%rbp), %r8
movl -0x84(%rbp), %r9d
movq -0x28(%rbp), %r10
movl 0x10(%rbp), %eax
leaq -0x1b0(%rbp), %r11
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x1fe10
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1fa41
jmp 0x1fdde
movq -0x78(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x68(%rbp), %rcx
movl -0x84(%rbp), %r8d
leaq -0x1b0(%rbp), %r9
callq 0x200b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1fa70
jmp 0x1fdde
movq -0x58(%rbp), %rdi
callq 0x1efb0
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x1fc54
leaq -0x1d8(%rbp), %rdi
movl $0x6, %esi
movl $0x10, %edx
callq 0x5870
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x1fc21
movq $0x0, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
jae 0x1fc0e
movq -0x38(%rbp), %rax
shlq $0x3, %rax
movq -0x40(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x1e0(%rbp)
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
xorps %xmm0, %xmm0
ucomiss (%rax,%rcx,4), %xmm0
jbe 0x1fb05
xorps %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
jmp 0x1fb22
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x214(%rbp)
movss -0x214(%rbp), %xmm1
movss 0x92ec2(%rip), %xmm0    # 0xb29f4
ucomiss %xmm1, %xmm0
jbe 0x1fb8c
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
xorps %xmm0, %xmm0
ucomiss (%rax,%rcx,4), %xmm0
jbe 0x1fb5d
xorps %xmm0, %xmm0
movss %xmm0, -0x218(%rbp)
jmp 0x1fb7a
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x218(%rbp)
movss -0x218(%rbp), %xmm0
movss %xmm0, -0x21c(%rbp)
jmp 0x1fb9e
movss 0x92e60(%rip), %xmm0    # 0xb29f4
movss %xmm0, -0x21c(%rbp)
jmp 0x1fb9e
movss -0x21c(%rbp), %xmm0
movss 0x92e6a(%rip), %xmm1    # 0xb2a18
mulss %xmm1, %xmm0
movss 0x92e5a(%rip), %xmm1    # 0xb2a14
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x1e4(%rbp)
movl -0x1e4(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x80(%rbp), %rax
movq -0x1e0(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x1e4(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x80(%rbp), %rax
movq -0x1e0(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1fabb
jmp 0x1fc10
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1faa5
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x14(%rbp), %r8d
movl -0x18(%rbp), %r9d
leaq -0x1d8(%rbp), %rcx
callq 0x6d60
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1fc4f
jmp 0x1fdde
jmp 0x1fddc
leaq -0x210(%rbp), %rdi
movl $0x6, %esi
movl $0x8, %edx
callq 0x5870
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x1fdaf
movq $0x0, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
jae 0x1fd9c
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
xorps %xmm0, %xmm0
ucomiss (%rax,%rcx,4), %xmm0
jbe 0x1fcb9
xorps %xmm0, %xmm0
movss %xmm0, -0x220(%rbp)
jmp 0x1fcd6
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x220(%rbp)
movss -0x220(%rbp), %xmm1
movss 0x92d0e(%rip), %xmm0    # 0xb29f4
ucomiss %xmm1, %xmm0
jbe 0x1fd40
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
xorps %xmm0, %xmm0
ucomiss (%rax,%rcx,4), %xmm0
jbe 0x1fd11
xorps %xmm0, %xmm0
movss %xmm0, -0x224(%rbp)
jmp 0x1fd2e
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x224(%rbp)
movss -0x224(%rbp), %xmm0
movss %xmm0, -0x228(%rbp)
jmp 0x1fd52
movss 0x92cac(%rip), %xmm0    # 0xb29f4
movss %xmm0, -0x228(%rbp)
jmp 0x1fd52
movss -0x228(%rbp), %xmm0
movss 0x92cae(%rip), %xmm1    # 0xb2a10
mulss %xmm1, %xmm0
movss 0x92ca6(%rip), %xmm1    # 0xb2a14
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movb %al, %dl
movq -0x80(%rbp), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq -0x40(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1fc88
jmp 0x1fd9e
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1fc72
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x14(%rbp), %r8d
movl -0x18(%rbp), %r9d
leaq -0x210(%rbp), %rcx
callq 0x6d60
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1fdda
jmp 0x1fdde
jmp 0x1fddc
jmp 0x1fdde
leaq -0x1b0(%rbp), %rdi
callq 0x1df80
movq -0x78(%rbp), %rdi
callq 0x1df10
movq -0x80(%rbp), %rdi
callq 0x1df10
movl -0x2c(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopl (%rax,%rax)