// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rsi, %rbx
movl %edi, %eax
decq %rax
cmpq $-0x1, %rax
je 0xdf4f
leaq -0x1(%rax), %r14
cmpq $0x0, (%rbx,%rax,8)
movq %r14, %rax
je 0xdf17
leal 0x2(%r14), %r15d
leaq (,%r15,4), %rdi
callq 0x33d0
movq %rax, %rdi
xorl %eax, %eax
cmpq %rax, %r15
je 0xdf5e
andl $0x0, (%rdi,%rax,4)
incq %rax
jmp 0xdf41
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdx
leal 0x3(%r14), %ecx
xorl %eax, %eax
pushq $-0x1
popq %rsi
pushq $0x1
popq %r8
xorl %r10d, %r10d
movq %rdx, %r9
cmpq %rcx, %rax
jae 0xdfea
cmpq %r15, %rax
je 0xdf89
cmpq %r9, (%rbx,%rax,8)
jne 0xdf89
incl %r10d
jmp 0xdfe5
testq %r9, %r9
sete %r11b
cmpl $0x5, %r10d
setge %bpl
testb %bpl, %r11b
jne 0xdfb5
testq %r9, %r9
setne %r11b
cmpl $0x7, %r10d
setge %bpl
andb %r11b, %bpl
cmpb $0x1, %bpl
jne 0xdfd6
movl %r10d, %r10d
movq %rsi, %r11
leaq (%r10,%r11), %r12
cmpq $-0x1, %r12
je 0xdfd6
leal (%rax,%r11), %r12d
movl $0x1, (%rdi,%r12,4)
decq %r11
jmp 0xdfbb
movl %r8d, %r10d
cmpq %r15, %rax
je 0xdfe5
movq (%rbx,%rax,8), %r9
movl %r8d, %r10d
incq %rax
jmp 0xdf74
decl %r14d
xorl %esi, %esi
pushq $0x1
popq %r8
pushq $-0x1
popq %r9
xorl %eax, %eax
xorl %r10d, %r10d
cmpq %rcx, %rsi
jae 0xe0d3
cmpq %r15, %rsi
je 0xe031
cmpl $0x0, (%rdi,%rsi,4)
jne 0xe031
movq (%rbx,%rsi,8), %r11
movq %rdx, %r12
subq %r11, %r12
negq %r12
movq %rdx, %r13
subq %r11, %r13
cmovbq %r12, %r13
cmpq $0x3, %r13
jbe 0xe0cf
cmpl $0x3, %eax
jg 0xe03d
jne 0xe072
testq %r10, %r10
jne 0xe072
movl %eax, %r11d
shrl %eax
addq %r10, %rax
xorl %edx, %edx
divq %r11
cmpl $0x2, %eax
cmovll %r8d, %eax
testq %r10, %r10
cmoveq %r10, %rax
movq %r9, %rdx
leaq (%r11,%rdx), %r10
cmpq $-0x1, %r10
je 0xe072
leal (%rsi,%rdx), %r10d
movq %rax, (%rbx,%r10,8)
decq %rdx
jmp 0xe05b
cmpq %r14, %rsi
jae 0xe09d
movq (%rbx,%rsi,8), %rax
leal 0x2(%rsi), %edx
leal 0x3(%rsi), %r10d
movq (%rbx,%r10,8), %r10
addq 0x8(%rbx,%rsi,8), %rax
addq (%rbx,%rdx,8), %rax
leaq (%r10,%rax), %rdx
addq $0x2, %rdx
shrq $0x2, %rdx
jmp 0xe0aa
cmpq %r15, %rsi
jae 0xe0a8
movq (%rbx,%rsi,8), %rdx
jmp 0xe0aa
xorl %edx, %edx
cmpq %r15, %rsi
jne 0xe0b7
movl %r8d, %eax
xorl %r11d, %r11d
jmp 0xe0c4
movq (%rbx,%rsi,8), %r11
movl %r8d, %eax
xorl %r10d, %r10d
addq %r10, %r11
incq %rsi
movq %r11, %r10
jmp 0xdffc
incl %eax
jmp 0xe0c1
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3050