// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movl %esi, %r15d
movq %rdi, %r12
movslq %esi, %r13
leaq (,%r13,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x33e0
movq %rax, %rbx
testl %r13d, %r13d
jle 0x14ace
movl %r15d, %r13d
leaq (,%r13,4), %rdx
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x3260
xorl %r15d, %r15d
movq (%r12,%rbp,8), %rax
testq %rax, %rax
je 0x14ac4
movslq %r15d, %r15
leaq (%r15,%r15,2), %rcx
movq %rax, (%rbx,%rcx,8)
movl %ebp, 0x10(%rbx,%rcx,8)
incl %r15d
incq %rbp
cmpq %rbp, %r13
jne 0x14aa9
jmp 0x14ad1
xorl %r15d, %r15d
movl $0x1, %r12d
movl $0x1, %eax
movl 0x8(%rsp), %ebp
movl %ebp, %ecx
shll %cl, %eax
cmpl %r15d, %eax
jge 0x14af3
movq %rbx, %rdi
callq 0x3050
jmp 0x14b2d
testl %r15d, %r15d
je 0x14b22
cmpl $0x1, %r15d
je 0x14b16
cmpl $0x2, %r15d
jne 0x14b3f
movslq 0x10(%rbx), %rax
incl (%r14,%rax,4)
movslq 0x28(%rbx), %rax
incl (%r14,%rax,4)
jmp 0x14b22
movslq 0x10(%rbx), %rax
movl $0x1, (%r14,%rax,4)
movq %rbx, %rdi
callq 0x3050
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
jle 0x14bb5
movl %r15d, %r12d
leaq (,%r12,8), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movq (%rbx,%rcx), %rdx
movq %rdx, %rsi
shrq $0x37, %rsi
jne 0x14d8d
shlq $0x9, %rdx
movslq 0x10(%rbx,%rcx), %rsi
orq %rdx, %rsi
movq %rsi, (%rbx,%rcx)
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x14b55
leaq 0x21a(%rip), %rcx       # 0x14da0
movl $0x18, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30e0
testl %r15d, %r15d
jle 0x14bcc
shlq $0x3, %r12
leaq (%r12,%r12,2), %rax
xorl %ecx, %ecx
shrq $0x9, (%rbx,%rcx)
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x14ba5
jmp 0x14bcc
movslq %r15d, %rsi
leaq 0x1e1(%rip), %rcx       # 0x14da0
movl $0x18, %edx
movq %rbx, %rdi
callq 0x30e0
leal -0x1(%r15), %r13d
cmpl %ebp, %r15d
cmovgl %ebp, %r13d
leal (%r15,%r15), %eax
movl %eax, 0x14(%rsp)
imull %r13d, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x33e0
movq %rax, %rbp
movslq %r13d, %r12
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x33e0
movl %r13d, %r8d
movq %rax, %r13
leaq 0x30(%rbp), %rax
movq %rax, 0x18(%rsp)
movq (%rbx), %rcx
movq %rcx, (%rbp)
movl $0x1, 0x10(%rbp)
xorl %ecx, %ecx
movq %rcx, 0x8(%rbp)
movq 0x18(%rbx), %rdx
movq %rdx, 0x18(%rbp)
movl $0x2, 0x28(%rbp)
movq %rcx, 0x20(%rbp)
testl %r12d, %r12d
jle 0x14c62
movq %rbp, %rcx
addq $0x18, %rcx
movl %r8d, %edx
shlq $0x4, %rdx
xorl %esi, %esi
movq %rbp, (%r13,%rsi)
movq %rcx, 0x8(%r13,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x14c4f
movq %rbp, 0x8(%rsp)
cmpl $0x2, %r15d
jle 0x14ca9
movl 0x14(%rsp), %eax
addl $-0x5, %eax
decl %r8d
cmpl $0x2, %eax
movl $0x1, %r12d
cmovgel %eax, %r12d
movq %r13, %rdi
movq %rbx, %rsi
movl %r15d, %edx
leaq 0x18(%rsp), %rcx
movl %r8d, %ebp
callq 0x14da5
movl %ebp, %r8d
decl %r12d
jne 0x14c84
movq 0x18(%rsp), %rax
jmp 0x14cac
decl %r8d
movslq %r8d, %rdi
shlq $0x4, %rdi
movq -0x8(%r13,%rdi), %rcx
movq 0x8(%r13,%rdi), %rsi
movslq 0x10(%rsi), %rdx
cmpl %r15d, %edx
jge 0x14cf3
leaq (%rdi,%r13), %r8
movq -0x10(%r8), %r8
movq (%rcx), %r9
addq (%r8), %r9
leaq (%rdx,%rdx,2), %r8
cmpq (%rbx,%r8,8), %r9
jbe 0x14cf3
addq %r13, %rdi
addq $0x8, %rdi
movq 0x8(%rsi), %rcx
movq %rax, (%rdi)
incl %edx
movl %edx, 0x10(%rax)
jmp 0x14cf6
movq %rsi, %rax
movq %rcx, 0x8(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl $0x10, %ecx
movl 0x10(%rax), %edx
decl %ecx
movl %edx, 0x20(%rsp,%rcx,4)
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x14d16
cmpl $0xf, %ecx
ja 0x14d73
movl 0x5c(%rsp), %eax
movq %rbx, %rdx
addq $-0x8, %rdx
movl $0x1, %esi
movl $0xf, %edi
movslq 0x1c(%rsp,%rdi,4), %r8
cmpl %r8d, %eax
jle 0x14d69
cltq
leaq (%rax,%rax,2), %r9
leaq (%rdx,%r9,8), %r9
movslq (%r9), %r10
movl %esi, (%r14,%r10,4)
decq %rax
addq $-0x18, %r9
cmpq %r8, %rax
jg 0x14d56
decq %rdi
incl %esi
cmpq %rcx, %rdi
jae 0x14d42
movq %r13, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
movq 0x8(%rsp), %rdi
jmp 0x14b25
movq %rbx, %rdi
callq 0x3050
movl $0x1, %r12d
jmp 0x14b2d