// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
imull %ecx, %edx
movq %rdx, 0x30(%rsp)
xorl %ebp, %ebp
testl $0xfffffffb, (%r8)      # imm = 0xFFFFFFFB
sete %bl
movq %r8, %rdi
callq 0x3660
movl %eax, %r13d
movq %r15, %rdi
callq 0x35f5
movl %eax, %r12d
cmpl $0x1, %eax
sete %sil
cmpl $0x9, %eax
setae %al
movl $0x101, %ecx            # imm = 0x101
pushq $0x2
popq %rdx
cmovael %ecx, %edx
movl %edx, 0x24(%rsp)
movb %sil, 0x1f(%rsp)
orb %sil, %al
jne 0x4b96
cmpl $0x2, %r12d
jne 0x4b83
pushq $0x4
popq %rax
movl %eax, 0x24(%rsp)
jmp 0x4b96
cmpl $0x4, %r12d
pushq $0x10
popq %rax
movl $0x100, %ecx            # imm = 0x100
cmovel %eax, %ecx
movl %ecx, 0x24(%rsp)
movb %bl, %bpl
xorl $0x1, %r13d
leaq 0x70(%rsp), %rdi
callq 0x41dc
cmpl $0x10, 0x4(%r15)
jne 0x4dfc
movl %r12d, %ebx
xorl %r12d, %r12d
cmpq %r12, 0x30(%rsp)
je 0x4df9
movq %r15, (%rsp)
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %r12, %r9
callq 0x4297
movzwl 0x1a(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x4c27
movzwl 0x18(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x4c27
movzwl 0x16(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x4c27
movzwl 0x1c(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
incq %r12
cmpl %eax, %ecx
je 0x4bb8
movl %r13d, %ebx
leaq 0x1a(%rsp), %rax
andw $0x0, (%rax)
leaq 0x18(%rsp), %rax
andw $0x0, (%rax)
leaq 0x16(%rsp), %rax
andw $0x0, (%rax)
leaq 0x1c(%rsp), %rax
andw $0x0, (%rax)
movl $0x10, 0x418(%r14)
xorl %r12d, %r12d
pushq $0x1
popq %r13
cmpq %r12, 0x30(%rsp)
je 0x4d69
movq %r15, (%rsp)
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %r12, %r9
callq 0x4297
movb $0x1, %al
testl %ebp, %ebp
jne 0x4cb9
movzwl 0x1a(%rsp), %ecx
cmpw 0x18(%rsp), %cx
jne 0x4cb0
xorl %eax, %eax
movl $0x0, %ebp
cmpw 0x16(%rsp), %cx
je 0x4cbc
movl $0x1, (%r14)
movb $0x1, %al
movl %r13d, %ebp
testl %ebx, %ebx
jne 0x4d5a
movzwl 0x1a(%rsp), %ecx
cmpw 0x8(%r14), %cx
jne 0x4ceb
movzwl 0x18(%rsp), %edx
cmpw 0xa(%r14), %dx
jne 0x4ceb
movzwl 0x16(%rsp), %edx
cmpw 0xc(%r14), %dx
sete %dl
jmp 0x4ced
xorl %edx, %edx
movzwl 0x1c(%rsp), %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x4d3c
testl %esi, %esi
jne 0x4d4d
movl 0x4(%r14), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x4d4d
xorl %ebx, %ebx
orl 0x10(%r14), %esi
jne 0x4d61
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 0x4d61
cmpl $0x0, 0x4(%r14)
setne %cl
andb %dl, %cl
xorl %ebx, %ebx
cmpb $0x1, %cl
jne 0x4d61
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
movl %r13d, %ebx
testb %al, %al
jne 0x4d69
incq %r12
jmp 0x4c60
cmpl $0x0, 0x4(%r14)
je 0x5165
cmpl $0x0, 0x10(%r14)
jne 0x5165
xorl %r12d, %r12d
leaq 0x1a(%rsp), %rbx
leaq 0x16(%rsp), %r13
leaq 0x1c(%rsp), %rbp
cmpq %r12, 0x30(%rsp)
je 0x5165
movq %r15, (%rsp)
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x28(%rsp), %r8
movq %r12, %r9
callq 0x4297
cmpw $0x0, 0x1c(%rsp)
je 0x4df4
movzwl 0x1a(%rsp), %eax
cmpw 0x8(%r14), %ax
jne 0x4df4
movzwl 0x18(%rsp), %eax
cmpw 0xa(%r14), %ax
jne 0x4df4
movzwl 0x16(%rsp), %eax
cmpw 0xc(%r14), %ax
jne 0x4df4
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
incq %r12
jmp 0x4d91
movl %ebx, %r12d
pushq $0x1
popq %rbx
xorl %ecx, %ecx
leaq 0x1a(%rsp), %rax
movb %cl, (%rax)
leaq 0x18(%rsp), %rax
movb %cl, (%rax)
leaq 0x16(%rsp), %rax
movb %cl, (%rax)
leaq 0x1c(%rsp), %rax
movb %cl, (%rax)
xorl %r9d, %r9d
movq %r15, 0x60(%rsp)
movl %r12d, 0x54(%rsp)
cmpq %r9, 0x30(%rsp)
je 0x5136
movq %rcx, 0x40(%rsp)
movq %r15, (%rsp)
leaq 0x1a(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x16(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %r9, 0x68(%rsp)
callq 0x47f8
movl 0x418(%r14), %r10d
cmpl $0x8, %r10d
setae %al
movb 0x1f(%rsp), %cl
orb %al, %cl
testb $0x1, %cl
jne 0x4ec1
movb 0x1a(%rsp), %cl
leal 0x1(%rcx), %edx
movl %ebx, %eax
cmpb $0x2, %dl
jb 0x4eb2
movzbl %cl, %ecx
movl %ecx, %eax
movb $0x11, %dl
divb %dl
movzbl %ah, %edx
pushq $0x8
popq %rax
testb %dl, %dl
jne 0x4eb2
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 0x4ec1
movl %eax, 0x418(%r14)
movl %eax, %r10d
cmpl %r12d, %r10d
setae 0x1f(%rsp)
movb $0x1, %r12b
movl %r10d, %eax
movb $0x1, %r11b
testl %ebp, %ebp
movl %ebx, %r8d
jne 0x4f1f
movb 0x1a(%rsp), %cl
cmpb 0x18(%rsp), %cl
jne 0x4ef5
xorl %r11d, %r11d
movl %r10d, %eax
movl $0x0, %r8d
cmpb 0x16(%rsp), %cl
je 0x4f1f
movl $0x1, (%r14)
movl %r10d, %eax
movb $0x1, %r11b
movl %ebx, %r8d
cmpl $0x7, %r10d
ja 0x4f1f
movl $0x8, 0x418(%r14)
pushq $0x8
popq %rax
movb $0x1, %r11b
movl %ebx, %r8d
testl %r13d, %r13d
je 0x5053
movb $0x1, %r12b
movl %ebx, %edx
movl %edx, 0x58(%rsp)
movl %r8d, 0x5c(%rsp)
cmpl $0x0, 0x40(%rsp)
movl %ebx, %ecx
jne 0x5017
movb 0x1a(%rsp), %al
movb 0x18(%rsp), %cl
movb 0x16(%rsp), %bl
movb 0x1c(%rsp), %bpl
movb %al, 0x22(%rsp)
movzbl %al, %r13d
movb %cl, 0x23(%rsp)
movzbl %cl, %r15d
movzbl %bl, %ecx
movzbl %bpl, %r8d
leaq 0x70(%rsp), %rdi
movl %r13d, %esi
movl %r15d, %edx
movl %ecx, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
movl %r10d, 0x40(%rsp)
movl %r11d, 0x3c(%rsp)
callq 0x8ece
movl 0x3c(%rsp), %r11d
movl 0x40(%rsp), %r10d
xorl %ecx, %ecx
testl %eax, %eax
jns 0x5017
movb %bpl, 0x20(%rsp)
movb %bl, 0x21(%rsp)
movl 0x14(%r14), %r9d
leaq 0x70(%rsp), %rdi
movl %r13d, %esi
movl %r15d, %edx
movl 0x50(%rsp), %ecx
movq %r14, %r15
movl 0x4c(%rsp), %r8d
callq 0x41f6
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x4ffe
leal (,%rax,4), %ecx
movb 0x22(%rsp), %dl
movb %dl, 0x18(%r15,%rax,4)
movb 0x23(%rsp), %dl
movb %dl, 0x19(%r15,%rcx)
movb 0x21(%rsp), %dl
movb %dl, 0x1a(%r15,%rcx)
movb 0x20(%rsp), %dl
movb %dl, 0x1b(%r15,%rcx)
incl %eax
movl %eax, 0x14(%r15)
xorl %ecx, %ecx
cmpl 0x24(%rsp), %eax
setae %cl
movl 0x40(%rsp), %r10d
movl 0x3c(%rsp), %r11d
andb %cl, %r12b
andb %r12b, %r11b
movq 0x68(%rsp), %r9
incq %r9
cmpb $0x1, %r11b
movq 0x60(%rsp), %r15
movl 0x5c(%rsp), %ebp
movl 0x58(%rsp), %r13d
movl 0x54(%rsp), %r12d
pushq $0x1
popq %rbx
jne 0x4e2a
cmpl %r12d, %r10d
jb 0x4e2a
jmp 0x5136
movzbl 0x1a(%rsp), %ecx
cmpw %cx, 0x8(%r14)
jne 0x507a
movzbl 0x18(%rsp), %edx
cmpw %dx, 0xa(%r14)
jne 0x507a
movzbl 0x16(%rsp), %edx
cmpw %dx, 0xc(%r14)
sete %dl
jmp 0x507c
xorl %edx, %edx
movzbl 0x1c(%rsp), %esi
cmpl $0xff, %esi
je 0x50f5
testl %esi, %esi
jne 0x50dd
movl 0x4(%r14), %esi
testl %esi, %esi
sete %dil
orb %dl, %dil
testb $0x1, %dil
je 0x50dd
xorl %r12d, %r12d
orl 0x10(%r14), %esi
movl $0x0, %edx
jne 0x4f2d
movl $0x1, 0x4(%r14)
movw %cx, 0x8(%r14)
movzbl 0x18(%rsp), %eax
movw %ax, 0xa(%r14)
movzbl 0x16(%rsp), %eax
movw %ax, 0xc(%r14)
xorl %r12d, %r12d
xorl %edx, %edx
jmp 0x4f2d
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, %eax
ja 0x4f2b
jmp 0x5126
cmpl $0x0, 0x4(%r14)
setne %cl
andb %dl, %cl
xorl %r12d, %r12d
movl $0x0, %edx
cmpb $0x1, %cl
jne 0x4f2d
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, %eax
ja 0x4f28
movl $0x8, 0x418(%r14)
jmp 0x4f28
cmpl $0x0, 0x4(%r14)
je 0x5144
cmpl $0x0, 0x10(%r14)
je 0x5183
movd 0x8(%r14), %xmm0
pmullw 0x5ede(%rip), %xmm0     # 0xb030
movd %xmm0, 0x8(%r14)
imull $0x101, 0xc(%r14), %eax # imm = 0x101
movw %ax, 0xc(%r14)
leaq 0x70(%rsp), %rdi
callq 0x4abb
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
leaq 0x1a(%rsp), %rbx
leaq 0x16(%rsp), %r13
leaq 0x1c(%rsp), %rbp
cmpq %r12, 0x30(%rsp)
je 0x5144
movq %r15, (%rsp)
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x28(%rsp), %r8
movq %r12, %r9
callq 0x47f8
cmpb $0x0, 0x1c(%rsp)
je 0x5208
movzbl 0x1a(%rsp), %eax
cmpw %ax, 0x8(%r14)
jne 0x5208
movzbl 0x18(%rsp), %eax
cmpw %ax, 0xa(%r14)
jne 0x5208
movzbl 0x16(%rsp), %eax
cmpw %ax, 0xc(%r14)
jne 0x5208
movl $0x1, 0x10(%r14)
andl $0x0, 0x4(%r14)
cmpl $0x7, 0x418(%r14)
ja 0x5208
movl $0x8, 0x418(%r14)
incq %r12
jmp 0x5195