// File: /workspace/github_cpp_makefile_star2_O2/phillip-h[P]fractal-gen/lodepng.cpp
// Function: lodepng_get_color_profile(LodePNGColorProfile*, unsigned char const*, unsigned int, unsigned int, LodePNGColorMode const*)
unsigned lodepng_get_color_profile(LodePNGColorProfile* profile,
                                   const unsigned char* in, unsigned w, unsigned h,
                                   const LodePNGColorMode* mode)
{
  unsigned error = 0;
  size_t i;
  ColorTree tree;
  size_t numpixels = w * h;

  unsigned colored_done = lodepng_is_greyscale_type(mode) ? 1 : 0;
  unsigned alpha_done = lodepng_can_have_alpha(mode) ? 0 : 1;
  unsigned numcolors_done = 0;
  unsigned bpp = lodepng_get_bpp(mode);
  unsigned bits_done = bpp == 1 ? 1 : 0;
  unsigned maxnumcolors = 257;
  unsigned sixteen = 0;
  if(bpp <= 8) maxnumcolors = bpp == 1 ? 2 : (bpp == 2 ? 4 : (bpp == 4 ? 16 : 256));

  color_tree_init(&tree);

  /*Check if the 16-bit input is truly 16-bit*/
  if(mode->bitdepth == 16)
  {
    unsigned short r, g, b, a;
    for(i = 0; i != numpixels; ++i)
    {
      getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
      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*/
      {
        sixteen = 1;
        break;
      }
    }
  }

  if(sixteen)
  {
    unsigned short r = 0, g = 0, b = 0, a = 0;
    profile->bits = 16;
    bits_done = numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/

    for(i = 0; i != numpixels; ++i)
    {
      getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);

      if(!colored_done && (r != g || r != b))
      {
        profile->colored = 1;
        colored_done = 1;
      }

      if(!alpha_done)
      {
        unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b);
        if(a != 65535 && (a != 0 || (profile->key && !matchkey)))
        {
          profile->alpha = 1;
          alpha_done = 1;
          if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        }
        else if(a == 0 && !profile->alpha && !profile->key)
        {
          profile->key = 1;
          profile->key_r = r;
          profile->key_g = g;
          profile->key_b = b;
        }
        else if(a == 65535 && profile->key && matchkey)
        {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          profile->alpha = 1;
          alpha_done = 1;
        }
      }
      if(alpha_done && numcolors_done && colored_done && bits_done) break;
    }

    if(profile->key && !profile->alpha)
    {
      for(i = 0; i != numpixels; ++i)
      {
        getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode);
        if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b)
        {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          profile->alpha = 1;
          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);

      if(!bits_done && profile->bits < 8)
      {
        /*only r is checked, < 8 bits is only relevant for greyscale*/
        unsigned bits = getValueRequiredBits(r);
        if(bits > profile->bits) profile->bits = bits;
      }
      bits_done = (profile->bits >= bpp);

      if(!colored_done && (r != g || r != b))
      {
        profile->colored = 1;
        colored_done = 1;
        if(profile->bits < 8) profile->bits = 8; /*PNG has no colored modes with less than 8-bit per channel*/
      }

      if(!alpha_done)
      {
        unsigned matchkey = (r == profile->key_r && g == profile->key_g && b == profile->key_b);
        if(a != 255 && (a != 0 || (profile->key && !matchkey)))
        {
          profile->alpha = 1;
          alpha_done = 1;
          if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        }
        else if(a == 0 && !profile->alpha && !profile->key)
        {
          profile->key = 1;
          profile->key_r = r;
          profile->key_g = g;
          profile->key_b = b;
        }
        else if(a == 255 && profile->key && matchkey)
        {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          profile->alpha = 1;
          alpha_done = 1;
          if(profile->bits < 8) profile->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, profile->numcolors);
          if(profile->numcolors < 256)
          {
            unsigned char* p = profile->palette;
            unsigned n = profile->numcolors;
            p[n * 4 + 0] = r;
            p[n * 4 + 1] = g;
            p[n * 4 + 2] = b;
            p[n * 4 + 3] = a;
          }
          ++profile->numcolors;
          numcolors_done = profile->numcolors >= maxnumcolors;
        }
      }

      if(alpha_done && numcolors_done && colored_done && bits_done) break;
    }

    if(profile->key && !profile->alpha)
    {
      for(i = 0; i != numpixels; ++i)
      {
        getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode);
        if(a != 0 && r == profile->key_r && g == profile->key_g && b == profile->key_b)
        {
          /* Color key cannot be used if an opaque pixel also has that RGB color. */
          profile->alpha = 1;
          alpha_done = 1;
        }
      }
    }

    /*make the profile's key always 16-bit for consistency - repeat each byte twice*/
    profile->key_r += (profile->key_r << 8);
    profile->key_g += (profile->key_g << 8);
    profile->key_b += (profile->key_b << 8);
  }

  color_tree_cleanup(&tree);
  return error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
