// File: /workspace/github_cpp_makefile_star2_O3/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 %r14d, %r14d
xorl %ebp, %ebp
xorl %eax, %eax
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r15d, %r15d
movq %rsi, 0x10(%rsp)
cmpl $0x0, (%rsi,%r14,4)
je 0x39c95
cmpl %ebp, %r15d
jl 0x39c81
movl %r8d, %ebx
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl $0x400, %edx            # imm = 0x400
cmovel %edx, %ecx
movq %rcx, %rbp
movslq %ecx, %r12
shlq $0x2, %r12
movq %r12, %rsi
movq %rax, (%rsp)
callq 0x48d0
movq %rax, %r13
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x48d0
movq %r13, %rdi
testq %r13, %r13
je 0x39dc6
testq %rax, %rax
je 0x39dc6
movq 0x10(%rsp), %rsi
movl %ebx, %r8d
movslq %r15d, %rdx
movl %r14d, (%rdi,%rdx,4)
movl (%rsi,%r14,4), %ecx
movl %ecx, (%rax,%rdx,4)
addl %ecx, %r8d
incl %r15d
incq %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x39c22
movq 0x1000(%rsi), %r9
testq %r9, %r9
je 0x39d83
movl (%r9), %edx
testl %edx, %edx
je 0x39d83
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r9), %rcx
movl %r12d, %r10d
shrl $0x4, %r10d
movl (%rcx,%r10,4), %r10d
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %r10d
testb $0x3, %r10b
jne 0x39d73
cmpl %ebp, %r15d
jl 0x39d4e
movl %r15d, 0xc(%rsp)
movl %r8d, %ebx
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl %ecx, %ebp
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %ebp
movslq %ebp, %rsi
shlq $0x2, %rsi
movq %rsi, (%rsp)
movq %rax, %r15
callq 0x48d0
movq %rax, %r13
movq %r15, %rdi
movq (%rsp), %rsi
callq 0x48d0
movl $0x3, %r15d
testq %r13, %r13
je 0x39db4
testq %rax, %rax
je 0x39db4
movq %r13, %rdi
movl %ebx, %r8d
movq 0x10(%rsp), %rsi
movq 0x1000(%rsi), %r9
movl 0xc(%rsp), %r15d
movq 0x18(%r9), %rcx
movl %r12d, %edx
movl (%rcx,%rdx,4), %ecx
movslq %r15d, %r10
movl %ecx, (%rdi,%r10,4)
movq 0x20(%r9), %rcx
movl (%rcx,%rdx,4), %ecx
movl %ecx, (%rax,%r10,4)
addl %ecx, %r8d
incl %r15d
movl (%r9), %edx
incl %r12d
addl $0x2, %r14d
cmpl %edx, %r12d
jne 0x39cc2
movl %r15d, 0x100c(%rsi)
cmpl 0x1008(%rsi), %r8d
jne 0x39deb
movq %rax, %rbx
callq 0x4610
movq %rbx, %rdi
callq 0x4610
xorl %eax, %eax
cmpl $0x2, %r15d
setl %al
leal 0x1(,%rax,2), %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdi, %rdi
je 0x39dd6
movq %rax, %rbx
callq 0x4610
movq %rbx, %rax
movl $0x3, %r15d
testq %rax, %rax
je 0x39db4
movq %rax, %rdi
callq 0x4610
jmp 0x39db4
leaq 0x19715(%rip), %rdi     # 0x53507
leaq 0x1969b(%rip), %rsi     # 0x53494
leaq 0x19719(%rip), %rcx     # 0x53519
movl $0xb4, %edx
callq 0x4350