// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
vpxor %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %r13
vmovdqa %xmm0, (%r13)
xorl %r15d, %r15d
movq %r15, 0x10(%r13)
vmovdqa %xmm0, (%rsp)
movq %r15, 0x10(%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x21e0
movq %rax, 0x8(%rbx)
testq %rax, %rax
movl $0x53, %r14d
cmovnel %r15d, %r14d
movl 0x18(%rbx), %r15d
incl %r15d
movq 0x8(%r13), %r12
movq %r13, %rdi
movq %r15, %rsi
callq 0xdfaf
movl %eax, %ebp
subq %r12, %r15
seta %al
andb %bpl, %al
cmpb $0x1, %al
jne 0xdc74
shlq $0x2, %r12
addq 0x20(%rsp), %r12
shlq $0x2, %r15
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20b0
testl %ebp, %ebp
je 0xdcba
movl 0x18(%rbx), %r15d
incl %r15d
movq %rsp, %rdi
movq 0x8(%rdi), %r12
movq %r15, %rsi
callq 0xdfaf
movl %eax, %ebp
subq %r12, %r15
seta %al
andb %bpl, %al
cmpb $0x1, %al
jne 0xdcb6
shlq $0x2, %r12
addq (%rsp), %r12
shlq $0x2, %r15
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20b0
testl %ebp, %ebp
jne 0xdcc0
movl $0x53, %r14d
leaq 0x28(%rsp), %rax
testl %r14d, %r14d
je 0xdcff
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %xmm0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x21b0
xorl %ebx, %ebx
movq %rbx, 0x20(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %xmm0, 0x8(%rsp)
movq (%rsp), %rdi
callq 0x21b0
movq %rbx, (%rsp)
jmp 0xddb9
cmpl $0x0, 0x1c(%rbx)
je 0xdd1f
movq 0x20(%rsp), %rcx
movq 0x10(%rbx), %rdx
xorl %esi, %esi
movl %esi, %edi
movl (%rdx,%rdi,4), %edi
incl (%rcx,%rdi,4)
incl %esi
cmpl 0x1c(%rbx), %esi
jne 0xdd10
cmpl $0x0, 0x18(%rbx)
je 0xdd4e
movq (%rsp), %rcx
movl $0x1, %edx
movq 0x20(%rsp), %rsi
leal -0x1(%rdx), %edi
movl (%rsi,%rdi,4), %r8d
addl (%rcx,%rdi,4), %r8d
addl %r8d, %r8d
movl %edx, %edi
movl %r8d, (%rcx,%rdi,4)
incl %edx
cmpl 0x18(%rbx), %edx
jbe 0xdd33
cmpl $0x0, 0x1c(%rbx)
je 0xdd83
movq 0x10(%rbx), %rcx
xorl %edx, %edx
movq (%rsp), %rsi
movl %edx, %edx
movl (%rcx,%rdx,4), %edi
testq %rdi, %rdi
je 0xdd7c
movl (%rsi,%rdi,4), %r8d
leal 0x1(%r8), %r9d
movl %r9d, (%rsi,%rdi,4)
movq 0x8(%rbx), %rdi
movl %r8d, (%rdi,%rdx,4)
incl %edx
cmpl 0x1c(%rbx), %edx
jne 0xdd5e
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %xmm0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x21b0
xorl %r15d, %r15d
movq %r15, 0x20(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %xmm0, 0x8(%rsp)
movq (%rsp), %rdi
callq 0x21b0
movq %r15, (%rsp)
testl %r14d, %r14d
je 0xddce
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x21e0
movq %rax, (%rbx)
testq %rax, %rax
je 0xdf91
testq %r14, %r14
je 0xde5a
leaq 0xf(%r14), %rcx
andq $-0x10, %rcx
leaq -0x1(%r14), %rdx
vpbroadcastq %rdx, %zmm0
xorl %edx, %edx
vpmovsxbq 0x439e(%rip), %zmm1 # 0x121b0
vpmovsxbq 0x439c(%rip), %zmm2 # 0x121b8
vpbroadcastd 0x44a2(%rip), %zmm3 # 0x122c8
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 0xde26
testq %r15, %r15
je 0xdf0a
movq 0x10(%rbx), %rcx
xorl %edx, %edx
xorl %edi, %edi
xorl %esi, %esi
movl (%rcx,%rdx,4), %r11d
testl %r11d, %r11d
je 0xdefe
movq 0x8(%rbx), %r8
movl $0x1, %r10d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
testl %edi, %edi
setns %r12b
leal 0x2(%rdi), %ebp
cmpl %r15d, %ebp
setbe %bpl
andb %r12b, %bpl
cmpb $0x1, %bpl
jne 0xdee2
leal (%r11,%r9), %r12d
shrxl %r12d, (%r8,%rdx,4), %r12d
andl $0x1, %r12d
leal (%r12,%rdi,2), %r12d
movl (%rax,%r12,4), %edi
cmpl $0x7fff, %edi           # imm = 0x7FFF
jne 0xdece
cmpl %r11d, %r10d
jne 0xded3
movl %edx, (%rax,%r12,4)
xorl %edi, %edi
jmp 0xdee2
subl %r15d, %edi
jmp 0xdee2
movl %esi, %edi
incl %edi
leal 0x1(%rsi,%r15), %esi
movl %esi, (%rax,%r12,4)
movl %edi, %esi
testb %bpl, %bpl
je 0xdf9c
movl (%rcx,%rdx,4), %r11d
leal 0x1(%r10), %ebp
decl %r9d
cmpl %r11d, %r10d
movl %ebp, %r10d
jne 0xde8a
incq %rdx
cmpq %r15, %rdx
jne 0xde6d
testl %r14d, %r14d
je 0xdfa7
leaq 0xf(%r14), %rcx
andq $-0x10, %rcx
decq %r14
vpbroadcastq %r14, %zmm0
xorl %r14d, %r14d
vpmovsxbq 0x427f(%rip), %zmm1 # 0x121b0
vpmovsxbq 0x427d(%rip), %zmm2 # 0x121b8
vpbroadcastd 0x4383(%rip), %zmm3 # 0x122c8
vpxor %xmm4, %xmm4, %xmm4
xorl %edx, %edx
vpbroadcastq %rdx, %zmm5
vporq %zmm1, %zmm5, %zmm6
vporq %zmm2, %zmm5, %zmm5
vpcmpleuq %zmm0, %zmm5, %k0
vpcmpleuq %zmm0, %zmm6, %k1
kunpckbw %k0, %k1, %k1
vmovdqu32 (%rax,%rdx,4), %zmm5 {%k1} {z}
vpcmpeqd %zmm3, %zmm5, %k1 {%k1}
vmovdqu32 %zmm4, (%rax,%rdx,4) {%k1}
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xdf4b
jmp 0xddb9
movl $0x53, %r14d
jmp 0xddb9
movl $0x37, %r14d
jmp 0xddb9
xorl %r14d, %r14d
jmp 0xddb9