// File: 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
subq $0xa70, %rsp            # imm = 0xA70
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x84, %edx
callq 0x3190
cmpl $0x0, -0x18(%rbp)
je 0x1ea95
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x1ea90
movq -0x8(%rbp), %rax
addq $0x8f4a, %rax           # imm = 0x8F4A
movslq -0xc(%rbp), %rcx
imulq $0x120, %rcx, %rcx      # imm = 0x120
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl -0xb0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1ea4c
jmp 0x1ec77
movl $0x0, -0xa4c(%rbp)
movq -0x8(%rbp), %rax
addq $0x81ca, %rax           # imm = 0x81CA
movslq -0xc(%rbp), %rcx
imulq $0x240, %rcx, %rcx      # imm = 0x240
addq %rcx, %rax
movq %rax, -0xa58(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x1eb2b
movq -0xa58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpw $0x0, (%rax,%rcx,2)
je 0x1eb1e
movq -0xa58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movw (%rax,%rcx,2), %cx
movslq -0xa4c(%rbp), %rax
movw %cx, -0x5c0(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
movw %ax, %cx
movl -0xa4c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xa4c(%rbp)
cltq
movw %cx, -0x5be(%rbp,%rax,4)
jmp 0x1eb20
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1eac5
movl -0xa4c(%rbp), %edi
leaq -0x5c0(%rbp), %rsi
leaq -0xa40(%rbp), %rdx
callq 0x1edc0
movq %rax, -0xa48(%rbp)
movq -0xa48(%rbp), %rdi
movl -0xa4c(%rbp), %esi
callq 0x1f000
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xa4c(%rbp), %eax
jge 0x1eb9a
movq -0xa48(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzwl (%rax,%rcx,4), %eax
movl -0xb0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1eb64
leaq -0xb0(%rbp), %rdi
movl -0xa4c(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x1f2b0
movq -0x8(%rbp), %rdi
addq $0x8f4a, %rdi           # imm = 0x8F4A
movslq -0xc(%rbp), %rax
imulq $0x120, %rax, %rax      # imm = 0x120
addq %rax, %rdi
xorl %esi, %esi
movl $0x120, %edx            # imm = 0x120
callq 0x3190
movq -0x8(%rbp), %rdi
addq $0x888a, %rdi           # imm = 0x888A
movslq -0xc(%rbp), %rax
imulq $0x240, %rax, %rax      # imm = 0x240
addq %rax, %rdi
xorl %esi, %esi
movl $0x240, %edx            # imm = 0x240
callq 0x3190
movl $0x1, -0x1c(%rbp)
movl -0xa4c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x1ec75
movslq -0x1c(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x1ec68
movl -0x1c(%rbp), %eax
movb %al, %dl
movq -0x8(%rbp), %rax
addq $0x8f4a, %rax           # imm = 0x8F4A
movslq -0xc(%rbp), %rcx
imulq $0x120, %rcx, %rcx      # imm = 0x120
addq %rcx, %rax
movq -0xa48(%rbp), %rcx
movl -0x20(%rbp), %esi
addl $-0x1, %esi
movl %esi, -0x20(%rbp)
movslq %esi, %rsi
movzwl 0x2(%rcx,%rsi,4), %ecx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ec1f
jmp 0x1ec6a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1ec09
jmp 0x1ec77
movl $0x0, -0x13c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x2, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x1ecc4
movl -0x20(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl $0x1, %eax
cltq
addl -0xb0(%rbp,%rax,4), %ecx
shll %ecx
movl %ecx, -0x20(%rbp)
movslq -0x1c(%rbp), %rax
movl %ecx, -0x140(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1ec8f
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x1edaa
movl $0x0, -0xa5c(%rbp)
movq -0x8(%rbp), %rax
addq $0x8f4a, %rax           # imm = 0x8F4A
movslq -0xc(%rbp), %rcx
imulq $0x120, %rcx, %rcx      # imm = 0x120
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0xa64(%rbp)
cmpl $0x0, %eax
jne 0x1ed11
jmp 0x1ed9c
movl -0xa64(%rbp), %eax
movl %eax, %ecx
movl -0x140(%rbp,%rcx,4), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x140(%rbp,%rcx,4)
movl %eax, -0xa60(%rbp)
movl -0xa64(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x1ed73
movl -0xa5c(%rbp), %eax
shll %eax
movl -0xa60(%rbp), %ecx
andl $0x1, %ecx
orl %ecx, %eax
movl %eax, -0xa5c(%rbp)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
movl -0xa60(%rbp), %eax
shrl %eax
movl %eax, -0xa60(%rbp)
jmp 0x1ed3b
movl -0xa5c(%rbp), %eax
movw %ax, %dx
movq -0x8(%rbp), %rax
addq $0x888a, %rax           # imm = 0x888A
movslq -0xc(%rbp), %rcx
imulq $0x240, %rcx, %rcx      # imm = 0x240
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1eccb
addq $0xa70, %rsp            # imm = 0xA70
popq %rbp
retq
nopw %cs:(%rax,%rax)