// File: /workspace/github_cpp_makefile_star2_O2/Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: lodepng_compute_color_stats(LodePNGColorStats*, unsigned char const*, unsigned int, unsigned int, LodePNGColorMode const*)
void lodepng_compute_color_stats(LodePNGColorStats* stats,
                                 const unsigned char* in, unsigned w, unsigned h,
                                 const LodePNGColorMode* mode_in) {
  size_t i;
  ColorTree tree;
  size_t numpixels = (size_t)w * (size_t)h;

  /* mark things as done already if it would be impossible to have a more expensive case */
  unsigned colored_done = lodepng_is_greyscale_type(mode_in) ? 1 : 0;
  unsigned alpha_done = lodepng_can_have_alpha(mode_in) ? 0 : 1;
  unsigned numcolors_done = 0;
  unsigned bpp = lodepng_get_bpp(mode_in);
  unsigned bits_done = (stats->bits == 1 && bpp == 1) ? 1 : 0;
  unsigned sixteen = 0; /* whether the input image is 16 bit */
  unsigned maxnumcolors = 257;
  if(bpp <= 8) maxnumcolors = LODEPNG_MIN(257, stats->numcolors + (1u << bpp));

  stats->numpixels += numpixels;

  /*if palette not allowed, no need to compute numcolors*/
  if(!stats->allow_palette) numcolors_done = 1;

  color_tree_init(&tree);

  /*If the stats was already filled in from previous data, fill its palette in tree
  and mark things as done already if we know they are the most expensive case already*/
  if(stats->alpha) alpha_done = 1;
  if(stats->colored) colored_done = 1;
  if(stats->bits == 16) numcolors_done = 1;
  if(stats->bits >= bpp) bits_done = 1;
  if(stats->numcolors >= maxnumcolors) numcolors_done = 1;

  if(!numcolors_done) {
    for(i = 0; i < stats->numcolors; i++) {
      const unsigned char* color = &stats->palette[i * 4];
      color_tree_add(&tree, color[0], color[1], color[2], color[3], (unsigned int) i);
    }
  }

  /*Check if the 16-bit input is truly 16-bit*/
  if(mode_in->bitdepth == 16 && !sixteen) {
    unsigned short r, g, b, a;
    for(i = 0; i != numpixels; ++i) {
      getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
      if((r & 255) != ((r >> 8) & 255) || (g & 255) != ((g >> 8) & 255) ||
         (b & 255) != ((b >> 8) & 255) || (a & 255) != ((a >> 8) & 255)) /*first and second byte differ*/ {
        stats->bits = 16;
        sixteen = 1;
        bits_done = 1;
        numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/
        break;
      }
    }
  }

  if(sixteen) {
    unsigned short r = 0, g = 0, b = 0, a = 0;

    for(i = 0; i != numpixels; ++i) {
      getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);

      if(!colored_done && (r != g || r != b)) {
        stats->colored = 1;
        colored_done = 1;
      }

      if(!alpha_done) {
        unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b);
        if(a != 65535 && (a != 0 || (stats->key && !matchkey))) {
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
        } else if(a == 0 && !stats->alpha && !stats->key) {
          stats->key = 1;
          stats->key_r = r;
          stats->key_g = g;
          stats->key_b = b;
        } else if(a == 65535 && stats->key && matchkey) {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
        }
      }
      if(alpha_done && numcolors_done && colored_done && bits_done) break;
    }

    if(stats->key && !stats->alpha) {
      for(i = 0; i != numpixels; ++i) {
        getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
        if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
        }
      }
    }
  } else /* < 16-bit */ {
    unsigned char r = 0, g = 0, b = 0, a = 0;
    for(i = 0; i != numpixels; ++i) {
      getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);

      if(!bits_done && stats->bits < 8) {
        /*only r is checked, < 8 bits is only relevant for grayscale*/
        unsigned bits = getValueRequiredBits(r);
        if(bits > stats->bits) stats->bits = bits;
      }
      bits_done = (stats->bits >= bpp);

      if(!colored_done && (r != g || r != b)) {
        stats->colored = 1;
        colored_done = 1;
        if(stats->bits < 8) stats->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/
      }

      if(!alpha_done) {
        unsigned matchkey = (r == stats->key_r && g == stats->key_g && b == stats->key_b);
        if(a != 255 && (a != 0 || (stats->key && !matchkey))) {
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
          if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        } else if(a == 0 && !stats->alpha && !stats->key) {
          stats->key = 1;
          stats->key_r = r;
          stats->key_g = g;
          stats->key_b = b;
        } else if(a == 255 && stats->key && matchkey) {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
          if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        }
      }

      if(!numcolors_done) {
        if(!color_tree_has(&tree, r, g, b, a)) {
          color_tree_add(&tree, r, g, b, a, stats->numcolors);
          if(stats->numcolors < 256) {
            unsigned char* p = stats->palette;
            unsigned n = stats->numcolors;
            p[n * 4 + 0] = r;
            p[n * 4 + 1] = g;
            p[n * 4 + 2] = b;
            p[n * 4 + 3] = a;
          }
          ++stats->numcolors;
          numcolors_done = stats->numcolors >= maxnumcolors;
        }
      }

      if(alpha_done && numcolors_done && colored_done && bits_done) break;
    }

    if(stats->key && !stats->alpha) {
      for(i = 0; i != numpixels; ++i) {
        getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
        if(a != 0 && r == stats->key_r && g == stats->key_g && b == stats->key_b) {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          stats->alpha = 1;
          stats->key = 0;
          alpha_done = 1;
          if(stats->bits < 8) stats->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        }
      }
    }

    /*make the stats's key always 16-bit for consistency - repeat each byte twice*/
    stats->key_r += (stats->key_r << 8);
    stats->key_g += (stats->key_g << 8);
    stats->key_b += (stats->key_b << 8);
  }

  color_tree_cleanup(&tree);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, %rbx
