// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x14(%rdi), %ebp
leaq (,%rbp,4), %rdi
callq 0x3a5a0
movq %rax, %r15
movq %rax, (%rbx)
movl 0x10(%rbx), %eax
movq %rax, 0x10(%rsp)
leal 0x1(%rax), %r13d
leaq (,%r13,4), %r14
movq %r14, %rdi
callq 0x3a5a0
movq %rax, %r12
movq %r14, %rdi
callq 0x3a5a0
movq %rax, %r14
testq %r15, %r15
setne %al
testq %r12, %r12
setne %cl
andb %al, %cl
testq %r14, %r14
setne %sil
xorl %eax, %eax
movl $0x53, %edx
andb %cl, %sil
movb %sil, 0x3(%rsp)
cmovnel %eax, %edx
movl %edx, 0x4(%rsp)
je 0x706d0
testl %r13d, %r13d
je 0x7037d
movq %r13, 0x8(%rsp)
movl 0x8(%rsp), %r13d
shlq $0x2, %r13
movq %r14, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3a260
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq 0x8(%rsp), %r13
callq 0x3a260
testq %rbp, %rbp
je 0x70397
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
incl (%r12,%rdx,4)
incq %rcx
cmpq %rcx, %rbp
jne 0x70388
cmpl $0x0, 0x10(%rsp)
je 0x703c6
cmpl $0x3, %r13d
movl $0x2, %eax
cmovael %r13d, %eax
movl (%r14), %ecx
decq %rax
xorl %edx, %edx
addl (%r12,%rdx,4), %ecx
addl %ecx, %ecx
movl %ecx, 0x4(%r14,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x703b3
movl 0x14(%rbx), %r13d
testq %r13, %r13
movl 0x4(%rsp), %ebp
je 0x7040a
movq 0x8(%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %ecx
testq %rcx, %rcx
je 0x70402
movl (%r14,%rcx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%r14,%rcx,4)
movl %esi, (%r15,%rdx,4)
movb (%rax,%rdx,4), %cl
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
shll %cl, %edi
notl %edi
andl %esi, %edi
movl %edi, (%r15,%rdx,4)
incq %rdx
cmpq %rdx, %r13
jne 0x703d9
movq %r12, %rdi
callq 0x3a530
movq %r14, %rdi
callq 0x3a530
cmpb $0x0, 0x3(%rsp)
je 0x706e4
movl $0x800, %edi            # imm = 0x800
callq 0x3a5a0
testq %rax, %rax
je 0x706f5
movq %rax, %r14
xorl %ebp, %ebp
movl $0x800, %edx            # imm = 0x800
movq %rax, %rdi
xorl %esi, %esi
callq 0x3a260
testq %r13, %r13
je 0x704a6
movq 0x8(%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpl $0xa, %esi
jb 0x7049e
movl (%r15,%rdx,4), %edi
leal -0x9(%rsi), %ecx
shrl %cl, %edi
xorl %r8d, %r8d
movl $0x8, %r9d
xorl %ecx, %ecx
xorl %r10d, %r10d
btl %r9d, %edi
setb %r10b
shll %cl, %r10d
orl %r10d, %r8d
incl %ecx
decl %r9d
cmpl $0x9, %ecx
jne 0x70473
movl %r8d, %ecx
movl (%r14,%rcx,4), %edi
cmpl %esi, %edi
cmoval %edi, %esi
movl %esi, (%r14,%rcx,4)
incq %rdx
cmpq %r13, %rdx
jne 0x70457
movl $0x200, %r15d           # imm = 0x200
xorl %eax, %eax
movl (%r14,%rax,4), %edx
leal -0x9(%rdx), %ecx
movl $0x1, %r12d
shll %cl, %r12d
cmpl $0xa, %edx
cmovbl %ebp, %r12d
movq %r15, %r13
addq %r12, %r15
incq %rax
cmpq $0x200, %rax            # imm = 0x200
jne 0x704ae
movq %r15, %rdi
callq 0x3a5a0
movq %rax, %rbp
movq %rax, 0x18(%rbx)
leaq (%r15,%r15), %rdi
callq 0x3a5a0
movq %rax, 0x20(%rbx)
testq %rbp, %rbp
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
movl $0x53, %ebp
jne 0x706dc
testq %r15, %r15
je 0x70528
movl %r12d, %eax
addq %r13, %rax
xorl %ecx, %ecx
movq 0x18(%rbx), %rdx
movb $0x10, (%rdx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x70518
movl $0x200, %eax            # imm = 0x200
xorl %edx, %edx
movl (%r14,%rdx,4), %ecx
cmpl $0xa, %ecx
jb 0x70554
movq 0x18(%rbx), %rsi
movb %cl, (%rsi,%rdx)
movq 0x20(%rbx), %rsi
movw %ax, (%rsi,%rdx,2)
addb $-0x9, %cl
movl $0x1, %esi
shll %cl, %esi
addq %rsi, %rax
incq %rdx
cmpq $0x200, %rdx            # imm = 0x200
jne 0x7052f
movq %r14, %rdi
callq 0x3a530
cmpl $0x0, 0x14(%rbx)
je 0x7068c
xorl %edi, %edi
xorl %r8d, %r8d
movq 0x8(%rbx), %rax
movl (%rax,%rdi,4), %eax
movl $0x0, %r9d
testl %eax, %eax
je 0x705ad
movq (%rbx), %rcx
movl (%rcx,%rdi,4), %edx
leal -0x1(%rax), %esi
xorl %r9d, %r9d
xorl %ecx, %ecx
xorl %r10d, %r10d
btl %esi, %edx
setb %r10b
shll %cl, %r10d
orl %r10d, %r9d
incl %ecx
addl $-0x1, %esi
jb 0x70596
testl %eax, %eax
je 0x70603
incq %r8
cmpl $0x9, %eax
ja 0x7060a
movl $0x9, %edx
subl %eax, %edx
movl $0x1, %esi
movl $0x1, %r10d
leal -0x1(%r10), %r11d
movl %eax, %ecx
shll %cl, %r11d
orl %r9d, %r11d
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%r11)
jne 0x7066a
movb %al, (%rcx,%r11)
movl %r10d, %ebp
movl %edx, %ecx
shrl %cl, %ebp
movq 0x20(%rbx), %rcx
movw %di, (%rcx,%r11,2)
incl %r10d
testl %ebp, %ebp
je 0x705cb
jmp 0x70668
movl $0x13, %esi
jmp 0x7066a
movl %r9d, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movq 0x18(%rbx), %rdx
movzbl (%rdx,%rcx), %edx
movl $0x1, %esi
subl %eax, %edx
jb 0x7066a
movq 0x20(%rbx), %rsi
movzwl (%rsi,%rcx,2), %r10d
leal -0x9(%rax), %esi
shrl $0x9, %r9d
movl $0x1, %r11d
leal -0x1(%r11), %r14d
movl %esi, %ecx
shll %cl, %r14d
orl %r9d, %r14d
addl %r10d, %r14d
movq 0x18(%rbx), %rcx
movb %al, (%rcx,%r14)
movl %r11d, %ebp
movl %edx, %ecx
shrl %cl, %ebp
movq 0x20(%rbx), %rcx
movw %di, (%rcx,%r14,2)
incl %r11d
testl %ebp, %ebp
je 0x7063a
xorl %esi, %esi
cmpl $0x13, %esi
je 0x70677
testl %esi, %esi
jne 0x70723
incq %rdi
movl 0x14(%rbx), %eax
cmpq %rax, %rdi
jb 0x70577
cmpq $0x2, %r8
jae 0x706fc
testq %r15, %r15
je 0x7071f
movl %r12d, %eax
addq %rax, %r13
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%rax)
jne 0x706c6
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
movw $0xffff, (%rcx,%rax,2)  # imm = 0xFFFF
incq %rax
cmpq %rax, %r13
jne 0x7069f
jmp 0x706e4
movq %r12, %rdi
callq 0x3a530
movl 0x4(%rsp), %ebp
movq %r14, %rdi
callq 0x3a530
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x53, %ebp
jmp 0x706e4
testq %r15, %r15
je 0x7071f
movq 0x18(%rbx), %rax
movl %r12d, %ecx
addq %rcx, %r13
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpb $0x10, (%rax,%rcx)
je 0x70723
incq %rcx
cmpq %rcx, %r13
jne 0x7070f
jmp 0x706e4
xorl %ebp, %ebp
jmp 0x706e4
movl $0x37, %ebp
jmp 0x706e4