// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x18(%rdi), %r14
movl 0x4c(%rdi), %esi
movq %r14, %rdi
callq 0x236c7
movl $0x26ae, %r15d          # imm = 0x26AE
testl %eax, %eax
je 0x237d3
movl 0x48(%rbx), %eax
leal 0x1(%rax), %r15d
incl %eax
jne 0x237e0
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x237f7
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %eax, %eax
jmp 0x23894
leaq (,%r15,8), %rdi
callq 0x5420
movq %rax, %r12
testq %rax, %rax
sete %r13b
leaq (,%r15,4), %rdx
testq %r15, %r15
sete %al
orb %r13b, %al
movq %r15, -0x38(%rbp)
jne 0x23822
movq %r12, %rdi
xorl %esi, %esi
movq %rdx, %r15
callq 0x5170
movq %r15, %rdx
movq -0x38(%rbp), %r15
testb %r13b, %r13b
je 0x23834
xorl %r13d, %r13d
xorl %eax, %eax
movl $0x26ae, %r15d          # imm = 0x26AE
jmp 0x23894
testq %r15, %r15
je 0x23857
leaq (,%r15,8), %rdi
movq %rdx, %r15
callq 0x5420
movq %r15, %rdx
movq %rax, %r13
testq %rax, %rax
sete %al
jmp 0x2385c
xorl %r13d, %r13d
xorl %eax, %eax
movl $0x26ae, -0x2c(%rbp)    # imm = 0x26AE
testb %al, %al
jne 0x2388e
cmpq $0x0, -0x38(%rbp)
je 0x23887
movl $0x0, -0x2c(%rbp)
movq %r13, %rdi
xorl %esi, %esi
movq %rax, %r15
callq 0x5170
movq %r15, %rax
jmp 0x2388e
movl $0x0, -0x2c(%rbp)
xorb $0x1, %al
movl -0x2c(%rbp), %r15d
testb %al, %al
je 0x23a61
movq %rax, -0x38(%rbp)
cmpl $0x0, 0x4c(%rbx)
je 0x238be
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
incl (%r12,%rdx,4)
incq %rcx
movl 0x4c(%rbx), %edx
cmpq %rdx, %rcx
jb 0x238ac
cmpl $0x0, 0x48(%rbx)
je 0x238e5
movl $0x1, %eax
leal -0x1(%rax), %ecx
movl (%r12,%rcx,4), %edx
addl (%r13,%rcx,4), %edx
addl %edx, %edx
movl %eax, %ecx
movl %edx, (%r13,%rcx,4)
incl %eax
cmpl 0x48(%rbx), %eax
jbe 0x238c9
cmpl $0x0, 0x4c(%rbx)
je 0x23917
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
testq %rdx, %rdx
je 0x2390c
movl (%r13,%rdx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%r13,%rdx,4)
movq (%r14), %rdx
movl %esi, (%rdx,%rcx,4)
incq %rcx
movl 0x4c(%rbx), %edx
cmpq %rdx, %rcx
jb 0x238f1
movq %r12, %rdi
callq 0x5390
movq %r13, %rdi
callq 0x5390
cmpb $0x0, -0x38(%rbp)
je 0x23a71
movl 0x4c(%rbx), %esi
addl %esi, %esi
movq %rbx, %rdi
callq 0x236c7
testl %eax, %eax
je 0x23a83
testl $0x7fffffff, 0x4c(%rbx) # imm = 0x7FFFFFFF
je 0x23968
movq (%rbx), %rax
xorl %ecx, %ecx
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
movl 0x4c(%rbx), %edx
addl %edx, %edx
cmpq %rdx, %rcx
jb 0x23954
cmpl $0x0, 0x4c(%rbx)
je 0x23a31
movq 0x30(%rbx), %rax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax,%rcx,4), %r9d
testl %r9d, %r9d
je 0x23a1c
movl $0x1, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl 0x4c(%rbx), %r14d
leal -0x2(%r14), %r11d
cmpl %r11d, %esi
jbe 0x239a6
movl %esi, %r10d
jmp 0x239f7
movq (%rbx), %r15
movq 0x18(%rbx), %r10
movl (%r10,%rcx,4), %r10d
leal (%r9,%r8), %r12d
xorl %r13d, %r13d
btl %r12d, %r10d
setb %r13b
leal (%r13,%rsi,2), %r12d
movl (%r15,%r12,4), %r10d
cmpl $0x7fff, %r10d          # imm = 0x7FFF
jne 0x239e0
cmpl %r9d, %edi
jne 0x239e5
movl %ecx, (%r15,%r12,4)
xorl %r10d, %r10d
jmp 0x239f7
subl %r14d, %r10d
jmp 0x239f7
movl %edx, %r10d
incl %r10d
addl %r14d, %edx
incl %edx
movl %edx, (%r15,%r12,4)
movl %r10d, %edx
cmpl %r11d, %esi
ja 0x23a8b
movl (%rax,%rcx,4), %r9d
leal 0x1(%rdi), %r11d
decl %r8d
movl %r10d, %esi
cmpl %r9d, %edi
movl %r11d, %edi
jb 0x23994
jmp 0x23a1f
movl %esi, %r10d
incq %rcx
movl 0x4c(%rbx), %edi
movl %r10d, %esi
cmpq %rdi, %rcx
jb 0x2397c
testl $0x7fffffff, 0x4c(%rbx) # imm = 0x7FFFFFFF
je 0x23a5c
movq (%rbx), %rax
xorl %ecx, %ecx
cmpl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
jne 0x23a4f
movl $0x0, (%rax,%rcx,4)
incq %rcx
movl 0x4c(%rbx), %edx
addl %edx, %edx
cmpq %rdx, %rcx
jb 0x23a3f
xorl %r15d, %r15d
jmp 0x23a71
movq %r12, %rdi
callq 0x5390
movq %r13, %rdi
callq 0x5390
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x26ad, %r15d          # imm = 0x26AD
jmp 0x23a71
movl $0x37, %r15d
jmp 0x23a71