// File: /workspace/github_c_makefile_star2_O2/wkoszek[P]mini_gzip/miniz.c
// Function: tdefl_optimize_huffman_table
static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
{
  int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; MZ_CLEAR_OBJ(num_codes);
  if (static_table)
  {
    for (i = 0; i < table_len; i++) num_codes[d->m_huff_code_sizes[table_num][i]]++;
  }
  else
  {
    tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
    int num_used_syms = 0;
    const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
    for (i = 0; i < table_len; i++) if (pSym_count[i]) { syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; syms0[num_used_syms++].m_sym_index = (mz_uint16)i; }

    pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);

    for (i = 0; i < num_used_syms; i++) num_codes[pSyms[i].m_key]++;

    tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);

    MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); MZ_CLEAR_OBJ(d->m_huff_codes[table_num]);
    for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
      for (l = num_codes[i]; l > 0; l--) d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
  }

  next_code[1] = 0; for (j = 0, i = 2; i <= code_size_limit; i++) next_code[i] = j = ((j + num_codes[i - 1]) << 1);

  for (i = 0; i < table_len; i++)
  {
    mz_uint rev_code = 0, code, code_size; if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) continue;
    code = next_code[code_size]++; for (l = code_size; l > 0; l--, code >>= 1) rev_code = (rev_code << 1) | (code & 1);
    d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x15c8, %rsp           # imm = 0x15C8
