// File: /workspace/github_c_makefile_star2_O0/pje[P]pixelsort/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, grey_ok, palette_ok;

  lodepng_color_profile_init(&prof);
  error = 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*/
  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
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 0x5a00
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movq -0x28(%rbp), %r8
leaq -0x444(%rbp), %rdi
callq 0x5ae0
movl %eax, -0x448(%rbp)
cmpl $0x0, -0x448(%rbp)
je 0x64aa
movl -0x448(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x680f
movq -0x10(%rbp), %rax
movl $0x0, 0x18(%rax)
cmpl $0x0, -0x440(%rbp)
je 0x64d4
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
cmpl $0x10, %eax
ja 0x64d4
movl $0x1, -0x434(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x444(%rbp)
movb %al, -0x46d(%rbp)
jne 0x64f7
cmpl $0x0, -0x434(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x46d(%rbp)
movb -0x46d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x458(%rbp)
movl -0x430(%rbp), %eax
movl %eax, -0x450(%rbp)
cmpl $0x2, -0x450(%rbp)
ja 0x652a
movl $0x1, %eax
movl %eax, -0x474(%rbp)
jmp 0x6568
cmpl $0x4, -0x450(%rbp)
ja 0x6540
movl $0x2, %eax
movl %eax, -0x478(%rbp)
jmp 0x655c
movl -0x450(%rbp), %edx
movl $0x8, %eax
movl $0x4, %ecx
cmpl $0x10, %edx
cmovbel %ecx, %eax
movl %eax, -0x478(%rbp)
movl -0x478(%rbp), %eax
movl %eax, -0x474(%rbp)
movl -0x474(%rbp), %eax
movl %eax, -0x454(%rbp)
xorl %eax, %eax
cmpl $0x100, -0x450(%rbp)    # imm = 0x100
movb %al, -0x479(%rbp)
ja 0x65b0
movl -0x450(%rbp), %ecx
shll %ecx
movl -0x1c(%rbp), %edx
imull -0x20(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x479(%rbp)
jae 0x65b0
cmpl $0x8, -0x2c(%rbp)
setbe %al
movb %al, -0x479(%rbp)
movb -0x479(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x45c(%rbp)
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
movl -0x450(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jae 0x65de
movl $0x0, -0x45c(%rbp)
cmpl $0x0, -0x458(%rbp)
je 0x65fc
movl -0x2c(%rbp), %eax
cmpl -0x454(%rbp), %eax
ja 0x65fc
movl $0x0, -0x45c(%rbp)
cmpl $0x0, -0x45c(%rbp)
je 0x673e
leaq -0x444(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x468(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2b50
movl $0x0, -0x44c(%rbp)
movl -0x44c(%rbp), %eax
cmpl -0x430(%rbp), %eax
jae 0x66e1
movq -0x10(%rbp), %rdi
movq -0x468(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %sil
movq -0x468(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x468(%rbp), %rax
movl -0x44c(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %cl
movq -0x468(%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 0x2c70
movl %eax, -0x448(%rbp)
cmpl $0x0, -0x448(%rbp)
je 0x66cb
jmp 0x66e1
jmp 0x66cd
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
jmp 0x662e
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 0x6739
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jb 0x6739
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x6739
movq -0x10(%rbp), %rdi
callq 0x2b30
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2ba0
jmp 0x6806
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x434(%rbp)
je 0x676f
movl -0x444(%rbp), %edx
movl $0x4, %eax
movl $0x6, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x480(%rbp)
jmp 0x6788
movl -0x444(%rbp), %edx
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x480(%rbp)
movl -0x480(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x440(%rbp)
je 0x6804
cmpl $0x0, -0x434(%rbp)
jne 0x6804
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x46c(%rbp)
movzwl -0x43c(%rbp), %ecx
andl -0x46c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movzwl -0x43a(%rbp), %ecx
andl -0x46c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movzwl -0x438(%rbp), %ecx
andl -0x46c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
jmp 0x6806
movl -0x448(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopl (%rax,%rax)