movq %rsi, 0x30(%rsp)
movq %rdi, %r13
movl %edx, %eax
movl %ecx, %ecx
imulq %rax, %rcx
movq %rcx, 0x20(%rsp)
movl (%r8), %r14d
movq %r8, %rdi
callq 0x760e5
movl %eax, %r12d
xorl $0x1, %r12d
movq %rbx, 0x48(%rsp)
movq %rbx, %rdi
callq 0x7607a
movl %eax, %ebx
movl 0x418(%r13), %ebp
movl %ebp, %eax
xorl $0x1, %eax
movl %ebx, %ecx
xorl $0x1, %ecx
orl %eax, %ecx
sete 0x50(%rsp)
movl $0x101, %r15d           # imm = 0x101
cmpl $0x8, %ebx
ja 0x77ad7
pushq $0x1
popq %r15
movl %ebx, %ecx
shll %cl, %r15d
addl 0x14(%r13), %r15d
movl $0x101, %eax            # imm = 0x101
cmpl %eax, %r15d
cmovael %eax, %r15d
testl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
sete %r14b
movq 0x20(%rsp), %rax
addq %rax, 0x420(%r13)
cmpl $0x0, 0x428(%r13)
sete 0x17(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x7739b
cmpl $0x0, 0x10(%r13)
pushq $0x1
popq %rax
cmovnel %eax, %r12d
movl %r12d, 0x10(%rsp)
cmpl $0x0, (%r13)
setne %r12b
orb %r14b, %r12b
cmpl $0x10, %ebp
sete %r14b
movl %ebx, 0x1c(%rsp)
cmpl %ebx, %ebp
setae %bl
movq %r13, 0x28(%rsp)
movl 0x14(%r13), %ecx
movl %r15d, 0x5c(%rsp)
cmpl %r15d, %ecx
setae %al
orb 0x17(%rsp), %r14b
orb %al, %r14b
je 0x78186
movq %rcx, 0x68(%rsp)
movzbl %r12b, %ebp
movq 0x48(%rsp), %r12
cmpl $0x10, 0x4(%r12)
movl 0x1c(%rsp), %r13d
jne 0x77de5
xorl %r15d, %r15d
cmpq %r15, 0x20(%rsp)
je 0x77de5
movq %r12, (%rsp)
leaq 0xc(%rsp), %rdi
leaq 0xa(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x77456
movzwl 0xc(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x77be5
movzwl 0xa(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x77be5
movzwl 0x8(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x77be5
movzwl 0xe(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
incq %r15
cmpl %eax, %ecx
je 0x77b76
movl %ebp, %ebx
movq 0x28(%rsp), %r14
movl $0x10, 0x418(%r14)
leaq 0xc(%rsp), %rax
andw $0x0, (%rax)
leaq 0xa(%rsp), %rax
andw $0x0, (%rax)
leaq 0x8(%rsp), %rax
andw $0x0, (%rax)
leaq 0xe(%rsp), %r15
andw $0x0, (%r15)
xorl %r13d, %r13d
pushq $0x1
popq %rbp
cmpq %r13, 0x20(%rsp)
je 0x77d4d
movq %r12, (%rsp)
leaq 0xc(%rsp), %rdi
leaq 0xa(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r15, %rcx
movq 0x30(%rsp), %r8
movq %r13, %r9
callq 0x77456
movb $0x1, %cl
movb $0x1, %al
testl %ebx, %ebx
movl %ebp, %ebx
jne 0x77c7f
movzwl 0xc(%rsp), %edx
cmpw 0xa(%rsp), %dx
jne 0x77c74
xorl %eax, %eax
movl $0x0, %ebx
cmpw 0x8(%rsp), %dx
je 0x77c7f
movl $0x1, (%r14)
movb $0x1, %al
movl %ebp, %ebx
cmpl $0x0, 0x10(%rsp)
jne 0x77d3c
movzwl 0xc(%rsp), %edx
cmpw 0x8(%r14), %dx
jne 0x77cb1
movzwl 0xa(%rsp), %ecx
cmpw 0xa(%r14), %cx
jne 0x77cb1
movzwl 0x8(%rsp), %ecx
cmpw 0xc(%r14), %cx
sete %cl
jmp 0x77cb3
xorl %ecx, %ecx
movzwl 0xe(%rsp), %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x77d14
testl %esi, %esi
jne 0x77d2d
movl 0x4(%r14), %esi
testl %esi, %esi
sete %dil
orb %cl, %dil
testb $0x1, %dil
je 0x77d2d
xorl %ecx, %ecx
orl 0x10(%r14), %esi
movl $0x0, 0x10(%rsp)
jne 0x77d40
movl $0x1, 0x4(%r14)
movw %dx, 0x8(%r14)
movzwl 0xa(%rsp), %ecx
movw %cx, 0xa(%r14)
movzwl 0x8(%rsp), %ecx
movw %cx, 0xc(%r14)
xorl %ecx, %ecx
movl $0x0, 0x10(%rsp)
jmp 0x77d40
cmpl $0x0, 0x4(%r14)
setne %dl
andb %cl, %dl
xorl %ecx, %ecx
movl $0x0, 0x10(%rsp)
cmpb $0x1, %dl
jne 0x77d40
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
movb $0x1, %cl
movl %ebp, 0x10(%rsp)
andb %cl, %al
incq %r13
cmpb $0x1, %al
jne 0x77c22
cmpl $0x0, 0x4(%r14)
je 0x7816a
cmpl $0x0, 0x10(%r14)
jne 0x7816a
xorl %r15d, %r15d
leaq 0xc(%rsp), %rbx
leaq 0xa(%rsp), %r13
leaq 0x8(%rsp), %rbp
leaq 0xe(%rsp), %r12
cmpq %r15, 0x20(%rsp)
je 0x7816a
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x77456
cmpw $0x0, 0xe(%rsp)
je 0x77de0
movzwl 0xc(%rsp), %eax
cmpw 0x8(%r14), %ax
jne 0x77de0
movzwl 0xa(%rsp), %eax
cmpw 0xa(%r14), %ax
jne 0x77de0
movzwl 0x8(%rsp), %eax
cmpw 0xc(%r14), %ax
jne 0x77de0
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
incq %r15
jmp 0x77d7a
orb 0x50(%rsp), %bl
movzbl %r14b, %edx
xorl %r9d, %r9d
leaq 0xc(%rsp), %rax
movb %r9b, (%rax)
leaq 0xa(%rsp), %rax
movb %r9b, (%rax)
leaq 0x8(%rsp), %rax
movb %r9b, (%rax)
leaq 0xe(%rsp), %rax
movb %r9b, (%rax)
movq 0x28(%rsp), %r14
cmpq %r9, 0x20(%rsp)
je 0x78137
movl %edx, %r12d
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
leaq 0xc(%rsp), %rdi
leaq 0xa(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq 0x30(%rsp), %r8
movq %r9, 0x50(%rsp)
callq 0x775ce
movl 0x418(%r14), %r10d
cmpl $0x8, %r10d
setae %al
orb %al, %bl
testb $0x1, %bl
je 0x77e6b
leaq 0x70(%rsp), %rdi
jmp 0x77eb8
movb 0xc(%rsp), %cl
leal 0x1(%rcx), %edx
pushq $0x1
popq %rax
cmpb $0x2, %dl
leaq 0x70(%rsp), %rdi
jb 0x77ea9
movzbl %cl, %ecx
movl %ecx, %eax
movb $0x11, %dl
divb %dl
movzbl %ah, %edx
pushq $0x8
popq %rax
testb %dl, %dl
jne 0x77ea9
movl %ecx, %eax
movb $0x55, %cl
divb %cl
movzbl %ah, %eax
xorl %ecx, %ecx
testb %al, %al
setne %cl
leal 0x2(,%rcx,2), %eax
cmpl %r10d, %eax
jbe 0x77eb8
movl %eax, 0x418(%r14)
movl %eax, %r10d
cmpl %r13d, %r10d
setae 0x17(%rsp)
movb $0x1, %r9b
movl %r10d, %eax
movb $0x1, %bl
testl %ebp, %ebp
pushq $0x1
popq %rbp
jne 0x77f0e
movb 0xc(%rsp), %cl
cmpb 0xa(%rsp), %cl
jne 0x77ee9
xorl %ebx, %ebx
movl %r10d, %eax
movl $0x0, %ebp
cmpb 0x8(%rsp), %cl
je 0x77f0e
movl $0x1, (%r14)
movl %r10d, %eax
movb $0x1, %bl
pushq $0x1
popq %rbp
cmpl $0x7, %r10d
ja 0x77f0e
movl $0x8, 0x418(%r14)
pushq $0x8
popq %rax
movb $0x1, %bl
cmpl $0x0, 0x10(%rsp)
je 0x77f2c
movb $0x1, %r9b
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
testl %r12d, %r12d
je 0x77f53
pushq $0x1
popq %rdx
jmp 0x780c9
movzbl 0xc(%rsp), %ecx
cmpw %cx, 0x8(%r14)
jne 0x77fae
movzbl 0xa(%rsp), %edx
cmpw %dx, 0xa(%r14)
jne 0x77fae
movzbl 0x8(%rsp), %edx
cmpw %dx, 0xc(%r14)
sete %dl
jmp 0x77fb0
movl %r10d, 0x3c(%rsp)
movl %r9d, 0x40(%rsp)
movl %ebp, 0x44(%rsp)
movb 0xc(%rsp), %al
movb 0xa(%rsp), %r15b
movb 0x8(%rsp), %bpl
movb 0xe(%rsp), %r14b
movb %al, 0x1b(%rsp)
movzbl %al, %esi
movzbl %r15b, %edx
movzbl %bpl, %r12d
movzbl %r14b, %r13d
movl %esi, 0x64(%rsp)
movl %edx, 0x60(%rsp)
movl %r12d, %ecx
movl %r13d, %r8d
callq 0x7d0d0
xorl %edx, %edx
testl %eax, %eax
js 0x78039
movq 0x28(%rsp), %r14
jmp 0x780b6
xorl %edx, %edx
movzbl 0xe(%rsp), %esi
cmpl $0xff, %esi
je 0x780f3
testl %esi, %esi
jne 0x7801e
movl 0x4(%r14), %esi
testl %esi, %esi
sete %r8b
orb %dl, %r8b
testb $0x1, %r8b
je 0x7801e
xorl %r9d, %r9d
orl 0x10(%r14), %esi
movl $0x0, 0x10(%rsp)
jne 0x77f1f
movl $0x1, 0x4(%r14)
movw %cx, 0x8(%r14)
movzbl 0xa(%rsp), %eax
movw %ax, 0xa(%r14)
movzbl 0x8(%rsp), %eax
movw %ax, 0xc(%r14)
xorl %r9d, %r9d
movl $0x0, 0x10(%rsp)
jmp 0x77f1f
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, %eax
ja 0x77f18
jmp 0x78127
movb %r14b, 0x18(%rsp)
movb %bpl, 0x19(%rsp)
movb %r15b, 0x1a(%rsp)
leaq 0x70(%rsp), %rdi
movl 0x64(%rsp), %esi
movl 0x60(%rsp), %edx
movl %r12d, %ecx
movl %r13d, %r8d
movq 0x68(%rsp), %r9
callq 0x773b5
movq 0x28(%rsp), %r14
movl 0x14(%r14), %ecx
cmpq $0xff, %rcx
ja 0x780a2
leal (,%rcx,4), %eax
movb 0x1b(%rsp), %dl
movb %dl, 0x18(%r14,%rcx,4)
movb 0x1a(%rsp), %dl
movb %dl, 0x19(%r14,%rax)
movb 0x19(%rsp), %dl
movb %dl, 0x1a(%r14,%rax)
movb 0x18(%rsp), %dl
movb %dl, 0x1b(%r14,%rax)
incl %ecx
movl %ecx, 0x14(%r14)
xorl %edx, %edx
movq %rcx, 0x68(%rsp)
cmpl 0x5c(%rsp), %ecx
setae %dl
movl 0x44(%rsp), %ebp
movl 0x1c(%rsp), %r13d
movl 0x40(%rsp), %r9d
movl 0x3c(%rsp), %r10d
testl %edx, %edx
setne %al
andb %r9b, %bl
andb %al, %bl
movq 0x50(%rsp), %r9
incq %r9
cmpb $0x1, %bl
movb 0x17(%rsp), %bl
jne 0x77e15
cmpl %r13d, %r10d
jb 0x77e15
jmp 0x78137
cmpl $0x0, 0x4(%r14)
setne %cl
andb %dl, %cl
xorl %r9d, %r9d
movl $0x0, 0x10(%rsp)
cmpb $0x1, %cl
jne 0x77f1f
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, %eax
ja 0x77f15
movl $0x8, 0x418(%r14)
jmp 0x77f15
cmpl $0x0, 0x4(%r14)
je 0x78149
cmpl $0x0, 0x10(%r14)
je 0x781ca
movq 0x8(%r14), %xmm0
pmullw 0x18369(%rip), %xmm0    # 0x904c0
movd %xmm0, 0x8(%r14)
imull $0x101, 0xc(%r14), %eax # imm = 0x101
movw %ax, 0xc(%r14)
leaq 0x70(%rsp), %rdi
callq 0x77876
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
leaq 0x70(%rsp), %rbp
movq 0x28(%rsp), %r13
movl %ecx, %eax
cmpq %rax, %r15
jae 0x77b54
movzbl 0x18(%r13,%r15,4), %esi
movzbl 0x19(%r13,%r15,4), %edx
movzbl 0x1a(%r13,%r15,4), %ecx
movzbl 0x1b(%r13,%r15,4), %r8d
movq %rbp, %rdi
movl %r15d, %r9d
callq 0x773b5
incq %r15
movl 0x14(%r13), %ecx
jmp 0x78193
xorl %r15d, %r15d
leaq 0xc(%rsp), %rbx
leaq 0xa(%rsp), %r13
leaq 0x8(%rsp), %rbp
leaq 0xe(%rsp), %r12
cmpq %r15, 0x20(%rsp)
je 0x78149
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x775ce
cmpb $0x0, 0xe(%rsp)
je 0x7825b
movzbl 0xc(%rsp), %eax
cmpw %ax, 0x8(%r14)
jne 0x7825b
movzbl 0xa(%rsp), %eax
cmpw %ax, 0xa(%r14)
jne 0x7825b
movzbl 0x8(%rsp), %eax
cmpw %ax, 0xc(%r14)
jne 0x7825b
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, 0x418(%r14)
ja 0x7825b
movl $0x8, 0x418(%r14)
incq %r15
jmp 0x781e1