// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %rdx, %rbx
movq %rdi, %r12
movl %ecx, -0x2c(%rbp)
movl %ecx, 0x48(%rdi)
testq %rdx, %rdx
je 0x241d8
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 0x241be
jmp 0x241dc
xorl %edi, %edi
xorl %edx, %edx
testq %rbx, %rbx
je 0x2427c
movq %rdi, -0x70(%rbp)
movq %rdx, -0x80(%rbp)
movq %rsi, -0x58(%rbp)
movl %ebx, 0x4c(%r12)
leaq 0x30(%r12), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x236c7
movl 0x4c(%r12), %r14d
movq 0x38(%r12), %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x236c7
subq %r15, %r14
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x24246
shlq $0x2, %r15
addq (%r13), %r15
shlq $0x2, %r14
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl %eax, %r14d
callq 0x5170
movl %r14d, %eax
testl %eax, %eax
je 0x24287
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x24292
cmpq $0x1, %rax
jne 0x242b6
xorl %eax, %eax
movq -0x58(%rbp), %rdx
cmpl $0x0, (%rdx,%rax,4)
je 0x24270
movq (%r13), %rcx
movl $0x1, (%rcx,%rax,4)
incq %rax
movl %eax, %ecx
cmpq %rbx, %rcx
jb 0x2425f
jmp 0x2429d
movl $0x50, %r15d
jmp 0x243b6
movl $0x26b1, %r15d          # imm = 0x26B1
jmp 0x243b6
movq 0x30(%r12), %rax
movl $0x1, (%rax)
movq %r12, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23793
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, -0xc0(%rbp)
movq %rax, -0xb0(%rbp)
movl %ecx, -0xa8(%rbp)
movl 0x4c(%r12), %edx
movq -0x58(%rbp), %rsi
movq -0x70(%rbp), %rcx
callq 0x2464c
movl %eax, %r15d
testl %eax, %eax
movl -0x2c(%rbp), %ebx
je 0x243cb
testl %r15d, %r15d
movq -0x80(%rbp), %rsi
jne 0x24370
decq %rsi
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x24856
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x24365
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 0x2435b
movq (%r13), %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 0x24348
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0x2432c
movq %r12, %rdi
callq 0x23793
movl %eax, %r15d
xorl %r14d, %r14d
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x24856
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq (%rbx), %rdi
callq 0x5390
movq %r14, (%rbx)
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x24856
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq (%rbx), %rdi
callq 0x5390
movq %r14, (%rbx)
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, %r15d
testl %ebx, %ebx
je 0x24302
movl $0x1, %r14d
xorl %r15d, %r15d
movq %r12, -0xd0(%rbp)
movq %r13, -0xc8(%rbp)
movq %r12, %rbx
movl %r14d, -0x64(%rbp)
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %r12
movaps -0xc0(%rbp), %xmm0
movq %r12, -0xb8(%rbp)
movq -0x40(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movq %rax, -0xb0(%rbp)
movaps %xmm0, -0x50(%rbp)
movq %r14, -0xc0(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x24856
testl %eax, %eax
je 0x2463e
movq %r15, -0x78(%rbp)
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %r13
movq -0x40(%rbp), %r15
cmpq $0x2, %r12
jb 0x245cf
movl -0x38(%rbp), %eax
movq %rax, -0x90(%rbp)
xorl %ecx, %ecx
movl $0x1, %ebx
movq %r12, -0xd8(%rbp)
leaq 0x1(%r13), %rax
movq %rax, %rdx
imulq -0x90(%rbp), %rdx
cmpq %r15, %rdx
movq %rcx, -0x98(%rbp)
movq %rax, -0xe0(%rbp)
jbe 0x244bc
addq %rdx, %rdx
movq %rdx, -0x88(%rbp)
movq %rdx, %rsi
movq %rdi, -0x60(%rbp)
callq 0x5500
movq -0x98(%rbp), %rcx
testq %rax, %rax
jne 0x244c6
jmp 0x2462c
movq %rdi, %rax
movq %r15, -0x88(%rbp)
imulq -0x90(%rbp), %r13
leaq (%rax,%r13), %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r13)
movq $0x0, 0x10(%rax,%r13)
movl %ecx, %r12d
shlq $0x5, %r12
movss 0x18(%r14,%r12), %xmm0
movq %rax, -0x60(%rbp)
movss %xmm0, 0x18(%rax,%r13)
movq 0x8(%r14,%r12), %rsi
movq %r15, %rdi
callq 0x236c7
testl %eax, %eax
je 0x2452e
movq 0x8(%r14,%r12), %rax
testq %rax, %rax
je 0x2452e
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 0x24520
shlq $0x5, %rbx
cmpq $0x0, 0x8(%r14,%rbx)
je 0x24576
movl $0x1, %r13d
xorl %eax, %eax
movq (%r14,%rbx), %rcx
movl (%rcx,%rax,4), %r12d
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0x236c7
testl %eax, %eax
je 0x24569
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %r12d, -0x4(%rax,%rcx,4)
movl %r13d, %eax
incl %r13d
cmpq %rax, 0x8(%r14,%rbx)
ja 0x24542
movss 0x18(%r14,%rbx), %xmm0
addss 0x18(%r15), %xmm0
movss %xmm0, 0x18(%r15)
movq -0x98(%rbp), %rcx
leal 0x2(%rcx), %eax
addl $0x3, %ecx
movq %rcx, %rbx
movq -0xe0(%rbp), %rdx
movq %rdx, %r13
movq -0x88(%rbp), %rsi
movq %rsi, %r15
movq -0x60(%rbp), %r8
movq %r8, %rdi
movq -0xd8(%rbp), %r12
cmpq %rcx, %r12
movl %eax, %ecx
ja 0x24473
movq %r8, %rdi
movq %rsi, %r15
movq %rdx, %r13
movl -0x2c(%rbp), %ebx
movq %r13, -0x48(%rbp)
movq %r15, -0x40(%rbp)
movq %rdi, -0x50(%rbp)
movl -0x64(%rbp), %r14d
cmpl %ebx, %r14d
movq -0xd0(%rbp), %r12
movq -0x78(%rbp), %r15
jae 0x2460b
movl 0x4c(%r12), %edx
leaq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x70(%rbp), %rcx
callq 0x2464c
movl %eax, %r15d
cmpl %ebx, %r14d
movq -0xc8(%rbp), %r13
jae 0x24302
incl %r14d
testl %r15d, %r15d
je 0x243f0
jmp 0x24302
movl $0x26b3, %eax           # imm = 0x26B3
movq %rax, -0x78(%rbp)
movl -0x2c(%rbp), %ebx
movq -0x60(%rbp), %rdi
jmp 0x245d2
movl $0x26b2, %r15d          # imm = 0x26B2
movq %rbx, %r12
jmp 0x24302