// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/htslib/cram/cram_stats.c
// Function: cram_stats_encoding
enum cram_encoding cram_stats_encoding(cram_fd *fd, cram_stats *st) {
    int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX;
    int *vals = NULL, *freqs = NULL, vals_alloc = 0;

    //cram_stats_dump(st);

    /* Count number of unique symbols */
    for (nvals = i = 0; i < MAX_STAT_VAL; i++) {
	if (!st->freqs[i])
	    continue;
	if (nvals >= vals_alloc) {
	    vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
	    vals  = realloc(vals,  vals_alloc * sizeof(int));
	    freqs = realloc(freqs, vals_alloc * sizeof(int));
	    if (!vals || !freqs) {
		if (vals)  free(vals);
		if (freqs) free(freqs);
		return E_HUFFMAN; // Cannot do much else atm
	    }
	}
	vals[nvals] = i;
	freqs[nvals] = st->freqs[i];
	ntot += freqs[nvals];
	if (max_val < i) max_val = i;
	if (min_val > i) min_val = i;
	nvals++;
    }
    if (st->h) {
	khint_t k;
	int i;

	for (k = kh_begin(st->h); k != kh_end(st->h); k++) {
	    if (!kh_exist(st->h, k))
		continue;

	    if (nvals >= vals_alloc) {
		vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
		vals  = realloc(vals,  vals_alloc * sizeof(int));
		freqs = realloc(freqs, vals_alloc * sizeof(int));
		if (!vals || !freqs)
		    return E_HUFFMAN; // Cannot do much else atm
	    }
	    i = kh_key(st->h, k);
	    vals[nvals]=i;
	    freqs[nvals] = kh_val(st->h, k);
	    ntot += freqs[nvals];
	    if (max_val < i) max_val = i;
	    if (min_val > i) min_val = i;
	    nvals++;
	}
    }

    st->nvals = nvals;
    assert(ntot == st->nsamp);

    free(vals);
    free(freqs);

    /*
     * Simple policy that everything is external unless it can be
     * encoded using zero bits as a unary item huffman table.
     */
    return nvals <= 1 ? E_HUFFMAN : E_EXTERNAL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %eax, %eax
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %rsi, 0x8(%rsp)
cmpl $0x0, (%rsi,%rbx,4)
je 0x3a936
cmpl %ebp, %r13d
jl 0x3a924
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl %ecx, %ebp
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %ebp
movslq %ebp, %r12
shlq $0x2, %r12
movq %rax, 0x10(%rsp)
movq %r12, %rsi
callq 0x48d0
movq %rax, %r14
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x48d0
movq %r14, %rdi
testq %r14, %r14
je 0x3aa69
testq %rax, %rax
je 0x3aa69
movq 0x8(%rsp), %rsi
movslq %r13d, %rdx
movl %ebx, (%rdi,%rdx,4)
movl (%rsi,%rbx,4), %ecx
movl %ecx, (%rax,%rdx,4)
addl %ecx, %r15d
incl %r13d
incq %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x3a8cb
cmpq $0x0, 0x1000(%rsi)
je 0x3aa36
movq 0x1000(%rsi), %rcx
cmpl $0x0, (%rcx)
sete %r8b
je 0x3aa2a
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rcx), %rdx
movl %ebx, %ecx
shrl $0x4, %ecx
movl (%rdx,%rcx,4), %edx
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x3aa11
cmpl %ebp, %r13d
jl 0x3a9e9
movb %r8b, 0x3(%rsp)
movl %r15d, 0x4(%rsp)
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl %ecx, %ebp
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %ebp
movslq %ebp, %r12
shlq $0x2, %r12
movq %rax, 0x10(%rsp)
movq %r12, %rsi
callq 0x48d0
movq %rax, %r15
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x48d0
movq %r15, %rdi
testq %r15, %r15
je 0x3aa9e
testq %rax, %rax
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r15d
movb 0x3(%rsp), %r8b
je 0x3aa2a
movq 0x1000(%rsi), %r10
movq 0x18(%r10), %rcx
movl %ebx, %edx
movl (%rcx,%rdx,4), %ecx
movslq %r13d, %r8
movl %ecx, (%rdi,%r8,4)
movq 0x20(%r10), %rcx
movl (%rcx,%rdx,4), %ecx
movl %ecx, (%rax,%r8,4)
addl %ecx, %r15d
incl %r13d
incl %ebx
movq 0x1000(%rsi), %rcx
addl $0x2, %r14d
cmpl (%rcx), %ebx
sete %r8b
jne 0x3a969
movl $0x3, %r12d
testb $0x1, %r8b
je 0x3aa8c
movl %r13d, 0x100c(%rsi)
cmpl 0x1008(%rsi), %r15d
jne 0x3aab2
movq %rax, %rbx
callq 0x4610
movq %rbx, %rdi
callq 0x4610
xorl %eax, %eax
cmpl $0x2, %r13d
setl %al
leal 0x1(,%rax,2), %r12d
jmp 0x3aa8c
testq %rdi, %rdi
je 0x3aa79
movq %rax, %rbx
callq 0x4610
movq %rbx, %rax
movl $0x3, %r12d
testq %rax, %rax
je 0x3aa8c
movq %rax, %rdi
callq 0x4610
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r15d
movb 0x3(%rsp), %r8b
jmp 0x3aa2a
leaq 0x1a79e(%rip), %rdi     # 0x55257
leaq 0x1a724(%rip), %rsi     # 0x551e4
leaq 0x1a7a2(%rip), %rcx     # 0x55269
movl $0xb4, %edx
callq 0x4350