// File: /workspace/github_c_makefile_star2_O1/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, 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 0x4616
movl %eax, 0x28(%rsp)
xorl %esi, %esi
movq %r13, 0x38(%rsp)
movl 0x4(%r13), %eax
cmpq $0x6, %r15
ja 0x5d70
leaq 0x8cb8(%rip), %rcx      # 0xea24
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 0x5dca
movl $0x4, 0x2c(%rsp)
cmpl $0x2, %r14d
je 0x5dca
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
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl $0xffffffff, 0xf0(%rsp) # imm = 0xFFFFFFFF
cmpl $0x10, %eax
movq %r12, 0x18(%rsp)
jne 0x5ec3
movl %r15d, %ebx
cmpq $0x0, 0x20(%rsp)
je 0x5ec0
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
callq 0x53f2
movzwl 0xe(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x6369
movzwl 0xc(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x6369
movzwl 0xa(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x6369
movzwl 0x10(%rsp), %eax
movzbl %al, %ecx
shrl $0x8, %eax
cmpl %eax, %ecx
jne 0x6369
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0x5e42
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 0x6249
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
callq 0x59e3
testb $0x1, %r15b
jne 0x5f81
movl 0x418(%r12), %eax
cmpl $0x7, %eax
ja 0x5f81
movzbl 0xe(%rsp), %ecx
leal 0x1(%rcx), %esi
movl $0x1, %edx
cmpb $0x2, %sil
jb 0x5f75
imull $0xf0f0f0f1, %ecx, %esi # imm = 0xF0F0F0F1
movl $0x8, %edx
cmpl $0xf0f0f0f, %esi        # imm = 0xF0F0F0F
ja 0x5f75
imull $0xfcfcfcfd, %ecx, %ecx # imm = 0xFCFCFCFD
xorl %edx, %edx
cmpl $0x3030304, %ecx        # imm = 0x3030304
setae %dl
leal 0x2(,%rdx,2), %edx
cmpl %eax, %edx
jbe 0x5f81
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 0x5fe2
movb 0xe(%rsp), %al
cmpb 0xc(%rsp), %al
jne 0x5fbd
xorl %r9d, %r9d
movl $0x0, %ebp
cmpb 0xa(%rsp), %al
je 0x5fe2
movl $0x1, (%r12)
movb $0x1, %r9b
movl $0x1, %ebp
cmpl $0x7, %r10d
ja 0x5fe2
movl $0x8, 0x418(%r12)
movb $0x1, %r9b
cmpl $0x0, 0x28(%rsp)
je 0x611f
movb $0x1, %r13b
movl $0x1, %esi
movl %esi, 0x28(%rsp)
cmpl $0x0, 0x48(%rsp)
movl $0x1, %edx
jne 0x60e2
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 0xb0b5
movl 0x44(%rsp), %r9d
movl 0x48(%rsp), %r10d
xorl %edx, %edx
testl %eax, %eax
jns 0x60e2
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 0x5336
movl 0x14(%r14), %eax
cmpq $0xff, %rax
ja 0x60c9
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 0x6249
leaq 0x1(%rcx), %rbx
cmpq %rcx, 0x60(%rsp)
jne 0x5ef5
jmp 0x6249
movzbl 0xe(%rsp), %eax
cmpw %ax, 0x8(%r12)
jne 0x6149
movzbl 0xc(%rsp), %ecx
cmpw %cx, 0xa(%r12)
jne 0x6149
movzbl 0xa(%rsp), %ecx
cmpw %cx, 0xc(%r12)
sete %cl
jmp 0x614b
xorl %ecx, %ecx
movzbl 0x10(%rsp), %edx
cmpl $0xff, %edx
je 0x61aa
testl %edx, %edx
jne 0x61e8
cmpl $0x0, 0x4(%r12)
sete %sil
orb %cl, %sil
testb $0x1, %sil
je 0x61e8
cmpl $0xff, %edx
je 0x61aa
xorl %r13d, %r13d
movl $0x0, %esi
testl %edx, %edx
jne 0x5ff5
cmpl $0x0, 0x10(%r12)
jne 0x619b
cmpl $0x0, 0x4(%r12)
je 0x621a
xorl %r13d, %r13d
cmpl $0xff, %edx
jne 0x5ff5
cmpl $0x0, 0x4(%r12)
setne %al
andb %cl, %al
xorl %r13d, %r13d
movl $0x0, %esi
cmpb $0x1, %al
jne 0x5ff5
movl $0x1, 0x10(%r12)
movl $0x0, 0x4(%r12)
cmpl $0x7, 0x418(%r12)
ja 0x5fed
jmp 0x6209
movl $0x1, 0x10(%r12)
movl $0x0, 0x4(%r12)
cmpl $0x7, 0x418(%r12)
ja 0x5ff0
movl $0x8, 0x418(%r12)
jmp 0x5fed
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 0x5ff5
cmpl $0x0, 0x4(%r12)
je 0x631b
movq 0x18(%rsp), %rax
cmpl $0x0, 0x10(%rax)
setne %al
cmpq $0x0, 0x20(%rsp)
sete %cl
orb %al, %cl
jne 0x631b
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
callq 0x59e3
cmpb $0x0, 0x10(%rsp)
je 0x630d
movzbl 0xe(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0x8(%rcx)
jne 0x630d
movzbl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xa(%rcx)
jne 0x630d
movzbl 0xa(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw %ax, 0xc(%rcx)
jne 0x630d
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
movl $0x0, 0x4(%rax)
cmpl $0x7, 0x418(%rax)
ja 0x630d
movq 0x18(%rsp), %rax
movl $0x8, 0x418(%rax)
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0x6289
movq 0x18(%rsp), %rsi
movl 0x8(%rsi), %eax
movl 0xc(%rsi), %ecx
movl %eax, %edx
shll $0x8, %edx
addl %eax, %edx
movw %dx, 0x8(%rsi)
movzwl 0xa(%rsi), %eax
movl %eax, %edx
shll $0x8, %edx
addl %eax, %edx
movw %dx, 0xa(%rsi)
movl %ecx, %eax
shll $0x8, %eax
addl %ecx, %eax
movw %ax, 0xc(%rsi)
leaq 0x70(%rsp), %rdi
callq 0x5ccb
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 0x53f2
movb $0x1, %cl
movb $0x1, %al
testl %ebp, %ebp
movl $0x1, %ebp
jne 0x6409
movzwl 0xe(%rsp), %edx
cmpw 0xc(%rsp), %dx
jne 0x63fa
xorl %eax, %eax
movl $0x0, %ebp
cmpw 0xa(%rsp), %dx
je 0x6409
movl $0x1, (%r12)
movb $0x1, %al
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x64bc
movzwl 0xe(%rsp), %edx
cmpw 0x8(%r12), %dx
jne 0x643d
movzwl 0xc(%rsp), %ecx
cmpw 0xa(%r12), %cx
jne 0x643d
movzwl 0xa(%rsp), %ecx
cmpw 0xc(%r12), %cx
sete %sil
jmp 0x643f
xorl %esi, %esi
movzwl 0x10(%rsp), %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
je 0x648f
testl %edi, %edi
jne 0x64a8
cmpl $0x0, 0x4(%r12)
sete %cl
orb %sil, %cl
testb $0x1, %cl
je 0x64a8
cmpl $0xffff, %edi           # imm = 0xFFFF
je 0x648f
xorl %ecx, %ecx
movl $0x0, %r14d
testl %edi, %edi
jne 0x64c2
cmpl $0x0, 0x10(%r12)
jne 0x6485
cmpl $0x0, 0x4(%r12)
je 0x64d8
xorl %ecx, %ecx
cmpl $0xffff, %edi           # imm = 0xFFFF
jne 0x64c2
cmpl $0x0, 0x4(%r12)
setne %dl
andb %sil, %dl
xorl %ecx, %ecx
movl $0x0, %r14d
cmpb $0x1, %dl
jne 0x64c2
movl $0x1, 0x10(%r12)
movl $0x0, 0x4(%r12)
movb $0x1, %cl
movl $0x1, %r14d
testb %cl, %al
jne 0x6504
leaq 0x1(%r15), %rax
cmpq %r15, %rbx
movq %rax, %r15
jne 0x63ab
jmp 0x6504
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 0x64c2
cmpl $0x0, 0x4(%r12)
je 0x634b
movq 0x18(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x634b
xorl %r14d, %r14d
leaq 0xe(%rsp), %r15
leaq 0xc(%rsp), %r12
leaq 0xa(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x30(%rsp), %r8
movq %r14, %r9
callq 0x53f2
cmpw $0x0, 0x10(%rsp)
je 0x65a3
movzwl 0xe(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0x8(%rcx), %ax
jne 0x65a3
movzwl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0xa(%rcx), %ax
jne 0x65a3
movzwl 0xa(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpw 0xc(%rcx), %ax
jne 0x65a3
movq 0x18(%rsp), %rax
movl $0x1, 0x10(%rax)
movl $0x0, 0x4(%rax)
incq %r14
cmpq %r14, 0x20(%rsp)
jne 0x6536
jmp 0x634b