// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/libraries/libwebp/src/utils/palette.c
// Function: GetColorPalette
int GetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {
  int i;
  int x, y;
  int num_colors = 0;
  uint8_t in_use[COLOR_HASH_SIZE] = {0};
  uint32_t colors[COLOR_HASH_SIZE] = {0};
  const uint32_t* argb = pic->argb;
  const int width = pic->width;
  const int height = pic->height;
  uint32_t last_pix = ~argb[0];  // so we're sure that last_pix != argb[0]
  assert(pic != NULL);
  assert(pic->use_argb);

  for (y = 0; y < height; ++y) {
    for (x = 0; x < width; ++x) {
      int key;
      if (argb[x] == last_pix) {
        continue;
      }
      last_pix = argb[x];
      key = VP8LHashPix(last_pix, COLOR_HASH_RIGHT_SHIFT);
      while (1) {
        if (!in_use[key]) {
          colors[key] = last_pix;
          in_use[key] = 1;
          ++num_colors;
          if (num_colors > MAX_PALETTE_SIZE) {
            return MAX_PALETTE_SIZE + 1;  // Exact count not needed.
          }
          break;
        } else if (colors[key] == last_pix) {
          break;  // The color is already there.
        } else {
          // Some other color sits here, so do linear conflict resolution.
          ++key;
          key &= (COLOR_HASH_SIZE - 1);  // Key mask.
        }
      }
    }
    argb += pic->argb_stride;
  }

  if (palette != NULL) {  // Fill the colors into palette.
    num_colors = 0;
    for (i = 0; i < COLOR_HASH_SIZE; ++i) {
      if (in_use[i]) {
        palette[num_colors] = colors[i];
        ++num_colors;
      }
    }
    qsort(palette, num_colors, sizeof(*palette), PaletteCompareColorsForQsort);
  }
  return num_colors;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1408, %rsp           # imm = 0x1408
movq %rsi, %rbx
movq %rdi, %r14
movq %rsp, %rdi
xorl %r15d, %r15d
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x56ff0
leaq 0x400(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x56ff0
cmpl $0x0, (%r14)
je 0xe155b
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
movq 0x48(%r14), %rdx
movl (%rdx), %edi
notl %edi
testl %eax, %eax
cmovlel %r15d, %eax
testl %ecx, %ecx
cmovlel %r15d, %ecx
xorl %ebp, %ebp
cmpl %ecx, %r15d
je 0xe151a
xorl %esi, %esi
cmpq %rax, %rsi
je 0xe14f8
movl (%rdx,%rsi,4), %r8d
cmpl %edi, %r8d
jne 0xe14b4
movl %edi, %r8d
jmp 0xe14f0
imull $0x1e35a7bd, %r8d, %edi # imm = 0x1E35A7BD
shrl $0x16, %edi
movl %edi, %edi
cmpb $0x0, (%rsp,%rdi)
je 0xe14da
cmpl %r8d, 0x400(%rsp,%rdi,4)
je 0xe14f0
incl %edi
andl $0x3ff, %edi            # imm = 0x3FF
jmp 0xe14be
movl %r8d, 0x400(%rsp,%rdi,4)
movb $0x1, (%rsp,%rdi)
cmpl $0xff, %ebp
jg 0xe1505
incl %ebp
incq %rsi
movl %r8d, %edi
jmp 0xe14a1
movslq 0x50(%r14), %rsi
leaq (%rdx,%rsi,4), %rdx
incl %r15d
jmp 0xe149a
movl $0x101, %ebp            # imm = 0x101
movl %ebp, %eax
addq $0x1408, %rsp           # imm = 0x1408
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
testq %rbx, %rbx
je 0xe150a
xorl %eax, %eax
xorl %ebp, %ebp
movslq %ebp, %rsi
cmpq $0x400, %rax            # imm = 0x400
je 0xe1547
cmpb $0x0, (%rsp,%rax)
je 0xe1542
movl 0x400(%rsp,%rax,4), %ecx
movl %ecx, (%rbx,%rsi,4)
incl %esi
movl %esi, %ebp
incq %rax
jmp 0xe1523
leaq -0x145(%rip), %rcx      # 0xe1409
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x587a0
jmp 0xe150a
leaq 0x3c648(%rip), %rdi     # 0x11dbaa
leaq 0x3c5ee(%rip), %rsi     # 0x11db57
leaq 0x3c5fb(%rip), %rcx     # 0x11db6b
pushq $0x6a
popq %rdx
callq 0x56770