// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq 0x420(%rdx), %rsi
movl 0x4(%rdx), %eax
movl 0x10(%rdx), %r9d
movl 0x418(%rdx), %edi
movl $0x0, 0x18(%rbx)
testl %eax, %eax
sete %cl
cmpq $0x11, %rsi
setae %dl
orb %cl, %dl
cmpl $0x9, %edi
movl $0x8, %r10d
cmovael %edi, %r10d
movl $0x8, %ecx
testb %dl, %dl
cmovnel %edi, %r10d
movl 0x14(%r15), %r8d
cmpl $0x0, (%r15)
sete %r11b
cmpl $0x0, 0x42c(%r15)
setne %dil
andb %r11b, %dil
cmpl $0x8, %r10d
cmovael %r10d, %ecx
testb %dil, %dil
cmovnel %r10d, %ecx
movl $0x1, %r12d
cmpq $0x3, %r8
jb 0x6d0b7
movl $0x2, %r12d
cmpl $0x5, %r8d
jb 0x6d0b7
xorl %r10d, %r10d
cmpl $0x11, %r8d
setae %r10b
leal 0x4(,%r10,4), %r12d
testl %r9d, %r9d
sete %r9b
andb %r9b, %dl
cmpl %r12d, %ecx
setbe %r9b
cmpl $0x0, 0x428(%r15)
je 0x6d15e
movl %edi, %r10d
andb %dl, %r10b
andb %r9b, %r10b
jne 0x6d15e
leal -0x101(%r8), %r9d
cmpl $0xffffff00, %r9d       # imm = 0xFFFFFF00
jb 0x6d15e
addq %r8, %r8
cmpq %r8, %rsi
jb 0x6d15e
cmpl $0x8, %ecx
ja 0x6d15e
leaq 0x8(%rbx), %r13
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x6d110
callq 0x3a540
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
cmpl $0x0, 0x14(%r15)
je 0x6d1c1
movq %r14, (%rsp)
xorl %r14d, %r14d
movzbl 0x18(%r15,%r14,4), %esi
movzbl 0x19(%r15,%r14,4), %edx
movzbl 0x1a(%r15,%r14,4), %ecx
movzbl 0x1b(%r15,%r14,4), %r8d
movq %rbx, %rdi
callq 0x67d89
testl %eax, %eax
jne 0x6d1c5
incq %r14
movl 0x14(%r15), %eax
cmpq %rax, %r14
jne 0x6d12a
xorl %ebp, %ebp
jmp 0x6d1c7
cmpq $0x11, %rsi
setae %sil
movl %ecx, 0x4(%rbx)
xorb $0x1, %dil
movzbl %dil, %edi
leal 0x4(,%rdi,2), %r8d
addl %edi, %edi
testb %dl, %dl
cmovel %r8d, %edi
movl %edi, (%rbx)
testl %eax, %eax
setne %al
andb %sil, %al
xorl %ebp, %ebp
cmpb $0x1, %al
jne 0x6d20c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll %cl, %eax
notl %eax
movzwl 0x8(%r15), %ecx
andl %eax, %ecx
movl %ecx, 0x1c(%rbx)
movzwl 0xa(%r15), %ecx
andl %eax, %ecx
movl %ecx, 0x20(%rbx)
movzwl 0xc(%r15), %ecx
andl %eax, %ecx
movl %ecx, 0x24(%rbx)
movl $0x1, 0x18(%rbx)
jmp 0x6d20c
xorl %ebp, %ebp
jmp 0x6d1cb
movl %eax, %ebp
movq (%rsp), %r14
movl $0x3, (%rbx)
movl %r12d, 0x4(%rbx)
cmpl $0x3, (%r14)
jne 0x6d20c
movq 0x10(%r14), %rax
cmpq 0x10(%rbx), %rax
jb 0x6d20c
cmpl %r12d, 0x4(%r14)
jne 0x6d20c
movq (%r13), %rdi
testq %rdi, %rdi
je 0x6d1f9
callq 0x3a540
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x67cd4
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq