// File: /workspace/github_cpp_makefile_star2_O2/drummyfish[P]flowchart/src/lodepng.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
{
  uivector blcount;
  uivector nextcode;
  unsigned bits, n, error = 0;

  uivector_init(&blcount);
  uivector_init(&nextcode);

  tree->tree1d = (unsigned*)mymalloc(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 %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x20(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rsp)
movaps %xmm0, (%r15)
movaps %xmm0, (%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x2260
movq %rax, %r14
movq %rax, 0x8(%rbx)
movl 0x18(%rbx), %esi
incl %esi
movq %r15, %rdi
callq 0x969c
testl %eax, %eax
je 0x9533
movl 0x18(%rbx), %esi
incl %esi
movq %rsp, %rdi
callq 0x969c
testq %r14, %r14
je 0x9533
testl %eax, %eax
je 0x9533
movq 0x20(%rsp), %rcx
xorl %eax, %eax
movl 0x1c(%rbx), %edx
cmpq %rdx, %rax
jae 0x9559
movq 0x10(%rbx), %rdx
movl (%rdx,%rax,4), %edx
incl (%rcx,%rdx,4)
incq %rax
jmp 0x951c
leaq 0x20(%rsp), %rdi
callq 0x9241
movq %rsp, %rdi
callq 0x9241
pushq $0x53
popq %rcx
movl %ecx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq (%rsp), %rax
leal 0x1(%rsi), %edx
cmpl 0x18(%rbx), %edx
ja 0x957a
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 0x955f
xorl %ecx, %ecx
movl 0x1c(%rbx), %edx
cmpq %rdx, %rcx
jae 0x95a5
movq 0x10(%rbx), %rdx
movl (%rdx,%rcx,4), %edx
testq %rdx, %rdx
je 0x95a0
movl (%rax,%rdx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%rax,%rdx,4)
movq 0x8(%rbx), %rdx
movl %esi, (%rdx,%rcx,4)
incq %rcx
jmp 0x957c
leaq 0x20(%rsp), %rdi
callq 0x9241
movq %rsp, %rdi
callq 0x9241
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x2260
movq %rax, (%rbx)
testq %rax, %rax
je 0x9545
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x95eb
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
jmp 0x95da
leal -0x2(%r15), %edx
xorl %esi, %esi
pushq $0x37
popq %rcx
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %r15, %rsi
je 0x967d
movq 0x10(%rbx), %r9
xorl %r10d, %r10d
movl (%r9,%rsi,4), %r11d
cmpl %r11d, %r10d
jae 0x9675
cmpl %edx, %r8d
ja 0x9548
movq 0x8(%rbx), %r12
movl (%r12,%rsi,4), %ebp
movl %r10d, %r12d
notl %r12d
addl %r11d, %r12d
xorl %r13d, %r13d
btl %r12d, %ebp
setb %r13b
leal (%r13,%r8,2), %r12d
movl (%rax,%r12,4), %r8d
cmpl $0x7fff, %r8d           # imm = 0x7FFF
jne 0x9659
incl %r10d
cmpl %r11d, %r10d
jne 0x9661
movl %esi, (%rax,%r12,4)
xorl %r8d, %r8d
movl %r11d, %r10d
jmp 0x9605
subl %r15d, %r8d
incl %r10d
jmp 0x9605
movl %edi, %r8d
incl %r8d
addl %r15d, %edi
incl %edi
movl %edi, (%rax,%r12,4)
movl %r8d, %edi
jmp 0x9605
incq %rsi
jmp 0x95f9
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %r14
je 0x9548
cmpl $0x7fff, (%rax,%rdx,4)  # imm = 0x7FFF
jne 0x9697
andl $0x0, (%rax,%rdx,4)
incq %rdx
jmp 0x9681