// File: /workspace/github_c_makefile_star2_O1/ebiggers[P]xpack/lib/xpack_compress.c
// Function: choose_state_counts
static unsigned
choose_state_counts(const u32 freqs[], unsigned alphabet_size,
		    unsigned max_log2_num_states, u16 state_counts[])
{
	u32 total_freq = 0;
	unsigned num_used_syms = 0;
	unsigned log2_num_states;
	unsigned sym;

	/* Compute the total frequency and the number of used symbols. */
	for (sym = 0; sym < alphabet_size; sym++) {
		if (freqs[sym] != 0) {
			num_used_syms++;
			total_freq += freqs[sym];
		}
	}

	/*
	 * If no symbols from this alphabet were used, then output a code that
	 * contains an arbitrary unused symbol.
	 */
	if (total_freq == 0) {
		state_counts[0] = 1;
		for (sym = 1; sym < alphabet_size; sym++)
			state_counts[sym] = 0;
		return 0;
	}

	/* Select the number of states to use. */
	log2_num_states = select_log2_num_states(total_freq, num_used_syms,
						 max_log2_num_states);

	/* Decide how many states to assign to each symbol. */
	compute_state_counts(freqs, total_freq, state_counts,
			     alphabet_size, log2_num_states);

	return log2_num_states;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r8
movl %esi, %r9d
movl %esi, %esi
testl %r9d, %r9d
je 0x4b6f
xorl %ecx, %ecx
xorl %eax, %eax
xorl %ebp, %ebp
movl (%rdi,%rcx,4), %r10d
addl %r10d, %ebp
cmpl $0x1, %r10d
sbbl $-0x1, %eax
incq %rcx
cmpq %rcx, %rsi
jne 0x4b57
jmp 0x4b73
xorl %ebp, %ebp
xorl %eax, %eax
testl %ebp, %ebp
je 0x4c26
movl $0x1, %r11d
movl %edx, %ecx
shll %cl, %r11d
movl $0x1, %r10d
movl %ebp, %ecx
shrl $0x2, %ecx
cmpl %ecx, %r11d
cmovbl %r11d, %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
leal -0x1(%rcx), %eax
bsrl %eax, %ebx
xorl $-0x20, %ebx
addl $0x21, %ebx
xorl %r11d, %r11d
cmpl $0x2, %ecx
cmovbl %r11d, %ebx
movl %ebx, %ecx
shll %cl, %r10d
movl $0x80000000, %eax       # imm = 0x80000000
xorl %edx, %edx
divl %ebp
testl %r9d, %r9d
je 0x4c4f
movl $0x1e, %ecx
subl %ebx, %ecx
xorl %edx, %edx
xorl %r11d, %r11d
xorl %ebp, %ebp
movl (%rdi,%rdx,4), %r14d
movl %r14d, %r15d
imull %eax, %r15d
shrl %cl, %r15d
movl %r11d, %r12d
incl %r15d
movl %r15d, %r13d
shrl %r13d
xorl %r11d, %r11d
testl %r14d, %r14d
setne %r11b
cmpl $0x2, %r15d
cmovael %r13d, %r11d
subl %r11d, %r10d
cmpl %r12d, %r11d
cmoval %edx, %ebp
movw %r11w, (%r8,%rdx,2)
cmovbel %r12d, %r11d
incq %rdx
cmpq %rdx, %rsi
jne 0x4bd8
shrl $0x2, %r11d
movl %ebp, %ecx
jmp 0x4c51
movw $0x1, (%r8)
xorl %ebx, %ebx
cmpl $0x2, %r9d
jb 0x4caa
addq $0x2, %r8
decl %r9d
addq %r9, %r9
xorl %ebx, %ebx
movq %r8, %rdi
xorl %esi, %esi
movq %r9, %rdx
callq 0x1040
jmp 0x4caa
xorl %ecx, %ecx
movl %r10d, %eax
negl %eax
cmpl %eax, %r11d
jle 0x4c62
addw %r10w, (%r8,%rcx,2)
jmp 0x4caa
testl %r10d, %r10d
je 0x4caa
movl $0x3, %ecx
testl %r9d, %r9d
je 0x4ca4
xorl %edx, %edx
movzwl (%r8,%rdx,2), %edi
cmpl $0x2, %edi
jb 0x4c98
leal -0x1(%rdi), %r10d
shrl %cl, %r10d
cmpl %eax, %r10d
cmovael %eax, %r10d
subl %r10d, %edi
movw %di, (%r8,%rdx,2)
subl %r10d, %eax
je 0x4ca2
incq %rdx
cmpq %rdx, %rsi
jne 0x4c73
jmp 0x4ca4
xorl %eax, %eax
decl %ecx
testl %eax, %eax
jne 0x4c6c
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq