// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: lodepng_get_color_profile
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;
          profile->key = 0;
          alpha_done = 1;
        }
        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;
          profile->key = 0;
          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;
          profile->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);

      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;
          profile->key = 0;
          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;
          profile->key = 0;
          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;
          profile->key = 0;
          alpha_done = 1;
          if(profile->bits < 8) profile->bits = 8; /*PNG has no alphachannel modes with less than 8-bit per channel*/
        }
      }
    }

    /*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, 0x30(%rsp)
movl %edx, %r14d
movq %rsi, 0x48(%rsp)
movq %rdi, %r12
movl (%r8), %r15d
testl $0xfffffffb, %r15d      # imm = 0xFFFFFFFB
sete %bl
movq %r8, %rdi
callq 0x4cc1
xorl %edi, %edi
movq %r13, 0x50(%rsp)
movl 0x4(%r13), %esi
cmpq $0x6, %r15
ja 0x63d5
leaq 0x8733(%rip), %rcx      # 0xeb04
movl (%rcx,%r15,4), %edi
movq 0x30(%rsp), %rcx
imull %r14d, %ecx
movq %rcx, 0x30(%rsp)
imull %esi, %edi
cmpl $0x1, %edi
sete %r15b
cmpl $0x9, %edi
setae %cl
movl $0x101, %edx            # imm = 0x101
movl $0x2, %r8d
cmovael %edx, %r8d
movl %r8d, 0x38(%rsp)
orb %r15b, %cl
jne 0x642d
movl $0x4, 0x38(%rsp)
cmpl $0x2, %edi
je 0x642d
cmpl $0x4, %edi
movl $0x10, %ecx
movl $0x100, %edx            # imm = 0x100
cmovel %ecx, %edx
movl %edx, 0x38(%rsp)
movl %edi, 0x3c(%rsp)
movl %eax, %r14d
xorl $0x1, %r14d
xorl %ebp, %ebp
movb %bl, %bpl
pxor %xmm0, %xmm0
movdqa %xmm0, 0xe0(%rsp)
movdqa %xmm0, 0xd0(%rsp)
movdqa %xmm0, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movl $0xffffffff, 0xf0(%rsp) # imm = 0xFFFFFFFF
cmpl $0x10, %esi
movq %r12, 0x20(%rsp)
jne 0x6531
cmpq $0x0, 0x30(%rsp)
je 0x6531
xorl %r13d, %r13d
leaq 0x1a(%rsp), %rbx
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x48(%rsp), %r8
movq %r13, %r9
callq 0x5a61
movzwl 0x1a(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x69ae
movzwl 0x18(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x69ae
movzwl 0x16(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x69ae
movzwl 0x1c(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x69ae
incq %r13
cmpq %r13, 0x30(%rsp)
jne 0x64b3
movb %r15b, 0x1f(%rsp)
movl %r14d, 0x40(%rsp)
movl %ebp, 0x44(%rsp)
xorl %ebx, %ebx
movb %bl, 0x1a(%rsp)
movb %bl, 0x18(%rsp)
movb %bl, 0x16(%rsp)
movb %bl, 0x1c(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x6892
xorl %r13d, %r13d
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x48(%rsp), %r8
movq %r13, %r9
callq 0x604f
movq 0x20(%rsp), %r8
movl 0x418(%r8), %r10d
cmpl $0x8, %r10d
setae %al
orb 0x1f(%rsp), %al
testb $0x1, %al
je 0x65b4
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %r9d
jmp 0x660a
movb 0x1a(%rsp), %cl
leal 0x1(%rcx), %edx
movl $0x1, %eax
cmpb $0x2, %dl
movl 0x44(%rsp), %esi
jb 0x65f2
movzbl %cl, %ecx
movl %ecx, %eax
shll $0x4, %eax
movl %ecx, %edx
subl %eax, %edx
movl $0x8, %eax
cmpb $0xf, %dl
ja 0x65f2
leal (%rcx,%rcx,2), %eax
negl %eax
xorl %ecx, %ecx
cmpb $0x4, %al
setae %cl
leal 0x2(,%rcx,2), %eax
cmpl %r10d, %eax
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %r9d
jbe 0x660a
movl %eax, 0x418(%r8)
movl %eax, %r10d
cmpl %r9d, %r10d
setae %dil
movb $0x1, %bpl
movl %r10d, %eax
movb $0x1, %r11b
testl %esi, %esi
movl $0x1, %esi
jne 0x6669
movb 0x1a(%rsp), %cl
cmpb 0x18(%rsp), %cl
jne 0x663e
xorl %r11d, %r11d
movl %r10d, %eax
movl $0x0, %esi
cmpb 0x16(%rsp), %cl
je 0x6669
movl $0x1, (%r8)
movl %r10d, %eax
movb $0x1, %r11b
movl $0x1, %esi
cmpl $0x7, %r10d
ja 0x6669
movl $0x8, 0x418(%r8)
movl $0x8, %eax
movb $0x1, %r11b
testl %edx, %edx
movl %esi, 0x44(%rsp)
movb %dil, 0x1f(%rsp)
je 0x66a9
movb $0x1, %bpl
movl $0x1, %edx
movl $0x1, %ecx
testl %ebx, %ebx
movl %edx, 0x40(%rsp)
je 0x66d4
andb %cl, %bpl
andb %bpl, %r11b
cmpb $0x1, %r11b
jne 0x6729
cmpl %r9d, %r10d
jb 0x6729
jmp 0x6892
movzbl 0x1a(%rsp), %ecx
cmpw %cx, 0x8(%r8)
jne 0x673e
movzbl 0x18(%rsp), %edx
cmpw %dx, 0xa(%r8)
jne 0x673e
movzbl 0x16(%rsp), %edx
cmpw %dx, 0xc(%r8)
sete %dl
jmp 0x6740
movl %ebp, 0x64(%rsp)
movl %r11d, 0x68(%rsp)
movl %r10d, 0x6c(%rsp)
movb 0x1a(%rsp), %r15b
movb 0x18(%rsp), %bpl
movb 0x16(%rsp), %bl
movb 0x1c(%rsp), %r14b
movzbl %r15b, %esi
movzbl %bpl, %edx
movzbl %bl, %r12d
movzbl %r14b, %r8d
leaq 0x70(%rsp), %rdi
movl %esi, 0x5c(%rsp)
movl %edx, 0x60(%rsp)
movl %r12d, %ecx
movl %r8d, 0x58(%rsp)
callq 0xb6f1
xorl %ecx, %ecx
testl %eax, %eax
js 0x67c1
incq %r13
movl %ecx, %ebx
cmpq %r13, 0x30(%rsp)
jne 0x6560
jmp 0x6892
xorl %edx, %edx
movzbl 0x1c(%rsp), %esi
cmpl $0xff, %esi
je 0x684f
testl %esi, %esi
jne 0x67a3
movl 0x4(%r8), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x67a3
xorl %ebp, %ebp
orl 0x10(%r8), %esi
movl $0x0, %edx
jne 0x667e
movl $0x1, 0x4(%r8)
movw %cx, 0x8(%r8)
movzbl 0x18(%rsp), %eax
movw %ax, 0xa(%r8)
movzbl 0x16(%rsp), %eax
movw %ax, 0xc(%r8)
xorl %ebp, %ebp
xorl %edx, %edx
jmp 0x667e
movl $0x1, 0x10(%r8)
movl $0x0, 0x4(%r8)
cmpl $0x7, %eax
ja 0x6679
jmp 0x6882
movb %r14b, 0x2d(%rsp)
movb %bl, 0x2e(%rsp)
movb %bpl, 0x2f(%rsp)
movq 0x20(%rsp), %rax
movl 0x14(%rax), %r9d
leaq 0x70(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x60(%rsp), %edx
movl %r12d, %ecx
movq %rax, %r14
movl 0x58(%rsp), %r8d
callq 0x599a
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x6828
leal (,%rax,4), %ecx
movb %r15b, 0x18(%r14,%rax,4)
movb 0x2f(%rsp), %dl
movb %dl, 0x19(%r14,%rcx)
movb 0x2e(%rsp), %dl
movb %dl, 0x1a(%r14,%rcx)
movb 0x2d(%rsp), %dl
movb %dl, 0x1b(%r14,%rcx)
incl %eax
movl %eax, 0x14(%r14)
xorl %ecx, %ecx
cmpl 0x38(%rsp), %eax
setae %cl
movl 0x3c(%rsp), %r9d
movl 0x6c(%rsp), %r10d
movl 0x68(%rsp), %r11d
movl 0x64(%rsp), %ebp
jmp 0x668b
cmpl $0x0, 0x4(%r8)
setne %cl
andb %dl, %cl
xorl %ebp, %ebp
movl $0x0, %edx
cmpb $0x1, %cl
jne 0x667e
movl $0x1, 0x10(%r8)
movl $0x0, 0x4(%r8)
cmpl $0x7, %eax
ja 0x6676
movl $0x8, 0x418(%r8)
jmp 0x6676
movq 0x20(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x6966
cmpl $0x0, 0x30(%rsp)
sete %al
movq 0x20(%rsp), %rcx
cmpl $0x0, 0x10(%rcx)
setne %cl
orb %al, %cl
jne 0x6966
xorl %r13d, %r13d
leaq 0x1a(%rsp), %rbp
leaq 0x18(%rsp), %r12
leaq 0x16(%rsp), %r14
leaq 0x1c(%rsp), %r15
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq 0x48(%rsp), %r8
movq %r13, %r9
callq 0x604f
cmpb $0x0, 0x1c(%rsp)
je 0x6958
movzbl 0x1a(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw %ax, 0x8(%rcx)
jne 0x6958
movzbl 0x18(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw %ax, 0xa(%rcx)
jne 0x6958
movzbl 0x16(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw %ax, 0xc(%rcx)
jne 0x6958
movq 0x20(%rsp), %rax
movl $0x1, 0x10(%rax)
movl $0x0, 0x4(%rax)
cmpl $0x7, 0x418(%rax)
ja 0x6958
movq 0x20(%rsp), %rax
movl $0x8, 0x418(%rax)
incq %r13
cmpq %r13, 0x30(%rsp)
jne 0x68d4
movq 0x20(%rsp), %rdx
movd 0x8(%rdx), %xmm0
movdqa %xmm0, %xmm1
psllw $0x8, %xmm1
paddw %xmm0, %xmm1
movd %xmm1, 0x8(%rdx)
movl 0xc(%rdx), %eax
movl %eax, %ecx
shll $0x8, %ecx
addl %eax, %ecx
movw %cx, 0xc(%rdx)
leaq 0x70(%rsp), %rdi
callq 0x6336
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
leaq 0x1a(%rsp), %r15
movw %r13w, (%r15)
leaq 0x18(%rsp), %rax
movw %r13w, (%rax)
leaq 0x16(%rsp), %rax
movw %r13w, (%rax)
leaq 0x1c(%rsp), %rax
movw %r13w, (%rax)
movl $0x10, 0x418(%r12)
movl %ebp, %ebx
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x48(%rsp), %r8
movq %r13, %r9
callq 0x5a61
movl $0x1, %ebp
movb $0x1, %al
testl %ebx, %ebx
jne 0x6a3f
movzwl 0x1a(%rsp), %ecx
cmpw 0x18(%rsp), %cx
jne 0x6a30
xorl %eax, %eax
movl $0x0, %ebp
cmpw 0x16(%rsp), %cx
je 0x6a3f
movl $0x1, (%r12)
movb $0x1, %al
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x6aef
movzwl 0x1a(%rsp), %ecx
cmpw 0x8(%r12), %cx
jne 0x6a72
movzwl 0x18(%rsp), %edx
cmpw 0xa(%r12), %dx
jne 0x6a72
movzwl 0x16(%rsp), %edx
cmpw 0xc(%r12), %dx
sete %dl
jmp 0x6a74
xorl %edx, %edx
movzwl 0x1c(%rsp), %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x6aca
testl %esi, %esi
jne 0x6add
movl 0x4(%r12), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x6add
xorl %r14d, %r14d
orl 0x10(%r12), %esi
jne 0x6af9
movl $0x1, 0x4(%r12)
movw %cx, 0x8(%r12)
movzwl 0x18(%rsp), %eax
movw %ax, 0xa(%r12)
movzwl 0x16(%rsp), %eax
movw %ax, 0xc(%r12)
jmp 0x6af9
cmpl $0x0, 0x4(%r12)
setne %cl
andb %dl, %cl
xorl %r14d, %r14d
cmpb $0x1, %cl
jne 0x6af9
movl $0x1, 0x10(%r12)
movl $0x0, 0x4(%r12)
movl $0x1, %r14d
testb %al, %al
jne 0x6b07
incq %r13
cmpq %r13, 0x30(%rsp)
jne 0x69e1
cmpl $0x0, 0x4(%r12)
je 0x6990
movq 0x20(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x6990
xorl %r14d, %r14d
leaq 0x1a(%rsp), %r15
leaq 0x18(%rsp), %r12
leaq 0x16(%rsp), %r13
leaq 0x1c(%rsp), %rbp
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x48(%rsp), %r8
movq %r14, %r9
callq 0x5a61
cmpw $0x0, 0x1c(%rsp)
je 0x6ba6
movzwl 0x1a(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw 0x8(%rcx), %ax
jne 0x6ba6
movzwl 0x18(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw 0xa(%rcx), %ax
jne 0x6ba6
movzwl 0x16(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpw 0xc(%rcx), %ax
jne 0x6ba6
movq 0x20(%rsp), %rax
movl $0x1, 0x10(%rax)
movl $0x0, 0x4(%rax)
incq %r14
cmpq %r14, 0x30(%rsp)
jne 0x6b39
jmp 0x6990