// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
{
  uivector blcount;
  uivector nextcode;
  unsigned error = 0;
  unsigned bits, n;

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

  tree->tree1d = (unsigned*)lodepng_malloc(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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x13160
leaq 0x18(%rsp), %rdi
callq 0x13160
movq 0x48(%rsp), %rax
movl 0x1c(%rax), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x6cd0
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x12fc8
movl $0x53, 0x14(%rsp)
movq 0x48(%rsp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x30(%rsp), %rdi
xorl %edx, %edx
callq 0x13190
cmpl $0x0, %eax
je 0x13008
movq 0x48(%rsp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x18(%rsp), %rdi
xorl %edx, %edx
callq 0x13190
cmpl $0x0, %eax
jne 0x13010
movl $0x53, 0x14(%rsp)
cmpl $0x0, 0x14(%rsp)
jne 0x13119
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
je 0x1305c
movq 0x30(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x13023
movl $0x1, 0x10(%rsp)
movl 0x10(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x18(%rcx), %eax
ja 0x130af
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq 0x30(%rsp), %rax
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
addl (%rax,%rcx,4), %edx
shll %edx
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x13064
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
je 0x13117
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xc(%rsp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x13108
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0xc(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, (%rax,%rcx,4)
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movl 0xc(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x1310a
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x130b7
jmp 0x13119
leaq 0x30(%rsp), %rdi
callq 0x13220
leaq 0x18(%rsp), %rdi
callq 0x13220
cmpl $0x0, 0x14(%rsp)
jne 0x13144
movq 0x48(%rsp), %rdi
callq 0x13260
movl %eax, 0x54(%rsp)
jmp 0x1314c
movl 0x14(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)