// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
vpxor %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %r12
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rdi, %rbx
vmovdqa %xmm0, (%r12)
movq %rax, 0x10(%r12)
vmovdqa %xmm0, (%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x21e0
movq %rax, 0x8(%rbx)
movq %rax, %r14
movq %r12, %rdi
movl 0x18(%rbx), %r15d
incl %r15d
movq %r15, %rsi
callq 0xde33
testl %eax, %eax
je 0xdbda
testq %r15, %r15
je 0xdb86
movq 0x20(%rsp), %rdi
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x20b0
movl 0x18(%rbx), %r15d
movq %rsp, %rdi
incl %r15d
movq %r15, %rsi
callq 0xde33
testl %eax, %eax
je 0xdbda
testq %r15, %r15
je 0xdbb3
movq (%rsp), %rdi
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x20b0
testq %r14, %r14
je 0xdbda
cmpl $0x0, 0x1c(%rbx)
je 0xdc04
movq 0x20(%rsp), %rcx
movq 0x10(%rbx), %rdx
xorl %eax, %eax
movl %eax, %esi
movl (%rdx,%rsi,4), %esi
incl %eax
incl (%rcx,%rsi,4)
cmpl 0x1c(%rbx), %eax
jne 0xdbc9
jmp 0xdc06
movq 0x20(%rsp), %rdi
callq 0x21b0
movq (%rsp), %rdi
callq 0x21b0
movl $0x53, %ecx
movl %ecx, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
xorl %eax, %eax
cmpl $0x0, 0x18(%rbx)
movq (%rsp), %r14
je 0xdc35
movq 0x20(%rsp), %rax
movl (%r14), %ecx
movl $0x1, %edx
leal -0x1(%rdx), %esi
addl (%rax,%rsi,4), %ecx
movl %edx, %esi
incl %edx
addl %ecx, %ecx
movl %ecx, (%r14,%rsi,4)
cmpl 0x18(%rbx), %edx
jbe 0xdc1d
movl 0x1c(%rbx), %eax
testl %eax, %eax
je 0xdc64
movq 0x10(%rbx), %rcx
xorl %edx, %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %esi
testq %rsi, %rsi
je 0xdc5e
movl (%r14,%rsi,4), %eax
leal 0x1(%rax), %edi
movl %edi, (%r14,%rsi,4)
movq 0x8(%rbx), %rsi
movl %eax, (%rsi,%rdx,4)
movl 0x1c(%rbx), %eax
incl %edx
cmpl %eax, %edx
jne 0xdc3f
movq 0x20(%rsp), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x21b0
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x21e0
movq %rax, (%rbx)
testq %rax, %rax
je 0xdbed
testq %r14, %r14
je 0xdd02
vpmovsxbq 0x455a(%rip), %zmm1 # 0x12200
vpmovsxbq 0x4540(%rip), %zmm2 # 0x121f0
vpbroadcastd 0x466e(%rip), %zmm3 # 0x12328
leaq 0xf(%r14), %rcx
leaq -0x1(%r14), %rdx
vpbroadcastq %rdx, %zmm0
andq $-0x10, %rcx
xorl %edx, %edx
vpbroadcastq %rdx, %zmm4
vporq %zmm1, %zmm4, %zmm5
vporq %zmm2, %zmm4, %zmm4
vpcmpleuq %zmm0, %zmm4, %k0
vpcmpleuq %zmm0, %zmm5, %k1
kunpckbw %k0, %k1, %k1
vmovdqu32 %zmm3, (%rax,%rdx,4) {%k1}
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xdcce
testq %r15, %r15
je 0xde2c
movq 0x10(%rbx), %rdx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
movl (%rdx,%rsi,4), %r10d
testl %r10d, %r10d
je 0xdd9e
movq 0x8(%rbx), %r9
xorl %r11d, %r11d
movl $0x37, %ecx
testl %r8d, %r8d
js 0xdbf2
leal 0x2(%r8), %ebp
cmpl %r15d, %ebp
ja 0xdbf2
movl %r11d, %ecx
notl %ecx
addl %r10d, %ecx
shrxl %ecx, (%r9,%rsi,4), %ecx
andl $0x1, %ecx
leal (%rcx,%r8,2), %ecx
movl (%rax,%rcx,4), %r8d
cmpl $0x7fff, %r8d           # imm = 0x7FFF
jne 0xdd73
incl %r11d
cmpl %r10d, %r11d
jne 0xdd7e
movl %esi, (%rax,%rcx,4)
xorl %r8d, %r8d
jmp 0xdd92
incl %r11d
subl %r15d, %r8d
movl %r11d, %r10d
jmp 0xdd92
movl %edi, %r8d
leal 0x1(%rdi,%r15), %edi
incl %r8d
movl %r11d, %r10d
movl %edi, (%rax,%rcx,4)
movl %r8d, %edi
movl %r10d, %r11d
movl (%rdx,%rsi,4), %r10d
cmpl %r10d, %r11d
jne 0xdd26
incq %rsi
cmpq %r15, %rsi
jne 0xdd16
testl %r14d, %r14d
je 0xde2c
vpmovsxbq 0x4447(%rip), %zmm1 # 0x12200
vpmovsxbq 0x442d(%rip), %zmm2 # 0x121f0
vpbroadcastd 0x455b(%rip), %zmm3 # 0x12328
leaq 0xf(%r14), %rdx
decq %r14
vpxor %xmm4, %xmm4, %xmm4
xorl %ecx, %ecx
xorl %esi, %esi
andq $-0x10, %rdx
vpbroadcastq %r14, %zmm0
vpbroadcastq %rsi, %zmm5
vporq %zmm1, %zmm5, %zmm6
vporq %zmm2, %zmm5, %zmm5
vpcmpleuq %zmm0, %zmm5, %k0
vpcmpleuq %zmm0, %zmm6, %k1
kunpckbw %k0, %k1, %k1
vmovdqu32 (%rax,%rsi,4), %zmm5 {%k1} {z}
vpcmpeqd %zmm3, %zmm5, %k1 {%k1}
vmovdqu32 %zmm4, (%rax,%rsi,4) {%k1}
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0xdde6
jmp 0xdbf2
xorl %ecx, %ecx
jmp 0xdbf2