// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: lodepng_auto_choose_color(LodePNGColorMode*, unsigned char const*, unsigned int, unsigned int, LodePNGColorMode const*)
unsigned lodepng_auto_choose_color(LodePNGColorMode* mode_out,
                                   const unsigned char* image, unsigned w, unsigned h,
                                   const LodePNGColorMode* mode_in)
{
  LodePNGColorProfile prof;
  unsigned error = 0;
  unsigned i, n, palettebits, grey_ok, palette_ok;

  lodepng_color_profile_init(&prof);
  error = lodepng_get_color_profile(&prof, image, w, h, mode_in);
  if(error) return error;
  mode_out->key_defined = 0;

  if(prof.key && w * h <= 16)
  {
    prof.alpha = 1; /*too few pixels to justify tRNS chunk overhead*/
    if(prof.bits < 8) prof.bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
  }
  grey_ok = !prof.colored && !prof.alpha; /*grey without alpha, with potentially low bits*/
  n = prof.numcolors;
  palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
  palette_ok = n <= 256 && (n * 2 < w * h) && prof.bits <= 8;
  if(w * h < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/
  if(grey_ok && prof.bits <= palettebits) palette_ok = 0; /*grey is less overhead*/

  if(palette_ok)
  {
    unsigned char* p = prof.palette;
    lodepng_palette_clear(mode_out); /*remove potential earlier palette*/
    for(i = 0; i != prof.numcolors; ++i)
    {
      error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]);
      if(error) break;
    }

    mode_out->colortype = LCT_PALETTE;
    mode_out->bitdepth = palettebits;

    if(mode_in->colortype == LCT_PALETTE && mode_in->palettesize >= mode_out->palettesize
        && mode_in->bitdepth == mode_out->bitdepth)
    {
      /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/
      lodepng_color_mode_cleanup(mode_out);
      lodepng_color_mode_copy(mode_out, mode_in);
    }
  }
  else /*8-bit or 16-bit per channel*/
  {
    mode_out->bitdepth = prof.bits;
    mode_out->colortype = prof.alpha ? (prof.colored ? LCT_RGBA : LCT_GREY_ALPHA)
                                     : (prof.colored ? LCT_RGB : LCT_GREY);

    if(prof.key && !prof.alpha)
    {
      unsigned mask = (1u << mode_out->bitdepth) - 1u; /*profile always uses 16-bit, mask converts it*/
      mode_out->key_r = prof.key_r & mask;
      mode_out->key_g = prof.key_g & mask;
      mode_out->key_b = prof.key_b & mask;
      mode_out->key_defined = 1;
    }
  }

  return error;
}

