// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: HuffmanTree_makeFromLengths2
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
{
  uivector blcount;
  uivector nextcode;
  unsigned error = 0;
  unsigned bits, n;

  uivector_init(&blcount);
  uivector_init(&nextcode);

  tree->tree1d = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
  if(!tree->tree1d) error = 83; /*alloc fail*/

  if(!uivector_resizev(&blcount, tree->maxbitlen + 1, 0)
  || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0))
    error = 83; /*alloc fail*/

  if(!error)
  {
    /*step 1: count number of instances of each code length*/
    for(bits = 0; bits != tree->numcodes; ++bits) ++blcount.data[tree->lengths[bits]];
    /*step 2: generate the nextcode values*/
    for(bits = 1; bits <= tree->maxbitlen; ++bits)
    {
      nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1;
    }
    /*step 3: generate all the codes*/
    for(n = 0; n != tree->numcodes; ++n)
    {
      if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++;
    }
  }

  uivector_cleanup(&blcount);
  uivector_cleanup(&nextcode);

  if(!error) return HuffmanTree_make2DTree(tree);
  else return error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
leaq 0x20(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rsp)
movaps %xmm0, (%r15)
movaps %xmm0, (%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x10f0
movq %rax, %r14
movq %rax, 0x8(%rbx)
movl 0x18(%rbx), %esi
incl %esi
movq %r15, %rdi
callq 0x84f1
testl %eax, %eax
je 0x8386
movl 0x18(%rbx), %esi
incl %esi
movq %rsp, %rdi
callq 0x84f1
testq %r14, %r14
je 0x8386
testl %eax, %eax
je 0x8386
xorl %eax, %eax
movq 0x20(%rsp), %rcx
cmpl 0x1c(%rbx), %eax
je 0x83aa
movq 0x10(%rbx), %rdx
movl %eax, %esi
movl (%rdx,%rsi,4), %edx
incl (%rcx,%rdx,4)
incl %eax
jmp 0x8371
leaq 0x20(%rsp), %rdi
callq 0x8526
movq %rsp, %rdi
callq 0x8526
pushq $0x53
popq %rcx
movl %ecx, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq (%rsp), %rax
leal 0x1(%rsi), %edx
cmpl 0x18(%rbx), %edx
ja 0x83cb
movl %esi, %esi
movl (%rcx,%rsi,4), %edi
addl (%rax,%rsi,4), %edi
addl %edi, %edi
movl %edx, %esi
movl %edi, (%rax,%rsi,4)
movl %edx, %esi
jmp 0x83b0
xorl %ecx, %ecx
cmpl 0x1c(%rbx), %ecx
je 0x83f6
movq 0x10(%rbx), %rsi
movl %ecx, %edx
movl (%rsi,%rdx,4), %esi
testq %rsi, %rsi
je 0x83f2
movl (%rax,%rsi,4), %edi
leal 0x1(%rdi), %r8d
movl %r8d, (%rax,%rsi,4)
movq 0x8(%rbx), %rsi
movl %edi, (%rsi,%rdx,4)
incl %ecx
jmp 0x83cd
leaq 0x20(%rsp), %rdi
callq 0x8526
movq %rsp, %rdi
callq 0x8526
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x10f0
movq %rax, (%rbx)
testq %rax, %rax
je 0x8398
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x843c
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
jmp 0x842b
xorl %edx, %edx
pushq $0x37
popq %rcx
xorl %esi, %esi
xorl %edi, %edi
cmpq %r15, %rdx
je 0x84d2
movq 0x10(%rbx), %r8
xorl %r9d, %r9d
movl (%r8,%rdx,4), %r10d
cmpl %r10d, %r9d
je 0x84ca
testl %edi, %edi
js 0x839b
leal 0x2(%rdi), %r11d
cmpl %r15d, %r11d
ja 0x839b
movq 0x8(%rbx), %r11
movl (%r11,%rdx,4), %r11d
movl %r9d, %ebp
notl %ebp
addl %r10d, %ebp
xorl %r12d, %r12d
btl %ebp, %r11d
setb %r12b
leal (%r12,%rdi,2), %r11d
movl (%rax,%r11,4), %edi
cmpl $0x7fff, %edi           # imm = 0x7FFF
jne 0x84b1
incl %r9d
cmpl %r10d, %r9d
jne 0x84b9
movl %edx, (%rax,%r11,4)
xorl %edi, %edi
movl %r10d, %r9d
jmp 0x8455
subl %r15d, %edi
incl %r9d
jmp 0x8455
movl %esi, %edi
incl %edi
addl %r15d, %esi
incl %esi
movl %esi, (%rax,%r11,4)
movl %edi, %esi
jmp 0x8455
incq %rdx
jmp 0x8445
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %r14
je 0x839b
cmpl $0x7fff, (%rax,%rdx,4)  # imm = 0x7FFF
jne 0x84ec
andl $0x0, (%rax,%rdx,4)
incq %rdx
jmp 0x84d6