// File: /workspace/github_c_makefile_star2_O2/imaya[P]zopfli-for-emscripten/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;

  /* 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. */
  }

  /* Sort the leaves from lightest to heaviest. */
  qsort(leaves, numsymbols, sizeof(Node), LeafComparator);

  /* Initialize node memory pool. */
  pool.size = 2 * maxbits * (maxbits + 1);
  pool.nodes = (Node*)malloc(pool.size * sizeof(*pool.nodes));
  pool.next = pool.nodes;
  for (i = 0; i < pool.size; i++) {
    pool.nodes[i].inuse = 0;
  }

  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; i++) {
    char final = i == numBoundaryPMRuns - 1;
    BoundaryPM(lists, maxbits, leaves, numsymbols, &pool, maxbits - 1, final);
  }

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
movq %rdi, %rbp
movslq %esi, %r15
imulq $0x18, %r15, %rdi
callq 0x1120
movq %rax, %r12
movq %rax, %rbx
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
cmpq %rax, %r15
je 0x3fab
andl $0x0, (%r14,%rax,4)
incq %rax
jmp 0x3f9c
xorl %eax, %eax
xorl %r13d, %r13d
movslq %r13d, %rsi
cmpq %rax, %r15
je 0x3fd8
movq (%rbp,%rax,8), %rcx
testq %rcx, %rcx
je 0x3fd3
imulq $0x18, %rsi, %rdx
movq %rcx, (%rbx,%rdx)
movl %eax, 0x10(%rbx,%rdx)
incl %esi
movl %esi, %r13d
incq %rax
jmp 0x3fb0
pushq $0x1
popq %rbp
movl %ebp, %eax
movq 0x10(%rsp), %rcx
shll %cl, %eax
cmpl %r13d, %eax
jl 0x41c1
testl %r13d, %r13d
je 0x400b
cmpl $0x1, %r13d
jne 0x4013
movslq 0x10(%rbx), %rax
movl $0x1, (%r14,%rax,4)
xorl %ebp, %ebp
jmp 0x41c1
movl %r13d, %ebp
jmp 0x41c1
leaq 0x1c0(%rip), %rcx       # 0x41da
pushq $0x18
popq %rdx
movq %rbx, %rdi
callq 0x1060
movq 0x10(%rsp), %rcx
leal 0x1(%rcx), %eax
imull %ecx, %eax
addl %eax, %eax
movl %eax, 0x30(%rsp)
movslq %eax, %r15
imulq $0x18, %r15, %rdi
callq 0x1120
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
xorl %ecx, %ecx
testl %r15d, %r15d
cmovlel %ecx, %r15d
imulq $0x18, %r15, %rdx
cmpq %rcx, %rdx
je 0x4069
movb $0x0, 0x14(%rax,%rcx)
addq $0x18, %rcx
jmp 0x4059
movq %r12, 0x40(%rsp)
movq 0x10(%rsp), %r15
movslq %r15d, %rdi
shlq $0x4, %rdi
movq %rdi, 0x38(%rsp)
callq 0x1120
movq %rax, %rbp
xorl %r12d, %r12d
leaq 0x20(%rsp), %rdx
xorl %edi, %edi
movl %r15d, %esi
callq 0x4304
movq %rax, %r15
xorl %edi, %edi
movq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x4304
movq (%rbx), %rcx
movq %rcx, (%r15)
movl $0x1, 0x10(%r15)
andq $0x0, 0x8(%r15)
movb $0x1, %cl
movb %cl, 0x14(%r15)
movq 0x18(%rbx), %rdx
movq %rdx, (%rax)
movl $0x2, 0x10(%rax)
andq $0x0, 0x8(%rax)
movb %cl, 0x14(%rax)
movq 0x10(%rsp), %rdx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
shlq $0x4, %rcx
cmpq %r12, %rcx
je 0x4104
movq %r15, (%rbp,%r12)
movq %rax, 0x8(%rbp,%r12)
addq $0x10, %r12
jmp 0x40ef
leal (,%r13,2), %eax
leal -0x5(,%r13,2), %ecx
movl %ecx, 0x1c(%rsp)
cmpl $0x5, %eax
pushq $0x4
popq %r12
cmovgel %eax, %r12d
movq 0x10(%rsp), %rax
decl %eax
movl %eax, 0x18(%rsp)
addl $-0x4, %r12d
xorl %r15d, %r15d
cmpl %r15d, %r12d
je 0x4169
xorl %eax, %eax
cmpl %r15d, 0x1c(%rsp)
sete %al
movl %eax, (%rsp)
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
movl %r13d, %ecx
leaq 0x20(%rsp), %r8
movl 0x18(%rsp), %r9d
callq 0x41df
incl %r15d
jmp 0x4135
movq 0x38(%rsp), %rax
addq %rbp, %rax
addq $-0x8, %rax
movq 0x40(%rsp), %rdi
addq $0x10, %rdi
movq (%rax), %rax
testq %rax, %rax
je 0x41aa
movq %rdi, %rcx
xorl %edx, %edx
movslq 0x10(%rax), %rsi
cmpq %rsi, %rdx
jge 0x41a4
movslq (%rcx), %rsi
incl (%r14,%rsi,4)
incq %rdx
addq $0x18, %rcx
jmp 0x418b
addq $0x8, %rax
jmp 0x417e
movq %rbp, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
xorl %ebp, %ebp
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq