// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rdx, %rbx
movq %rdi, %r15
movl %ecx, -0x34(%rbp)
movl %ecx, 0x48(%rdi)
xorl %eax, %eax
xorl %edi, %edi
xorl %edx, %edx
movl %eax, %eax
cmpq %rbx, %rax
jae 0x16ef3
movl (%rsi,%rax,4), %ecx
addq %rcx, %rdi
cmpq $0x1, %rcx
sbbq $-0x1, %rdx
incl %eax
jmp 0x16eda
testq %rbx, %rbx
je 0x16f55
movq %rdi, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, -0x68(%rbp)
movl %ebx, 0x4c(%r15)
leaq 0x30(%r15), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x16562
movl 0x4c(%r15), %esi
movq %r12, %rdi
callq 0x16846
testl %eax, %eax
je 0x16f5d
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x16f67
cmpq $0x1, %rax
jne 0x16f8a
xorl %eax, %eax
movq -0x68(%rbp), %rdx
cmpl %ebx, %eax
jae 0x16f71
cmpl $0x0, (%rdx,%rax,4)
je 0x16f50
movq (%r12), %rcx
movl $0x1, (%rcx,%rax,4)
incq %rax
jmp 0x16f3b
pushq $0x50
popq %rbx
jmp 0x17214
movl $0x26b1, %ebx           # imm = 0x26B1
jmp 0x17214
movq 0x30(%r15), %rax
movl $0x1, (%rax)
movq %r15, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1664b
xorps %xmm0, %xmm0
leaq -0x60(%rbp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
pushq $0x20
popq %rax
andq $0x0, -0xa0(%rbp)
movl %eax, 0x18(%rdi)
movaps %xmm0, -0xb0(%rbp)
movl %eax, -0x98(%rbp)
movl 0x4c(%r15), %edx
movq -0x68(%rbp), %rsi
movq -0x80(%rbp), %rcx
callq 0x17228
movl %eax, %ebx
testl %eax, %eax
jne 0x171ff
xorl %eax, %eax
pushq $0x1
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebx, %ebx
movq %r15, -0xc0(%rbp)
movq %r12, -0xb8(%rbp)
cmpl -0x34(%rbp), %esi
ja 0x1715b
testl %ebx, %ebx
jne 0x1715b
movl %esi, -0x30(%rbp)
movq -0x60(%rbp), %r14
movq -0x58(%rbp), %rsi
movq %rsi, -0x70(%rbp)
movq %rdx, -0x58(%rbp)
movq -0x50(%rbp), %rdx
movq %rdx, -0x88(%rbp)
movq %rax, -0x50(%rbp)
movq %rcx, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x173bc
testl %eax, %eax
je 0x171ce
movl $0x0, -0x2c(%rbp)
xorl %r15d, %r15d
movl %r15d, %r12d
orl $0x1, %r12d
cmpq %r12, -0x70(%rbp)
jbe 0x17112
movq -0x58(%rbp), %rsi
incq %rsi
leaq -0x60(%rbp), %rdi
callq 0x16d47
testl %eax, %eax
je 0x1710b
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
decq %rcx
movl -0x48(%rbp), %edx
imulq %rcx, %rdx
leaq (%rax,%rdx), %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rdx)
andq $0x0, 0x10(%rax,%rdx)
movl %r15d, %r13d
shlq $0x5, %r13
movss 0x18(%r14,%r13), %xmm0
movss %xmm0, 0x18(%rax,%rdx)
movq 0x8(%r14,%r13), %rsi
movq %rbx, %rdi
callq 0x16562
testl %eax, %eax
je 0x170cb
movq 0x8(%r14,%r13), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x170cb
movq (%r14,%r13), %rdx
movl (%rdx,%rcx,4), %edx
movq (%rbx), %rsi
movl %edx, (%rsi,%rcx,4)
incq %rcx
jmp 0x170b4
shlq $0x5, %r12
xorl %eax, %eax
movl %eax, %r13d
cmpq %r13, 0x8(%r14,%r12)
jbe 0x170f1
movq (%r14,%r12), %rax
movl (%rax,%r13,4), %esi
movq %rbx, %rdi
callq 0x16dcc
leal 0x1(%r13), %eax
jmp 0x170d1
movss 0x18(%r14,%r12), %xmm0
addss 0x18(%rbx), %xmm0
movss %xmm0, 0x18(%rbx)
addl $0x2, %r15d
jmp 0x1703c
movl $0x26b3, -0x2c(%rbp)    # imm = 0x26B3
movl -0x30(%rbp), %esi
cmpl -0x34(%rbp), %esi
movq -0xc0(%rbp), %r15
movq -0xb8(%rbp), %r12
jae 0x17143
movl 0x4c(%r15), %edx
leaq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x80(%rbp), %rcx
callq 0x17228
movl -0x30(%rbp), %esi
movl %eax, -0x2c(%rbp)
incl %esi
movq -0x88(%rbp), %rax
movq -0x70(%rbp), %rdx
movq %r14, %rcx
movl -0x2c(%rbp), %ebx
jmp 0x16fe8
movq %rdx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
movq %rcx, -0xb0(%rbp)
testl %ebx, %ebx
jne 0x171ff
movq -0x78(%rbp), %rsi
decq %rsi
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
callq 0x173bc
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl 0x18(%rbx), %edx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x171f5
movq %rdx, %rdi
imulq %rsi, %rdi
movq 0x8(%rax,%rdi), %r8
xorl %r9d, %r9d
movl %r9d, %r9d
cmpq %r9, %r8
jbe 0x171ca
movq (%r12), %r10
movq (%rax,%rdi), %r11
movl (%r11,%r9,4), %r11d
incl (%r10,%r11,4)
incl %r9d
jmp 0x171ad
incl %esi
jmp 0x17197
movq -0x70(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq %r14, -0xb0(%rbp)
movl $0x26b2, %ebx           # imm = 0x26B2
jmp 0x171ff
movq %r15, %rdi
callq 0x1664b
movl %eax, %ebx
leaq -0x60(%rbp), %rdi
callq 0x1740b
leaq -0xb0(%rbp), %rdi
callq 0x1740b
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq