// File: /workspace/github_cpp_makefile_star2_O1/jimmiebergmann[P]fbx-file/miniz.c
// Function: tdefl_optimize_huffman_table(tdefl_compressor*, int, int, int, int)
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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x15b8, %rsp           # imm = 0x15B8
movl %r8d, %r12d
movq %rcx, -0x30(%rbp)
movl %edx, %r15d
movl %esi, %r13d
movq %rdi, %rbx
leaq -0xe0(%rbp), %rdi
xorl %r14d, %r14d
movl $0x84, %edx
xorl %esi, %esi
callq 0x3160
movl %r13d, %r9d
movl %r15d, %r13d
testl %r12d, %r12d
je 0xf73d
testl %r15d, %r15d
movq -0x30(%rbp), %r10
jle 0xfb36
leaq (%r9,%r9,8), %rax
shlq $0x5, %rax
addq %rbx, %rax
addq $0x8f4a, %rax           # imm = 0x8F4A
xorl %ecx, %ecx
movzbl (%rax,%rcx), %edx
incl -0xe0(%rbp,%rdx,4)
incq %rcx
cmpq %rcx, %r13
jne 0xf725
jmp 0xfb36
leaq (%r9,%r9,8), %rdi
movq %rdi, %r8
shlq $0x6, %r8
testl %r15d, %r15d
jle 0xf784
leaq (%rbx,%r8), %rax
addq $0x81ca, %rax           # imm = 0x81CA
xorl %ecx, %ecx
xorl %r14d, %r14d
movzwl (%rax,%rcx,2), %edx
testw %dx, %dx
je 0xf77c
movslq %r14d, %rsi
movw %dx, -0xd60(%rbp,%rsi,4)
leal 0x1(%rsi), %r14d
movw %cx, -0xd5e(%rbp,%rsi,4)
incq %rcx
cmpq %rcx, %r13
jne 0xf75c
movq %r8, -0x40(%rbp)
movq %rdi, -0x48(%rbp)
movq %r9, -0x50(%rbp)
leaq -0x8e0(%rbp), %r12
movl $0x800, %edx            # imm = 0x800
movq %r12, %rdi
xorl %esi, %esi
callq 0x3160
movl %r14d, %eax
testl %r14d, %r14d
je 0xf7d7
xorl %ecx, %ecx
movzwl -0xd60(%rbp,%rcx,4), %edx
movzbl %dl, %esi
incl -0x8e0(%rbp,%rsi,4)
shrl $0x6, %edx
andl $-0x4, %edx
incl -0x4e0(%rbp,%rdx)
incq %rcx
cmpq %rcx, %rax
jne 0xf7b0
movl %r15d, -0x34(%rbp)
cmpl %r14d, -0x4e0(%rbp)
sete %sil
movl $0x2, %edi
movq %rdi, %rdx
movl $0x1, %edi
xorl %ecx, %ecx
testb $0x1, %sil
movl $0x0, %esi
jne 0xf7eb
leaq -0xd60(%rbp), %r15
leaq -0x15e0(%rbp), %r8
xorl %esi, %esi
movq %r15, %rdi
movq %r8, %r15
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r9d, -0x1160(%rbp,%r8,4)
addl (%r12,%r8,4), %r9d
incq %r8
cmpq $0x100, %r8             # imm = 0x100
jne 0xf81c
testl %r14d, %r14d
je 0xf86c
xorl %r8d, %r8d
movzwl (%rdi,%r8,4), %r9d
shrl %cl, %r9d
movzbl %r9b, %r9d
movl -0x1160(%rbp,%r9,4), %r10d
leal 0x1(%r10), %r11d
movl %r11d, -0x1160(%rbp,%r9,4)
movl (%rdi,%r8,4), %r9d
movl %r9d, (%r15,%r10,4)
incq %r8
cmpq %r8, %rax
jne 0xf83c
incq %rsi
addl $0x8, %ecx
addq $0x400, %r12            # imm = 0x400
movq %rdi, %r8
cmpq %rdx, %rsi
jne 0xf810
testl %r14d, %r14d
movq -0x30(%rbp), %r11
je 0xf9d8
cmpl $0x1, %r14d
jne 0xf89f
movw $0x1, (%r15)
jmp 0xf9d8
movzwl 0x4(%r15), %ecx
addw %cx, (%r15)
leal -0x1(%r14), %ecx
cmpl $0x3, %r14d
jl 0xf941
movl %ecx, %edx
xorl %r8d, %r8d
movl $0x2, %esi
movl $0x1, %edi
movslq %r8d, %r9
cmpl %r14d, %esi
jge 0xf8dc
movslq %esi, %r10
movzwl (%r15,%r10,4), %r10d
cmpw %r10w, (%r15,%r9,4)
jae 0xf8f1
movzwl (%r15,%r9,4), %r8d
movw %r8w, (%r15,%rdi,4)
leal 0x1(%r9), %r8d
movw %di, (%r15,%r9,4)
jmp 0xf8f8
incl %esi
movw %r10w, (%r15,%rdi,4)
movslq %r8d, %r9
cmpl %r14d, %esi
jge 0xf918
movslq %esi, %r10
cmpq %r9, %rdi
jle 0xf92d
movzwl (%r15,%r9,4), %r11d
cmpw (%r15,%r10,4), %r11w
movq -0x30(%rbp), %r11
jae 0xf92d
movzwl (%r15,%r9,4), %r8d
addw %r8w, (%r15,%rdi,4)
leal 0x1(%r9), %r8d
movw %di, (%r15,%r9,4)
jmp 0xf939
incl %esi
movzwl (%r15,%r10,4), %r9d
addw %r9w, (%r15,%rdi,4)
incq %rdi
cmpq %rdi, %rdx
jne 0xf8c5
movslq %r14d, %rdx
movw $0x0, -0x8(%r15,%rdx,4)
cmpl $0x3, %r14d
jl 0xf971
leal -0x3(%r14), %edx
incq %rdx
movzwl -0x4(%r15,%rdx,4), %esi
movzwl (%r15,%rsi,4), %esi
incl %esi
movw %si, -0x4(%r15,%rdx,4)
decq %rdx
jg 0xf959
leal -0x2(%r14), %edx
movl $0x1, %edi
xorl %esi, %esi
testl %edx, %edx
js 0xf9a5
leal 0x1(%rdx), %r8d
xorl %r9d, %r9d
movl %edx, %r10d
movzwl (%r15,%r10,4), %r10d
cmpl %r10d, %esi
jne 0xf9aa
incl %r9d
decl %edx
cmpl %r9d, %r8d
jne 0xf987
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0xf9ad
xorl %r8d, %r8d
jmp 0xf9ad
movl %r9d, %r8d
cmpl %r8d, %edi
jle 0xf9cd
movslq %ecx, %rcx
leaq (%r15,%rcx,4), %r9
addl %r8d, %ecx
subl %edi, %ecx
movw %si, (%r9)
decl %edi
addq $-0x4, %r9
cmpl %r8d, %edi
jg 0xf9be
leal (%r8,%r8), %edi
incl %esi
testl %r8d, %r8d
jne 0xf97c
testl %r14d, %r14d
jle 0xf9f3
xorl %ecx, %ecx
movzwl (%r15,%rcx,4), %edx
incl -0xe0(%rbp,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xf9df
cmpl $0x2, %r14d
jl 0xfaa2
cmpl $0x1f, %r11d
jg 0xfa27
movl %r11d, %eax
movl -0xe0(%rbp,%rax,4), %ecx
movslq %r11d, %rdx
addl -0xdc(%rbp,%rdx,4), %ecx
incq %rdx
movl %ecx, -0xe0(%rbp,%rax,4)
cmpq $0x20, %rdx
jne 0xfa10
testl %r11d, %r11d
jle 0xfa4e
movl %r11d, %edx
incq %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movl -0xe4(%rbp,%rdx,4), %esi
shll %cl, %esi
addl %esi, %eax
decq %rdx
incl %ecx
cmpq $0x1, %rdx
jg 0xfa36
jmp 0xfa50
xorl %eax, %eax
movl $0x1, %edx
movl %r11d, %ecx
shlq %cl, %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
je 0xfaa2
movl %r11d, %ecx
decl -0xe0(%rbp,%rcx,4)
movq %rcx, %rsi
cmpl $0x2, %esi
jl 0xfa9b
movl -0xe4(%rbp,%rsi,4), %edi
decq %rsi
testl %edi, %edi
je 0xfa6f
decl %edi
movl %edi, -0xe0(%rbp,%rsi,4)
incl %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addl $0x2, -0xe0(%rbp,%rsi,4)
decl %eax
cmpq %rax, %rdx
jne 0xfa65
movq -0x48(%rbp), %r12
shlq $0x5, %r12
leaq (%rbx,%r12), %rdi
addq $0x8f4a, %rdi           # imm = 0x8F4A
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x3160
movq -0x40(%rbp), %rax
leaq (%rbx,%rax), %rdi
addq $0x888a, %rdi           # imm = 0x888A
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x3160
movq -0x30(%rbp), %r10
testl %r10d, %r10d
movq -0x50(%rbp), %r9
jle 0xfb32
leaq 0x8f4a(%rbx), %rax
leal 0x1(%r10), %ecx
addq $-0x2, %r15
movl $0x1, %edx
addq %r12, %rax
movl -0xe0(%rbp,%rdx,4), %esi
testl %esi, %esi
jle 0xfb2a
movslq %r14d, %r14
leaq (%r15,%r14,4), %rdi
subl %esi, %r14d
incl %esi
movzwl (%rdi), %r8d
movb %dl, (%r8,%rax)
addq $-0x4, %rdi
decl %esi
cmpl $0x1, %esi
jg 0xfb17
incq %rdx
cmpq %rcx, %rdx
jne 0xfb00
movl -0x34(%rbp), %r15d
movl $0x0, -0x8dc(%rbp)
cmpl $0x2, %r10d
jl 0xfb68
movl %r10d, %eax
decq %rax
xorl %ecx, %ecx
xorl %edx, %edx
addl -0xdc(%rbp,%rcx,4), %edx
addl %edx, %edx
movl %edx, -0x8d8(%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xfb50
testl %r15d, %r15d
jle 0xfbd7
leaq 0x8f4a(%rbx), %rsi
addq $0x888a, %rbx           # imm = 0x888A
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 0xfbcf
movl %esi, %r8d
movl -0x8e0(%rbp,%r8,4), %edi
leal 0x1(%rdi), %r9d
movl %r9d, -0x8e0(%rbp,%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 0xfbb6
movw %r8w, (%rcx,%rax,2)
incq %rax
cmpq %r13, %rax
jne 0xfb92
addq $0x15b8, %rsp           # imm = 0x15B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq