// File: /workspace/github_c_makefile_star2_O1/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, %r15d
leaq -0x4(,%r15,4), %rax
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xe950
incq %r12
leaq -0x4(%rax), %rcx
cmpq $0x0, (%rbx,%rax,2)
movq %rcx, %rax
je 0xe75c
addq $0x8, %rcx
movabsq $0x3fffffffc, %rdi      # imm = 0x3FFFFFFFC
andq %rcx, %rdi
callq 0x33d0
movq %rax, %r14
movq %r15, %r13
subq %r12, %r13
movl %r13d, %eax
leaq 0x4(,%rax,4), %rdx
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x3260
movabsq $-0xfffffffe, %rax      # imm = 0xFFFFFFFF00000002
addq %r15, %rax
cmpq %r12, %rax
je 0xe93a
movq (%rbx), %rcx
leaq 0x2(%r13), %rax
incq %r13
movl %r13d, %edx
movl %eax, %esi
movl $0x1, %edi
xorl %r8d, %r8d
movq %rcx, %r9
cmpq %rdx, %r8
je 0xe7eb
cmpq %r9, (%rbx,%r8,8)
jne 0xe7eb
incl %ebp
jmp 0xe84c
testq %r9, %r9
sete %r10b
cmpl $0x5, %ebp
setge %r11b
testb %r11b, %r10b
jne 0xe815
testq %r9, %r9
setne %r10b
cmpl $0x7, %ebp
setge %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
jne 0xe83e
cmpl $0x2, %ebp
cmovll %edi, %ebp
movq $-0x1, %r10
leal (%r8,%r10), %r11d
movl $0x1, (%r14,%r11,4)
leaq (%r10,%rbp), %r11
decq %r11
decq %r10
cmpq $-0x1, %r11
jne 0xe822
movl $0x1, %ebp
cmpq %rdx, %r8
je 0xe84c
movq (%rbx,%r8,8), %r9
incq %r8
cmpq %rsi, %r8
jne 0xe7dc
subl %r12d, %r15d
addl $-0x2, %r15d
movl %r13d, %esi
movl %eax, %edi
xorl %r8d, %r8d
movl $0x1, %r9d
xorl %r10d, %r10d
xorl %eax, %eax
cmpq %rsi, %r8
je 0xe89b
cmpl $0x0, (%r14,%r8,4)
jne 0xe89b
movq (%rbx,%r8,8), %rdx
movq %rcx, %r11
subq %rdx, %r11
negq %r11
movq %rcx, %r12
subq %rdx, %r12
cmovbq %r11, %r12
cmpq $0x4, %r12
jb 0xe923
cmpl $0x3, %eax
jg 0xe8a7
jne 0xe8e0
testq %r10, %r10
jne 0xe8e0
movl %eax, %ecx
shrl %eax
addq %r10, %rax
xorl %edx, %edx
divq %rcx
cmpl $0x2, %eax
cmovll %r9d, %eax
testq %r10, %r10
cmoveq %r10, %rax
movq $-0x1, %rdx
leal (%r8,%rdx), %r10d
movq %rax, (%rbx,%r10,8)
leaq (%rcx,%rdx), %r10
decq %r10
decq %rdx
cmpq $-0x1, %r10
jne 0xe8c8
cmpq %r15, %r8
jae 0xe90c
movq (%rbx,%r8,8), %rax
leal 0x2(%r8), %ecx
leal 0x3(%r8), %edx
movq (%rbx,%rdx,8), %rdx
addq 0x8(%rbx,%r8,8), %rax
addq (%rbx,%rcx,8), %rax
leaq (%rdx,%rax), %rcx
addq $0x2, %rcx
shrq $0x2, %rcx
jmp 0xe915
cmpq %rsi, %r8
jae 0xe91c
movq (%rbx,%r8,8), %rcx
xorl %eax, %eax
xorl %r10d, %r10d
jmp 0xe923
xorl %eax, %eax
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpq %rsi, %r8
je 0xe92c
addq (%rbx,%r8,8), %r10
incl %eax
incq %r8
cmpq %rdi, %r8
jne 0xe86e
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3050
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq