// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: HuffmanTree_makeFromLengths2
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
movq %rdi, %rbx
pxor %xmm0, %xmm0
leaq 0x20(%rsp), %r12
movdqa %xmm0, (%r12)
xorl %eax, %eax
movq %rax, 0x10(%r12)
movdqa %xmm0, (%rsp)
movq %rax, 0x10(%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x1100
movq %rax, %r14
movq %rax, 0x8(%rbx)
movl 0x18(%rbx), %r15d
incl %r15d
movq %r12, %rdi
movq %r15, %rsi
callq 0xacb8
testl %eax, %eax
je 0xaa1c
testq %r15, %r15
je 0xa9c8
movq 0x20(%rsp), %rdi
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x10b0
movl 0x18(%rbx), %r15d
incl %r15d
movq %rsp, %rdi
movq %r15, %rsi
callq 0xacb8
testl %eax, %eax
je 0xaa1c
testq %r15, %r15
je 0xa9f5
movq (%rsp), %rdi
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x10b0
testq %r14, %r14
je 0xaa1c
cmpl $0x0, 0x1c(%rbx)
je 0xaa43
movq 0x20(%rsp), %rcx
movq 0x10(%rbx), %rdx
xorl %eax, %eax
movl %eax, %esi
movl (%rdx,%rsi,4), %esi
incl (%rcx,%rsi,4)
incl %eax
cmpl 0x1c(%rbx), %eax
jne 0xaa0b
jmp 0xaa45
movq 0x20(%rsp), %rdi
callq 0x1030
movq (%rsp), %rdi
callq 0x1030
movl $0x53, %ecx
movl %ecx, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
cmpl $0x0, 0x18(%rbx)
movq (%rsp), %r14
je 0xaa74
movq 0x20(%rsp), %rax
movl (%r14), %ecx
movl $0x1, %edx
leal -0x1(%rdx), %esi
addl (%rax,%rsi,4), %ecx
addl %ecx, %ecx
movl %edx, %esi
movl %ecx, (%r14,%rsi,4)
incl %edx
cmpl 0x18(%rbx), %edx
jbe 0xaa5c
movl 0x1c(%rbx), %eax
testl %eax, %eax
je 0xaaa3
movq 0x10(%rbx), %rcx
xorl %edx, %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %esi
testq %rsi, %rsi
je 0xaa9d
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 0xaa7e
movq 0x20(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x1100
movq %rax, (%rbx)
testq %rax, %rax
je 0xaa2f
testq %r14, %r14
je 0xabda
leaq 0x3(%r14), %rcx
andq $-0x4, %rcx
leaq -0x1(%r14), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x3543(%rip), %xmm1     # 0xe040
movdqa 0x350b(%rip), %xmm2     # 0xe010
xorl %edx, %edx
movdqa 0x3541(%rip), %xmm3     # 0xe050
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x3541(%rip), %xmm5     # 0xe060
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0xab5b
movl $0x7fff, (%rax,%rdx,4)  # imm = 0x7FFF
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0xab72
movl $0x7fff, 0x4(%rax,%rdx,4) # imm = 0x7FFF
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %esi
testb $0x1, %sil
je 0xabb2
movl $0x7fff, 0x8(%rax,%rdx,4) # imm = 0x7FFF
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0xabc5
movl $0x7fff, 0xc(%rax,%rdx,4) # imm = 0x7FFF
addq $0x4, %rdx
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
cmpq %rdx, %rcx
jne 0xab1f
testq %r15, %r15
je 0xacb1
movq 0x10(%rbx), %rdx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
movl (%rdx,%rsi,4), %r10d
testl %r10d, %r10d
je 0xac7f
movq 0x8(%rbx), %r9
xorl %r11d, %r11d
movl $0x37, %ecx
testl %r8d, %r8d
js 0xaa34
leal 0x2(%r8), %ebp
cmpl %r15d, %ebp
ja 0xaa34
movl (%r9,%rsi,4), %ecx
movl %r11d, %ebp
notl %ebp
addl %r10d, %ebp
xorl %r12d, %r12d
btl %ebp, %ecx
setb %r12b
leal (%r12,%r8,2), %ecx
movl (%rax,%rcx,4), %r8d
cmpl $0x7fff, %r8d           # imm = 0x7FFF
jne 0xac54
incl %r11d
cmpl %r10d, %r11d
jne 0xac5f
movl %esi, (%rax,%rcx,4)
xorl %r8d, %r8d
jmp 0xac73
subl %r15d, %r8d
incl %r11d
movl %r11d, %r10d
jmp 0xac73
movl %edi, %r8d
incl %r8d
addl %r15d, %edi
incl %edi
movl %edi, (%rax,%rcx,4)
movl %r11d, %r10d
movl %r8d, %edi
movl %r10d, %r11d
movl (%rdx,%rsi,4), %r10d
cmpl %r10d, %r11d
jne 0xac02
incq %rsi
cmpq %r15, %rsi
jne 0xabee
testl %r14d, %r14d
je 0xacb1
xorl %edx, %edx
xorl %ecx, %ecx
cmpl $0x7fff, (%rax,%rdx,4)  # imm = 0x7FFF
jne 0xaca4
movl $0x0, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %r14
jne 0xac94
jmp 0xaa34
xorl %ecx, %ecx
jmp 0xaa34