// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x14(%rdi), %ebp
leaq (,%rbp,4), %rdi
callq 0x525b0
movq %rax, (%rsp)
movq %rax, (%rbx)
movl 0x10(%rbx), %r14d
leal 0x1(%r14), %r12d
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x525b0
movq %rax, %r13
movq %r15, %rdi
callq 0x525b0
movq (%rsp), %r9
movq %rax, %r15
testq %r9, %r9
je 0x7c3e7
testq %r13, %r13
je 0x7c3e7
testq %r15, %r15
je 0x7c3e7
xorl %eax, %eax
cmpq %rax, %r12
je 0x7c3f4
andl $0x0, (%r15,%rax,4)
andl $0x0, (%r13,%rax,4)
incq %rax
jmp 0x7c3d2
movq %r13, %rdi
callq 0x52530
jmp 0x7c53f
xorl %eax, %eax
cmpq %rax, %rbp
je 0x7c40c
movq 0x8(%rbx), %rcx
movl (%rcx,%rax,4), %ecx
incl (%r13,%rcx,4)
incq %rax
jmp 0x7c3f6
pushq $0x1
popq %rax
cmpq %r14, %rax
ja 0x7c429
movl -0x4(%r13,%rax,4), %ecx
addl -0x4(%r15,%rax,4), %ecx
addl %ecx, %ecx
movl %ecx, (%r15,%rax,4)
incq %rax
jmp 0x7c40f
xorl %eax, %eax
pushq $-0x1
popq %rdx
cmpq %rax, %rbp
je 0x7c463
movq 0x8(%rbx), %rcx
movl (%rcx,%rax,4), %esi
testq %rsi, %rsi
je 0x7c45e
movl (%r15,%rsi,4), %edi
leal 0x1(%rdi), %r8d
movl %r8d, (%r15,%rsi,4)
movl %edi, (%r9,%rax,4)
movb (%rcx,%rax,4), %cl
movl %edx, %esi
shll %cl, %esi
notl %esi
andl %edi, %esi
movl %esi, (%r9,%rax,4)
incq %rax
jmp 0x7c42e
movq %r13, %rdi
callq 0x52530
movq %r15, %rdi
callq 0x52530
movl $0x800, %edi            # imm = 0x800
callq 0x525b0
testq %rax, %rax
je 0x7c547
movq %rax, %r15
xorl %eax, %eax
cmpq $0x200, %rax            # imm = 0x200
je 0x7c49d
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x7c48b
xorl %r14d, %r14d
pushq $0x9
popq %r13
cmpq %r14, %rbp
je 0x7c4e4
movq 0x8(%rbx), %rax
movl (%rax,%r14,4), %r12d
cmpl $0xa, %r12d
jb 0x7c4df
movq (%rsp), %rax
movl (%rax,%r14,4), %edi
leal -0x9(%r12), %ecx
shrl %cl, %edi
movl %r13d, %esi
callq 0x7c6e9
movl %eax, %eax
movl (%r15,%rax,4), %ecx
cmpl %r12d, %ecx
cmoval %ecx, %r12d
movl %r12d, (%r15,%rax,4)
incq %r14
jmp 0x7c4a4
movl $0x200, %r12d           # imm = 0x200
xorl %eax, %eax
pushq $0x1
popq %rbp
xorl %edx, %edx
cmpq $0x200, %rdx            # imm = 0x200
je 0x7c513
movl (%r15,%rdx,4), %esi
leal -0x9(%rsi), %ecx
movl %ebp, %edi
shll %cl, %edi
cmpl $0xa, %esi
cmovbl %eax, %edi
addq %rdi, %r12
incq %rdx
jmp 0x7c4f1
movq %r12, %rdi
callq 0x525b0
movq %rax, %r13
movq %rax, 0x18(%rbx)
leaq (%r12,%r12), %rdi
callq 0x525b0
movq %rax, 0x20(%rbx)
testq %r13, %r13
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x7c55b
movq %r15, %rdi
callq 0x52530
pushq $0x53
popq %rsi
movl %esi, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
cmpq %rax, %r12
je 0x7c56f
movq 0x18(%rbx), %rcx
movb $0x10, (%rcx,%rax)
incq %rax
jmp 0x7c55d
movw $0x200, %dx             # imm = 0x200
xorl %eax, %eax
cmpq $0x200, %rax            # imm = 0x200
je 0x7c5a5
movl (%r15,%rax,4), %ecx
cmpl $0xa, %ecx
jb 0x7c5a0
movq 0x18(%rbx), %rsi
movb %cl, (%rsi,%rax)
addb $-0x9, %cl
movl %ebp, %esi
shll %cl, %esi
movq 0x20(%rbx), %rcx
movw %dx, (%rcx,%rax,2)
addl %edx, %esi
movl %esi, %edx
incq %rax
jmp 0x7c575
movq %r15, %rdi
callq 0x52530
xorl %r15d, %r15d
pushq $0x37
popq %rbp
xorl %r13d, %r13d
movl 0x14(%rbx), %eax
cmpq %rax, %r13
jae 0x7c68d
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl (%rcx,%r13,4), %r14d
movl (%rax,%r13,4), %edi
movl %r14d, %esi
callq 0x7c6e9
testl %r14d, %r14d
je 0x7c685
incq %r15
cmpl $0x9, %r14d
ja 0x7c629
pushq $0x9
popq %rdx
subl %r14d, %edx
xorl %esi, %esi
movl %esi, %edi
movl %edx, %ecx
shrl %cl, %edi
testl %edi, %edi
jne 0x7c685
movl %esi, %edi
movl %r14d, %ecx
shll %cl, %edi
orl %eax, %edi
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%rdi)
jne 0x7c6e2
movb %r14b, (%rcx,%rdi)
movq 0x20(%rbx), %rcx
movw %r13w, (%rcx,%rdi,2)
incl %esi
jmp 0x7c5f3
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movq 0x18(%rbx), %rdx
movzbl (%rdx,%rcx), %edx
subl %r14d, %edx
jb 0x7c6e2
movq 0x20(%rbx), %rsi
movzwl (%rsi,%rcx,2), %edi
leal -0x9(%r14), %esi
shrl $0x9, %eax
xorl %r8d, %r8d
movl %r8d, %r9d
movl %edx, %ecx
shrl %cl, %r9d
testl %r9d, %r9d
jne 0x7c685
movl %r8d, %r9d
movl %esi, %ecx
shll %cl, %r9d
orl %eax, %r9d
addl %edi, %r9d
movq 0x18(%rbx), %rcx
movb %r14b, (%rcx,%r9)
movq 0x20(%rbx), %rcx
movw %r13w, (%rcx,%r9,2)
incl %r8d
jmp 0x7c654
incq %r13
jmp 0x7c5b6
xorl %esi, %esi
xorl %eax, %eax
cmpq $0x2, %r15
jae 0x7c6cb
cmpq %rax, %r12
je 0x7c54a
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%rax)
jne 0x7c6c6
xorl %edx, %edx
cmpq $0x200, %rax            # imm = 0x200
setae %dl
leal (%rdx,%rdx,8), %edx
incl %edx
movb %dl, (%rcx,%rax)
movq 0x20(%rbx), %rcx
orw $-0x1, (%rcx,%rax,2)
incq %rax
jmp 0x7c697
cmpq %rax, %r12
je 0x7c54a
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%rax)
leaq 0x1(%rax), %rax
jne 0x7c6cb
movl %ebp, %esi
jmp 0x7c54a