// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x4c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcea0
leaq -0x40(%rbp), %rdi
callq 0xcea0
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x3840
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xe244
movl $0x53, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
xorl %edx, %edx
callq 0xe3b0
cmpl $0x0, %eax
je 0xe280
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0xe3b0
cmpl $0x0, %eax
jne 0xe287
movl $0x53, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0xe370
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0xe2ca
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%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 0xe298
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
ja 0xe312
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 0xe2d1
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0xe36e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0xe361
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%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 0x8(%rax), %rax
movl -0x48(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0xe363
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xe319
jmp 0xe370
leaq -0x28(%rbp), %rdi
callq 0xd080
leaq -0x40(%rbp), %rdi
callq 0xd080
cmpl $0x0, -0x4c(%rbp)
jne 0xe396
movq -0x10(%rbp), %rdi
callq 0xe430
movl %eax, -0x4(%rbp)
jmp 0xe39c
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)