movl %r8d, 0x10(%rsp)
movl %ecx, %r15d
movl %edx, %ebx
movl %esi, %r12d
movq %rdi, %r14
leaq 0x30(%rsp), %rdi
xorl %ebp, %ebp
movl $0x84, %edx
xorl %esi, %esi
callq 0x10a0
movl %r12d, %esi
movslq %r15d, %r13
cmpl $0x0, 0x10(%rsp)
je 0x66e8
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x120, %rsi, %rcx      # imm = 0x120
addq %r14, %rcx
addq $0x8f52, %rcx           # imm = 0x8F52
cmpq %rbp, %rax
je 0x6911
movzbl (%rcx,%rbp), %edx
incl 0x30(%rsp,%rdx,4)
incq %rbp
jmp 0x66d2
movl %r15d, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
imulq $0x240, %rsi, %rax      # imm = 0x240
movq %r14, 0x18(%rsp)
movq %rax, 0x28(%rsp)
addq %r14, %rax
addq $0x81d2, %rax           # imm = 0x81D2
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
xorl %edx, %edx
movslq %ebp, %r14
cmpq %rdx, %rcx
je 0x6744
movzwl (%rax,%rdx,2), %esi
testw %si, %si
je 0x673f
movw %si, 0x8c0(%rsp,%r14,4)
leal 0x1(%r14), %ebp
movw %dx, 0x8c2(%rsp,%r14,4)
incq %rdx
jmp 0x6718
leaq 0xc0(%rsp), %r15
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x10a0
movl %ebp, %eax
cmpq %r12, %rax
je 0x678a
movzwl 0x8c0(%rsp,%r12,4), %ecx
movzbl %cl, %edx
incl 0xc0(%rsp,%rdx,4)
shrl $0x6, %ecx
andl $-0x4, %ecx
incl 0x4c0(%rsp,%rcx)
incq %r12
jmp 0x6760
cmpl %ebp, 0x4c0(%rsp)
sete %sil
pushq $0x2
popq %r8
pushq $0x1
popq %rdi
movq %r8, %rdx
xorl %ecx, %ecx
movq %rdi, %r8
testb $0x1, %sil
movl $0x0, %esi
jne 0x679c
leaq 0x1140(%rsp), %rsi
leaq 0x8c0(%rsp), %r8
xorl %edi, %edi
movq %r8, %r12
cmpq %rdx, %rdi
je 0x6833
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq $0x100, %r8             # imm = 0x100
je 0x67e9
movl %r9d, 0xd40(%rsp,%r8,4)
addl (%r15,%r8,4), %r9d
incq %r8
jmp 0x67cf
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x681e
movzwl (%r12,%r8,4), %r9d
shrl %cl, %r9d
movzbl %r9b, %r9d
movl 0xd40(%rsp,%r9,4), %r10d
leal 0x1(%r10), %r11d
movl %r11d, 0xd40(%rsp,%r9,4)
movl (%r12,%r8,4), %r9d
movl %r9d, (%rsi,%r10,4)
incq %r8
jmp 0x67ec
incq %rdi
addl $0x8, %ecx
addq $0x400, %r15            # imm = 0x400
movq %rsi, %r8
movq %r12, %rsi
jmp 0x67c1
testl %ebp, %ebp
je 0x6847
cmpl $0x1, %ebp
jne 0x6a2b
movw $0x1, (%r12)
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
movq 0x18(%rsp), %r8
cmpq %rax, %rcx
je 0x686b
movzwl (%r12,%rax,4), %edx
incl 0x30(%rsp,%rdx,4)
incq %rax
jmp 0x6858
cmpl $0x2, %ebp
jge 0x69b6
leaq 0x8f52(%r8), %r14
imulq $0x120, 0x10(%rsp), %rax # imm = 0x120
movq %rax, 0x20(%rsp)
leaq (%r8,%rax), %rdi
addq $0x8f52, %rdi           # imm = 0x8F52
xorl %r15d, %r15d
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x10a0
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %rdi
addq $0x8892, %rdi           # imm = 0x8892
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x10a0
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %r15d
incl %r15d
addq $-0x2, %r12
pushq $0x1
popq %rax
addq 0x20(%rsp), %r14
cmpq %r15, %rax
je 0x6907
movl 0x30(%rsp,%rax,4), %ecx
movslq %ebp, %rdx
leaq (%r12,%rdx,4), %rdx
testl %ecx, %ecx
jle 0x6902
movzwl (%rdx), %esi
movb %al, (%rsi,%r14)
decl %ecx
decl %ebp
addq $-0x4, %rdx
jmp 0x68ed
incq %rax
jmp 0x68dd
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rsi
pushq $0x2
popq %rax
andl $0x0, 0xc4(%rsp)
xorl %ecx, %ecx
cmpq %r13, %rax
jg 0x6935
addl 0x2c(%rsp,%rax,4), %ecx
addl %ecx, %ecx
movl %ecx, 0xc0(%rsp,%rax,4)
incq %rax
jmp 0x691e
leaq 0x8f52(%r14), %rdx
addq $0x8892, %r14           # imm = 0x8892
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
imulq $0x120, %rsi, %rcx      # imm = 0x120
addq %rdx, %rcx
imulq $0x240, %rsi, %rdx      # imm = 0x240
addq %r14, %rdx
cmpq %rbx, %rax
je 0x69a4
movzbl (%rax,%rcx), %esi
testl %esi, %esi
je 0x699f
movl %esi, %r8d
movl 0xc0(%rsp,%r8,4), %edi
leal 0x1(%rdi), %r9d
movl %r9d, 0xc0(%rsp,%r8,4)
xorl %r8d, %r8d
testl %esi, %esi
jle 0x699a
movl %edi, %r9d
andl $0x1, %r9d
leal (%r9,%r8,2), %r8d
decl %esi
shrl %edi
jmp 0x6985
movw %r8w, (%rdx,%rax,2)
incq %rax
jmp 0x695e
addq $0x15c8, %rsp           # imm = 0x15C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %eax
movq %r13, %rcx
cmpq $0x20, %rcx
jge 0x69d0
movl 0x34(%rsp,%rcx,4), %edx
incq %rcx
addl %edx, 0x30(%rsp,%rax,4)
jmp 0x69bd
xorl %ecx, %ecx
movq %rax, %rsi
xorl %edx, %edx
testl %esi, %esi
jle 0x69ea
movl 0x30(%rsp,%rsi,4), %edi
shll %cl, %edi
addl %edi, %edx
decq %rsi
incl %ecx
jmp 0x69d7
pushq $0x1
popq %rsi
movl %eax, %ecx
shlq %cl, %rsi
movl %edx, %ecx
cmpq %rcx, %rsi
je 0x6874
decl 0x30(%rsp,%rax,4)
movq %rax, %rcx
cmpl $0x2, %ecx
jl 0x6a27
movl 0x2c(%rsp,%rcx,4), %edi
decq %rcx
testl %edi, %edi
je 0x6a04
decl %edi
movl %edi, 0x30(%rsp,%rcx,4)
incl %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl $0x2, 0x30(%rsp,%rcx,4)
decl %edx
jmp 0x69f2
movzwl 0x4(%r12), %eax
addw %ax, (%r12)
leal -0x1(%rbp), %eax
movslq %eax, %rcx
pushq $0x2
popq %rdx
xorl %edi, %edi
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
jge 0x6abf
movslq %edi, %r8
movzwl (%r12,%r8,4), %r9d
cmpl %ebp, %edx
jge 0x6a63
movslq %edx, %r10
movzwl (%r12,%r10,4), %r10d
cmpw %r10w, %r9w
jae 0x6a76
movw %r9w, (%r12,%rsi,4)
leal 0x1(%r8), %edi
movw %si, (%r12,%r8,4)
movslq %edi, %r8
jmp 0x6a7d
incl %edx
movw %r10w, (%r12,%rsi,4)
cmpl %ebp, %edx
jge 0x6a9b
movslq %edx, %r9
cmpq %r8, %rsi
jle 0x6aae
movzwl (%r12,%r8,4), %r10d
movzwl (%r12,%r9,4), %r9d
cmpw %r9w, %r10w
jb 0x6aa0
jmp 0x6ab3
movzwl (%r12,%r8,4), %r10d
addw %r10w, (%r12,%rsi,4)
incl %edi
movw %si, (%r12,%r8,4)
jmp 0x6aba
movzwl (%r12,%r9,4), %r9d
incl %edx
addw %r9w, (%r12,%rsi,4)
incq %rsi
jmp 0x6a44
leal -0x2(%rbp), %ecx
andw $0x0, -0x8(%r12,%r14,4)
leal -0x3(%rbp), %edx
testl %edx, %edx
js 0x6ae7
movl %edx, %esi
movzwl (%r12,%rsi,4), %edi
movzwl (%r12,%rdi,4), %edi
incl %edi
movw %di, (%r12,%rsi,4)
decl %edx
jmp 0x6acc
xorl %edx, %edx
pushq $0x1
popq %rdi
pushq $-0x1
popq %rsi
testl %edi, %edi
je 0x6847
cmpl $-0x1, %ecx
movl %esi, %r9d
cmovll %ecx, %r9d
movl %ecx, %r8d
subl %r9d, %r8d
xorl %r10d, %r10d
cmpl %r8d, %r10d
je 0x6b23
movl %ecx, %r11d
movzwl (%r12,%r11,4), %r11d
cmpl %r11d, %edx
jne 0x6b28
incl %r10d
decl %ecx
jmp 0x6b0a
movl %r9d, %ecx
jmp 0x6b2b
movl %r10d, %r8d
movslq %eax, %r9
leaq (%r12,%r9,4), %r9
cmpl %r8d, %edi
jle 0x6b45
movw %dx, (%r9)
decl %edi
decl %eax
addq $-0x4, %r9
jmp 0x6b32
addl %r8d, %r8d
incl %edx
movl %r8d, %edi
jmp 0x6aef