// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r8, %r13
movq %rcx, 0x20(%rsp)
movl %edx, %r14d
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
movl (%r8), %r15d
xorl %ebp, %ebp
testl $0xfffffffb, %r15d      # imm = 0xFFFFFFFB
sete %bl
movq %r8, %rdi
callq 0x777c
movl %eax, 0x28(%rsp)
xorl %esi, %esi
movq %r13, 0x38(%rsp)
movl 0x4(%r13), %eax
cmpq $0x6, %r15
ja 0x8ee9
leaq 0x929f(%rip), %rcx      # 0x12184
movl (%rcx,%r15,4), %esi
movq 0x20(%rsp), %rcx
imull %r14d, %ecx
movq %rcx, 0x20(%rsp)
imull %eax, %esi
cmpl $0x1, %esi
sete %r15b
cmpl $0x9, %esi
setae %cl
movl $0x101, %edx            # imm = 0x101
movl $0x2, %edi
cmovael %edx, %edi
movl %edi, 0x2c(%rsp)
orb %r15b, %cl
movl %esi, %r14d
jne 0x8f43
movl $0x4, 0x2c(%rsp)
cmpl $0x2, %r14d
je 0x8f43
cmpl $0x4, %r14d
movl $0x10, %ecx
movl $0x100, %edx            # imm = 0x100
cmovel %ecx, %edx
movl %edx, 0x2c(%rsp)
xorl $0x1, 0x28(%rsp)
movb %bl, %bpl
vpxor %xmm0, %xmm0, %xmm0
vmovdqu64 %zmm0, 0xb0(%rsp)
vmovdqu64 %zmm0, 0x70(%rsp)
movl $0xffffffff, 0xf0(%rsp) # imm = 0xFFFFFFFF
cmpl $0x10, %eax
movq %r12, 0x18(%rsp)
jne 0x901d
movl %r15d, %ebx
cmpq $0x0, 0x20(%rsp)
je 0x901a
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
movq %r13, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
vzeroupper
callq 0x85b2
movzwl 0xe(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x9486
movzwl 0xc(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x9486
movzwl 0xa(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x9486
movzwl 0x10(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x9486
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0x8f95
movl %ebx, %r15d
xorl %edx, %edx
movb %dl, 0xe(%rsp)
movb %dl, 0xc(%rsp)
movb %dl, 0xa(%rsp)
movb %dl, 0x10(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x938d
movq 0x20(%rsp), %rax
decq %rax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movl %r14d, 0x5c(%rsp)
movq %rdx, 0x48(%rsp)
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq 0x30(%rsp), %r8
movq %rbx, %r9
vzeroupper
callq 0x8b64
testb $0x1, %r15b
jne 0x90db
movl 0x418(%r12), %eax
cmpl $0x7, %eax
ja 0x90db
movzbl 0xe(%rsp), %ecx
leal 0x1(%rcx), %esi
movl $0x1, %edx
cmpb $0x2, %sil
jb 0x90cf
imull $0xf0f0f0f1, %ecx, %esi # imm = 0xF0F0F0F1
movl $0x8, %edx
cmpl $0xf0f0f0f, %esi        # imm = 0xF0F0F0F
ja 0x90cf
imull $0xfcfcfcfd, %ecx, %ecx # imm = 0xFCFCFCFD
xorl %edx, %edx
cmpl $0x3030304, %ecx        # imm = 0x3030304
setae %dl
leal 0x2(%rdx,%rdx), %edx
cmpl %eax, %edx
jbe 0x90db
movl %edx, 0x418(%r12)
movq %rbx, 0x68(%rsp)
movl 0x418(%r12), %r10d
cmpl %r14d, %r10d
setae 0x17(%rsp)
movb $0x1, %r13b
movb $0x1, %r9b
testl %ebp, %ebp
movl $0x1, %ebp
jne 0x913c
movb 0xe(%rsp), %al
cmpb 0xc(%rsp), %al
jne 0x9117
xorl %r9d, %r9d
movl $0x0, %ebp
cmpb 0xa(%rsp), %al
je 0x913c
movl $0x1, (%r12)
movb $0x1, %r9b
movl $0x1, %ebp
cmpl $0x7, %r10d
ja 0x913c
movl $0x8, 0x418(%r12)
movb $0x1, %r9b
cmpl $0x0, 0x28(%rsp)
je 0x9279
movb $0x1, %r13b
movl $0x1, %esi
movl %esi, 0x28(%rsp)
cmpl $0x0, 0x48(%rsp)
movl $0x1, %edx
jne 0x923c
movb 0xe(%rsp), %al
movb 0xc(%rsp), %cl
movb 0xa(%rsp), %bl
movb 0x10(%rsp), %r14b
movb %al, 0x15(%rsp)
movzbl %al, %esi
movb %cl, 0x16(%rsp)
movzbl %cl, %r12d
movzbl %bl, %r15d
movzbl %r14b, %r8d
leaq 0x70(%rsp), %rdi
movl %esi, 0x58(%rsp)
movl %r12d, %edx
movl %r15d, %ecx
movl %r10d, 0x48(%rsp)
movl %r8d, 0x54(%rsp)
movl %r9d, 0x44(%rsp)
callq 0xeb70
movl 0x44(%rsp), %r9d
movl 0x48(%rsp), %r10d
xorl %edx, %edx
testl %eax, %eax
jns 0x923c
movb %r14b, 0x13(%rsp)
movb %bl, 0x14(%rsp)
movq 0x18(%rsp), %rax
movl 0x14(%rax), %r9d
leaq 0x70(%rsp), %rdi
movl 0x58(%rsp), %esi
movl %r12d, %edx
movq %rax, %r14
movl %r15d, %ecx
movl 0x54(%rsp), %r8d
callq 0x8503
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x9223
leal (,%rax,4), %ecx
movb 0x15(%rsp), %dl
movb %dl, 0x18(%r14,%rax,4)
movb 0x16(%rsp), %dl
movb %dl, 0x19(%r14,%rcx)
movb 0x14(%rsp), %dl
movb %dl, 0x1a(%r14,%rcx)
movb 0x13(%rsp), %dl
movb %dl, 0x1b(%r14,%rcx)
incl %eax
movl %eax, 0x14(%r14)
xorl %edx, %edx
cmpl 0x2c(%rsp), %eax
setae %dl
movl 0x48(%rsp), %r10d
movl 0x44(%rsp), %r9d
movl 0x5c(%rsp), %r14d
cmpl %r14d, %r10d
setae %al
andb %dl, %r13b
andb %r13b, %r9b
testb %al, %r9b
movq 0x18(%rsp), %r12
movb 0x17(%rsp), %r15b
movq 0x68(%rsp), %rcx
jne 0x938d
leaq 0x1(%rcx), %rbx
cmpq %rcx, 0x60(%rsp)
jne 0x904f
jmp 0x938d
movzbl 0xe(%rsp), %eax
cmpw %ax, 0x8(%r12)
jne 0x92a3
movzbl 0xc(%rsp), %ecx
cmpw %cx, 0xa(%r12)
jne 0x92a3
movzbl 0xa(%rsp), %ecx
cmpw %cx, 0xc(%r12)
sete %cl
jmp 0x92a5
xorl %ecx, %ecx
movzbl 0x10(%rsp), %edx
cmpl $0xff, %edx
je 0x9300
testl %edx, %edx
jne 0x9335
cmpl $0x0, 0x4(%r12)
sete %sil
orb %cl, %sil
testb $0x1, %sil
je 0x9335
cmpl $0xff, %edx
je 0x9300
xorl %r13d, %r13d
movl $0x0, %esi
testl %edx, %edx
jne 0x914f
cmpl $0x0, 0x10(%r12)
jne 0x92f1
cmpl $0x0, 0x4(%r12)
je 0x935e
xorl %r13d, %r13d
cmpl $0xff, %edx
jne 0x914f
cmpl $0x0, 0x4(%r12)
setne %al
andb %cl, %al
xorl %r13d, %r13d
movl $0x0, %esi
cmpb $0x1, %al
jne 0x914f
movl $0x1, 0x10(%r12)
cmpl $0x7, 0x418(%r12)
ja 0x9147
jmp 0x934d
movl $0x1, 0x10(%r12)
cmpl $0x7, 0x418(%r12)
ja 0x914a
movl $0x8, 0x418(%r12)
jmp 0x9147
movl $0x1, 0x4(%r12)
movw %ax, 0x8(%r12)
movzbl 0xc(%rsp), %eax
movw %ax, 0xa(%r12)
movzbl 0xa(%rsp), %eax
movw %ax, 0xc(%r12)
xorl %r13d, %r13d
xorl %esi, %esi
jmp 0x914f
cmpl $0x0, 0x4(%r12)
je 0x943f
movq 0x18(%rsp), %rax
cmpl $0x0, 0x10(%rax)
setne %al
cmpq $0x0, 0x20(%rsp)
sete %cl
orb %al, %cl
jne 0x943f
xorl %r15d, %r15d
leaq 0xe(%rsp), %r13
leaq 0xc(%rsp), %rbp
leaq 0xa(%rsp), %r12
leaq 0x10(%rsp), %r14
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
vzeroupper
callq 0x8b64
cmpb $0x0, 0x10(%rsp)
je 0x9435
movzbl 0xe(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0x8(%rcx)
jne 0x9435
movzbl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xa(%rcx)
jne 0x9435
movzbl 0xa(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xc(%rcx)
jne 0x9435
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0x93cd
movq 0x18(%rsp), %rdx
vmovd 0x8(%rdx), %xmm0
vpsllw $0x8, %xmm0, %xmm1
vpaddw %xmm1, %xmm0, %xmm0
vmovd %xmm0, 0x8(%rdx)
movl 0xc(%rdx), %eax
movl %eax, %ecx
shll $0x8, %ecx
addl %eax, %ecx
movw %cx, 0xc(%rdx)
leaq 0x70(%rsp), %rdi
vzeroupper
callq 0x8e44
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
leaq 0xe(%rsp), %rax
movw %r15w, (%rax)
leaq 0xc(%rsp), %rax
movw %r15w, (%rax)
leaq 0xa(%rsp), %rax
movw %r15w, (%rax)
leaq 0x10(%rsp), %r13
movw %r15w, (%r13)
movl $0x10, 0x418(%r12)
movq 0x20(%rsp), %rax
leaq -0x1(%rax), %rbx
movl 0x28(%rsp), %r14d
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
movq %r13, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x85b2
movb $0x1, %cl
movb $0x1, %al
testl %ebp, %ebp
movl $0x1, %ebp
jne 0x9526
movzwl 0xe(%rsp), %edx
cmpw 0xc(%rsp), %dx
jne 0x9517
xorl %eax, %eax
movl $0x0, %ebp
cmpw 0xa(%rsp), %dx
je 0x9526
movl $0x1, (%r12)
movb $0x1, %al
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x95f4
movzwl 0xe(%rsp), %edx
cmpw 0x8(%r12), %dx
jne 0x955a
movzwl 0xc(%rsp), %esi
cmpw 0xa(%r12), %si
jne 0x955a
movzwl 0xa(%rsp), %esi
cmpw 0xc(%r12), %si
sete %sil
jmp 0x955c
xorl %esi, %esi
movzwl 0x10(%rsp), %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
je 0x95ae
testl %edi, %edi
jne 0x95d2
cmpl $0x0, 0x4(%r12)
sete %r8b
orb %sil, %r8b
testb $0x1, %r8b
je 0x95d2
cmpl $0xffff, %edi           # imm = 0xFFFF
je 0x95ae
xorl %ecx, %ecx
movl $0x0, %r14d
testl %edi, %edi
jne 0x95fa
cmpl $0x0, 0x10(%r12)
jne 0x95a4
cmpl $0x0, 0x4(%r12)
je 0x9610
xorl %ecx, %ecx
cmpl $0xffff, %edi           # imm = 0xFFFF
jne 0x95fa
cmpl $0x0, 0x4(%r12)
setne %dl
andb %sil, %dl
xorl %ecx, %ecx
movl $0x0, %r14d
cmpb $0x1, %dl
jne 0x95fa
movl $0x1, 0x10(%r12)
jmp 0x95f2
movl $0x1, 0x10(%r12)
cmpl $0x7, 0x418(%r12)
ja 0x95f4
movl $0x8, 0x418(%r12)
movb $0x1, %cl
movl $0x1, %r14d
testb %cl, %al
jne 0x963c
leaq 0x1(%r15), %rax
cmpq %r15, %rbx
movq %rax, %r15
jne 0x94c8
jmp 0x963c
movl $0x1, 0x4(%r12)
movw %dx, 0x8(%r12)
movzwl 0xc(%rsp), %ecx
movw %cx, 0xa(%r12)
movzwl 0xa(%rsp), %ecx
movw %cx, 0xc(%r12)
xorl %ecx, %ecx
xorl %r14d, %r14d
jmp 0x95fa
cmpl $0x0, 0x4(%r12)
je 0x9465
movq 0x18(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x9465
xorl %r15d, %r15d
leaq 0xe(%rsp), %r12
leaq 0xc(%rsp), %r14
leaq 0xa(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x85b2
cmpw $0x0, 0x10(%rsp)
je 0x96d4
movzwl 0xe(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0x8(%rcx), %ax
jne 0x96d4
movzwl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0xa(%rcx), %ax
jne 0x96d4
movzwl 0xa(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0xc(%rcx), %ax
jne 0x96d4
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0x966e
jmp 0x9465