imull %ecx, %edx
xorl %ebp, %ebp
testl $0xfffffffb, (%r8)      # imm = 0xFFFFFFFB
movq %rdi, %r14
movq %r8, %rdi
movq %r8, %r15
movq %rsi, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
sete %bl
callq 0x5fdb
movq %r15, %rdi
movl %eax, %r13d
callq 0x5f6e
cmpl $0x1, %eax
movl %eax, %r12d
movl $0x101, %ecx            # imm = 0x101
sete %sil
cmpl $0x9, %eax
setae %al
pushq $0x2
popq %rdx
cmovael %ecx, %edx
orb %sil, %al
movb %sil, 0x1f(%rsp)
movl %edx, 0x24(%rsp)
jne 0x7518
cmpl $0x2, %r12d
jne 0x7505
pushq $0x4
popq %rax
movl %eax, 0x24(%rsp)
jmp 0x7518
cmpl $0x4, %r12d
pushq $0x10
popq %rax
movl $0x100, %ecx            # imm = 0x100
cmovel %eax, %ecx
movl %ecx, 0x24(%rsp)
leaq 0x70(%rsp), %rdi
movb %bl, %bpl
xorl $0x1, %r13d
callq 0x6ba4
cmpl $0x10, 0x4(%r15)
jne 0x7774
movl %r12d, %ebx
xorl %r12d, %r12d
cmpq %r12, 0x30(%rsp)
je 0x7771
movq 0x28(%rsp), %r8
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %r12, %r9
movq %r15, (%rsp)
callq 0x6c5d
movzwl 0x1a(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x75a9
movzwl 0x18(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x75a9
movzwl 0x16(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x75a9
movzwl 0x1c(%rsp), %eax
incq %r12
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
je 0x753a
leaq 0x18(%rsp), %rcx
leaq 0x1a(%rsp), %rax
leaq 0x16(%rsp), %rdx
movl %r13d, %ebx
xorl %r12d, %r12d
andw $0x0, (%rcx)
leaq 0x1c(%rsp), %rcx
andw $0x0, (%rax)
andw $0x0, (%rdx)
andw $0x0, (%rcx)
movl $0x10, 0x418(%r14)
pushq $0x1
popq %r13
cmpq %r12, 0x30(%rsp)
je 0x76e6
movq 0x28(%rsp), %r8
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %r12, %r9
movq %r15, (%rsp)
callq 0x6c5d
movb $0x1, %al
testl %ebp, %ebp
jne 0x763b
movzwl 0x1a(%rsp), %ecx
cmpw 0x18(%rsp), %cx
jne 0x7632
movl $0x0, %ebp
xorl %eax, %eax
cmpw 0x16(%rsp), %cx
je 0x763e
movb $0x1, %al
movl $0x1, (%r14)
movl %r13d, %ebp
testl %ebx, %ebx
jne 0x76d7
movzwl 0x1a(%rsp), %ecx
cmpw 0x8(%r14), %cx
jne 0x766d
movzwl 0x18(%rsp), %edx
cmpw 0xa(%r14), %dx
jne 0x766d
movzwl 0x16(%rsp), %edx
cmpw 0xc(%r14), %dx
sete %dl
jmp 0x766f
xorl %edx, %edx
movzwl 0x1c(%rsp), %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x76be
testl %esi, %esi
jne 0x76cf
movl 0x4(%r14), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x76cf
xorl %ebx, %ebx
orl 0x10(%r14), %esi
jne 0x76de
movl $0x1, 0x4(%r14)
movw %cx, 0x8(%r14)
movzwl 0x18(%rsp), %eax
movw %ax, 0xa(%r14)
movzwl 0x16(%rsp), %eax
movw %ax, 0xc(%r14)
jmp 0x76de
cmpl $0x0, 0x4(%r14)
setne %cl
xorl %ebx, %ebx
andb %dl, %cl
cmpb $0x1, %cl
jne 0x76de
movl $0x1, 0x10(%r14)
movl %r13d, %ebx
testb %al, %al
jne 0x76e6
incq %r12
jmp 0x75e2
cmpl $0x0, 0x4(%r14)
je 0x7ad8
cmpl $0x0, 0x10(%r14)
jne 0x7ad8
leaq 0x1a(%rsp), %rbx
leaq 0x16(%rsp), %r13
leaq 0x1c(%rsp), %rbp
xorl %r12d, %r12d
cmpq %r12, 0x30(%rsp)
je 0x7ad8
movq 0x28(%rsp), %r8
leaq 0x18(%rsp), %rsi
movq %r15, (%rsp)
movq %rbx, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movq %r12, %r9
callq 0x6c5d
cmpw $0x0, 0x1c(%rsp)
je 0x776c
movzwl 0x1a(%rsp), %eax
cmpw 0x8(%r14), %ax
jne 0x776c
movzwl 0x18(%rsp), %eax
cmpw 0xa(%r14), %ax
jne 0x776c
movzwl 0x16(%rsp), %eax
cmpw 0xc(%r14), %ax
jne 0x776c
movl $0x1, 0x10(%r14)
incq %r12
jmp 0x770e
movl %ebx, %r12d
pushq $0x1
popq %rbx
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x1a(%rsp), %rax
leaq 0x16(%rsp), %rsi
xorl %r9d, %r9d
movq %r15, 0x60(%rsp)
movl %r12d, 0x54(%rsp)
movb %cl, (%rdx)
leaq 0x1c(%rsp), %rdx
movb %cl, (%rax)
movb %cl, (%rsi)
movb %cl, (%rdx)
cmpq %r9, 0x30(%rsp)
je 0x7aa4
movq 0x28(%rsp), %r8
movq %rcx, 0x40(%rsp)
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %r15, (%rsp)
movq %r9, 0x68(%rsp)
callq 0x717c
movl 0x418(%r14), %r10d
movb 0x1f(%rsp), %cl
cmpl $0x8, %r10d
setae %al
orb %al, %cl
testb $0x1, %cl
jne 0x7836
movb 0x1a(%rsp), %cl
movl %ebx, %eax
leal 0x1(%rcx), %edx
cmpb $0x2, %dl
jb 0x7827
movzbl %cl, %ecx
movb $0x11, %dl
movl %ecx, %eax
divb %dl
movzbl %ah, %edx
pushq $0x8
popq %rax
testb %dl, %dl
jne 0x7827
movl %ecx, %eax
movb $0x55, %cl
divb %cl
xorl %ecx, %ecx
movzbl %ah, %eax
testb %al, %al
setne %cl
leal 0x2(%rcx,%rcx), %eax
cmpl %r10d, %eax
jbe 0x7836
movl %eax, %r10d
movl %eax, 0x418(%r14)
cmpl %r12d, %r10d
movb $0x1, %r12b
movb $0x1, %r11b
movl %r10d, %eax
movl %ebx, %r8d
setae 0x1f(%rsp)
testl %ebp, %ebp
jne 0x7894
movb 0x1a(%rsp), %cl
cmpb 0x18(%rsp), %cl
jne 0x786a
movl $0x0, %r8d
xorl %r11d, %r11d
movl %r10d, %eax
cmpb 0x16(%rsp), %cl
je 0x7894
movb $0x1, %r11b
movl $0x1, (%r14)
movl %r10d, %eax
movl %ebx, %r8d
cmpl $0x7, %r10d
ja 0x7894
movl $0x8, 0x418(%r14)
pushq $0x8
popq %rax
movb $0x1, %r11b
movl %ebx, %r8d
testl %r13d, %r13d
je 0x79cb
movb $0x1, %r12b
movl %ebx, %edx
cmpl $0x0, 0x40(%rsp)
movl %ebx, %ecx
movl %edx, 0x58(%rsp)
movl %r8d, 0x5c(%rsp)
jne 0x798f
movb 0x1a(%rsp), %al
movb 0x18(%rsp), %cl
movb 0x1c(%rsp), %bpl
movb 0x16(%rsp), %bl
leaq 0x70(%rsp), %rdi
movl %r10d, 0x40(%rsp)
movl %r11d, 0x3c(%rsp)
movzbl %al, %r13d
movzbl %cl, %r15d
movb %cl, 0x23(%rsp)
movzbl %bl, %ecx
movzbl %bpl, %r8d
movb %al, 0x22(%rsp)
movl %r13d, %esi
movl %r15d, %edx
movl %ecx, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
callq 0xbeb7
movl 0x3c(%rsp), %r11d
movl 0x40(%rsp), %r10d
xorl %ecx, %ecx
testl %eax, %eax
jns 0x798f
movl 0x14(%r14), %r9d
movl 0x50(%rsp), %ecx
movl 0x4c(%rsp), %r8d
leaq 0x70(%rsp), %rdi
movl %r15d, %edx
movl %r13d, %esi
movb %bpl, 0x20(%rsp)
movb %bl, 0x21(%rsp)
movq %r14, %r15
callq 0x6bbe
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x7976
movb 0x22(%rsp), %dl
movb 0x23(%rsp), %sil
leal (,%rax,4), %ecx
movb 0x21(%rsp), %dil
movb %dl, 0x18(%r15,%rax,4)
movb %sil, 0x19(%r15,%rcx)
movb 0x20(%rsp), %sil
movb %dil, 0x1a(%r15,%rcx)
movb %sil, 0x1b(%r15,%rcx)
incl %eax
xorl %ecx, %ecx
cmpl 0x24(%rsp), %eax
movl 0x40(%rsp), %r10d
movl 0x3c(%rsp), %r11d
movl %eax, 0x14(%r15)
setae %cl
andb %cl, %r12b
movq 0x68(%rsp), %r9
movq 0x60(%rsp), %r15
movl 0x5c(%rsp), %ebp
movl 0x58(%rsp), %r13d
andb %r12b, %r11b
movl 0x54(%rsp), %r12d
incq %r9
cmpb $0x1, %r11b
pushq $0x1
popq %rbx
jne 0x77a2
cmpl %r12d, %r10d
jb 0x77a2
jmp 0x7aa4
movzbl 0x1a(%rsp), %ecx
cmpw %cx, 0x8(%r14)
jne 0x79f2
movzbl 0x18(%rsp), %edx
cmpw %dx, 0xa(%r14)
jne 0x79f2
movzbl 0x16(%rsp), %edx
cmpw %dx, 0xc(%r14)
sete %dl
jmp 0x79f4
xorl %edx, %edx
movzbl 0x1c(%rsp), %esi
cmpl $0xff, %esi
je 0x7a68
testl %esi, %esi
jne 0x7a55
movl 0x4(%r14), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x7a55
xorl %r12d, %r12d
orl 0x10(%r14), %esi
movl $0x0, %edx
jne 0x78a2
movl $0x1, 0x4(%r14)
movw %cx, 0x8(%r14)
xorl %r12d, %r12d
xorl %edx, %edx
movzbl 0x18(%rsp), %eax
movw %ax, 0xa(%r14)
movzbl 0x16(%rsp), %eax
movw %ax, 0xc(%r14)
jmp 0x78a2
movl $0x1, 0x10(%r14)
cmpl $0x7, %eax
ja 0x78a0
jmp 0x7a94
cmpl $0x0, 0x4(%r14)
setne %cl
xorl %r12d, %r12d
andb %dl, %cl
movl $0x0, %edx
cmpb $0x1, %cl
jne 0x78a2
movl $0x1, 0x10(%r14)
cmpl $0x7, %eax
ja 0x789d
movl $0x8, 0x418(%r14)
jmp 0x789d
cmpl $0x0, 0x4(%r14)
je 0x7ab2
cmpl $0x0, 0x10(%r14)
je 0x7af6
vmovd 0x8(%r14), %xmm0
vpbroadcastw 0x691b(%rip), %xmm1 # 0xe3dc
vpmullw %xmm1, %xmm0, %xmm0
vmovd %xmm0, 0x8(%r14)
imull $0x101, 0xc(%r14), %eax # imm = 0x101
movw %ax, 0xc(%r14)
leaq 0x70(%rsp), %rdi
callq 0x743d
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a(%rsp), %rbx
leaq 0x16(%rsp), %r13
leaq 0x1c(%rsp), %rbp
xorl %r12d, %r12d
cmpq %r12, 0x30(%rsp)
je 0x7ab2
movq 0x28(%rsp), %r8
leaq 0x18(%rsp), %rsi
movq %r15, (%rsp)
movq %rbx, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movq %r12, %r9
callq 0x717c
cmpb $0x0, 0x1c(%rsp)
je 0x7b61
movzbl 0x1a(%rsp), %eax
cmpw %ax, 0x8(%r14)
jne 0x7b61
movzbl 0x18(%rsp), %eax
cmpw %ax, 0xa(%r14)
jne 0x7b61
movzbl 0x16(%rsp), %eax
cmpw %ax, 0xc(%r14)
jne 0x7b61
movl $0x1, 0x10(%r14)
incq %r12
jmp 0x7b08