// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/arlib/deps/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 $0x15c8, %rsp           # imm = 0x15C8
movl %r8d, %r15d
movl %ecx, %r14d
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
movl $0x84, %edx
xorl %esi, %esi
callq 0x3170
movl %r12d, %r9d
movl %ebp, %r12d
testl %r15d, %r15d
je 0x2de3b
testl %ebp, %ebp
jle 0x2e255
leaq (%r9,%r9,8), %rax
shlq $0x5, %rax
addq %rbx, %rax
addq $0x8f4a, %rax           # imm = 0x8F4A
xorl %ecx, %ecx
movzbl (%rax,%rcx), %edx
incl 0x30(%rsp,%rdx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x2de26
jmp 0x2e255
leaq (%r9,%r9,8), %rax
movq %rax, 0x18(%rsp)
shlq $0x6, %rax
testl %ebp, %ebp
movl %ebp, 0xc(%rsp)
movq %r9, 0x28(%rsp)
movq %rax, 0x10(%rsp)
jle 0x2deec
addq %rbx, %rax
addq $0x81ca, %rax           # imm = 0x81CA
xorl %ecx, %ecx
xorl %r15d, %r15d
movzwl (%rax,%rcx,2), %edx
testw %dx, %dx
je 0x2de8c
movslq %r15d, %rsi
movw %dx, 0x8c0(%rsp,%rsi,4)
leal 0x1(%rsi), %r15d
movw %cx, 0x8c2(%rsp,%rsi,4)
incq %rcx
cmpq %rcx, %r12
jne 0x2de6c
leaq 0xc0(%rsp), %rdi
xorl %r13d, %r13d
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x3170
testl %r15d, %r15d
sete %dl
je 0x2df03
movl %r15d, %eax
xorl %ecx, %ecx
movzwl 0x8c0(%rsp,%rcx,4), %esi
movzbl %sil, %edi
incl 0xc0(%rsp,%rdi,4)
shrl $0x6, %esi
andl $-0x4, %esi
incl 0x4c0(%rsp,%rsi)
incq %rcx
cmpq %rcx, %rax
jne 0x2deb8
movl 0x4c0(%rsp), %eax
movl %r15d, %r13d
jmp 0x2df07
leaq 0xc0(%rsp), %rdi
xorl %r13d, %r13d
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x3170
movb $0x1, %dl
xorl %eax, %eax
movq %r14, 0x20(%rsp)
cmpl %r13d, %eax
sete %al
movl $0x2, %edi
movq %rdi, %rsi
movl $0x1, %edi
xorl %ecx, %ecx
testb $0x1, %al
movl $0x0, %eax
jne 0x2df17
movl %r13d, %eax
leaq 0x8c0(%rsp), %r15
leaq 0x1140(%rsp), %r10
leaq 0xc0(%rsp), %rdi
xorl %r8d, %r8d
movq %r15, %r9
movq %r10, %r15
xorl %r10d, %r10d
xorl %r11d, %r11d
movl %r11d, 0xd40(%rsp,%r10,4)
addl (%rdi,%r10,4), %r11d
incq %r10
cmpq $0x100, %r10            # imm = 0x100
jne 0x2df54
testb %dl, %dl
jne 0x2dfa3
xorl %r10d, %r10d
movzwl (%r9,%r10,4), %r11d
shrl %cl, %r11d
movzbl %r11b, %r11d
movl 0xd40(%rsp,%r11,4), %r14d
leal 0x1(%r14), %ebp
movl %ebp, 0xd40(%rsp,%r11,4)
movl (%r9,%r10,4), %r11d
movl %r11d, (%r15,%r14,4)
incq %r10
cmpq %r10, %rax
jne 0x2df73
incq %r8
addl $0x8, %ecx
addq $0x400, %rdi            # imm = 0x400
movq %r9, %r10
cmpq %rsi, %r8
jne 0x2df48
testl %r13d, %r13d
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rbp
je 0x2e1ca
cmpl $0x1, %r13d
jne 0x2dfdc
movw $0x1, (%r15)
jmp 0x2e125
movzwl 0x4(%r15), %ecx
addw %cx, (%r15)
leal -0x1(%r13), %ecx
cmpl $0x2, %r13d
jle 0x2e0aa
movl %ecx, %edx
xorl %r8d, %r8d
movl $0x2, %esi
movl $0x1, %edi
movslq %r8d, %r9
movzwl (%r15,%r9,4), %r10d
cmpl %r13d, %esi
jge 0x2e01d
movslq %esi, %r11
movzwl (%r15,%r11,4), %r11d
cmpw %r11w, %r10w
jae 0x2e030
movw %r10w, (%r15,%rdi,4)
leal 0x1(%r9), %r8d
movw %di, (%r15,%r9,4)
movslq %r8d, %r9
jmp 0x2e037
incl %esi
movw %r11w, (%r15,%rdi,4)
cmpl %r13d, %esi
jge 0x2e056
movslq %esi, %r10
cmpq %r9, %rdi
jle 0x2e06a
movzwl (%r15,%r9,4), %r11d
movzwl (%r15,%r10,4), %r10d
cmpw %r10w, %r11w
jb 0x2e05b
jmp 0x2e06f
movzwl (%r15,%r9,4), %r11d
addw %r11w, (%r15,%rdi,4)
incl %r8d
movw %di, (%r15,%r9,4)
jmp 0x2e076
movzwl (%r15,%r10,4), %r10d
incl %esi
addw %r10w, (%r15,%rdi,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x2e002
leal -0x2(%r13), %edx
movslq %r13d, %rsi
movw $0x0, -0x8(%r15,%rsi,4)
leal -0x3(%r13), %esi
movzwl (%r15,%rsi,4), %edi
movzwl (%r15,%rdi,4), %edi
incl %edi
movw %di, (%r15,%rsi,4)
addq $-0x1, %rsi
jb 0x2e091
jmp 0x2e0b9
leal -0x2(%r13), %edx
movslq %r13d, %rsi
movw $0x0, -0x8(%r15,%rsi,4)
movl $0x1, %edi
xorl %esi, %esi
testl %edx, %edx
js 0x2e0e9
leal 0x1(%rdx), %r8d
xorl %r9d, %r9d
movl %edx, %r10d
movzwl (%r15,%r10,4), %r10d
cmpl %r10d, %esi
jne 0x2e0ee
incl %r9d
decl %edx
cmpl %r9d, %r8d
jne 0x2e0cb
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x2e0f1
xorl %r8d, %r8d
jmp 0x2e0f1
movl %r9d, %r8d
cmpl %r8d, %edi
jle 0x2e111
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 0x2e102
leal (%r8,%r8), %edi
incl %esi
testl %r8d, %r8d
jne 0x2e0c0
testl %r13d, %r13d
jle 0x2e1ca
xorl %ecx, %ecx
movzwl (%r15,%rcx,4), %edx
incl 0x30(%rsp,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x2e127
cmpl $0x2, %r13d
jl 0x2e1ca
movl %r14d, %eax
cmpl $0x1f, %r14d
jg 0x2e16c
movl 0x30(%rsp,%rax,4), %ecx
movslq %r14d, %rdx
addl 0x34(%rsp,%rdx,4), %ecx
incq %rdx
movl %ecx, 0x30(%rsp,%rax,4)
cmpq $0x20, %rdx
jne 0x2e152
testl %r14d, %r14d
jle 0x2e305
leaq 0x1(%rax), %rsi
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x2c(%rsp,%rsi,4), %edi
shll %cl, %edi
addl %edi, %edx
decq %rsi
incl %ecx
cmpq $0x1, %rsi
ja 0x2e174
movl $0x1, %esi
movl %eax, %ecx
shlq %cl, %rsi
movl %edx, %ecx
cmpq %rcx, %rsi
je 0x2e1ca
decl 0x30(%rsp,%rax,4)
movq %rax, %rcx
cmpq $0x2, %rcx
jl 0x2e1c3
movl 0x2c(%rsp,%rcx,4), %edi
decq %rcx
testl %edi, %edi
je 0x2e19f
decl %edi
movl %edi, 0x30(%rsp,%rcx,4)
incl %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl $0x2, 0x30(%rsp,%rcx,4)
decl %edx
cmpq %rdx, %rsi
jne 0x2e198
shlq $0x5, %rbp
leaq (%rbx,%rbp), %rdi
addq $0x8f4a, %rdi           # imm = 0x8F4A
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x3170
movq 0x10(%rsp), %rax
leaq (%rbx,%rax), %rdi
addq $0x888a, %rdi           # imm = 0x888A
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x3170
testl %r14d, %r14d
movq 0x28(%rsp), %r9
jle 0x2e251
leaq 0x8f4a(%rbx), %rax
leal 0x1(%r14), %ecx
addq $-0x2, %r15
movl $0x1, %edx
addq %rbp, %rax
movl 0x30(%rsp,%rdx,4), %esi
testl %esi, %esi
jle 0x2e249
movslq %r13d, %r13
leaq (%r15,%r13,4), %rdi
subl %esi, %r13d
incl %esi
movzwl (%rdi), %r8d
movb %dl, (%r8,%rax)
addq $-0x4, %rdi
decl %esi
cmpl $0x1, %esi
ja 0x2e236
incq %rdx
cmpq %rcx, %rdx
jne 0x2e222
movl 0xc(%rsp), %ebp
movl $0x0, 0xc4(%rsp)
cmpl $0x2, %r14d
jl 0x2e285
movl %r14d, %eax
decq %rax
xorl %ecx, %ecx
xorl %edx, %edx
addl 0x34(%rsp,%rcx,4), %edx
addl %edx, %edx
movl %edx, 0xc8(%rsp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x2e270
testl %ebp, %ebp
jle 0x2e2f3
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 0x2e2eb
movl %esi, %r8d
movl 0xc0(%rsp,%r8,4), %edi
leal 0x1(%rdi), %r9d
movl %r9d, 0xc0(%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
ja 0x2e2d2
movw %r8w, (%rcx,%rax,2)
incq %rax
cmpq %r12, %rax
jne 0x2e2ae
addq $0x15c8, %rsp           # imm = 0x15C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
jmp 0x2e187