// File: 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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x4c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2d6a0
leaq -0x40(%rbp), %rdi
callq 0x2d6a0
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, %esi
callq 0x2d870
cmpl $0x0, %eax
je 0x2da7c
movq -0x10(%rbp), %rax
movl 0x48(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
xorl %edx, %edx
callq 0x2dbb0
cmpl $0x0, %eax
je 0x2da7c
movq -0x10(%rbp), %rax
movl 0x48(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x2dbb0
cmpl $0x0, %eax
jne 0x2da83
movl $0x26ae, -0x4c(%rbp)    # imm = 0x26AE
cmpl $0x0, -0x4c(%rbp)
jne 0x2db6c
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jae 0x2dac6
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x44(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2da94
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x48(%rcx), %eax
ja 0x2db0e
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
addl (%rax,%rcx,4), %edx
shll %edx
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2dacd
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jae 0x2db6a
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x48(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x2db5d
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x48(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x48(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x2db5f
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x2db15
jmp 0x2db6c
leaq -0x28(%rbp), %rdi
callq 0x2d9b0
leaq -0x40(%rbp), %rdi
callq 0x2d9b0
cmpl $0x0, -0x4c(%rbp)
jne 0x2db92
movq -0x10(%rbp), %rdi
callq 0x2dc30
movl %eax, -0x4(%rbp)
jmp 0x2db98
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)