// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movl (%r8), %r15d
movq %rdi, %r12
movq %r8, %rdi
movq %r8, %r13
movq %rcx, 0x20(%rsp)
movl %edx, %r14d
movq %rsi, 0x38(%rsp)
testl $0xfffffffb, %r15d      # imm = 0xFFFFFFFB
sete %bl
callq 0x778c
movl 0x4(%r13), %esi
xorl %edi, %edi
movq %r13, 0x40(%rsp)
cmpq $0x6, %r15
ja 0x8e63
leaq 0x9365(%rip), %rcx      # 0x121c4
movl (%rcx,%r15,4), %edi
movq 0x20(%rsp), %rcx
imull %esi, %edi
movl $0x101, %edx            # imm = 0x101
movl $0x2, %r8d
imull %r14d, %ecx
cmpl $0x1, %edi
sete %r15b
cmpl $0x9, %edi
setae %r9b
cmovael %edx, %r8d
orb %r15b, %r9b
movq %rcx, 0x20(%rsp)
movl %r8d, 0x28(%rsp)
jne 0x8ebc
movl $0x4, 0x28(%rsp)
cmpl $0x2, %edi
je 0x8ebc
cmpl $0x4, %edi
movl $0x10, %ecx
movl $0x100, %edx            # imm = 0x100
cmovel %ecx, %edx
movl %edx, 0x28(%rsp)
movl %eax, %r14d
xorl %ebp, %ebp
vpxor %xmm0, %xmm0, %xmm0
xorl $0x1, %r14d
movb %bl, %bpl
movl %edi, 0x2c(%rsp)
movq %r12, 0x18(%rsp)
vmovdqu64 %zmm0, 0xa0(%rsp)
vmovdqu64 %zmm0, 0x60(%rsp)
movl $0xffffffff, 0xe0(%rsp) # imm = 0xFFFFFFFF
cmpl $0x10, %esi
jne 0x8f98
cmpq $0x0, 0x20(%rsp)
je 0x8f98
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
leaq 0xe(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x12(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %r9
movq %rax, (%rsp)
vzeroupper
callq 0x8530
movzwl 0x10(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x93e6
movzwl 0xe(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x93e6
movzwl 0xc(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x93e6
movzwl 0x12(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x93e6
incq %r13
cmpq %r13, 0x20(%rsp)
jne 0x8f13
xorl %ebx, %ebx
cmpq $0x0, 0x20(%rsp)
movb %r15b, 0x14(%rsp)
movl %r14d, 0x30(%rsp)
movl %ebp, 0x34(%rsp)
movb %bl, 0x10(%rsp)
movb %bl, 0xe(%rsp)
movb %bl, 0xc(%rsp)
movb %bl, 0x12(%rsp)
je 0x92eb
xorl %r13d, %r13d
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
leaq 0x10(%rsp), %rdi
leaq 0xe(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x12(%rsp), %rcx
movq %r13, %r9
movq %rax, (%rsp)
vzeroupper
callq 0x8ae3
movq 0x18(%rsp), %r8
movl 0x418(%r8), %r10d
cmpl $0x8, %r10d
setae %al
orb 0x14(%rsp), %al
testb $0x1, %al
je 0x901e
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x2c(%rsp), %r9d
jmp 0x9071
movb 0x10(%rsp), %cl
movl 0x34(%rsp), %esi
movl $0x1, %eax
leal 0x1(%rcx), %edx
cmpb $0x2, %dl
jb 0x9059
movzbl %cl, %ecx
movl %ecx, %eax
shll $0x4, %eax
movl %ecx, %edx
subl %eax, %edx
movl $0x8, %eax
cmpb $0xf, %dl
ja 0x9059
leal (%rcx,%rcx,2), %eax
xorl %ecx, %ecx
negl %eax
cmpb $0x4, %al
setae %cl
leal 0x2(%rcx,%rcx), %eax
movl 0x30(%rsp), %edx
movl 0x2c(%rsp), %r9d
cmpl %r10d, %eax
jbe 0x9071
movl %eax, %r10d
movl %eax, 0x418(%r8)
cmpl %r9d, %r10d
movb $0x1, %bpl
movl %r10d, %eax
movb $0x1, %r11b
setae %dil
testl %esi, %esi
movl $0x1, %esi
jne 0x90d0
movb 0x10(%rsp), %cl
cmpb 0xe(%rsp), %cl
jne 0x90a5
movl $0x0, %esi
xorl %r11d, %r11d
movl %r10d, %eax
cmpb 0xc(%rsp), %cl
je 0x90d0
movb $0x1, %r11b
movl $0x1, %esi
movl $0x1, (%r8)
movl %r10d, %eax
cmpl $0x7, %r10d
ja 0x90d0
movl $0x8, %eax
movb $0x1, %r11b
movl $0x8, 0x418(%r8)
movl %esi, 0x34(%rsp)
movb %dil, 0x14(%rsp)
testl %edx, %edx
je 0x9110
movb $0x1, %bpl
movl $0x1, %edx
movl $0x1, %ecx
movl %edx, 0x30(%rsp)
testl %ebx, %ebx
je 0x913b
andb %cl, %bpl
andb %bpl, %r11b
cmpb $0x1, %r11b
jne 0x9190
cmpl %r9d, %r10d
jb 0x9190
jmp 0x92eb
movzbl 0x10(%rsp), %ecx
cmpw %cx, 0x8(%r8)
jne 0x91a5
movzbl 0xe(%rsp), %edx
cmpw %dx, 0xa(%r8)
jne 0x91a5
movzbl 0xc(%rsp), %edx
cmpw %dx, 0xc(%r8)
sete %dl
jmp 0x91a7
movl %ebp, 0x54(%rsp)
movb 0x10(%rsp), %r15b
movb 0xe(%rsp), %bpl
movb 0xc(%rsp), %bl
movb 0x12(%rsp), %r14b
leaq 0x60(%rsp), %rdi
movl %r11d, 0x58(%rsp)
movl %r10d, 0x5c(%rsp)
movzbl %r15b, %esi
movzbl %bpl, %edx
movzbl %bl, %r12d
movzbl %r14b, %r8d
movl %r12d, %ecx
movl %esi, 0x4c(%rsp)
movl %edx, 0x50(%rsp)
movl %r8d, 0x48(%rsp)
callq 0xe9cc
xorl %ecx, %ecx
testl %eax, %eax
js 0x9220
incq %r13
movl %ecx, %ebx
cmpq %r13, 0x20(%rsp)
jne 0x8fc7
jmp 0x92eb
xorl %edx, %edx
movzbl 0x12(%rsp), %esi
cmpl $0xff, %esi
je 0x92b0
testl %esi, %esi
jne 0x920a
movl 0x4(%r8), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x920a
xorl %ebp, %ebp
orl 0x10(%r8), %esi
movl $0x0, %edx
jne 0x90e5
movl $0x1, 0x4(%r8)
movw %cx, 0x8(%r8)
xorl %ebp, %ebp
xorl %edx, %edx
movzbl 0xe(%rsp), %eax
movw %ax, 0xa(%r8)
movzbl 0xc(%rsp), %eax
movw %ax, 0xc(%r8)
jmp 0x90e5
movl $0x1, 0x10(%r8)
cmpl $0x7, %eax
ja 0x90e0
jmp 0x92db
movq 0x18(%rsp), %rax
movl 0x4c(%rsp), %esi
movl 0x50(%rsp), %edx
movl 0x48(%rsp), %r8d
leaq 0x60(%rsp), %rdi
movb %r14b, 0x15(%rsp)
movb %bl, 0x16(%rsp)
movb %bpl, 0x17(%rsp)
movl %r12d, %ecx
movl 0x14(%rax), %r9d
movq %rax, %r14
callq 0x847b
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x9289
movb 0x17(%rsp), %dl
movb 0x16(%rsp), %dil
movb 0x15(%rsp), %sil
leal (,%rax,4), %ecx
movb %r15b, 0x18(%r14,%rax,4)
movb %dl, 0x19(%r14,%rcx)
movb %dil, 0x1a(%r14,%rcx)
movb %sil, 0x1b(%r14,%rcx)
incl %eax
xorl %ecx, %ecx
cmpl 0x28(%rsp), %eax
movl 0x2c(%rsp), %r9d
movl 0x5c(%rsp), %r10d
movl 0x58(%rsp), %r11d
movl 0x54(%rsp), %ebp
movl %eax, 0x14(%r14)
setae %cl
jmp 0x90f2
cmpl $0x0, 0x4(%r8)
setne %cl
xorl %ebp, %ebp
andb %dl, %cl
movl $0x0, %edx
cmpb $0x1, %cl
jne 0x90e5
movl $0x1, 0x10(%r8)
cmpl $0x7, %eax
ja 0x90dd
movl $0x8, 0x418(%r8)
jmp 0x90dd
movq 0x18(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x939f
cmpl $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rcx
sete %al
cmpl $0x0, 0x10(%rcx)
setne %cl
orb %al, %cl
jne 0x939f
leaq 0x10(%rsp), %rbp
leaq 0xe(%rsp), %r12
leaq 0xc(%rsp), %r14
leaq 0x12(%rsp), %r15
xorl %r13d, %r13d
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r13, %r9
movq %rax, (%rsp)
vzeroupper
callq 0x8ae3
cmpb $0x0, 0x12(%rsp)
je 0x9395
movzbl 0x10(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0x8(%rcx)
jne 0x9395
movzbl 0xe(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xa(%rcx)
jne 0x9395
movzbl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xc(%rcx)
jne 0x9395
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
incq %r13
cmpq %r13, 0x20(%rsp)
jne 0x932d
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 0x60(%rsp), %rdi
vzeroupper
callq 0x8dc4
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %r15
leaq 0xe(%rsp), %rax
leaq 0xc(%rsp), %rdx
leaq 0x12(%rsp), %rcx
xorl %r13d, %r13d
movw %r13w, (%r15)
movw %r13w, (%rax)
movw %r13w, (%rdx)
movw %r13w, (%rcx)
movl $0x10, 0x418(%r12)
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
leaq 0xe(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x12(%rsp), %rcx
movl %ebp, %ebx
movq %r15, %rdi
movq %r13, %r9
movq %rax, (%rsp)
callq 0x8530
movl $0x1, %ebp
movb $0x1, %al
testl %ebx, %ebx
jne 0x9477
movzwl 0x10(%rsp), %ecx
cmpw 0xe(%rsp), %cx
jne 0x9468
movl $0x0, %ebp
xorl %eax, %eax
cmpw 0xc(%rsp), %cx
je 0x9477
movb $0x1, %al
movl $0x1, %ebp
movl $0x1, (%r12)
testl %r14d, %r14d
jne 0x951e
movzwl 0x10(%rsp), %ecx
cmpw 0x8(%r12), %cx
jne 0x94aa
movzwl 0xe(%rsp), %edx
cmpw 0xa(%r12), %dx
jne 0x94aa
movzwl 0xc(%rsp), %edx
cmpw 0xc(%r12), %dx
sete %dl
jmp 0x94ac
xorl %edx, %edx
movzwl 0x12(%rsp), %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x9502
testl %esi, %esi
jne 0x9515
movl 0x4(%r12), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x9515
xorl %r14d, %r14d
orl 0x10(%r12), %esi
jne 0x9528
movl $0x1, 0x4(%r12)
movw %cx, 0x8(%r12)
movzwl 0xe(%rsp), %eax
movw %ax, 0xa(%r12)
movzwl 0xc(%rsp), %eax
movw %ax, 0xc(%r12)
jmp 0x9528
cmpl $0x0, 0x4(%r12)
setne %cl
xorl %r14d, %r14d
andb %dl, %cl
cmpb $0x1, %cl
jne 0x9528
movl $0x1, 0x10(%r12)
movl $0x1, %r14d
testb %al, %al
jne 0x9536
incq %r13
cmpq %r13, 0x20(%rsp)
jne 0x9419
cmpl $0x0, 0x4(%r12)
je 0x93c5
movq 0x18(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x93c5
leaq 0x10(%rsp), %r14
leaq 0xe(%rsp), %r15
leaq 0xc(%rsp), %r13
leaq 0x12(%rsp), %rbp
xorl %r12d, %r12d
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq %r12, %r9
movq %rax, (%rsp)
callq 0x8530
cmpw $0x0, 0x12(%rsp)
je 0x95ce
movq 0x18(%rsp), %rcx
movzwl 0x10(%rsp), %eax
cmpw 0x8(%rcx), %ax
jne 0x95ce
movq 0x18(%rsp), %rcx
movzwl 0xe(%rsp), %eax
cmpw 0xa(%rcx), %ax
jne 0x95ce
movq 0x18(%rsp), %rcx
movzwl 0xc(%rsp), %eax
cmpw 0xc(%rcx), %ax
jne 0x95ce
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
incq %r12
cmpq %r12, 0x20(%rsp)
jne 0x9568
jmp 0x93c5