// File: /workspace/github_c_makefile_star2_O3/madler[P]pigz/zopfli/src/zopfli/deflate.c
// Function: OptimizeHuffmanForRle
void OptimizeHuffmanForRle(unsigned length, size_t* counts) {
  unsigned i;
  int k, stride;
  size_t symbol, sum, limit;
  int* good_for_rle;

  /* 1) We don't want to touch the trailing zeros. We may break the
  rules of the format by adding more data in the distance codes. */
  for (; length > 0; --length) {
    if (counts[length - 1] != 0) {
      /* Now counts[0..length - 1] does not have trailing zeros. */
      break;
    }
  }
  if (length == 0) {
    return;
  }
  /* 2) Let's mark all population counts that already can be encoded
  with an rle code.*/
  good_for_rle = (int*)malloc(length * sizeof(int));
  for (i = 0; i < length; ++i) good_for_rle[i] = 0;

  /* Let's not spoil any of the existing good rle codes.
  Mark any seq of 0's that is longer than 5 as a good_for_rle.
  Mark any seq of non-0's that is longer than 7 as a good_for_rle.*/
  symbol = counts[0];
  stride = 0;
  for (i = 0; i < length + 1; ++i) {
    if (i == length || counts[i] != symbol) {
      if ((symbol == 0 && stride >= 5) || (symbol != 0 && stride >= 7)) {
        for (k = 0; k < stride; ++k) {
          good_for_rle[i - k - 1] = 1;
        }
      }
      stride = 1;
      if (i != length) {
        symbol = counts[i];
      }
    } else {
      ++stride;
    }
  }

  /* 3) Let's replace those population counts that lead to more rle codes. */
  stride = 0;
  limit = counts[0];
  sum = 0;
  for (i = 0; i < length + 1; ++i) {
    if (i == length || good_for_rle[i]
        /* Heuristic for selecting the stride ranges to collapse. */
        || AbsDiff(counts[i], limit) >= 4) {
      if (stride >= 4 || (stride >= 3 && sum == 0)) {
        /* The stride must end, collapse what we have, if we have enough (4). */
        int count = (sum + stride / 2) / stride;
        if (count < 1) count = 1;
        if (sum == 0) {
          /* Don't make an all zeros stride to be upgraded to ones. */
          count = 0;
        }
        for (k = 0; k < stride; ++k) {
          /* We don't want to change value at counts[i],
          that is already belonging to the next stride. Thus - 1. */
          counts[i - k - 1] = count;
        }
      }
      stride = 0;
      sum = 0;
      if (i < length - 3) {
        /* All interesting strides have a count of at least 4,
        at least when non-zeros. */
        limit = (counts[i] + counts[i + 1] +
                 counts[i + 2] + counts[i + 3] + 2) / 4;
      } else if (i < length) {
        limit = counts[i];
      } else {
        limit = 0;
      }
    }
    ++stride;
    if (i != length) {
      sum += counts[i];
    }
  }

  free(good_for_rle);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movabsq $-0xfffffffe, %r13      # imm = 0xFFFFFFFF00000002
movl %edi, %esi
leaq (%rbx,%rsi,8), %rax
addq $-0x8, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %rsi
je 0xecf9
incq %rdx
incq %r13
leaq -0x1(%rcx), %r15
cmpq $0x0, (%rax,%rcx,8)
movq %r15, %rcx
je 0xeb01
movq %rsi, %rax
subq %rdx, %rax
leal 0x1(%rax), %r12d
movq %rax, %rbp
movq %rax, 0x8(%rsp)
leaq (,%r12,4), %rdi
movq %rsi, 0x10(%rsp)
callq 0x33e0
movq %rax, %r14
movl %ebp, %eax
leaq 0x4(,%rax,4), %rdx
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x3260
movabsq $-0xfffffffe, %r11      # imm = 0xFFFFFFFF00000002
movq 0x10(%rsp), %r10
addq %r10, %r11
addq %r15, %r11
movq 0x8(%rsp), %r15
je 0xed08
movq (%rbx), %rcx
addq $0x2, %r15
movl %r15d, %eax
xorl %edx, %edx
movq %rcx, %rsi
cmpq %r12, %rdx
je 0xeb99
cmpq %rsi, (%rbx,%rdx,8)
jne 0xeb99
incl %ebp
jmp 0xebf6
testq %rsi, %rsi
sete %dil
cmpl $0x5, %ebp
setge %r8b
testb %r8b, %dil
jne 0xebc3
testq %rsi, %rsi
setne %dil
cmpl $0x7, %ebp
setge %r8b
andb %dil, %r8b
cmpb $0x1, %r8b
jne 0xebe8
movl %ebp, %edi
movq $-0x1, %r8
leal (%rdx,%r8), %r9d
movl $0x1, (%r14,%r9,4)
leaq (%rdi,%r8), %r9
decq %r9
decq %r8
cmpq $-0x1, %r9
jne 0xebcc
movl $0x1, %ebp
cmpq %r12, %rdx
je 0xebf6
movq (%rbx,%rdx,8), %rsi
incq %rdx
cmpq %rax, %rdx
jne 0xeb8a
testq %r11, %r11
je 0xed08
subl %r13d, %r10d
movl %r15d, %esi
xorl %edi, %edi
movl $0x1, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
cmpq %r12, %rdi
je 0xec4d
cmpl $0x0, (%r14,%rdi,4)
jne 0xec4d
movq (%rbx,%rdi,8), %rdx
movq %r10, %r15
movq %rcx, %r10
subq %rdx, %r10
negq %r10
movq %rcx, %r11
subq %rdx, %r11
cmovbq %r10, %r11
movq %r15, %r10
cmpq $0x3, %r11
jbe 0xecf5
cmpl $0x3, %eax
jg 0xec59
jne 0xec92
testq %r9, %r9
jne 0xec92
movl %eax, %ecx
shrl %eax
addq %r9, %rax
xorl %edx, %edx
divq %rcx
cmpl $0x2, %eax
cmovll %r8d, %eax
testq %r9, %r9
cmoveq %r9, %rax
movq $-0x1, %rdx
leal (%rdi,%rdx), %r9d
movq %rax, (%rbx,%r9,8)
leaq (%rcx,%rdx), %r9
decq %r9
decq %rdx
cmpq $-0x1, %r9
jne 0xec7a
cmpq %r10, %rdi
jae 0xecbc
movq (%rbx,%rdi,8), %rax
leal 0x2(%rdi), %ecx
leal 0x3(%rdi), %edx
movq (%rbx,%rdx,8), %rdx
addq 0x8(%rbx,%rdi,8), %rax
addq (%rbx,%rcx,8), %rax
leaq (%rdx,%rax), %rcx
addq $0x2, %rcx
shrq $0x2, %rcx
jmp 0xecc9
cmpq %r12, %rdi
jae 0xecc7
movq (%rbx,%rdi,8), %rcx
jmp 0xecc9
xorl %ecx, %ecx
cmpq %r12, %rdi
jne 0xecd8
movl $0x1, %eax
xorl %r9d, %r9d
jmp 0xece7
movq (%rbx,%rdi,8), %rdx
movl $0x1, %eax
xorl %r9d, %r9d
addq %rdx, %r9
incq %rdi
cmpq %rsi, %rdi
jne 0xec1a
jmp 0xed08
incl %eax
jmp 0xece4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3050