// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
xorl %r14d, %r14d
movq %r14, 0x10(%r15)
movaps %xmm0, (%rsp)
movq %r14, 0x10(%rsp)
movl 0x1c(%rdi), %edi
shlq $0x2, %rdi
callq 0x10f0
testq %rax, %rax
movl $0x53, %ebp
cmovnel %r14d, %ebp
movq %rax, 0x8(%rbx)
movl 0x18(%rbx), %r14d
incl %r14d
movq 0x8(%r15), %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0xa60b
movl %eax, %r15d
testl %eax, %eax
setne %al
subq %r12, %r14
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa37f
shlq $0x2, %r12
addq 0x20(%rsp), %r12
shlq $0x2, %r14
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x10b0
testl %r15d, %r15d
je 0xa3cd
movl 0x18(%rbx), %r14d
incl %r14d
movq %rsp, %rdi
movq 0x8(%rdi), %r12
movq %r14, %rsi
callq 0xa60b
movl %eax, %r15d
testl %eax, %eax
setne %al
subq %r12, %r14
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa3c8
shlq $0x2, %r12
addq (%rsp), %r12
shlq $0x2, %r14
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x10b0
testl %r15d, %r15d
jne 0xa3d2
movl $0x53, %ebp
leaq 0x28(%rsp), %rax
testl %ebp, %ebp
je 0xa40c
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x1030
xorl %ebx, %ebx
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq (%rsp), %rdi
callq 0x1030
movq %rbx, (%rsp)
jmp 0xa4c1
cmpl $0x0, 0x1c(%rbx)
je 0xa42c
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 0xa41d
cmpl $0x0, 0x18(%rbx)
je 0xa45b
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 0xa440
cmpl $0x0, 0x1c(%rbx)
je 0xa490
movq 0x10(%rbx), %rcx
xorl %edx, %edx
movq (%rsp), %rsi
movl %edx, %edx
movl (%rcx,%rdx,4), %edi
testq %rdi, %rdi
je 0xa489
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 0xa46b
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x1030
xorl %r14d, %r14d
movq %r14, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq (%rsp), %rdi
callq 0x1030
movq %r14, (%rsp)
testl %ebp, %ebp
je 0xa4d2
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x1c(%rbx), %r15d
leal (%r15,%r15), %r14d
leaq (,%r14,4), %rdi
callq 0x10f0
movq %rax, (%rbx)
testq %rax, %rax
je 0xa5f0
testq %r14, %r14
je 0xa509
xorl %ecx, %ecx
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
cmpq %rcx, %r14
jne 0xa4fa
testq %r15, %r15
je 0xa5ca
movq 0x10(%rbx), %rcx
xorl %edx, %edx
xorl %edi, %edi
movq $0x0, 0x40(%rsp)
movl (%rcx,%rdx,4), %r11d
testl %r11d, %r11d
je 0xa5be
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 0xa5a6
movl (%r8,%rdx,4), %r12d
leal (%r11,%r9), %r13d
xorl %esi, %esi
btl %r13d, %r12d
setb %sil
leal (%rsi,%rdi,2), %r12d
movl (%rax,%r12,4), %edi
cmpl $0x7fff, %edi           # imm = 0x7FFF
jne 0xa588
cmpl %r11d, %r10d
jne 0xa58d
movl %edx, (%rax,%r12,4)
xorl %edi, %edi
jmp 0xa5a6
subl %r15d, %edi
jmp 0xa5a6
movq 0x40(%rsp), %rsi
movl %esi, %edi
incl %edi
addl %r15d, %esi
incl %esi
movl %esi, (%rax,%r12,4)
movl %edi, %esi
movq %rsi, 0x40(%rsp)
testb %bpl, %bpl
je 0xa5fa
movl (%rcx,%rdx,4), %r11d
leal 0x1(%r10), %ebp
decl %r9d
cmpl %r11d, %r10d
movl %ebp, %r10d
jne 0xa540
incq %rdx
cmpq %r15, %rdx
jne 0xa523
testl %r14d, %r14d
je 0xa604
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
jne 0xa5e3
movl $0x0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0xa5d3
jmp 0xa4c1
movl $0x53, %ebp
jmp 0xa4c1
movl $0x37, %ebp
jmp 0xa4c1
xorl %ebp, %ebp
jmp 0xa4c1