// Assembly:
subq $0x488, %rsp            # imm = 0x488
movq %rdi, 0x478(%rsp)
movq %rsi, 0x470(%rsp)
movl %edx, 0x46c(%rsp)
movl %ecx, 0x468(%rsp)
movq %r8, 0x460(%rsp)
movl $0x0, 0x40(%rsp)
leaq 0x44(%rsp), %rdi
callq 0xbea0
movq 0x470(%rsp), %rsi
movl 0x46c(%rsp), %edx
movl 0x468(%rsp), %ecx
movq 0x460(%rsp), %r8
leaq 0x44(%rsp), %rdi
callq 0xbf10
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
je 0xcbc2
movl 0x40(%rsp), %eax
movl %eax, 0x484(%rsp)
jmp 0xcf30
movq 0x478(%rsp), %rax
movl $0x0, 0x18(%rax)
cmpl $0x0, 0x48(%rsp)
je 0xcc0b
movl 0x46c(%rsp), %eax
imull 0x468(%rsp), %eax
cmpl $0x10, %eax
ja 0xcc0b
movl $0x1, 0x54(%rsp)
cmpl $0x8, 0x45c(%rsp)
jae 0xcc09
movl $0x8, 0x45c(%rsp)
jmp 0xcc0b
xorl %eax, %eax
cmpl $0x0, 0x44(%rsp)
movb %al, 0x1b(%rsp)
jne 0xcc26
cmpl $0x0, 0x54(%rsp)
setne %al
xorb $-0x1, %al
movb %al, 0x1b(%rsp)
movb 0x1b(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x30(%rsp)
movl 0x58(%rsp), %eax
movl %eax, 0x38(%rsp)
cmpl $0x2, 0x38(%rsp)
ja 0xcc4d
movl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xcc7f
cmpl $0x4, 0x38(%rsp)
ja 0xcc5f
movl $0x2, %eax
movl %eax, 0x10(%rsp)
jmp 0xcc77
movl 0x38(%rsp), %edx
movl $0x8, %eax
movl $0x4, %ecx
cmpl $0x10, %edx
cmovbel %ecx, %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x34(%rsp)
xorl %eax, %eax
cmpl $0x100, 0x38(%rsp)      # imm = 0x100
movb %al, 0xf(%rsp)
ja 0xccc5
movl 0x38(%rsp), %ecx
shll %ecx
movl 0x46c(%rsp), %edx
imull 0x468(%rsp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0xf(%rsp)
jae 0xccc5
cmpl $0x8, 0x45c(%rsp)
setbe %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x2c(%rsp)
movl 0x46c(%rsp), %eax
imull 0x468(%rsp), %eax
movl 0x38(%rsp), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0xccf3
movl $0x0, 0x2c(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0xcd0f
movl 0x45c(%rsp), %eax
cmpl 0x34(%rsp), %eax
ja 0xcd0f
movl $0x0, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xce5b
leaq 0x44(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
movq 0x478(%rsp), %rdi
callq 0x8ab0
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x58(%rsp), %eax
je 0xcdd8
movq 0x478(%rsp), %rdi
movq 0x20(%rsp), %rax
movl 0x3c(%rsp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %sil
movq 0x20(%rsp), %rax
movl 0x3c(%rsp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq 0x20(%rsp), %rax
movl 0x3c(%rsp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movl 0x3c(%rsp), %r8d
shll $0x2, %r8d
addl $0x3, %r8d
movl %r8d, %r8d
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl (%rax,%r8), %r8d
callq 0x8bd0
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
je 0xcdc6
jmp 0xcdd8
jmp 0xcdc8
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0xcd3d
movq 0x478(%rsp), %rax
movl $0x3, (%rax)
movl 0x34(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x460(%rsp), %rax
cmpl $0x3, (%rax)
jne 0xce56
movq 0x460(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x478(%rsp), %rcx
cmpq 0x10(%rcx), %rax
jb 0xce56
movq 0x460(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x478(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jne 0xce56
movq 0x478(%rsp), %rdi
callq 0x8aa0
movq 0x478(%rsp), %rdi
movq 0x460(%rsp), %rsi
callq 0x8af0
jmp 0xcf25
movl 0x45c(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, 0x54(%rsp)
je 0xce8e
movl 0x44(%rsp), %edx
movl $0x4, %eax
movl $0x6, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x8(%rsp)
jmp 0xcea3
movl 0x44(%rsp), %edx
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, (%rax)
cmpl $0x0, 0x48(%rsp)
je 0xcf23
cmpl $0x0, 0x54(%rsp)
jne 0xcf23
movq 0x478(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, 0x1c(%rsp)
movzwl 0x4c(%rsp), %ecx
andl 0x1c(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, 0x1c(%rax)
movzwl 0x4e(%rsp), %ecx
andl 0x1c(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, 0x20(%rax)
movzwl 0x50(%rsp), %ecx
andl 0x1c(%rsp), %ecx
movq 0x478(%rsp), %rax
movl %ecx, 0x24(%rax)
movq 0x478(%rsp), %rax
movl $0x1, 0x18(%rax)
jmp 0xcf25
movl 0x40(%rsp), %eax
movl %eax, 0x484(%rsp)
movl 0x484(%rsp), %eax
addq $0x488, %rsp            # imm = 0x488
retq
nop