// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %rdi, %r13
movslq %esi, %r12
leaq (,%r12,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x33d0
movq %rax, %rbx
movl %ebp, %r15d
testl %r12d, %r12d
jle 0x13e17
leaq (,%r15,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3260
testl %ebp, %ebp
jle 0x13e46
xorl %eax, %eax
xorl %r12d, %r12d
movq (%r13,%rax,8), %rcx
testq %rcx, %rcx
je 0x13e3c
movslq %r12d, %r12
leaq (%r12,%r12,2), %rdx
movq %rcx, (%rbx,%rdx,8)
movl %eax, 0x10(%rbx,%rdx,8)
incl %r12d
incq %rax
cmpq %rax, %r15
jne 0x13e20
jmp 0x13e49
xorl %r12d, %r12d
movl $0x1, %ebp
movl $0x1, %eax
movl 0x8(%rsp), %r13d
movl %r13d, %ecx
shll %cl, %eax
cmpl %r12d, %eax
jl 0x13e9b
cmpl $0x2, %r12d
je 0x13e89
cmpl $0x1, %r12d
je 0x13e7b
testl %r12d, %r12d
jne 0x13eb4
movq %rbx, %rdi
movl %r12d, %ebp
jmp 0x13e9e
movslq 0x10(%rbx), %rax
movl $0x1, (%r14,%rax,4)
jmp 0x13e99
movslq 0x10(%rbx), %rax
incl (%r14,%rax,4)
movslq 0x28(%rbx), %rax
incl (%r14,%rax,4)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x3050
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %r15d
jle 0x13eed
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movq (%rbx,%rcx), %rdx
movq %rdx, %rsi
shrq $0x37, %rsi
jne 0x13e9b
shlq $0x9, %rdx
movslq 0x10(%rbx,%rcx), %rsi
orq %rdx, %rsi
movq %rsi, (%rbx,%rcx)
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x13ec7
movslq %r12d, %rsi
leaq 0x1eb(%rip), %rcx       # 0x140e2
movl $0x18, %edx
movq %rbx, %rdi
callq 0x30e0
testl %r12d, %r12d
jle 0x13f21
shlq $0x3, %r15
leaq (%r15,%r15,2), %rax
xorl %ecx, %ecx
shrq $0x9, (%rbx,%rcx)
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x13f13
leal -0x1(%r12), %r15d
cmpl %r13d, %r12d
cmovgl %r13d, %r15d
movl %r12d, %eax
imull %r15d, %eax
addl %eax, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x33d0
movq %rax, %r13
movq %rax, 0x10(%rsp)
movslq %r15d, %rbp
movq %rbp, %rdi
shlq $0x4, %rdi
movq %rdi, 0x18(%rsp)
callq 0x33d0
movq %r13, %rsi
movq %rax, %r13
leaq 0x30(%rsi), %rax
movq %rax, 0x10(%rsp)
movq (%rbx), %rax
movq %rax, (%rsi)
movl $0x1, 0x10(%rsi)
xorl %eax, %eax
movq %rax, 0x8(%rsi)
movq 0x18(%rbx), %rcx
movq %rcx, 0x18(%rsi)
movl $0x2, 0x28(%rsi)
movq %rax, 0x20(%rsi)
testl %ebp, %ebp
jle 0x13fbd
movq %rsi, %rax
addq $0x18, %rax
movl %r15d, %ecx
shlq $0x4, %rcx
xorl %edx, %edx
movq %rsi, (%r13,%rdx)
movq %rax, 0x8(%r13,%rdx)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x13faa
movq %rsi, 0x8(%rsp)
cmpl $0x3, %r12d
jl 0x13ff8
leal -0x5(,%r12,2), %eax
decl %r15d
cmpl $0x2, %eax
movl $0x1, %ebp
cmovgel %eax, %ebp
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
leaq 0x10(%rsp), %rcx
movl %r15d, %r8d
callq 0x140e7
decl %ebp
jne 0x13fde
movq 0x18(%rsp), %rcx
leaq (%rcx,%r13), %rax
addq $-0x8, %rax
movq -0x8(%rcx,%r13), %rcx
movslq 0x10(%rcx), %rdx
cmpl %r12d, %edx
jge 0x1403e
movq -0x18(%rax), %rsi
movq -0x10(%rax), %rdi
movq (%rdi), %rdi
addq (%rsi), %rdi
leaq (%rdx,%rdx,2), %rsi
cmpq (%rbx,%rsi,8), %rdi
jbe 0x1403e
movq 0x8(%rcx), %rsi
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
incl %edx
movl %edx, 0x10(%rcx)
jmp 0x14042
movq -0x10(%rax), %rsi
movq %rsi, 0x8(%rcx)
movq (%rax), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl $0x10, %ecx
testq %rax, %rax
je 0x14079
movl 0x10(%rax), %edx
decl %ecx
movl %edx, 0x20(%rsp,%rcx,4)
movq 0x8(%rax), %rax
jmp 0x14065
cmpl $0xf, %ecx
ja 0x140c6
movl 0x5c(%rsp), %eax
movl %ecx, %ecx
movq %rbx, %rdx
addq $-0x8, %rdx
movl $0x1, %esi
movl $0xf, %edi
movslq 0x1c(%rsp,%rdi,4), %r8
cmpl %r8d, %eax
jle 0x140bc
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 0x140a9
decq %rdi
incl %esi
cmpq %rcx, %rdi
jae 0x14095
movq %r13, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
jmp 0x13e9e