// File: /workspace/github_cpp_makefile_star2_O2/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, -0x38(%rbp)
movl %ecx, %r12d
movl %edx, %ebx
movl %esi, %r13d
movq %rdi, %r15
leaq -0xe0(%rbp), %rdi
xorl %r14d, %r14d
movl $0x84, %edx
xorl %esi, %esi
callq 0x3180
movl %r13d, %esi
movslq %r12d, %rdi
cmpl $0x0, -0x38(%rbp)
je 0xf0e6
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x120, %rsi, %rcx      # imm = 0x120
addq %r15, %rcx
addq $0x8f4a, %rcx           # imm = 0x8F4A
cmpq %r14, %rax
je 0xf31e
movzbl (%rcx,%r14), %edx
incl -0xe0(%rbp,%rdx,4)
incq %r14
jmp 0xf0cc
movl %r12d, -0x2c(%rbp)
movq %rsi, -0x38(%rbp)
imulq $0x240, %rsi, %rax      # imm = 0x240
movq %r15, -0x40(%rbp)
movq %rax, -0x58(%rbp)
addq %r15, %rax
addq $0x81ca, %rax           # imm = 0x81CA
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
xorl %edx, %edx
movslq %r14d, %r15
cmpq %rdx, %rcx
je 0xf13e
movzwl (%rax,%rdx,2), %esi
testw %si, %si
je 0xf139
movw %si, -0xd60(%rbp,%r15,4)
leal 0x1(%r15), %r14d
movw %dx, -0xd5e(%rbp,%r15,4)
incq %rdx
jmp 0xf112
movq %rdi, -0x48(%rbp)
leaq -0x8e0(%rbp), %r12
xorl %r13d, %r13d
movl $0x800, %edx            # imm = 0x800
movq %r12, %rdi
xorl %esi, %esi
callq 0x3180
movl %r14d, %eax
cmpq %r13, %rax
je 0xf188
movzwl -0xd60(%rbp,%r13,4), %ecx
movzbl %cl, %edx
incl -0x8e0(%rbp,%rdx,4)
shrl $0x6, %ecx
andl $-0x4, %ecx
incl -0x4e0(%rbp,%rcx)
incq %r13
jmp 0xf15e
cmpl %r14d, -0x4e0(%rbp)
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 0xf19a
leaq -0x15e0(%rbp), %rsi
leaq -0xd60(%rbp), %r8
xorl %edi, %edi
movq %r8, %r13
cmpq %rdx, %rdi
je 0xf231
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq $0x100, %r8             # imm = 0x100
je 0xf1e5
movl %r9d, -0x1160(%rbp,%r8,4)
addl (%r12,%r8,4), %r9d
incq %r8
jmp 0xf1cb
xorl %r8d, %r8d
cmpq %r8, %rax
je 0xf21c
movzwl (%r13,%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 (%r13,%r8,4), %r9d
movl %r9d, (%rsi,%r10,4)
incq %r8
jmp 0xf1e8
incq %rdi
addl $0x8, %ecx
addq $0x400, %r12            # imm = 0x400
movq %rsi, %r8
movq %r13, %rsi
jmp 0xf1bd
testl %r14d, %r14d
je 0xf247
cmpl $0x1, %r14d
jne 0xf44f
movw $0x1, (%r13)
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
movq -0x40(%rbp), %r8
cmpq %rax, %rcx
je 0xf270
movzwl (%r13,%rax,4), %edx
incl -0xe0(%rbp,%rdx,4)
incq %rax
jmp 0xf259
cmpl $0x2, %r14d
jge 0xf3c5
leaq 0x8f4a(%r8), %r15
imulq $0x120, -0x38(%rbp), %rax # imm = 0x120
movq %rax, -0x50(%rbp)
leaq (%r8,%rax), %rdi
addq $0x8f4a, %rdi           # imm = 0x8F4A
xorl %r12d, %r12d
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x3180
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
leaq (%rcx,%rax), %rdi
addq $0x888a, %rdi           # imm = 0x888A
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x3180
movl -0x2c(%rbp), %eax
testl %eax, %eax
cmovgl %eax, %r12d
incl %r12d
addq $-0x2, %r13
pushq $0x1
popq %rax
addq -0x50(%rbp), %r15
movq -0x48(%rbp), %rdi
cmpq %r12, %rax
je 0xf316
movl -0xe0(%rbp,%rax,4), %ecx
movslq %r14d, %rdx
leaq (,%rdx,4), %rdx
addq %r13, %rdx
testl %ecx, %ecx
jle 0xf311
movzwl (%rdx), %esi
movb %al, (%rsi,%r15)
decl %ecx
decl %r14d
addq $-0x4, %rdx
jmp 0xf2fb
incq %rax
jmp 0xf2e1
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %rsi
pushq $0x2
popq %rax
andl $0x0, -0x8dc(%rbp)
xorl %ecx, %ecx
cmpq %rdi, %rax
jg 0xf344
addl -0xe4(%rbp,%rax,4), %ecx
addl %ecx, %ecx
movl %ecx, -0x8e0(%rbp,%rax,4)
incq %rax
jmp 0xf32a
leaq 0x8f4a(%r15), %rdx
addq $0x888a, %r15           # 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 %r15, %rdx
cmpq %rbx, %rax
je 0xf3b3
movzbl (%rax,%rcx), %esi
testl %esi, %esi
je 0xf3ae
movl %esi, %r8d
movl -0x8e0(%rbp,%r8,4), %edi
leal 0x1(%rdi), %r9d
movl %r9d, -0x8e0(%rbp,%r8,4)
xorl %r8d, %r8d
testl %esi, %esi
jle 0xf3a9
movl %edi, %r9d
andl $0x1, %r9d
leal (%r9,%r8,2), %r8d
decl %esi
shrl %edi
jmp 0xf394
movw %r8w, (%rdx,%rax,2)
incq %rax
jmp 0xf36d
addq $0x15b8, %rsp           # imm = 0x15B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x2c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpq $0x20, %rcx
jge 0xf3e5
movl -0xdc(%rbp,%rcx,4), %edx
incq %rcx
addl %edx, -0xe0(%rbp,%rax,4)
jmp 0xf3cc
xorl %ecx, %ecx
movq %rax, %rsi
xorl %edx, %edx
testl %esi, %esi
jle 0xf402
movl -0xe0(%rbp,%rsi,4), %edi
shll %cl, %edi
addl %edi, %edx
decq %rsi
incl %ecx
jmp 0xf3ec
pushq $0x1
popq %rsi
movl %eax, %ecx
shlq %cl, %rsi
movl %edx, %ecx
cmpq %rcx, %rsi
je 0xf27a
decl -0xe0(%rbp,%rax,4)
movq %rax, %rcx
cmpl $0x2, %ecx
jl 0xf44b
movl -0xe4(%rbp,%rcx,4), %edi
decq %rcx
testl %edi, %edi
je 0xf41f
decl %edi
movl %edi, -0xe0(%rbp,%rcx,4)
incl %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl $0x2, -0xe0(%rbp,%rcx,4)
decl %edx
jmp 0xf40a
movzwl 0x4(%r13), %eax
addw %ax, (%r13)
leal -0x1(%r14), %eax
movslq %eax, %rcx
pushq $0x2
popq %rdx
xorl %edi, %edi
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
jge 0xf4f8
movslq %edi, %r8
movzwl (%r13,%r8,4), %r9d
cmpl %r14d, %edx
jge 0xf48e
movslq %edx, %r10
movzwl (%r13,%r10,4), %r10d
cmpw %r10w, %r9w
jae 0xf4a3
movw %r9w, (%r13,%rsi,4)
leal 0x1(%r8), %edi
movw %si, (%r13,%r8,4)
movslq %edi, %r8
jmp 0xf4ab
incl %edx
movw %r10w, (%r13,%rsi,4)
cmpl %r14d, %edx
jge 0xf4cc
movslq %edx, %r9
cmpq %r8, %rsi
jle 0xf4e2
movzwl (%r13,%r8,4), %r10d
movzwl (%r13,%r9,4), %r9d
cmpw %r9w, %r10w
jb 0xf4d2
jmp 0xf4e8
movzwl (%r13,%r8,4), %r10d
addw %r10w, (%r13,%rsi,4)
incl %edi
movw %si, (%r13,%r8,4)
jmp 0xf4f0
movzwl (%r13,%r9,4), %r9d
incl %edx
addw %r9w, (%r13,%rsi,4)
incq %rsi
jmp 0xf468
leal -0x2(%r14), %ecx
andw $0x0, -0x8(%r13,%r15,4)
leal -0x3(%r14), %edx
testl %edx, %edx
js 0xf525
movl %edx, %esi
movzwl (%r13,%rsi,4), %edi
movzwl (%r13,%rdi,4), %edi
incl %edi
movw %di, (%r13,%rsi,4)
decl %edx
jmp 0xf507
xorl %edx, %edx
pushq $0x1
popq %rdi
pushq $-0x1
popq %rsi
testl %edi, %edi
je 0xf247
cmpl $-0x1, %ecx
movl %esi, %r9d
cmovll %ecx, %r9d
movl %ecx, %r8d
subl %r9d, %r8d
xorl %r10d, %r10d
cmpl %r8d, %r10d
je 0xf562
movl %ecx, %r11d
movzwl (%r13,%r11,4), %r11d
cmpl %r11d, %edx
jne 0xf567
incl %r10d
decl %ecx
jmp 0xf548
movl %r9d, %ecx
jmp 0xf56a
movl %r10d, %r8d
movslq %eax, %r9
leaq (,%r9,4), %r9
addq %r13, %r9
cmpl %r8d, %edi
jle 0xf58b
movw %dx, (%r9)
decl %edi
decl %eax
addq $-0x4, %r9
jmp 0xf578
addl %r8d, %r8d
incl %edx
movl %r8d, %edi
jmp 0xf52d