// File: /workspace/github_cpp_makefile_star2_O1/drummyfish[P]flowchart/src/lodepng.cpp
// Function: HuffmanTree_makeFromLengths2(HuffmanTree*)
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
{
  uivector blcount;
  uivector nextcode;
  unsigned bits, n, error = 0;

  uivector_init(&blcount);
  uivector_init(&nextcode);

  tree->tree1d = (unsigned*)mymalloc(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
pushq %rax
movq %rdi, %rbx
movl 0x1c(%rdi), %r13d
leaq (,%r13,4), %rdi
callq 0x2270
movq %rax, %r14
movl 0x18(%rbx), %eax
leal 0x1(%rax), %ebp
movq %r14, 0x8(%rbx)
incl %eax
jne 0xbfd4
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0xbfeb
leaq (,%rbp,8), %rdi
callq 0x2270
movq %rax, %r15
testq %rax, %rax
sete %r12b
leaq (,%rbp,4), %rdx
testq %rbp, %rbp
sete %al
orb %r12b, %al
jne 0xc016
movq %r15, %rdi
xorl %esi, %esi
movq %r14, (%rsp)
movq %rdx, %r14
callq 0x2100
movq %r14, %rdx
movq (%rsp), %r14
testb %r12b, %r12b
je 0xc025
movl $0x53, %ebp
xorl %r12d, %r12d
jmp 0xc07f
testq %rbp, %rbp
je 0xc04e
leaq (,%rbp,8), %rdi
movq %r14, %r12
movq %rdx, %r14
callq 0x2270
movq %r14, %rdx
movq %r12, %r14
movq %rax, %r12
testq %rax, %rax
sete %cl
jmp 0xc053
xorl %r12d, %r12d
xorl %ecx, %ecx
testq %rbp, %rbp
sete %al
orb %cl, %al
jne 0xc06d
movq %r12, %rdi
xorl %esi, %esi
movq %rcx, %rbp
callq 0x2100
movq %rbp, %rcx
xorl %ebp, %ebp
testb %cl, %cl
movl $0x53, %eax
cmovnel %eax, %ebp
testq %r14, %r14
cmovel %eax, %ebp
testl %ebp, %ebp
je 0xc098
movq %r15, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
jmp 0xc119
testq %r13, %r13
je 0xc0b2
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
incl (%r15,%rdx,4)
incq %rcx
cmpq %rcx, %r13
jne 0xc0a3
cmpl $0x0, 0x18(%rbx)
je 0xc0d7
movl $0x1, %eax
leal -0x1(%rax), %ecx
movl (%r15,%rcx,4), %edx
addl (%r12,%rcx,4), %edx
addl %edx, %edx
movl %eax, %ecx
movl %edx, (%r12,%rcx,4)
incl %eax
cmpl 0x18(%rbx), %eax
jbe 0xc0bd
cmpl $0x0, 0x1c(%rbx)
je 0xc105
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
testq %rdx, %rdx
je 0xc0fa
movl (%r12,%rdx,4), %esi
leal 0x1(%rsi), %edi
movl %edi, (%r12,%rdx,4)
movl %esi, (%r14,%rcx,4)
incq %rcx
movl 0x1c(%rbx), %edx
cmpq %rdx, %rcx
jb 0xc0e3
movq %r15, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
testl %ebp, %ebp
je 0xc12a
movl %ebp, %eax
addq $0x8, %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 0x2270
movq %rax, (%rbx)
testq %rax, %rax
je 0xc236
testq %r14, %r14
je 0xc161
xorl %ecx, %ecx
movl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
incq %rcx
cmpq %rcx, %r14
jne 0xc152
testq %r15, %r15
je 0xc210
movq 0x10(%rbx), %rcx
leal -0x2(%r15), %edx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
movl (%rcx,%rsi,4), %r11d
testl %r11d, %r11d
je 0xc1fe
movl $0x1, %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpl %edx, %r8d
jbe 0xc198
movl %r8d, %ebp
jmp 0xc1e1
movq 0x8(%rbx), %r12
movl (%r12,%rsi,4), %ebp
leal (%r11,%r10), %r12d
xorl %r13d, %r13d
btl %r12d, %ebp
setb %r13b
leal (%r13,%r8,2), %r12d
movl (%rax,%r12,4), %ebp
cmpl $0x7fff, %ebp           # imm = 0x7FFF
jne 0xc1cd
cmpl %r11d, %r9d
jne 0xc1d2
movl %esi, (%rax,%r12,4)
xorl %ebp, %ebp
jmp 0xc1e1
subl %r15d, %ebp
jmp 0xc1e1
movl %edi, %ebp
incl %ebp
addl %r15d, %edi
incl %edi
movl %edi, (%rax,%r12,4)
movl %ebp, %edi
cmpl %edx, %r8d
ja 0xc240
movl (%rcx,%rsi,4), %r11d
leal 0x1(%r9), %r12d
decl %r10d
movl %ebp, %r8d
cmpl %r11d, %r9d
movl %r12d, %r9d
jb 0xc18e
jmp 0xc201
movl %r8d, %ebp
incq %rsi
movl %ebp, %r8d
cmpq %r15, %rsi
jne 0xc179
testl %r14d, %r14d
je 0xc24a
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpl $0x7fff, (%rax,%rcx,4)  # imm = 0x7FFF
jne 0xc229
movl $0x0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0xc219
jmp 0xc119
movl $0x53, %ebp
jmp 0xc119
movl $0x37, %ebp
jmp 0xc119
xorl %ebp, %ebp
jmp 0xc119