// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
leaq 0x8(%rsp), %r12
movq %rdi, %rbx
xorl %eax, %eax
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r15d
movq %rax, 0x10(%r12)
movq %rax, (%r12)
movq %rax, 0x6(%r12)
movq %r12, %rdi
movl $0x1, 0x418(%r12)
callq 0x8e0e
movl $0x0, 0x18(%rbx)
movl 0x4(%r12), %eax
testl %eax, %eax
sete %cl
imull %r15d, %ebp
cmpl $0x11, %ebp
setae %dl
orb %cl, %dl
jne 0x965e
movl 0x420(%rsp), %ecx
movl $0x8, %edx
cmpl $0x9, %ecx
cmovael %ecx, %edx
xorl %ecx, %ecx
movl %edx, 0x420(%rsp)
jmp 0x9666
cmpl $0x0, 0x18(%rsp)
sete %cl
movl 0x8(%rsp), %edx
movl 0x1c(%rsp), %r12d
movl $0x1, %r15d
testl %edx, %edx
sete %dil
cmpq $0x3, %r12
jb 0x96af
movl $0x2, %r15d
cmpl $0x5, %r12d
jb 0x96af
xorl %esi, %esi
cmpl $0x11, %r12d
setae %r8b
cmpl $0x101, %r12d           # imm = 0x101
jae 0x9749
movb %r8b, %sil
leal 0x4(,%rsi,4), %r15d
movl 0x420(%rsp), %esi
leal (%r12,%r12), %r8d
cmpl %ebp, %r8d
jae 0x9750
andb %cl, %dil
cmpl $0x9, %esi
setae %r8b
cmpl %r15d, %esi
setbe %r9b
andb %dil, %r9b
orb %r8b, %r9b
jne 0x9750
movq 0x8(%rbx), %rdi
leaq 0x8(%rbx), %r13
testq %rdi, %rdi
je 0x96ee
callq 0x21b0
vxorps %xmm0, %xmm0, %xmm0
vmovups %xmm0, (%r13)
testq %r12, %r12
je 0x97ad
shlq $0x2, %r12
movq %r13, (%rsp)
xorl %r13d, %r13d
movl %r13d, %eax
movzbl 0x20(%rsp,%rax), %esi
leal 0x1(%r13), %eax
movq %rbx, %rdi
movzbl 0x20(%rsp,%rax), %edx
leal 0x2(%r13), %eax
movzbl 0x20(%rsp,%rax), %ecx
leal 0x3(%r13), %eax
movzbl 0x20(%rsp,%rax), %r8d
callq 0x769f
testl %eax, %eax
jne 0x97b1
addq $0x4, %r13
cmpq %r13, %r12
jne 0x970c
xorl %ebp, %ebp
jmp 0x97b3
movl 0x420(%rsp), %esi
movl %esi, 0x4(%rbx)
testb %cl, %cl
je 0x979c
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
xorl %ebp, %ebp
addl %ecx, %ecx
movl %ecx, (%rbx)
testl %eax, %eax
je 0x97fa
movzwl 0x10(%rsp), %eax
bzhil %esi, %eax, %eax
movl %eax, 0x1c(%rbx)
movzwl 0x12(%rsp), %eax
bzhil %esi, %eax, %eax
movl %eax, 0x20(%rbx)
movzwl 0x14(%rsp), %eax
bzhil %esi, %eax, %eax
movl %eax, 0x24(%rbx)
movl $0x1, 0x18(%rbx)
jmp 0x97fa
xorl %eax, %eax
testl %edx, %edx
setne %al
xorl %ebp, %ebp
leal 0x4(%rax,%rax), %eax
movl %eax, (%rbx)
jmp 0x97fa
xorl %ebp, %ebp
jmp 0x97b7
movl %eax, %ebp
movq (%rsp), %r13
movl $0x3, (%rbx)
movl %r15d, 0x4(%rbx)
cmpl $0x3, (%r14)
jne 0x97fa
movq 0x10(%r14), %rax
cmpq 0x10(%rbx), %rax
jb 0x97fa
cmpl %r15d, 0x4(%r14)
jne 0x97fa
movq (%r13), %rdi
testq %rdi, %rdi
je 0x97e5
callq 0x21b0
vxorps %xmm0, %xmm0, %xmm0
movq %rbx, %rdi
movq %r14, %rsi
vmovups %xmm0, (%r13)
callq 0x75fd
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq