// File: /workspace/github_c_makefile_star2_O0/enzo1982[P]smooth/libraries/libwebp/src/utils/palette.c
// Function: PaletteSortModifiedZeng
static int PaletteSortModifiedZeng(const WebPPicture* const pic,
                                   const uint32_t* const palette_in,
                                   uint32_t num_colors,
                                   uint32_t* const palette) {
  uint32_t i, j, ind;
  uint8_t remapping[MAX_PALETTE_SIZE];
  uint32_t* cooccurrence;
  struct Sum sums[MAX_PALETTE_SIZE];
  uint32_t first, last;
  uint32_t num_sums;
  // TODO(vrabaud) check whether one color images should use palette or not.
  if (num_colors <= 1) return 1;
  // Build the co-occurrence matrix.
  cooccurrence =
      (uint32_t*)WebPSafeCalloc(num_colors * num_colors, sizeof(*cooccurrence));
  if (cooccurrence == NULL) {
    return 0;
  }
  if (!CoOccurrenceBuild(pic, palette_in, num_colors, cooccurrence)) {
    WebPSafeFree(cooccurrence);
    return 0;
  }

  // Initialize the mapping list with the two best indices.
  CoOccurrenceFindMax(cooccurrence, num_colors, &remapping[0], &remapping[1]);

  // We need to append and prepend to the list of remapping. To this end, we
  // actually define the next start/end of the list as indices in a vector (with
  // a wrap around when the end is reached).
  first = 0;
  last = 1;
  num_sums = num_colors - 2;  // -2 because we know the first two values
  if (num_sums > 0) {
    // Initialize the sums with the first two remappings and find the best one
    struct Sum* best_sum = &sums[0];
    best_sum->index = 0u;
    best_sum->sum = 0u;
    for (i = 0, j = 0; i < num_colors; ++i) {
      if (i == remapping[0] || i == remapping[1]) continue;
      sums[j].index = i;
      sums[j].sum = cooccurrence[i * num_colors + remapping[0]] +
                    cooccurrence[i * num_colors + remapping[1]];
      if (sums[j].sum > best_sum->sum) best_sum = &sums[j];
      ++j;
    }

    while (num_sums > 0) {
      const uint8_t best_index = best_sum->index;
      // Compute delta to know if we need to prepend or append the best index.
      int32_t delta = 0;
      const int32_t n = num_colors - num_sums;
      for (ind = first, j = 0; (ind + j) % num_colors != last + 1; ++j) {
        const uint16_t l_j = remapping[(ind + j) % num_colors];
        delta += (n - 1 - 2 * (int32_t)j) *
                 (int32_t)cooccurrence[best_index * num_colors + l_j];
      }
      if (delta > 0) {
        first = (first == 0) ? num_colors - 1 : first - 1;
        remapping[first] = best_index;
      } else {
        ++last;
        remapping[last] = best_index;
      }
      // Remove best_sum from sums.
      *best_sum = sums[num_sums - 1];
      --num_sums;
      // Update all the sums and find the best one.
      best_sum = &sums[0];
      for (i = 0; i < num_sums; ++i) {
        sums[i].sum += cooccurrence[best_index * num_colors + sums[i].index];
        if (sums[i].sum > best_sum->sum) best_sum = &sums[i];
      }
    }
  }
  assert((last + 1) % num_colors == first);
  WebPSafeFree(cooccurrence);

  // Re-map the palette.
  for (i = 0; i < num_colors; ++i) {
    palette[i] = palette_in[remapping[(first + i) % num_colors]];
  }
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x990, %rsp            # imm = 0x990
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
cmpl $0x1, -0x1c(%rbp)
ja 0x18393c
movl $0x1, -0x4(%rbp)
jmp 0x183d7e
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
movl $0x4, %esi
callq 0x5a9f0
movq %rax, -0x148(%rbp)
cmpq $0x0, -0x148(%rbp)
jne 0x18396e
movl $0x0, -0x4(%rbp)
jmp 0x183d7e
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x148(%rbp), %rcx
callq 0x184030
cmpl $0x0, %eax
jne 0x1839a2
movq -0x148(%rbp), %rdi
callq 0x5b920
movl $0x0, -0x4(%rbp)
jmp 0x183d7e
movq -0x148(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq -0x140(%rbp), %rdx
leaq -0x140(%rbp), %rcx
addq $0x1, %rcx
callq 0x1842a0
movl $0x0, -0x954(%rbp)
movl $0x1, -0x958(%rbp)
movl -0x1c(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x95c(%rbp)
cmpl $0x0, -0x95c(%rbp)
jbe 0x183ce3
leaq -0x950(%rbp), %rax
movq %rax, -0x968(%rbp)
movq -0x968(%rbp), %rax
movb $0x0, (%rax)
movq -0x968(%rbp), %rax
movl $0x0, 0x4(%rax)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x183ae9
movl -0x2c(%rbp), %eax
movzbl -0x140(%rbp), %ecx
cmpl %ecx, %eax
je 0x183a4c
movl -0x2c(%rbp), %eax
movzbl -0x13f(%rbp), %ecx
cmpl %ecx, %eax
jne 0x183a51
jmp 0x183adb
movl -0x2c(%rbp), %eax
movb %al, %cl
movl -0x30(%rbp), %eax
movb %cl, -0x950(%rbp,%rax,8)
movq -0x148(%rbp), %rax
movl -0x2c(%rbp), %ecx
imull -0x1c(%rbp), %ecx
movzbl -0x140(%rbp), %edx
addl %edx, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %ecx
movq -0x148(%rbp), %rax
movl -0x2c(%rbp), %edx
imull -0x1c(%rbp), %edx
movzbl -0x13f(%rbp), %esi
addl %esi, %edx
movl %edx, %edx
addl (%rax,%rdx,4), %ecx
movl -0x30(%rbp), %eax
movl %ecx, -0x94c(%rbp,%rax,8)
movl -0x30(%rbp), %eax
movl -0x94c(%rbp,%rax,8), %eax
movq -0x968(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x183ad2
movl -0x30(%rbp), %eax
movl %eax, %ecx
leaq -0x950(%rbp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x968(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x183a24
jmp 0x183aeb
cmpl $0x0, -0x95c(%rbp)
jbe 0x183ce1
movq -0x968(%rbp), %rax
movb (%rax), %al
movb %al, -0x969(%rbp)
movl $0x0, -0x970(%rbp)
movl -0x1c(%rbp), %eax
subl -0x95c(%rbp), %eax
movl %eax, -0x974(%rbp)
movl -0x954(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl -0x30(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl -0x958(%rbp), %eax
addl $0x1, %eax
cmpl %eax, %edx
je 0x183bac
movl -0x34(%rbp), %eax
addl -0x30(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %edx, %eax
movzbl -0x140(%rbp,%rax), %eax
movw %ax, -0x976(%rbp)
movl -0x974(%rbp), %eax
subl $0x1, %eax
movl -0x30(%rbp), %ecx
shll %ecx
subl %ecx, %eax
movq -0x148(%rbp), %rcx
movzbl -0x969(%rbp), %edx
imull -0x1c(%rbp), %edx
movzwl -0x976(%rbp), %esi
addl %esi, %edx
movl %edx, %edx
imull (%rcx,%rdx,4), %eax
addl -0x970(%rbp), %eax
movl %eax, -0x970(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x183b30
cmpl $0x0, -0x970(%rbp)
jle 0x183bfc
cmpl $0x0, -0x954(%rbp)
jne 0x183bcc
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x97c(%rbp)
jmp 0x183bdb
movl -0x954(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x97c(%rbp)
movl -0x97c(%rbp), %eax
movl %eax, -0x954(%rbp)
movb -0x969(%rbp), %cl
movl -0x954(%rbp), %eax
movb %cl, -0x140(%rbp,%rax)
jmp 0x183c1e
movl -0x958(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x958(%rbp)
movb -0x969(%rbp), %cl
movl -0x958(%rbp), %eax
movb %cl, -0x140(%rbp,%rax)
movq -0x968(%rbp), %rax
movl -0x95c(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq -0x950(%rbp,%rcx,8), %rcx
movq %rcx, (%rax)
movl -0x95c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x95c(%rbp)
leaq -0x950(%rbp), %rax
movq %rax, -0x968(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x95c(%rbp), %eax
jae 0x183cdc
movq -0x148(%rbp), %rax
movzbl -0x969(%rbp), %ecx
imull -0x1c(%rbp), %ecx
movl -0x2c(%rbp), %edx
movzbl -0x950(%rbp,%rdx,8), %edx
addl %edx, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %ecx
movl -0x2c(%rbp), %eax
addl -0x94c(%rbp,%rax,8), %ecx
movl %ecx, -0x94c(%rbp,%rax,8)
movl -0x2c(%rbp), %eax
movl -0x94c(%rbp,%rax,8), %eax
movq -0x968(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x183ccf
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq -0x950(%rbp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x968(%rbp)
jmp 0x183cd1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x183c5f
jmp 0x183aeb
jmp 0x183ce3
movl -0x958(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x1c(%rbp)
cmpl -0x954(%rbp), %edx
jne 0x183cfb
jmp 0x183d1a
leaq 0x3dbfe(%rip), %rdi     # 0x1c1900
leaq 0x3dadf(%rip), %rsi     # 0x1c17e8
movl $0x172, %edx            # imm = 0x172
leaq 0x3dc0c(%rip), %rcx     # 0x1c1921
callq 0x598a0
movq -0x148(%rbp), %rdi
callq 0x5b920
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x183d77
movq -0x18(%rbp), %rax
movq %rax, -0x988(%rbp)
movl -0x954(%rbp), %eax
addl -0x2c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0x988(%rbp), %rax
movl %edx, %ecx
movzbl -0x140(%rbp,%rcx), %ecx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x183d2d
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x990, %rsp            # imm = 0x990
popq %rbp
retq
nopw (%rax,%rax)