// File: /workspace/github_c_makefile_star2_O1/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 $0x15b8, %rsp           # imm = 0x15B8
movl %r8d, %r15d
movl %ecx, %r14d
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x20(%rsp), %rdi
xorl %r13d, %r13d
movl $0x84, %edx
xorl %esi, %esi
callq 0x10a0
movl %r12d, %r9d
movl %ebp, %r12d
testl %r15d, %r15d
je 0x6c33
testl %ebp, %ebp
jle 0x7028
leaq (%r9,%r9,8), %rax
shlq $0x5, %rax
addq %rbx, %rax
addq $0x8f52, %rax           # imm = 0x8F52
xorl %ecx, %ecx
movzbl (%rax,%rcx), %edx
incl 0x20(%rsp,%rdx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x6c1e
jmp 0x7028
leaq (%r9,%r9,8), %rdi
movq %rdi, %r8
shlq $0x6, %r8
testl %ebp, %ebp
jle 0x6c79
leaq (%rbx,%r8), %rax
addq $0x81d2, %rax           # imm = 0x81D2
xorl %ecx, %ecx
xorl %r13d, %r13d
movzwl (%rax,%rcx,2), %edx
testw %dx, %dx
je 0x6c71
movslq %r13d, %rsi
movw %dx, 0x8b0(%rsp,%rsi,4)
leal 0x1(%rsi), %r13d
movw %cx, 0x8b2(%rsp,%rsi,4)
incq %rcx
cmpq %rcx, %r12
jne 0x6c51
movq %r8, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq %r9, 0x18(%rsp)
leaq 0xb0(%rsp), %r15
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x10a0
movl %r13d, %eax
testl %r13d, %r13d
je 0x6cd0
xorl %ecx, %ecx
movzwl 0x8b0(%rsp,%rcx,4), %edx
movzbl %dl, %esi
incl 0xb0(%rsp,%rsi,4)
shrl $0x6, %edx
andl $-0x4, %edx
incl 0x4b0(%rsp,%rdx)
incq %rcx
cmpq %rcx, %rax
jne 0x6ca9
movl %ebp, 0x4(%rsp)
cmpl %r13d, 0x4b0(%rsp)
sete %sil
movl $0x2, %edi
movq %rdi, %rdx
movl $0x1, %edi
xorl %ecx, %ecx
testb $0x1, %sil
movl $0x0, %esi
jne 0x6ce5
leaq 0x8b0(%rsp), %rbp
leaq 0x1130(%rsp), %r8
xorl %esi, %esi
movq %rbp, %rdi
movq %r8, %rbp
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r9d, 0xd30(%rsp,%r8,4)
addl (%r15,%r8,4), %r9d
incq %r8
cmpq $0x100, %r8             # imm = 0x100
jne 0x6d18
testl %r13d, %r13d
je 0x6d69
xorl %r8d, %r8d
movzwl (%rdi,%r8,4), %r9d
shrl %cl, %r9d
movzbl %r9b, %r9d
movl 0xd30(%rsp,%r9,4), %r10d
leal 0x1(%r10), %r11d
movl %r11d, 0xd30(%rsp,%r9,4)
movl (%rdi,%r8,4), %r9d
movl %r9d, (%rbp,%r10,4)
incq %r8
cmpq %r8, %rax
jne 0x6d38
incq %rsi
addl $0x8, %ecx
addq $0x400, %r15            # imm = 0x400
movq %rdi, %r8
cmpq %rdx, %rsi
jne 0x6d0c
testl %r13d, %r13d
movq 0x10(%rsp), %r15
je 0x6ee7
cmpl $0x1, %r13d
jne 0x6d9d
movw $0x1, (%rbp)
jmp 0x6ee7
movzwl 0x4(%rbp), %ecx
addw %cx, (%rbp)
leal -0x1(%r13), %ecx
cmpl $0x3, %r13d
jl 0x6e4b
movl %ecx, %edx
xorl %r8d, %r8d
movl $0x2, %esi
movl $0x1, %edi
movslq %r8d, %r9
cmpl %r13d, %esi
jge 0x6ddb
movslq %esi, %r10
movzwl (%rbp,%r10,4), %r10d
cmpw %r10w, (%rbp,%r9,4)
jae 0x6df3
movzwl (%rbp,%r9,4), %r8d
movw %r8w, (%rbp,%rdi,4)
leal 0x1(%r9), %r8d
movw %di, (%rbp,%r9,4)
jmp 0x6dfb
incl %esi
movw %r10w, (%rbp,%rdi,4)
movslq %r8d, %r9
cmpl %r13d, %esi
jge 0x6e19
movslq %esi, %r10
cmpq %r9, %rdi
jle 0x6e31
movzwl (%rbp,%r9,4), %r11d
cmpw (%rbp,%r10,4), %r11w
jae 0x6e31
movzwl (%rbp,%r9,4), %r8d
addw %r8w, (%rbp,%rdi,4)
leal 0x1(%r9), %r8d
movw %di, (%rbp,%r9,4)
jmp 0x6e3f
incl %esi
movzwl (%rbp,%r10,4), %r9d
addw %r9w, (%rbp,%rdi,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x6dc2
movslq %r13d, %rdx
movw $0x0, -0x8(%rbp,%rdx,4)
cmpl $0x3, %r13d
jl 0x6e78
leal -0x3(%r13), %edx
incq %rdx
movzwl -0x4(%rbp,%rdx,4), %esi
movzwl (%rbp,%rsi,4), %esi
incl %esi
movw %si, -0x4(%rbp,%rdx,4)
decq %rdx
jg 0x6e62
leal -0x2(%r13), %edx
movl $0x1, %edi
xorl %esi, %esi
testl %edx, %edx
js 0x6ead
leal 0x1(%rdx), %r8d
xorl %r9d, %r9d
movl %edx, %r10d
movzwl (%rbp,%r10,4), %r10d
cmpl %r10d, %esi
jne 0x6eb2
incl %r9d
decl %edx
cmpl %r9d, %r8d
jne 0x6e8e
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x6eb5
xorl %r8d, %r8d
jmp 0x6eb5
movl %r9d, %r8d
cmpl %r8d, %edi
jle 0x6edc
movslq %ecx, %rcx
leaq (,%rcx,4), %r9
addq %rbp, %r9
addl %r8d, %ecx
subl %edi, %ecx
movw %si, (%r9)
decl %edi
addq $-0x4, %r9
cmpl %r8d, %edi
jg 0x6ecd
leal (%r8,%r8), %edi
incl %esi
testl %r8d, %r8d
jne 0x6e83
testl %r13d, %r13d
jle 0x6eff
xorl %ecx, %ecx
movzwl (%rbp,%rcx,4), %edx
incl 0x20(%rsp,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x6eee
cmpl $0x2, %r13d
jl 0x6f96
cmpl $0x1f, %r14d
jg 0x6f2a
movl %r14d, %eax
movl 0x20(%rsp,%rax,4), %ecx
movslq %r14d, %rdx
addl 0x24(%rsp,%rdx,4), %ecx
incq %rdx
movl %ecx, 0x20(%rsp,%rax,4)
cmpq $0x20, %rdx
jne 0x6f19
testl %r14d, %r14d
jle 0x6f4e
movl %r14d, %edx
incq %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movl 0x1c(%rsp,%rdx,4), %esi
shll %cl, %esi
addl %esi, %eax
decq %rdx
incl %ecx
cmpq $0x1, %rdx
jg 0x6f39
jmp 0x6f50
xorl %eax, %eax
movl $0x1, %edx
movl %r14d, %ecx
shlq %cl, %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
je 0x6f96
movl %r14d, %ecx
decl 0x20(%rsp,%rcx,4)
movq %rcx, %rsi
cmpl $0x2, %esi
jl 0x6f8f
movl 0x1c(%rsp,%rsi,4), %edi
decq %rsi
testl %edi, %edi
je 0x6f6c
decl %edi
movl %edi, 0x20(%rsp,%rsi,4)
incl %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addl $0x2, 0x20(%rsp,%rsi,4)
decl %eax
cmpq %rax, %rdx
jne 0x6f65
shlq $0x5, %r15
leaq (%rbx,%r15), %rdi
addq $0x8f52, %rdi           # imm = 0x8F52
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x10a0
movq 0x8(%rsp), %rax
leaq (%rbx,%rax), %rdi
addq $0x8892, %rdi           # imm = 0x8892
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x10a0
testl %r14d, %r14d
movq 0x18(%rsp), %r9
jle 0x7024
leaq 0x8f52(%rbx), %rax
leal 0x1(%r14), %ecx
addq $-0x2, %rbp
movl $0x1, %edx
addq %r15, %rax
movl 0x20(%rsp,%rdx,4), %esi
testl %esi, %esi
jle 0x701c
movslq %r13d, %r13
leaq (,%r13,4), %rdi
addq %rbp, %rdi
subl %esi, %r13d
incl %esi
movzwl (%rdi), %r8d
movb %dl, (%r8,%rax)
addq $-0x4, %rdi
decl %esi
cmpl $0x1, %esi
jg 0x7009
incq %rdx
cmpq %rcx, %rdx
jne 0x6fee
movl 0x4(%rsp), %ebp
movl $0x0, 0xb4(%rsp)
cmpl $0x2, %r14d
jl 0x7058
movl %r14d, %eax
decq %rax
xorl %ecx, %ecx
xorl %edx, %edx
addl 0x24(%rsp,%rcx,4), %edx
addl %edx, %edx
movl %edx, 0xb8(%rsp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x7043
testl %ebp, %ebp
jle 0x70c6
leaq 0x8f52(%rbx), %rsi
addq $0x8892, %rbx           # imm = 0x8892
xorl %eax, %eax
leaq (%r9,%r9,8), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
addq %rsi, %rdx
shlq $0x6, %rcx
addq %rbx, %rcx
movzbl (%rax,%rdx), %esi
testl %esi, %esi
je 0x70be
movl %esi, %r8d
movl 0xb0(%rsp,%r8,4), %edi
leal 0x1(%rdi), %r9d
movl %r9d, 0xb0(%rsp,%r8,4)
incl %esi
xorl %r8d, %r8d
movl %edi, %r9d
andl $0x1, %r9d
leal (%r9,%r8,2), %r8d
shrl %edi
decl %esi
cmpl $0x1, %esi
jg 0x70a5
movw %r8w, (%rcx,%rax,2)
incq %rax
cmpq %r12, %rax
jne 0x7081
addq $0x15b8, %rsp           # imm = 0x15B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq