// File: /workspace/github_c_makefile_star2_O2/madler[P]pigz/zopfli/src/zopfli/katajainen.c
// Function: ZopfliLengthLimitedCodeLengths
int ZopfliLengthLimitedCodeLengths(
    const size_t* frequencies, int n, int maxbits, unsigned* bitlengths) {
  NodePool pool;
  int i;
  int numsymbols = 0;  /* Amount of symbols with frequency > 0. */
  int numBoundaryPMRuns;
  Node* nodes;

  /* Array of lists of chains. Each list requires only two lookahead chains at
  a time, so each list is a array of two Node*'s. */
  Node* (*lists)[2];

  /* One leaf per symbol. Only numsymbols leaves will be used. */
  Node* leaves = (Node*)malloc(n * sizeof(*leaves));

  /* Initialize all bitlengths at 0. */
  for (i = 0; i < n; i++) {
    bitlengths[i] = 0;
  }

  /* Count used symbols and place them in the leaves. */
  for (i = 0; i < n; i++) {
    if (frequencies[i]) {
      leaves[numsymbols].weight = frequencies[i];
      leaves[numsymbols].count = i;  /* Index of symbol this leaf represents. */
      numsymbols++;
    }
  }

  /* Check special cases and error conditions. */
  if ((1 << maxbits) < numsymbols) {
    free(leaves);
    return 1;  /* Error, too few maxbits to represent symbols. */
  }
  if (numsymbols == 0) {
    free(leaves);
    return 0;  /* No symbols at all. OK. */
  }
  if (numsymbols == 1) {
    bitlengths[leaves[0].count] = 1;
    free(leaves);
    return 0;  /* Only one symbol, give it bitlength 1, not 0. OK. */
  }
  if (numsymbols == 2) {
    bitlengths[leaves[0].count]++;
    bitlengths[leaves[1].count]++;
    free(leaves);
    return 0;
  }

  /* Sort the leaves from lightest to heaviest. Add count into the same
  variable for stable sorting. */
  for (i = 0; i < numsymbols; i++) {
    if (leaves[i].weight >=
        ((size_t)1 << (sizeof(leaves[0].weight) * CHAR_BIT - 9))) {
      free(leaves);
      return 1;  /* Error, we need 9 bits for the count. */
    }
    leaves[i].weight = (leaves[i].weight << 9) | leaves[i].count;
  }
  qsort(leaves, numsymbols, sizeof(Node), LeafComparator);
  for (i = 0; i < numsymbols; i++) {
    leaves[i].weight >>= 9;
  }

  if (numsymbols - 1 < maxbits) {
    maxbits = numsymbols - 1;
  }

  /* Initialize node memory pool. */
  nodes = (Node*)malloc(maxbits * 2 * numsymbols * sizeof(Node));
  pool.next = nodes;

  lists = (Node* (*)[2])malloc(maxbits * sizeof(*lists));
  InitLists(&pool, leaves, maxbits, lists);

  /* In the last list, 2 * numsymbols - 2 active chains need to be created. Two
  are already created in the initialization. Each BoundaryPM run creates one. */
  numBoundaryPMRuns = 2 * numsymbols - 4;
  for (i = 0; i < numBoundaryPMRuns - 1; i++) {
    BoundaryPM(lists, leaves, numsymbols, &pool, maxbits - 1);
  }
  BoundaryPMFinal(lists, leaves, numsymbols, &pool, maxbits - 1);

  ExtractBitLengths(lists[maxbits - 1][1], leaves, bitlengths);

  free(lists);
  free(leaves);
  free(nodes);
  return 0;  /* OK. */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r14
movl %edx, %ebp
movq %rdi, %r15
movslq %esi, %r12
imulq $0x18, %r12, %rdi
callq 0x33d0
movq %rax, %rbx
movq %rax, 0x8(%rsp)
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
cmpq %rax, %r12
je 0x133ae
andl $0x0, (%r14,%rax,4)
incq %rax
jmp 0x1339f
xorl %eax, %eax
xorl %r13d, %r13d
movslq %r13d, %rsi
cmpq %rax, %r12
je 0x133da
movq (%r15,%rax,8), %rcx
testq %rcx, %rcx
je 0x133d5
imulq $0x18, %rsi, %rdx
movq %rcx, (%rbx,%rdx)
movl %eax, 0x10(%rbx,%rdx)
incl %esi
movl %esi, %r13d
incq %rax
jmp 0x133b3
pushq $0x1
popq %r15
movl %r15d, %eax
movl %ebp, %ecx
shll %cl, %eax
cmpl %r13d, %eax
jge 0x133f4
movq %rbx, %rdi
callq 0x3050
jmp 0x1342e
testl %r13d, %r13d
je 0x13423
cmpl $0x1, %r13d
je 0x13417
cmpl $0x2, %r13d
jne 0x13440
movslq 0x10(%rbx), %rax
incl (%r14,%rax,4)
movslq 0x28(%rbx), %rax
incl (%r14,%rax,4)
jmp 0x13423
movslq 0x10(%rbx), %rax
movl $0x1, (%r14,%rax,4)
movq %rbx, %rdi
callq 0x3050
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
imulq $0x18, %rcx, %r12
cmpq %rax, %r12
je 0x1347a
movq (%rbx,%rax), %rcx
movq %rcx, %rdx
shrq $0x37, %rdx
jne 0x133ea
shlq $0x9, %rcx
movslq 0x10(%rbx,%rax), %rdx
orq %rcx, %rdx
movq %rdx, (%rbx,%rax)
addq $0x18, %rax
jmp 0x13452
leaq 0x1d5(%rip), %rcx       # 0x13656
pushq $0x18
popq %rdx
movq %rbx, %rdi
callq 0x30e0
xorl %eax, %eax
cmpq %rax, %r12
je 0x1349e
shrq $0x9, (%rbx,%rax)
addq $0x18, %rax
jmp 0x1348e
leal -0x1(%r13), %r15d
cmpl %ebp, %r13d
cmovgl %ebp, %r15d
leal (,%r13,2), %eax
movl %eax, 0x4(%rsp)
imull %r15d, %eax
cltq
imulq $0x18, %rax, %rdi
callq 0x33d0
movq %rax, %rbp
movslq %r15d, %r12
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x33d0
movq %rbp, %rsi
movq (%rbx), %rcx
movq %rcx, (%rbp)
movl $0x1, 0x10(%rbp)
andq $0x0, 0x8(%rbp)
movq %rax, %rbp
leaq 0x18(%rsi), %rax
movq 0x18(%rbx), %rcx
movq %rcx, 0x18(%rsi)
movl $0x2, 0x28(%rsi)
andq $0x0, 0x20(%rsi)
leaq 0x30(%rsi), %rcx
movq %rcx, 0x10(%rsp)
xorl %ecx, %ecx
testl %r12d, %r12d
movl $0x0, %edx
movl %r15d, %r12d
cmovgl %r15d, %edx
shlq $0x4, %rdx
cmpq %rcx, %rdx
je 0x1353a
movq %rsi, (%rbp,%rcx)
movq %rax, 0x8(%rbp,%rcx)
addq $0x10, %rcx
jmp 0x13525
movq %rsi, 0x18(%rsp)
decl %r12d
movl 0x4(%rsp), %eax
cmpl $0x6, %eax
pushq $0x5
popq %r15
cmovgel %eax, %r15d
addl $-0x5, %r15d
subl $0x1, %r15d
jb 0x13573
movq %rbp, %rdi
movq %rbx, %rsi
movl %r13d, %edx
leaq 0x10(%rsp), %rcx
movl %r12d, %r8d
callq 0x1365b
jmp 0x13555
movslq %r12d, %rsi
shlq $0x4, %rsi
movq -0x8(%rbp,%rsi), %rcx
movq 0x8(%rbp,%rsi), %rax
movslq 0x10(%rax), %rdx
cmpl %r13d, %edx
jge 0x135c4
leaq (%rsi,%rbp), %rdi
movq -0x10(%rdi), %rdi
movq (%rcx), %r8
addq (%rdi), %r8
imulq $0x18, %rdx, %rdi
cmpq (%rbx,%rdi), %r8
movq 0x8(%rsp), %r10
jbe 0x135c9
addq %rbp, %rsi
addq $0x8, %rsi
movq 0x8(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
incl %edx
movl %edx, 0x10(%rax)
jmp 0x135c9
movq 0x8(%rsp), %r10
movq %rcx, 0x8(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
pushq $0x10
popq %rcx
testq %rax, %rax
je 0x135fb
movl 0x10(%rax), %edx
decl %ecx
movl %edx, 0x20(%rsp,%rcx,4)
movq 0x8(%rax), %rax
jmp 0x135e7
movl 0x5c(%rsp), %eax
movl %ecx, %ecx
addq $-0x8, %r10
pushq $0x1
popq %rdx
pushq $0xf
popq %rsi
cmpq %rcx, %rsi
jb 0x1363c
decq %rsi
movl %esi, %edi
movslq 0x20(%rsp,%rdi,4), %rdi
cltq
imulq $0x18, %rax, %r8
addq %r10, %r8
cmpq %rdi, %rax
jle 0x13638
movslq (%r8), %r9
movl %edx, (%r14,%r9,4)
decq %rax
addq $-0x18, %r8
jmp 0x13623
incl %edx
jmp 0x1360b
movq %rbp, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
movq 0x18(%rsp), %rdi
jmp 0x13426