// File: /workspace/github_cpp_makefile_star2_O2/phillip-h[P]fractal-gen/lodepng.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
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 %rbx
subq $0x38, %rsp
vxorps %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %r15
andq $0x0, 0x10(%rsp)
movq %rdi, %rbx
vmovaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
vmovaps %xmm0, (%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x21c0
movq %rax, 0x8(%rbx)
movq %rax, %r14
movq %r15, %rdi
movl 0x18(%rbx), %esi
incl %esi
callq 0xb4e4
testl %eax, %eax
je 0xb384
movl 0x18(%rbx), %esi
movq %rsp, %rdi
incl %esi
callq 0xb4e4
testq %r14, %r14
je 0xb384
testl %eax, %eax
je 0xb384
movq 0x20(%rsp), %rcx
xorl %eax, %eax
cmpl 0x1c(%rbx), %eax
je 0xb3a6
movq 0x10(%rbx), %rdx
movl %eax, %esi
incl %eax
movl (%rdx,%rsi,4), %edx
incl (%rcx,%rdx,4)
jmp 0xb36f
leaq 0x20(%rsp), %rdi
callq 0xb519
movq %rsp, %rdi
callq 0xb519
pushq $0x53
popq %rcx
movl %ecx, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
xorl %esi, %esi
leal 0x1(%rsi), %edx
cmpl 0x18(%rbx), %edx
ja 0xb3c7
movl %esi, %esi
movl (%rcx,%rsi,4), %edi
addl (%rax,%rsi,4), %edi
movl %edx, %esi
addl %edi, %edi
movl %edi, (%rax,%rsi,4)
movl %edx, %esi
jmp 0xb3ac
xorl %ecx, %ecx
cmpl 0x1c(%rbx), %ecx
je 0xb3f2
movq 0x10(%rbx), %rsi
movl %ecx, %edx
movl (%rsi,%rdx,4), %esi
testq %rsi, %rsi
je 0xb3ee
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 0xb3c9
leaq 0x20(%rsp), %rdi
callq 0xb519
movq %rsp, %rdi
callq 0xb519
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x21c0
movq %rax, (%rbx)
testq %rax, %rax
je 0xb396
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0xb438
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
jmp 0xb427
xorl %edx, %edx
pushq $0x37
popq %rcx
xorl %esi, %esi
xorl %edi, %edi
cmpq %r15, %rdx
je 0xb4c5
movq 0x10(%rbx), %r8
xorl %r9d, %r9d
movl (%r8,%rdx,4), %r10d
cmpl %r10d, %r9d
je 0xb4bd
testl %edi, %edi
js 0xb399
leal 0x2(%rdi), %r11d
cmpl %r15d, %r11d
ja 0xb399
movq 0x8(%rbx), %r11
movl %r9d, %ebp
notl %ebp
addl %r10d, %ebp
shrxl %ebp, (%r11,%rdx,4), %r11d
andl $0x1, %r11d
leal (%r11,%rdi,2), %r11d
movl (%rax,%r11,4), %edi
cmpl $0x7fff, %edi           # imm = 0x7FFF
jne 0xb4a4
incl %r9d
cmpl %r10d, %r9d
jne 0xb4ac
movl %edx, (%rax,%r11,4)
xorl %edi, %edi
movl %r10d, %r9d
jmp 0xb44d
subl %r15d, %edi
incl %r9d
jmp 0xb44d
movl %esi, %edi
leal 0x1(%rsi,%r15), %esi
incl %edi
movl %esi, (%rax,%r11,4)
movl %edi, %esi
jmp 0xb44d
incq %rdx
jmp 0xb441
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %r14
je 0xb399
cmpl $0x7fff, (%rax,%rdx,4)  # imm = 0x7FFF
jne 0xb4df
andl $0x0, (%rax,%rdx,4)
incq %rdx
jmp 0xb4c9