// File: /workspace/github_cpp_makefile_star2_O2/mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) /*given that numcodes, lengths and maxbitlen are already filled in correctly. return value is error.*/
{
  uivector blcount;
  uivector nextcode;
  unsigned bits, n, error = 0;
  
  uivector_init(&blcount);
  uivector_init(&nextcode);
  if(!uivector_resize(&tree->tree1d, tree->numcodes)
  || !uivector_resizev(&blcount, tree->maxbitlen + 1, 0)
  || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0))
    error = 9902;
  
  if(!error)
  {
    /*step 1: count number of instances of each code length*/
    for(bits = 0; bits < tree->numcodes; bits++) blcount.data[tree->lengths.data[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.data[n] != 0) tree->tree1d.data[n] = nextcode.data[tree->lengths.data[n]]++;
  }
   
  uivector_cleanup(&blcount);
  uivector_cleanup(&nextcode);
  
  if(!error) return HuffmanTree_make2DTree(tree);
  else return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
andq $0x0, -0x50(%rbp)
xorps %xmm0, %xmm0
andq $0x0, -0x30(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x40(%rbp)
leaq 0x18(%rdi), %r14
movl 0x4c(%rdi), %esi
movq %r14, %rdi
callq 0x16562
testl %eax, %eax
je 0x166c6
movl 0x48(%rbx), %esi
incl %esi
leaq -0x60(%rbp), %rdi
callq 0x16846
testl %eax, %eax
je 0x166c6
movl 0x48(%rbx), %esi
incl %esi
leaq -0x40(%rbp), %rdi
callq 0x16846
testl %eax, %eax
je 0x166c6
movq -0x60(%rbp), %rcx
xorl %eax, %eax
movl 0x4c(%rbx), %edx
cmpq %rdx, %rax
jae 0x166ea
movq 0x30(%rbx), %rdx
movl (%rdx,%rax,4), %edx
incl (%rcx,%rdx,4)
incq %rax
jmp 0x166af
leaq -0x60(%rbp), %rdi
callq 0x16628
leaq -0x40(%rbp), %rdi
callq 0x16628
movl $0x26ae, %eax           # imm = 0x26AE
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq -0x40(%rbp), %rax
leal 0x1(%rsi), %edx
cmpl 0x48(%rbx), %edx
ja 0x1670b
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 0x166f0
xorl %ecx, %ecx
movl 0x4c(%rbx), %edx
cmpq %rdx, %rcx
jae 0x16735
movq 0x30(%rbx), %rdx
movl (%rdx,%rcx,4), %edx
testq %rdx, %rdx
je 0x16730
movl (%rax,%rdx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%rax,%rdx,4)
movq (%r14), %rdx
movl %esi, (%rdx,%rcx,4)
incq %rcx
jmp 0x1670d
leaq -0x60(%rbp), %rdi
callq 0x16628
leaq -0x40(%rbp), %rdi
callq 0x16628
movl 0x4c(%rbx), %esi
addl %esi, %esi
movq %rbx, %rdi
callq 0x16562
testl %eax, %eax
je 0x16814
xorl %eax, %eax
movl 0x4c(%rbx), %edi
leal (%rdi,%rdi), %ecx
cmpq %rcx, %rax
jae 0x16778
movq (%rbx), %rcx
movl $0x7fff, (%rcx,%rax,4)  # imm = 0x7FFF
incq %rax
jmp 0x1675e
xorl %ecx, %ecx
pushq $0x37
popq %rax
xorl %edx, %edx
xorl %esi, %esi
movl %edi, %r8d
cmpq %r8, %rcx
jae 0x1681e
movq 0x30(%rbx), %rdi
xorl %r8d, %r8d
movl (%rdi,%rcx,4), %r9d
cmpl %r9d, %r8d
jae 0x16809
movl 0x4c(%rbx), %r10d
leal -0x2(%r10), %r11d
cmpl %r11d, %esi
ja 0x166dd
movq (%rbx), %r11
movq 0x18(%rbx), %r14
movl (%r14,%rcx,4), %r14d
movl %r8d, %r15d
notl %r15d
addl %r9d, %r15d
xorl %r12d, %r12d
btl %r15d, %r14d
setb %r12b
leal (%r12,%rsi,2), %r14d
movl (%r11,%r14,4), %esi
cmpl $0x7fff, %esi           # imm = 0x7FFF
jne 0x167f0
incl %r8d
cmpl %r9d, %r8d
jne 0x167f8
movl %ecx, (%r11,%r14,4)
xorl %esi, %esi
movl %r9d, %r8d
jmp 0x16794
subl %r10d, %esi
incl %r8d
jmp 0x16794
movl %edx, %esi
incl %esi
addl %r10d, %edx
incl %edx
movl %edx, (%r11,%r14,4)
movl %esi, %edx
jmp 0x16794
incq %rcx
movl 0x4c(%rbx), %edi
jmp 0x16781
movl $0x26ad, %eax           # imm = 0x26AD
jmp 0x166dd
xorl %eax, %eax
xorl %ecx, %ecx
leal (%rdi,%rdi), %edx
cmpq %rdx, %rcx
jae 0x166dd
movq (%rbx), %rdx
cmpl $0x7fff, (%rdx,%rcx,4)  # imm = 0x7FFF
jne 0x16841
andl $0x0, (%rdx,%rcx,4)
movl 0x4c(%rbx), %edi
incq %rcx
jmp 0x16822