// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
addq $0x18, %rdi
movl 0x4c(%rbx), %esi
callq 0x22340
testl %eax, %eax
je 0x22528
movl 0x48(%rbx), %r13d
leal 0x1(%r13), %r12d
movl %r13d, %eax
incl %eax
jne 0x2244c
movq %r12, -0x30(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0x224a2
leaq (,%r12,8), %r15
movq %r15, %rdi
callq 0x4410
testq %rax, %rax
je 0x22528
movq %rax, %r14
movq %r12, -0x30(%rbp)
leaq (,%r12,4), %r12
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4170
movq %r15, %rdi
callq 0x4410
testq %rax, %rax
je 0x2252b
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4170
movl 0x4c(%rbx), %r12d
testq %r12, %r12
je 0x224c0
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
incl (%r14,%rdx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x224b1
testl %r13d, %r13d
je 0x224ef
movq -0x30(%rbp), %rcx
cmpl $0x3, %ecx
movl $0x2, %eax
cmovael %ecx, %eax
movl (%r15), %ecx
decq %rax
xorl %edx, %edx
addl (%r14,%rdx,4), %ecx
addl %ecx, %ecx
movl %ecx, 0x4(%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x224dc
testl %r12d, %r12d
je 0x2253d
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
testq %rdx, %rdx
je 0x22518
movl (%r15,%rdx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%r15,%rdx,4)
movq 0x18(%rbx), %rdx
movl %esi, (%rdx,%rcx,4)
movl 0x4c(%rbx), %r12d
incq %rcx
movl %r12d, %edx
cmpq %rdx, %rcx
jb 0x224fa
addl %r12d, %r12d
jmp 0x22540
xorl %r14d, %r14d
movq %r14, %rdi
callq 0x4380
movl $0x26ae, %eax           # imm = 0x26AE
jmp 0x2266d
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x4380
movq %r15, %rdi
callq 0x4380
movq %rbx, %rdi
movq %r12, %rsi
callq 0x22340
testl %eax, %eax
je 0x2265d
movl 0x4c(%rbx), %edi
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
je 0x22588
movq (%rbx), %rax
xorl %ecx, %ecx
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
movl 0x4c(%rbx), %edi
leal (%rdi,%rdi), %edx
cmpq %rdx, %rcx
jb 0x22573
testl %edi, %edi
je 0x2266b
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax,%rcx,4), %r8d
testl %r8d, %r8d
je 0x2261f
xorl %edi, %edi
movl 0x4c(%rbx), %r9d
leal -0x2(%r9), %r10d
cmpl %r10d, %esi
ja 0x22664
movq (%rbx), %r10
movq 0x18(%rbx), %r11
movl (%r11,%rcx,4), %r11d
movl %edi, %r14d
notl %r14d
addl %r8d, %r14d
xorl %r15d, %r15d
btl %r14d, %r11d
setb %r15b
leal (%r15,%rsi,2), %r11d
movl (%r10,%r11,4), %esi
cmpl $0x7fff, %esi           # imm = 0x7FFF
jne 0x225f4
incl %edi
cmpl %r8d, %edi
jne 0x225fe
movl %ecx, (%r10,%r11,4)
xorl %esi, %esi
jmp 0x22610
subl %r9d, %esi
incl %edi
movl %edi, %r8d
jmp 0x22610
movl %edx, %esi
incl %esi
addl %r9d, %edx
incl %edx
movl %edx, (%r10,%r11,4)
movl %edi, %r8d
movl %esi, %edx
movl %r8d, %edi
movl (%rax,%rcx,4), %r8d
cmpl %r8d, %edi
jb 0x225a5
movl 0x4c(%rbx), %edi
incq %rcx
movl %edi, %r8d
cmpq %r8, %rcx
jb 0x2259a
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
je 0x2266b
movq (%rbx), %rcx
xorl %edx, %edx
xorl %eax, %eax
cmpl $0x7fff, (%rcx,%rdx,4)  # imm = 0x7FFF
jne 0x22650
movl $0x0, (%rcx,%rdx,4)
movl 0x4c(%rbx), %edi
incq %rdx
leal (%rdi,%rdi), %esi
cmpq %rsi, %rdx
jb 0x2263d
jmp 0x2266d
movl $0x26ad, %eax           # imm = 0x26AD
jmp 0x2266d
movl $0x37, %eax
jmp 0x2266d
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq