// File: /workspace/github_cpp_makefile_star2_O3/mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: HuffmanTree_makeFromFrequencies(HuffmanTree*, unsigned int const*, unsigned long, unsigned int)
static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies, size_t numcodes, unsigned maxbitlen)
{
  unsigned i, j;
  size_t sum = 0, numpresent = 0;
  unsigned error = 0;
  
  vector prev_row; /*type Coin, the previous row of coins*/
  vector coins; /*type Coin, the coins of the currently calculated row*/
  
  tree->maxbitlen = maxbitlen;
  
  for(i = 0; i < numcodes; i++)
  {
    if(frequencies[i] > 0)
    {
      numpresent++;
      sum += frequencies[i];
    }
  }
  
  if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
  tree->numcodes = (unsigned)numcodes; /*number of symbols*/
  uivector_resize(&tree->lengths, 0);
  if(!uivector_resizev(&tree->lengths, tree->numcodes, 0)) return 9905;
  
  if(numpresent == 0) /*there are no symbols at all, in that case add one symbol of value 0 to the tree (see RFC 1951 section 3.2.7) */
  {
    tree->lengths.data[0] = 1;
    return HuffmanTree_makeFromLengths2(tree);
  }
  else if(numpresent == 1) /*the package merge algorithm gives wrong results if there's only one symbol (theoretically 0 bits would then suffice, but we need a proper symbol for zlib)*/
  {
    for(i = 0; i < numcodes; i++) if(frequencies[i]) tree->lengths.data[i] = 1;
    return HuffmanTree_makeFromLengths2(tree);
  }
  
  vector_init(&coins, sizeof(Coin));
  vector_init(&prev_row, sizeof(Coin));

  /*Package-Merge algorithm represented by coin collector's problem
  For every symbol, maxbitlen coins will be created*/
  
  /*first row, lowest denominator*/
  error = HuffmanTree_fillInCoins(&coins, frequencies, tree->numcodes, sum);
  if(!error)
  {
    for(j = 1; j <= maxbitlen && !error; j++) /*each of the remaining rows*/
    {
      vector_swap(&coins, &prev_row); /*swap instead of copying*/
      if(!vector_resized(&coins, 0, Coin_cleanup)) { error = 9906; break; }

      for(i = 0; i + 1 < prev_row.size; i += 2)
      {
        if(!vector_resize(&coins, coins.size + 1)) { error = 9907; break; }
        Coin_init((Coin*)vector_get(&coins, coins.size - 1));
        Coin_copy((Coin*)vector_get(&coins, coins.size - 1), (Coin*)vector_get(&prev_row, i));
        addCoins((Coin*)vector_get(&coins, coins.size - 1), (Coin*)vector_get(&prev_row, i + 1)); /*merge the coins into packages*/
      }
      if(j < maxbitlen)
      {
        error = HuffmanTree_fillInCoins(&coins, frequencies, tree->numcodes, sum);
      }
    }
  }
  
  if(!error)
  {
    /*keep the coins with lowest weight, so that they add up to the amount of symbols - 1*/
    vector_resized(&coins, numpresent - 1, Coin_cleanup);
    
    /*calculate the lenghts of each symbol, as the amount of times a coin of each symbol is used*/
    for(i = 0; i < coins.size; i++)
    {
      Coin* coin = (Coin*)vector_get(&coins, i);
      for(j = 0; j < coin->symbols.size; j++) tree->lengths.data[coin->symbols.data[j]]++;
    }
    
    error = HuffmanTree_makeFromLengths2(tree);
  }

  vector_cleanupd(&coins, Coin_cleanup);
  vector_cleanupd(&prev_row, Coin_cleanup);
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %ecx, 0x48(%rdi)
testq %rdx, %rdx
je 0x22dff
movl %ecx, %r12d
movq %rdx, %rbx
movq %rdi, %r15
xorl %eax, %eax
xorl %edx, %edx
xorl %edi, %edi
movl (%rsi,%rax,4), %ecx
addq %rcx, %rdi
cmpq $0x1, %rcx
sbbq $-0x1, %rdx
incq %rax
movl %eax, %ecx
cmpq %rbx, %rcx
jb 0x22d79
movq %rdi, -0x78(%rbp)
movq %rdx, -0x70(%rbp)
movq %rsi, -0x58(%rbp)
movl %ebx, 0x4c(%r15)
leaq 0x30(%r15), %r14
movq $0x0, 0x38(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22340
testl %eax, %eax
je 0x22e09
movq (%r14), %rdi
leaq (,%rbx,4), %rdx
xorl %esi, %esi
callq 0x4170
movq -0x70(%rbp), %rax
testq %rax, %rax
je 0x22e13
cmpq $0x1, %rax
jne 0x22e36
xorl %eax, %eax
movq -0x58(%rbp), %rdx
cmpl $0x0, (%rdx,%rax,4)
je 0x22df3
movq (%r14), %rcx
movl $0x1, (%rcx,%rax,4)
incq %rax
movl %eax, %ecx
cmpq %rbx, %rcx
jb 0x22de3
jmp 0x22e1d
movl $0x50, %eax
jmp 0x231b4
movl $0x26b1, %eax           # imm = 0x26B1
jmp 0x231b4
movq 0x30(%r15), %rax
movl $0x1, (%rax)
movq %r15, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240c
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movl $0x20, %ecx
movl %ecx, 0x18(%rdi)
movaps %xmm0, -0xb0(%rbp)
movq %rax, -0xa0(%rbp)
movl %ecx, -0x98(%rbp)
movl 0x4c(%r15), %edx
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rcx
callq 0x231c6
testl %eax, %eax
jne 0x2317f
testl %r12d, %r12d
je 0x230f3
movq %r14, -0xb8(%rbp)
movl $0x1, %ecx
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r15, -0xc0(%rbp)
movl %r12d, -0x2c(%rbp)
movl %ecx, -0x30(%rbp)
movq %r14, %rax
movq %r13, %rcx
movq %rbx, %rdx
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %r13
movq %rcx, -0x48(%rbp)
movq -0x40(%rbp), %rbx
movq %rdx, -0x40(%rbp)
movq %rax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x233b4
testl %eax, %eax
je 0x23165
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %r15
cmpq $0x2, %r13
jb 0x23078
movq %rbx, -0x68(%rbp)
movl -0x38(%rbp), %r12d
xorl %ecx, %ecx
movl $0x1, %ebx
movq %r13, -0x80(%rbp)
movq %r12, -0xc8(%rbp)
leaq 0x1(%r8), %rax
movq %rax, %rdx
imulq %r12, %rdx
cmpq %r15, %rdx
movq %rcx, -0x90(%rbp)
movq %rax, -0xd0(%rbp)
jbe 0x22f59
addq %rdx, %rdx
movq %rdx, -0x88(%rbp)
movq %rdx, %rsi
movq %r8, %r13
movq %rdi, -0x60(%rbp)
callq 0x44f0
movq %r13, %r8
movq -0x90(%rbp), %rcx
movq -0x80(%rbp), %r13
testq %rax, %rax
jne 0x22f63
jmp 0x2307c
movq %rdi, %rax
movq %r15, -0x88(%rbp)
imulq %r12, %r8
leaq (%rax,%r8), %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r8)
movq $0x0, 0x10(%rax,%r8)
movl %ecx, %r12d
shlq $0x5, %r12
movss 0x18(%r14,%r12), %xmm0
movq %rax, -0x60(%rbp)
movss %xmm0, 0x18(%rax,%r8)
movq 0x8(%r14,%r12), %rsi
movq %r15, %rdi
callq 0x22340
testl %eax, %eax
je 0x22fc7
movq 0x8(%r14,%r12), %rax
testq %rax, %rax
je 0x22fc7
movq (%r14,%r12), %rcx
movq (%r15), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movl %edi, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x22fb9
shlq $0x5, %rbx
cmpq $0x0, 0x8(%r14,%rbx)
je 0x2300f
movl $0x1, %r12d
xorl %eax, %eax
movq (%r14,%rbx), %rcx
movl (%rcx,%rax,4), %r13d
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0x22340
testl %eax, %eax
je 0x23002
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %r13d, -0x4(%rax,%rcx,4)
movl %r12d, %eax
incl %r12d
cmpq %rax, 0x8(%r14,%rbx)
ja 0x22fdb
movss 0x18(%r14,%rbx), %xmm0
addss 0x18(%r15), %xmm0
movss %xmm0, 0x18(%r15)
movq -0x90(%rbp), %rcx
leal 0x2(%rcx), %eax
addl $0x3, %ecx
movq %rcx, %rbx
movq -0xd0(%rbp), %rdx
movq %rdx, %r8
movq -0x88(%rbp), %rsi
movq %rsi, %r15
movq -0x60(%rbp), %r9
movq %r9, %rdi
movq -0x80(%rbp), %r13
cmpq %rcx, %r13
movl %eax, %ecx
movq -0xc8(%rbp), %r12
ja 0x22f0a
xorl %eax, %eax
movq %r9, %rdi
movq %rsi, %r15
movq %rdx, %r8
movl -0x2c(%rbp), %r12d
movq -0x68(%rbp), %rbx
jmp 0x2308d
xorl %eax, %eax
jmp 0x2308d
movl $0x26b3, %eax           # imm = 0x26B3
movl -0x2c(%rbp), %r12d
movq -0x68(%rbp), %rbx
movq -0x60(%rbp), %rdi
movq %r8, -0x48(%rbp)
movq %r15, -0x40(%rbp)
movq %rdi, -0x50(%rbp)
cmpl %r12d, -0x30(%rbp)
movq -0xc0(%rbp), %r15
jae 0x230cd
movl 0x4c(%r15), %edx
leaq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rcx
callq 0x231c6
movl -0x30(%rbp), %ecx
cmpl %r12d, %ecx
jae 0x230cd
incl %ecx
testl %eax, %eax
je 0x22ea3
movq %r13, -0xa8(%rbp)
movq %rbx, -0xa0(%rbp)
movq %r14, -0xb0(%rbp)
testl %eax, %eax
movq -0xb8(%rbp), %r14
jne 0x2317f
jmp 0x230fe
movq $0x0, -0xb0(%rbp)
movq -0x70(%rbp), %rsi
decq %rsi
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x233b4
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x2315b
movq -0x50(%rbp), %rcx
movl -0x38(%rbp), %edx
xorl %esi, %esi
movq %rsi, %r9
imulq %rdx, %r9
movq 0x8(%rcx,%r9), %rdi
testq %rdi, %rdi
je 0x23151
movq (%r14), %r8
movq (%rcx,%r9), %r9
xorl %r10d, %r10d
movl (%r9,%r10,4), %r11d
incl (%r8,%r11,4)
incq %r10
movl %r10d, %r11d
cmpq %r11, %rdi
ja 0x2313e
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0x23123
movq %r15, %rdi
callq 0x2240c
jmp 0x2317f
movq %r13, -0xa8(%rbp)
movq %rbx, -0xa0(%rbp)
movq %r14, -0xb0(%rbp)
movl $0x26b2, %eax           # imm = 0x26B2
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %r14d
callq 0x233b4
movq (%rbx), %rdi
callq 0x4380
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x233b4
movq (%rbx), %rdi
callq 0x4380
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq