// File: /workspace/github_cpp_makefile_star2_O2/drummyfish[P]flowchart/src/lodepng.cpp
// Function: lodepng_huffman_code_lengths(unsigned int*, unsigned int const*, unsigned long, unsigned int)
unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
                                      size_t numcodes, unsigned maxbitlen)
{
  unsigned i, j;
  size_t sum = 0, numpresent = 0;
  unsigned error = 0;
  Coin* coins; /*the coins of the currently calculated row*/
  Coin* prev_row; /*the previous row of coins*/
  unsigned numcoins;
  unsigned coinmem;

  if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/

  for(i = 0; i < numcodes; i++)
  {
    if(frequencies[i] > 0)
    {
      numpresent++;
      sum += frequencies[i];
    }
  }

  for(i = 0; i < numcodes; i++) lengths[i] = 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) */
  if(numpresent == 0)
  {
    lengths[0] = 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)*/
  else if(numpresent == 1)
  {
    for(i = 0; i < numcodes; i++) if(frequencies[i]) lengths[i] = 1;
  }
  else
  {
    /*Package-Merge algorithm represented by coin collector's problem
    For every symbol, maxbitlen coins will be created*/

    coinmem = numpresent * 2; /*max amount of coins needed with the current algo*/
    coins = (Coin*)mymalloc(sizeof(Coin) * coinmem);
    prev_row = (Coin*)mymalloc(sizeof(Coin) * coinmem);
    if(!coins || !prev_row) return 83; /*alloc fail*/
    init_coins(coins, coinmem);
    init_coins(prev_row, coinmem);

    /*first row, lowest denominator*/
    error = append_symbol_coins(coins, frequencies, numcodes, sum);
    numcoins = numpresent;
    sort_coins(coins, numcoins);
    if(!error)
    {
      unsigned numprev = 0;
      for(j = 1; j <= maxbitlen && !error; j++) /*each of the remaining rows*/
      {
        unsigned tempnum;
        Coin* tempcoins;
        /*swap prev_row and coins, and their amounts*/
        tempcoins = prev_row; prev_row = coins; coins = tempcoins;
        tempnum = numprev; numprev = numcoins; numcoins = tempnum;

        cleanup_coins(coins, numcoins);
        init_coins(coins, numcoins);

        numcoins = 0;

        /*fill in the merged coins of the previous row*/
        for(i = 0; i + 1 < numprev; i += 2)
        {
          /*merge prev_row[i] and prev_row[i + 1] into new coin*/
          Coin* coin = &coins[numcoins++];
          coin_copy(coin, &prev_row[i]);
          add_coins(coin, &prev_row[i + 1]);
        }
        /*fill in all the original symbols again*/
        if(j < maxbitlen)
        {
          error = append_symbol_coins(coins + numcoins, frequencies, numcodes, sum);
          numcoins += numpresent;
        }
        sort_coins(coins, numcoins);
      }
    }

    if(!error)
    {
      /*calculate the lenghts of each symbol, as the amount of times a coin of each symbol is used*/
      for(i = 0; i < numpresent - 1; i++)
      {
        Coin* coin = &coins[i];
        for(j = 0; j < coin->symbols.size; j++) lengths[coin->symbols.data[j]]++;
      }
    }

    cleanup_coins(coins, coinmem);
    myfree(coins);
    cleanup_coins(prev_row, coinmem);
    myfree(prev_row);
  }

  return error;
}

// Assembly:
testq %rdx, %rdx
je 0x26bd
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r13
movq %rdi, %rbx
movl %ecx, 0x4(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %eax
cmpq %r13, %rax
jae 0x2683
movl (%rsi,%rax,4), %ecx
addq %rcx, %rdx
cmpq $0x1, %rcx
sbbq $-0x1, %r14
incl %eax
jmp 0x266a
xorl %eax, %eax
cmpl %r13d, %eax
jae 0x2693
andl $0x0, (%rbx,%rax,4)
incq %rax
jmp 0x2685
testq %r14, %r14
je 0x26c1
cmpq $0x1, %r14
jne 0x26cc
xorl %eax, %eax
xorl %ecx, %ecx
cmpl %r13d, %ecx
jae 0x2908
cmpl $0x0, (%rsi,%rcx,4)
je 0x26b8
movl $0x1, (%rbx,%rcx,4)
incq %rcx
jmp 0x26a2
pushq $0x50
popq %rax
retq
movl $0x1, (%rbx)
jmp 0x2906
movq %rdx, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
leal (%r14,%r14), %ebp
movq %rbp, %r12
shlq $0x5, %r12
movq %r12, %rdi
callq 0x2260
movq %rax, %r15
movq %r12, %rdi
callq 0x2260
movq %rax, %r12
testq %r15, %r15
sete %al
testq %r12, %r12
sete %cl
orb %al, %cl
jne 0x2895
movq %rbx, 0x38(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2917
movq %r12, %rdi
movq %rbp, 0x30(%rsp)
movq %rbp, %rsi
callq 0x2917
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %r13, 0x40(%rsp)
movl %r13d, %edx
movq 0x20(%rsp), %rcx
callq 0x292f
movl %r14d, %esi
movq %r15, %rdi
callq 0x29b7
pushq $0x1
popq %rcx
xorl %eax, %eax
movq %r14, 0x18(%rsp)
movl %r14d, %r13d
movl 0x4(%rsp), %ebx
movq %r12, %r14
movl %r13d, 0xc(%rsp)
movq %r15, %r12
cmpl %ebx, %ecx
ja 0x289a
movl %ecx, 0x8(%rsp)
movl %eax, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x2a64
movq %r14, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2917
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %r14d, %ebx
orl $0x1, %ebx
movl %r13d, %edi
cmpl 0xc(%rsp), %ebx
jae 0x2842
shlq $0x5, %rdi
movq 0x10(%rsp), %rax
leaq (%rax,%rdi), %r15
movl %r14d, %ebp
shlq $0x5, %rbp
movss 0x18(%r12,%rbp), %xmm0
movss %xmm0, 0x18(%rax,%rdi)
movq 0x8(%r12,%rbp), %rsi
movq %r15, %rdi
callq 0x91f8
testl %eax, %eax
je 0x2801
addq %r12, %rbp
movq 0x8(%rbp), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2801
movq (%rbp), %rdx
movl (%rdx,%rcx,4), %edx
movq (%r15), %rsi
movl %edx, (%rsi,%rcx,4)
incq %rcx
jmp 0x27ea
incl %r13d
movl %ebx, %ebx
shlq $0x5, %rbx
addq %r12, %rbx
xorl %ebp, %ebp
cmpq 0x8(%rbx), %rbp
jae 0x2828
movq (%rbx), %rax
movl (%rax,%rbp,4), %esi
movq %r15, %rdi
callq 0x91c5
incq %rbp
jmp 0x280f
movss 0x18(%rbx), %xmm0
addss 0x18(%r15), %xmm0
movss %xmm0, 0x18(%r15)
addl $0x2, %r14d
jmp 0x279c
movl 0x4(%rsp), %ebx
cmpl %ebx, 0x8(%rsp)
jae 0x2873
shlq $0x5, %rdi
movq 0x10(%rsp), %r15
addq %r15, %rdi
movq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x292f
addl 0x18(%rsp), %r13d
jmp 0x2878
movq 0x10(%rsp), %r15
movl %r13d, %esi
movq %r15, %rdi
callq 0x29b7
movl 0x8(%rsp), %ecx
incl %ecx
movq %r12, %r14
movl 0xc(%rsp), %eax
jmp 0x2764
pushq $0x53
popq %rax
jmp 0x2908
movq 0x18(%rsp), %r9
decq %r9
xorl %eax, %eax
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %rbx
movl %eax, %eax
cmpq %rax, %r9
jbe 0x28e0
movq %rax, %rdx
shlq $0x5, %rdx
leaq (%r12,%rdx), %rcx
movq 0x8(%r12,%rdx), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rdx
jbe 0x28dc
movq (%rcx), %rdi
movl (%rdi,%rsi,4), %edi
incl (%r8,%rdi,4)
incl %esi
jmp 0x28c7
incl %eax
jmp 0x28ae
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2a64
movq %r12, %rdi
callq 0x2200
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2a64
movq %r14, %rdi
callq 0x2200
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq