// File: /workspace/github_cpp_makefile_star2_O3/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 0x3a5b0
movq %rax, %r14
movq %rax, (%rbx)
movl 0x10(%rbx), %eax
movq %rax, 0x10(%rsp)
incl %eax
movq %rax, 0x8(%rsp)
leaq (,%rax,4), %r13
movq %r13, %rdi
callq 0x3a5b0
movq %rax, %r12
movq %r13, %rdi
callq 0x3a5b0
movq %rax, %r15
testq %r14, %r14
je 0x6fd25
testq %r12, %r12
je 0x6fd25
testq %r15, %r15
je 0x6fd25
cmpl $0x0, 0x8(%rsp)
je 0x6fa17
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3a260
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3a260
testq %rbp, %rbp
je 0x6fa31
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
incl (%r12,%rdx,4)
incq %rcx
cmpq %rcx, %rbp
jne 0x6fa22
cmpl $0x0, 0x10(%rsp)
je 0x6fa63
movq 0x8(%rsp), %rcx
cmpl $0x3, %ecx
movl $0x2, %eax
cmovael %ecx, %eax
movl (%r15), %ecx
decq %rax
xorl %edx, %edx
addl (%r12,%rdx,4), %ecx
addl %ecx, %ecx
movl %ecx, 0x4(%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x6fa50
testl %ebp, %ebp
je 0x6fa9e
movq 0x8(%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %ecx
testq %rcx, %rcx
je 0x6fa96
movl (%r15,%rcx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%r15,%rcx,4)
movl %esi, (%r14,%rdx,4)
movb (%rax,%rdx,4), %cl
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
shll %cl, %edi
notl %edi
andl %esi, %edi
movl %edi, (%r14,%rdx,4)
incq %rdx
cmpq %rdx, %rbp
jne 0x6fa6d
movq %r12, %rdi
callq 0x3a540
movq %r15, %rdi
callq 0x3a540
movl $0x1, %edi
movl $0x800, %esi            # imm = 0x800
callq 0x3a2f0
movq %rax, %r15
movl $0x53, %eax
testq %r15, %r15
je 0x6fd3a
testl %ebp, %ebp
je 0x6fb27
movq 0x8(%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpl $0xa, %esi
jb 0x6fb1f
movl (%r14,%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 0x6faf4
movl %r8d, %ecx
movl (%r15,%rcx,4), %edi
cmpl %esi, %edi
cmoval %edi, %esi
movl %esi, (%r15,%rcx,4)
incq %rdx
cmpq %rbp, %rdx
jne 0x6fad8
movl $0x200, %r14d           # imm = 0x200
xorl %eax, %eax
xorl %edx, %edx
movl (%r15,%rdx,4), %esi
leal -0x9(%rsi), %ecx
movl $0x1, %ebp
shll %cl, %ebp
cmpl $0xa, %esi
cmovbl %eax, %ebp
movq %r14, %r13
addq %rbp, %r14
incq %rdx
cmpq $0x200, %rdx            # imm = 0x200
jne 0x6fb31
movq %r14, %rdi
callq 0x3a5b0
movq %rax, %r12
movq %rax, 0x18(%rbx)
leaq (%r14,%r14), %rdi
callq 0x3a5b0
movq %rax, 0x20(%rbx)
testq %r12, %r12
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
jne 0x6fd2d
testq %r14, %r14
je 0x6fba3
movl %ebp, %eax
addq %r13, %rax
xorl %ecx, %ecx
movq 0x18(%rbx), %rdx
movb $0x10, (%rdx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x6fb93
movw $0x200, %dx             # imm = 0x200
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
cmpl $0xa, %ecx
jb 0x6fbcf
movq 0x18(%rbx), %rsi
movb %cl, (%rsi,%rax)
addb $-0x9, %cl
movl $0x1, %esi
shll %cl, %esi
movq 0x20(%rbx), %rcx
movw %dx, (%rcx,%rax,2)
addl %edx, %esi
movl %esi, %edx
incq %rax
cmpq $0x200, %rax            # imm = 0x200
jne 0x6fba9
movq %r15, %rdi
callq 0x3a540
cmpl $0x0, 0x14(%rbx)
je 0x6fce6
xorl %edi, %edi
xorl %r8d, %r8d
movq 0x8(%rbx), %rax
movl (%rax,%rdi,4), %eax
testl %eax, %eax
je 0x6fcd1
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 0x6fc0e
incq %r8
cmpl $0x9, %eax
ja 0x6fc72
movl $0x9, %edx
subl %eax, %edx
movl $0x1, %esi
leal -0x1(%rsi), %r10d
movl %eax, %ecx
shll %cl, %r10d
orl %r9d, %r10d
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%r10)
jne 0x6fd6b
movb %al, (%rcx,%r10)
movl %esi, %r11d
movl %edx, %ecx
shrl %cl, %r11d
movq 0x20(%rbx), %rcx
movw %di, (%rcx,%r10,2)
incl %esi
testl %r11d, %r11d
je 0x6fc39
jmp 0x6fcd1
movl %r9d, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movq 0x18(%rbx), %rdx
movzbl (%rdx,%rcx), %edx
subl %eax, %edx
jb 0x6fd6b
movq 0x20(%rbx), %rsi
movzwl (%rsi,%rcx,2), %r10d
leal -0x9(%rax), %esi
shrl $0x9, %r9d
movl $0x1, %r11d
leal -0x1(%r11), %r15d
movl %esi, %ecx
shll %cl, %r15d
orl %r9d, %r15d
addl %r10d, %r15d
movq 0x18(%rbx), %rcx
movb %al, (%rcx,%r15)
movl %r11d, %r12d
movl %edx, %ecx
shrl %cl, %r12d
movq 0x20(%rbx), %rcx
movw %di, (%rcx,%r15,2)
incl %r11d
testl %r12d, %r12d
je 0x6fca1
incq %rdi
movl 0x14(%rbx), %eax
cmpq %rax, %rdi
jb 0x6fbf1
cmpq $0x2, %r8
jae 0x6fd49
testq %r14, %r14
je 0x6fd21
movl %ebp, %eax
addq %rax, %r13
xorl %eax, %eax
movq 0x18(%rbx), %rcx
cmpb $0x10, (%rcx,%rax)
jne 0x6fd19
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 0x6fcf2
xorl %eax, %eax
jmp 0x6fd3a
movq %r12, %rdi
callq 0x3a540
movq %r15, %rdi
callq 0x3a540
movl $0x53, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0x6fd21
movq 0x18(%rbx), %rdx
movl %ebp, %ecx
addq %rcx, %r13
xorl %ecx, %ecx
xorl %eax, %eax
cmpb $0x10, (%rdx,%rcx)
je 0x6fd6b
incq %rcx
cmpq %rcx, %r13
jne 0x6fd5b
jmp 0x6fd3a
movl $0x37, %eax
jmp 0x6fd3a