// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: lodepng_huffman_code_lengths
unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
                                      size_t numcodes, unsigned maxbitlen)
{
  unsigned error = 0;
  unsigned i;
  size_t numpresent = 0; /*number of symbols with non-zero frequency*/
  BPMNode* leaves; /*the symbols, only those with > 0 frequency*/

  if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
  if((1u << maxbitlen) < numcodes) return 80; /*error: represent all symbols*/

  leaves = (BPMNode*)lodepng_malloc(numcodes * sizeof(*leaves));
  if(!leaves) return 83; /*alloc fail*/

  for(i = 0; i != numcodes; ++i)
  {
    if(frequencies[i] > 0)
    {
      leaves[numpresent].weight = (int)frequencies[i];
      leaves[numpresent].index = i;
      ++numpresent;
    }
  }

  for(i = 0; i != numcodes; ++i) lengths[i] = 0;

  /*ensure at least two present symbols. There should be at least one symbol
  according to RFC 1951 section 3.2.7. Some decoders incorrectly require two. To
  make these work as well ensure there are at least two symbols. The
  Package-Merge code below also doesn't work correctly if there's only one
  symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/
  if(numpresent == 0)
  {
    lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/
  }
  else if(numpresent == 1)
  {
    lengths[leaves[0].index] = 1;
    lengths[leaves[0].index == 0 ? 1 : 0] = 1;
  }
  else
  {
    BPMLists lists;
    BPMNode* node;

    bpmnode_sort(leaves, numpresent);

    lists.listsize = maxbitlen;
    lists.memsize = 2 * maxbitlen * (maxbitlen + 1);
    lists.nextfree = 0;
    lists.numfree = lists.memsize;
    lists.memory = (BPMNode*)lodepng_malloc(lists.memsize * sizeof(*lists.memory));
    lists.freelist = (BPMNode**)lodepng_malloc(lists.memsize * sizeof(BPMNode*));
    lists.chains0 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
    lists.chains1 = (BPMNode**)lodepng_malloc(lists.listsize * sizeof(BPMNode*));
    if(!lists.memory || !lists.freelist || !lists.chains0 || !lists.chains1) error = 83; /*alloc fail*/

    if(!error)
    {
      for(i = 0; i != lists.memsize; ++i) lists.freelist[i] = &lists.memory[i];

      bpmnode_create(&lists, leaves[0].weight, 1, 0);
      bpmnode_create(&lists, leaves[1].weight, 2, 0);

      for(i = 0; i != lists.listsize; ++i)
      {
        lists.chains0[i] = &lists.memory[0];
        lists.chains1[i] = &lists.memory[1];
      }

      /*each boundaryPM call adds one chain to the last list, and we need 2 * numpresent - 2 chains.*/
      for(i = 2; i != 2 * numpresent - 2; ++i) boundaryPM(&lists, leaves, numpresent, (int)maxbitlen - 1, (int)i);

      for(node = lists.chains1[maxbitlen - 1]; node; node = node->tail)
      {
        for(i = 0; i != node->index; ++i) ++lengths[leaves[i].index];
      }
    }

    lodepng_free(lists.memory);
    lodepng_free(lists.freelist);
    lodepng_free(lists.chains0);
    lodepng_free(lists.chains1);
  }

  lodepng_free(leaves);
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x14fc
movl $0x50, -0x4(%rbp)
jmp 0x1847
movl -0x24(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
cmpq -0x20(%rbp), %rax
jae 0x151a
movl $0x50, -0x4(%rbp)
jmp 0x1847
imulq $0x18, -0x20(%rbp), %rdi
callq 0x13b0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x153b
movl $0x53, -0x4(%rbp)
jmp 0x1847
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpq -0x20(%rbp), %rax
je 0x159b
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jbe 0x158e
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
movq -0x40(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x40(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1590
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1542
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpq -0x20(%rbp), %rax
je 0x15c4
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x15a2
cmpq $0x0, -0x38(%rbp)
jne 0x15e5
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
jmp 0x1838
cmpq $0x1, -0x38(%rbp)
jne 0x1625
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movl 0x4(%rcx), %esi
xorl %ecx, %ecx
movl $0x1, %edx
cmpl $0x0, %esi
cmovel %edx, %ecx
movslq %ecx, %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x1836
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movl -0x24(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x24(%rbp), %eax
shll %eax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
movl %eax, -0x78(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x78(%rbp), %eax
imulq $0x18, %rax, %rdi
callq 0x13b0
movq %rax, -0x70(%rbp)
movl -0x78(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x13b0
movq %rax, -0x60(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x13b0
movq %rax, -0x50(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x13b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x16b8
cmpq $0x0, -0x60(%rbp)
je 0x16b8
cmpq $0x0, -0x50(%rbp)
je 0x16b8
cmpq $0x0, -0x48(%rbp)
jne 0x16bf
movl $0x53, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x1812
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x78(%rbp), %eax
je 0x16fc
movq -0x70(%rbp), %rdx
movl -0x2c(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdx
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x16d0
movq -0x40(%rbp), %rax
movl (%rax), %esi
leaq -0x78(%rbp), %rdi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1ac0
movq -0x40(%rbp), %rax
movl 0x18(%rax), %esi
leaq -0x78(%rbp), %rdi
movl $0x2, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1ac0
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x58(%rbp), %eax
je 0x1769
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rdx
addq $0x18, %rdx
movq -0x48(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1734
movl $0x2, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x38(%rbp), %rcx
shlq %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
je 0x17a9
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movl -0x2c(%rbp), %r8d
leaq -0x78(%rbp), %rdi
callq 0x1c70
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1770
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x1810
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x1800
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x2c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x17cb
jmp 0x1802
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x17bd
jmp 0x1812
movq -0x70(%rbp), %rdi
callq 0x1e60
movq -0x60(%rbp), %rdi
callq 0x1e60
movq -0x50(%rbp), %rdi
callq 0x1e60
movq -0x48(%rbp), %rdi
callq 0x1e60
jmp 0x1838
movq -0x40(%rbp), %rdi
callq 0x1e60
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)