// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: auto_choose_color(LodePNGColorMode*, LodePNGColorMode const*, LodePNGColorStats const*)
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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movq 0x60(%rsp), %rax
movq 0x420(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x60(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x30(%rsp)
movq 0x60(%rsp), %rax
movl 0x418(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x70(%rsp), %rax
movl $0x0, 0x18(%rax)
cmpl $0x0, 0x30(%rsp)
je 0x1486af
cmpq $0x10, 0x40(%rsp)
ja 0x1486af
movl $0x1, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
cmpl $0x8, 0x2c(%rsp)
jae 0x1486ad
movl $0x8, 0x2c(%rsp)
jmp 0x1486af
movq 0x60(%rsp), %rax
cmpl $0x0, (%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x38(%rsp)
movq 0x60(%rsp), %rax
cmpl $0x0, 0x42c(%rax)
jne 0x1486db
movl $0x0, 0x38(%rsp)
cmpl $0x0, 0x38(%rsp)
jne 0x1486f1
cmpl $0x8, 0x2c(%rsp)
jae 0x1486f1
movl $0x8, 0x2c(%rsp)
movq 0x60(%rsp), %rax
movl 0x14(%rax), %eax
movq %rax, 0x48(%rsp)
cmpq $0x2, 0x48(%rsp)
ja 0x148711
movl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x148746
cmpq $0x4, 0x48(%rsp)
ja 0x148724
movl $0x2, %eax
movl %eax, 0x14(%rsp)
jmp 0x14873e
movq 0x48(%rsp), %rdx
movl $0x8, %eax
movl $0x4, %ecx
cmpq $0x10, %rdx
cmovbel %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x58(%rsp)
xorl %eax, %eax
cmpq $0x100, 0x48(%rsp)      # imm = 0x100
movb %al, 0x13(%rsp)
ja 0x148779
xorl %eax, %eax
cmpl $0x8, 0x2c(%rsp)
movb %al, 0x13(%rsp)
ja 0x148779
cmpq $0x0, 0x48(%rsp)
setne %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0x1487a0
movl $0x0, 0x3c(%rsp)
cmpl $0x0, 0x38(%rsp)
je 0x1487c0
cmpl $0x0, 0x34(%rsp)
jne 0x1487c0
movl 0x2c(%rsp), %eax
cmpl 0x58(%rsp), %eax
ja 0x1487c0
movl $0x0, 0x3c(%rsp)
movq 0x60(%rsp), %rax
cmpl $0x0, 0x428(%rax)
jne 0x1487d6
movl $0x0, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
je 0x1488fd
movq 0x60(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rdi
callq 0x13f8e0
movq $0x0, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
je 0x148898
movq 0x70(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %rcx
shlq $0x2, %rcx
movb (%rax,%rcx), %sil
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %rcx
shlq $0x2, %rcx
movb 0x1(%rax,%rcx), %dl
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %rcx
shlq $0x2, %rcx
movb 0x2(%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %r8
shlq $0x2, %r8
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl 0x3(%rax,%r8), %r8d
callq 0x13fa40
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
je 0x148883
jmp 0x148898
jmp 0x148885
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x148802
movq 0x70(%rsp), %rax
movl $0x3, (%rax)
movl 0x58(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x68(%rsp), %rax
cmpl $0x3, (%rax)
jne 0x1488f8
movq 0x68(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x70(%rsp), %rcx
cmpq 0x10(%rcx), %rax
jb 0x1488f8
movq 0x68(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x1488f8
movq 0x70(%rsp), %rdi
callq 0x13f8d0
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x13f920
jmp 0x1489b4
movl 0x2c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, 0x34(%rsp)
je 0x14892a
movl 0x38(%rsp), %edx
movl $0x6, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0xc(%rsp)
jmp 0x14893f
movl 0x38(%rsp), %edx
movl $0x2, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, (%rax)
cmpl $0x0, 0x30(%rsp)
je 0x1489b2
movq 0x70(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, 0x1c(%rsp)
movq 0x60(%rsp), %rax
movzwl 0x8(%rax), %ecx
andl 0x1c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x1c(%rax)
movq 0x60(%rsp), %rax
movzwl 0xa(%rax), %ecx
andl 0x1c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x60(%rsp), %rax
movzwl 0xc(%rax), %ecx
andl 0x1c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x24(%rax)
movq 0x70(%rsp), %rax
movl $0x1, 0x18(%rax)
jmp 0x1489b4
movl 0x5c(%rsp), %eax
addq $0x78, %rsp
retq
nopl (%rax)