// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x448(%rbp)
leaq -0x444(%rbp), %rdi
callq 0x5e10
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movq -0x28(%rbp), %r8
leaq -0x444(%rbp), %rdi
callq 0x5e80
movl %eax, -0x448(%rbp)
cmpl $0x0, -0x448(%rbp)
je 0x6aea
movl -0x448(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6e10
movq -0x10(%rbp), %rax
movl $0x0, 0x18(%rax)
cmpl $0x0, -0x440(%rbp)
je 0x6b2d
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
cmpl $0x10, %eax
ja 0x6b2d
movl $0x1, -0x434(%rbp)
movl $0x0, -0x440(%rbp)
cmpl $0x8, -0x2c(%rbp)
jae 0x6b2b
movl $0x8, -0x2c(%rbp)
jmp 0x6b2d
movl -0x430(%rbp), %eax
movl %eax, -0x450(%rbp)
cmpl $0x2, -0x450(%rbp)
ja 0x6b4f
movl $0x1, %eax
movl %eax, -0x468(%rbp)
jmp 0x6b8d
cmpl $0x4, -0x450(%rbp)
ja 0x6b65
movl $0x2, %eax
movl %eax, -0x46c(%rbp)
jmp 0x6b81
movl -0x450(%rbp), %edx
movl $0x8, %eax
movl $0x4, %ecx
cmpl $0x10, %edx
cmovbel %ecx, %eax
movl %eax, -0x46c(%rbp)
movl -0x46c(%rbp), %eax
movl %eax, -0x468(%rbp)
movl -0x468(%rbp), %eax
movl %eax, -0x454(%rbp)
xorl %eax, %eax
cmpl $0x100, -0x450(%rbp)    # imm = 0x100
movb %al, -0x46d(%rbp)
ja 0x6bba
cmpl $0x8, -0x2c(%rbp)
setbe %al
movb %al, -0x46d(%rbp)
movb -0x46d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x458(%rbp)
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
movl -0x450(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x6be8
movl $0x0, -0x458(%rbp)
cmpl $0x0, -0x444(%rbp)
jne 0x6c06
movl -0x2c(%rbp), %eax
cmpl -0x454(%rbp), %eax
ja 0x6c06
movl $0x0, -0x458(%rbp)
cmpl $0x0, -0x458(%rbp)
je 0x6d48
leaq -0x444(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x460(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2ed0
movl $0x0, -0x44c(%rbp)
movl -0x44c(%rbp), %eax
cmpl -0x430(%rbp), %eax
je 0x6ceb
movq -0x10(%rbp), %rdi
movq -0x460(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %sil
movq -0x460(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x460(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %cl
movq -0x460(%rbp), %rax
movl -0x44c(%rbp), %r8d
shll $0x2, %r8d
addl $0x3, %r8d
movl %r8d, %r8d
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl (%rax,%r8), %r8d
callq 0x2ff0
movl %eax, -0x448(%rbp)
cmpl $0x0, -0x448(%rbp)
je 0x6cd5
jmp 0x6ceb
jmp 0x6cd7
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
jmp 0x6c38
movq -0x10(%rbp), %rax
movl $0x3, (%rax)
movl -0x454(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x6d43
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jb 0x6d43
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x6d43
movq -0x10(%rbp), %rdi
callq 0x2eb0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2f20
jmp 0x6e07
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x434(%rbp)
je 0x6d79
movl -0x444(%rbp), %edx
movl $0x4, %eax
movl $0x6, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x474(%rbp)
jmp 0x6d92
movl -0x444(%rbp), %edx
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x474(%rbp)
movl -0x474(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x440(%rbp)
je 0x6e05
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x464(%rbp)
movzwl -0x43c(%rbp), %ecx
andl -0x464(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movzwl -0x43a(%rbp), %ecx
andl -0x464(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movzwl -0x438(%rbp), %ecx
andl -0x464(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
jmp 0x6e07
movl -0x448(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopl (%rax)