// File: 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
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 0x4170
cmpl $0x0, -0x18(%rbp)
je 0x61c35
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x61c30
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 0x61bec
jmp 0x61e17
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 0x61ccb
movq -0xa58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpw $0x0, (%rax,%rcx,2)
je 0x61cbe
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 0x61cc0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x61c65
movl -0xa4c(%rbp), %edi
leaq -0x5c0(%rbp), %rsi
leaq -0xa40(%rbp), %rdx
callq 0x61f60
movq %rax, -0xa48(%rbp)
movq -0xa48(%rbp), %rdi
movl -0xa4c(%rbp), %esi
callq 0x621a0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xa4c(%rbp), %eax
jge 0x61d3a
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 0x61d04
leaq -0xb0(%rbp), %rdi
movl -0xa4c(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x62450
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 0x4170
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 0x4170
movl $0x1, -0x1c(%rbp)
movl -0xa4c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x61e15
movslq -0x1c(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x61e08
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 0x61dbf
jmp 0x61e0a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x61da9
jmp 0x61e17
movl $0x0, -0x13c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x2, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x61e64
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 0x61e2f
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x61f4a
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 0x61eb1
jmp 0x61f3c
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 0x61f13
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 0x61edb
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 0x61e6b
addq $0xa70, %rsp            # imm = 0xA70
popq %rbp
retq
nopw %cs:(%rax,%rax)