// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) {
  unsigned* blcount;
  unsigned* nextcode;
  unsigned error = 0;
  unsigned bits, n;

  tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
  blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
  nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
  if(!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/

  if(!error) {
    for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0;
    /*step 1: count number of instances of each code length*/
    for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
    /*step 2: generate the nextcode values*/
    for(bits = 1; bits <= tree->maxbitlen; ++bits) {
      nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u;
    }
    /*step 3: generate all the codes*/
    for(n = 0; n != tree->numcodes; ++n) {
      if(tree->lengths[n] != 0) {
        tree->codes[n] = nextcode[tree->lengths[n]]++;
        /*remove superfluous bits from the code*/
        tree->codes[n] &= ((1u << tree->lengths[n]) - 1u);
      }
    }
  }

  lodepng_free(blcount);
  lodepng_free(nextcode);

  if(!error) error = HuffmanTree_makeTable(tree);
  return error;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x13ce80
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
movl 0x10(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x13ce80
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movl 0x10(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x13ce80
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
je 0x14ca14
cmpq $0x0, 0x18(%rsp)
je 0x14ca14
cmpq $0x0, 0x10(%rsp)
jne 0x14ca1c
movl $0x53, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jne 0x14cb9f
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x20(%rsp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0x14ca6f
movq 0x10(%rsp), %rax
movl 0x4(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movq 0x18(%rsp), %rax
movl 0x4(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x14ca2f
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x14(%rcx), %eax
je 0x14cab0
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0x8(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x14ca77
movl $0x1, 0x8(%rsp)
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x10(%rcx), %eax
ja 0x14cb03
movq 0x10(%rsp), %rax
movl 0x8(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq 0x18(%rsp), %rax
movl 0x8(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
addl (%rax,%rcx,4), %edx
shll %edx
movq 0x10(%rsp), %rax
movl 0x8(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x14cab8
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x14(%rcx), %eax
je 0x14cb9d
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x4(%rsp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x14cb8b
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0x4(%rsp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, (%rax,%rcx,4)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x4(%rsp), %ecx
movl (%rax,%rcx,4), %ecx
movl $0x1, %edx
shll %cl, %edx
subl $0x1, %edx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rsp), %ecx
andl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x14cb8d
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x14cb0b
jmp 0x14cb9f
movq 0x18(%rsp), %rdi
callq 0x13e2d0
movq 0x10(%rsp), %rdi
callq 0x13e2d0
cmpl $0x0, 0xc(%rsp)
jne 0x14cbc8
movq 0x20(%rsp), %rdi
callq 0x14cbe0
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)