// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: lodepng_auto_choose_color
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, 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*/
    prof.key = 0;
    if(prof.bits < 8) prof.bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
  }
  n = prof.numcolors;
  palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
  palette_ok = n <= 256 && prof.bits <= 8;
  if(w * h < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/
  if(!prof.colored && 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)
    {
      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
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r15d
movq %rdi, %rbx
xorl %eax, %eax
leaq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq %rax, (%rdi)
movq %rax, 0x6(%rdi)
movl $0x1, 0x418(%rdi)
callq 0x5d15
movl $0x0, 0x18(%rbx)
imull %r15d, %ebp
cmpl $0x10, %ebp
ja 0x662b
cmpl $0x0, 0xc(%rsp)
je 0x662b
movl $0x1, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
cmpl $0x7, 0x420(%rsp)
ja 0x662b
movl $0x8, 0x420(%rsp)
movl 0x1c(%rsp), %eax
movl $0x1, %r15d
cmpl $0x3, %eax
jb 0x6655
movl $0x2, %r15d
cmpl $0x5, %eax
jb 0x6655
xorl %ecx, %ecx
cmpl $0x11, %eax
setae %cl
leal 0x4(,%rcx,4), %r15d
cmpl $0x101, %eax            # imm = 0x101
setae %dl
movl 0x420(%rsp), %ecx
cmpl $0x9, %ecx
setae %sil
addl %eax, %eax
cmpl %eax, %ebp
setb %dil
orb %dl, %dil
movl 0x8(%rsp), %eax
testl %eax, %eax
sete %dl
cmpl %r15d, %ecx
setbe %r8b
andb %dl, %r8b
orb %dil, %r8b
orb %sil, %r8b
je 0x66ec
movl %ecx, 0x4(%rbx)
xorl %edx, %edx
testl %eax, %eax
setne %dl
cmpl $0x0, 0x18(%rsp)
leal (%rdx,%rdx), %eax
leal 0x4(%rdx,%rdx), %edx
cmovel %eax, %edx
movl %edx, (%rbx)
xorl %ebp, %ebp
cmpl $0x0, 0xc(%rsp)
je 0x67aa
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll %cl, %eax
notl %eax
movzwl 0x10(%rsp), %ecx
andl %eax, %ecx
movl %ecx, 0x1c(%rbx)
movzwl 0x12(%rsp), %ecx
andl %eax, %ecx
movl %ecx, 0x20(%rbx)
movzwl 0x14(%rsp), %ecx
andl %eax, %ecx
movl %ecx, 0x24(%rbx)
movl $0x1, 0x18(%rbx)
jmp 0x67aa
leaq 0x8(%rbx), %r12
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x66fe
callq 0x1030
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
cmpl $0x0, 0x1c(%rsp)
je 0x675f
movq %r12, (%rsp)
xorl %r13d, %r13d
movl $0x3, %r12d
leal -0x3(%r12), %eax
movzbl 0x20(%rsp,%rax), %esi
leal -0x2(%r12), %eax
movzbl 0x20(%rsp,%rax), %edx
leal -0x1(%r12), %eax
movzbl 0x20(%rsp,%rax), %ecx
movl %r12d, %eax
movzbl 0x20(%rsp,%rax), %r8d
movq %rbx, %rdi
callq 0x4529
testl %eax, %eax
jne 0x6763
incl %r13d
addl $0x4, %r12d
cmpl 0x1c(%rsp), %r13d
jne 0x671a
xorl %ebp, %ebp
jmp 0x6765
xorl %ebp, %ebp
jmp 0x6769
movl %eax, %ebp
movq (%rsp), %r12
movl $0x3, (%rbx)
movl %r15d, 0x4(%rbx)
cmpl $0x3, (%r14)
jne 0x67aa
movq 0x10(%r14), %rax
cmpq 0x10(%rbx), %rax
jb 0x67aa
cmpl %r15d, 0x4(%r14)
jne 0x67aa
movq (%r12), %rdi
testq %rdi, %rdi
je 0x6797
callq 0x1030
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x448b
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq