// File: /workspace/github_c_makefile_star2_O0/tditlu[P]shadetabler/src/lodepng/lodepng.c
// Function: auto_choose_color
static unsigned auto_choose_color(LodePNGColorMode* mode_out,
                                  const LodePNGColorMode* mode_in,
                                  const LodePNGColorStats* stats) {
  unsigned error = 0;
  unsigned palettebits;
  size_t i, n;
  size_t numpixels = stats->numpixels;
  unsigned palette_ok, gray_ok;

  unsigned alpha = stats->alpha;
  unsigned key = stats->key;
  unsigned bits = stats->bits;

  mode_out->key_defined = 0;

  if(key && numpixels <= 16) {
    alpha = 1; /*too few pixels to justify tRNS chunk overhead*/
    key = 0;
    if(bits < 8) bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
  }

  gray_ok = !stats->colored;
  if(!stats->allow_greyscale) gray_ok = 0;
  if(!gray_ok && bits < 8) bits = 8;

  n = stats->numcolors;
  palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8));
  palette_ok = n <= 256 && bits <= 8 && n != 0; /*n==0 means likely numcolors wasn't computed*/
  if(numpixels < n * 2) palette_ok = 0; /*don't add palette overhead if image has only a few pixels*/
  if(gray_ok && !alpha && bits <= palettebits) palette_ok = 0; /*gray is less overhead*/
  if(!stats->allow_palette) palette_ok = 0;

  if(palette_ok) {
    const unsigned char* p = stats->palette;
    lodepng_palette_clear(mode_out); /*remove potential earlier palette*/
    for(i = 0; i != stats->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 = bits;
    mode_out->colortype = alpha ? (gray_ok ? LCT_GREY_ALPHA : LCT_RGBA)
                                : (gray_ok ? LCT_GREY : LCT_RGB);
    if(key) {
      unsigned mask = (1u << mode_out->bitdepth) - 1u; /*stats always uses 16-bit, mask converts it*/
      mode_out->key_r = stats->key_r & mask;
      mode_out->key_g = stats->key_g & mask;
      mode_out->key_b = stats->key_b & mask;
      mode_out->key_defined = 1;
    }
  }

  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq 0x420(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x418(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movl $0x0, 0x18(%rax)
cmpl $0x0, -0x48(%rbp)
je 0xe550
cmpq $0x10, -0x38(%rbp)
ja 0xe550
movl $0x1, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x8, -0x4c(%rbp)
jae 0xe54e
movl $0x8, -0x4c(%rbp)
jmp 0xe550
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x42c(%rax)
jne 0xe578
movl $0x0, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0xe58b
cmpl $0x8, -0x4c(%rbp)
jae 0xe58b
movl $0x8, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x30(%rbp)
cmpq $0x2, -0x30(%rbp)
ja 0xe5a7
movl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xe5d6
cmpq $0x4, -0x30(%rbp)
ja 0xe5b8
movl $0x2, %eax
movl %eax, -0x64(%rbp)
jmp 0xe5d0
movq -0x30(%rbp), %rdx
movl $0x8, %eax
movl $0x4, %ecx
cmpq $0x10, %rdx
cmovbel %ecx, %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x20(%rbp)
xorl %eax, %eax
cmpq $0x100, -0x30(%rbp)     # imm = 0x100
movb %al, -0x65(%rbp)
ja 0xe601
xorl %eax, %eax
cmpl $0x8, -0x4c(%rbp)
movb %al, -0x65(%rbp)
ja 0xe601
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0xe623
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0xe63e
cmpl $0x0, -0x44(%rbp)
jne 0xe63e
movl -0x4c(%rbp), %eax
cmpl -0x20(%rbp), %eax
ja 0xe63e
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x428(%rax)
jne 0xe652
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xe753
movq -0x18(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6350
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
je 0xe6f9
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x2, %rcx
movb (%rax,%rcx), %sil
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x2, %rcx
movb 0x1(%rax,%rcx), %dl
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq $0x2, %rcx
movb 0x2(%rax,%rcx), %cl
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %r8
shlq $0x2, %r8
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl 0x3(%rax,%r8), %r8d
callq 0x64d0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xe6e9
jmp 0xe6f9
jmp 0xe6eb
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xe679
movq -0x8(%rbp), %rax
movl $0x3, (%rax)
movl -0x20(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0xe74e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jb 0xe74e
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0xe74e
movq -0x8(%rbp), %rdi
callq 0x6330
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x63a0
jmp 0xe7f4
movl -0x4c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x44(%rbp)
je 0xe77b
movl -0x40(%rbp), %edx
movl $0x6, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x6c(%rbp)
jmp 0xe78e
movl -0x40(%rbp), %edx
movl $0x2, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x48(%rbp)
je 0xe7f2
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x8(%rax), %ecx
andl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
movzwl 0xa(%rax), %ecx
andl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rax
movzwl 0xc(%rax), %ecx
andl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x18(%rax)
jmp 0xe7f4
movl -0x1c(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)