// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/../foreign/miniz-c.h
// 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_ARR(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_ARR(d->m_huff_code_sizes[table_num]);
        MZ_CLEAR_ARR(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 0x18140
movl %r12d, %esi
movslq %r15d, %r13
cmpl $0x0, 0x10(%rsp)
je 0x1018c1
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x120, %rsi, %rcx      # imm = 0x120
addq %r14, %rcx
addq $0x8f4a, %rcx           # imm = 0x8F4A
cmpq %rbp, %rax
je 0x101aea
movzbl (%rcx,%rbp), %edx
incl 0x30(%rsp,%rdx,4)
incq %rbp
jmp 0x1018ab
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 $0x81ca, %rax           # imm = 0x81CA
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
xorl %edx, %edx
movslq %ebp, %r14
cmpq %rdx, %rcx
je 0x10191d
movzwl (%rax,%rdx,2), %esi
testw %si, %si
je 0x101918
movw %si, 0x8c0(%rsp,%r14,4)
leal 0x1(%r14), %ebp
movw %dx, 0x8c2(%rsp,%r14,4)
incq %rdx
jmp 0x1018f1
leaq 0xc0(%rsp), %r15
xorl %r12d, %r12d
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x18140
movl %ebp, %eax
cmpq %r12, %rax
je 0x101963
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 0x101939
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 0x101975
leaq 0x1140(%rsp), %rsi
leaq 0x8c0(%rsp), %r8
xorl %edi, %edi
movq %r8, %r12
cmpq %rdx, %rdi
je 0x101a0c
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq $0x100, %r8             # imm = 0x100
je 0x1019c2
movl %r9d, 0xd40(%rsp,%r8,4)
addl (%r15,%r8,4), %r9d
incq %r8
jmp 0x1019a8
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x1019f7
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 0x1019c5
incq %rdi
addl $0x8, %ecx
addq $0x400, %r15            # imm = 0x400
movq %rsi, %r8
movq %r12, %rsi
jmp 0x10199a
testl %ebp, %ebp
je 0x101a20
cmpl $0x1, %ebp
jne 0x101c04
movw $0x1, (%r12)
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
movq 0x18(%rsp), %r8
cmpq %rax, %rcx
je 0x101a44
movzwl (%r12,%rax,4), %edx
incl 0x30(%rsp,%rdx,4)
incq %rax
jmp 0x101a31
cmpl $0x2, %ebp
jge 0x101b8f
leaq 0x8f4a(%r8), %r14
imulq $0x120, 0x10(%rsp), %rax # imm = 0x120
movq %rax, 0x20(%rsp)
leaq (%r8,%rax), %rdi
addq $0x8f4a, %rdi           # imm = 0x8F4A
xorl %r15d, %r15d
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x18140
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %rdi
addq $0x888a, %rdi           # imm = 0x888A
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x18140
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 0x101ae0
movl 0x30(%rsp,%rax,4), %ecx
movslq %ebp, %rdx
leaq (%r12,%rdx,4), %rdx
testl %ecx, %ecx
jle 0x101adb
movzwl (%rdx), %esi
movb %al, (%rsi,%r14)
decl %ecx
decl %ebp
addq $-0x4, %rdx
jmp 0x101ac6
incq %rax
jmp 0x101ab6
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rsi
pushq $0x2
popq %rax
andl $0x0, 0xc4(%rsp)
xorl %ecx, %ecx
cmpq %r13, %rax
jg 0x101b0e
addl 0x2c(%rsp,%rax,4), %ecx
addl %ecx, %ecx
movl %ecx, 0xc0(%rsp,%rax,4)
incq %rax
jmp 0x101af7
leaq 0x8f4a(%r14), %rdx
addq $0x888a, %r14           # imm = 0x888A
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 0x101b7d
movzbl (%rax,%rcx), %esi
testl %esi, %esi
je 0x101b78
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 0x101b73
movl %edi, %r9d
andl $0x1, %r9d
leal (%r9,%r8,2), %r8d
decl %esi
shrl %edi
jmp 0x101b5e
movw %r8w, (%rdx,%rax,2)
incq %rax
jmp 0x101b37
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 0x101ba9
movl 0x34(%rsp,%rcx,4), %edx
incq %rcx
addl %edx, 0x30(%rsp,%rax,4)
jmp 0x101b96
xorl %ecx, %ecx
movq %rax, %rsi
xorl %edx, %edx
testl %esi, %esi
jle 0x101bc3
movl 0x30(%rsp,%rsi,4), %edi
shll %cl, %edi
addl %edi, %edx
decq %rsi
incl %ecx
jmp 0x101bb0
pushq $0x1
popq %rsi
movl %eax, %ecx
shlq %cl, %rsi
movl %edx, %ecx
cmpq %rcx, %rsi
je 0x101a4d
decl 0x30(%rsp,%rax,4)
movq %rax, %rcx
cmpl $0x2, %ecx
jl 0x101c00
movl 0x2c(%rsp,%rcx,4), %edi
decq %rcx
testl %edi, %edi
je 0x101bdd
decl %edi
movl %edi, 0x30(%rsp,%rcx,4)
incl %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl $0x2, 0x30(%rsp,%rcx,4)
decl %edx
jmp 0x101bcb
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 0x101c98
movslq %edi, %r8
movzwl (%r12,%r8,4), %r9d
cmpl %ebp, %edx
jge 0x101c3c
movslq %edx, %r10
movzwl (%r12,%r10,4), %r10d
cmpw %r10w, %r9w
jae 0x101c4f
movw %r9w, (%r12,%rsi,4)
leal 0x1(%r8), %edi
movw %si, (%r12,%r8,4)
movslq %edi, %r8
jmp 0x101c56
incl %edx
movw %r10w, (%r12,%rsi,4)
cmpl %ebp, %edx
jge 0x101c74
movslq %edx, %r9
cmpq %r8, %rsi
jle 0x101c87
movzwl (%r12,%r8,4), %r10d
movzwl (%r12,%r9,4), %r9d
cmpw %r9w, %r10w
jb 0x101c79
jmp 0x101c8c
movzwl (%r12,%r8,4), %r10d
addw %r10w, (%r12,%rsi,4)
incl %edi
movw %si, (%r12,%r8,4)
jmp 0x101c93
movzwl (%r12,%r9,4), %r9d
incl %edx
addw %r9w, (%r12,%rsi,4)
incq %rsi
jmp 0x101c1d
leal -0x2(%rbp), %ecx
andw $0x0, -0x8(%r12,%r14,4)
leal -0x3(%rbp), %edx
testl %edx, %edx
js 0x101cc0
movl %edx, %esi
movzwl (%r12,%rsi,4), %edi
movzwl (%r12,%rdi,4), %edi
incl %edi
movw %di, (%r12,%rsi,4)
decl %edx
jmp 0x101ca5
xorl %edx, %edx
pushq $0x1
popq %rdi
pushq $-0x1
popq %rsi
testl %edi, %edi
je 0x101a20
cmpl $-0x1, %ecx
movl %esi, %r9d
cmovll %ecx, %r9d
movl %ecx, %r8d
subl %r9d, %r8d
xorl %r10d, %r10d
cmpl %r8d, %r10d
je 0x101cfc
movl %ecx, %r11d
movzwl (%r12,%r11,4), %r11d
cmpl %r11d, %edx
jne 0x101d01
incl %r10d
decl %ecx
jmp 0x101ce3
movl %r9d, %ecx
jmp 0x101d04
movl %r10d, %r8d
movslq %eax, %r9
leaq (%r12,%r9,4), %r9
cmpl %r8d, %edi
jle 0x101d1e
movw %dx, (%r9)
decl %edi
decl %eax
addq $-0x4, %r9
jmp 0x101d0b
addl %r8d, %r8d
incl %edx
movl %r8d, %edi
jmp 0x101cc8