// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x0, -0x4(%rbp)
jbe 0x12547
movq -0x10(%rbp), %rax
movl -0x4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x1253a
jmp 0x12547
jmp 0x1253c
movl -0x4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x1251f
cmpl $0x0, -0x4(%rbp)
jne 0x12552
jmp 0x12810
movl -0x4(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x33d0
movq %rax, -0x40(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x1258c
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1256b
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x4(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x12654
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0x125cf
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpq -0x28(%rbp), %rax
je 0x1263b
cmpq $0x0, -0x28(%rbp)
jne 0x125dc
cmpl $0x5, -0x1c(%rbp)
jge 0x125e9
cmpq $0x0, -0x28(%rbp)
je 0x1261b
cmpl $0x7, -0x1c(%rbp)
jl 0x1261b
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x12619
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
subl -0x18(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl $0x1, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x125f0
jmp 0x1261b
movl $0x1, -0x1c(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0x12639
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x12644
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x12646
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x125a5
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x4(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x12807
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0x126b9
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x126b9
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movq -0x38(%rbp), %rsi
callq 0x12820
cmpq $0x4, %rax
jb 0x127d3
cmpl $0x4, -0x1c(%rbp)
jge 0x126d0
cmpl $0x3, -0x1c(%rbp)
jl 0x12749
cmpq $0x0, -0x30(%rbp)
jne 0x12749
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x50(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x44(%rbp)
cmpl $0x1, -0x44(%rbp)
jge 0x12708
movl $0x1, -0x44(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x12716
movl $0x0, -0x44(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x12747
movslq -0x44(%rbp), %rdx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl -0x18(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1271d
jmp 0x12749
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x30(%rbp)
movl -0x14(%rbp), %eax
movl -0x4(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jae 0x127ae
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
addq (%rcx,%rdx,8), %rax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
addq (%rcx,%rdx,8), %rax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
addl $0x3, %edx
movl %edx, %edx
addq (%rcx,%rdx,8), %rax
addq $0x2, %rax
shrq $0x2, %rax
movq %rax, -0x38(%rbp)
jmp 0x127d1
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x127c7
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x127cf
movq $0x0, -0x38(%rbp)
jmp 0x127d1
jmp 0x127d3
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0x127f7
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x127f9
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x12675
movq -0x40(%rbp), %rdi
callq 0x3050
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)