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

#if DEBUG_CRAM_STATS
    cram_stats_dump(st);
#endif

    /* 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;
            int *vals_tmp  = realloc(vals,  vals_alloc * sizeof(int));
            int *freqs_tmp = realloc(freqs, vals_alloc * sizeof(int));
            if (!vals_tmp || !freqs_tmp) {
                free(vals_tmp  ? vals_tmp  : vals);
                free(freqs_tmp ? freqs_tmp : freqs);
                return E_HUFFMAN; // Cannot do much else atm
            }
            vals = vals_tmp;
            freqs = freqs_tmp;
        }
        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;
                int *vals_tmp  = realloc(vals,  vals_alloc * sizeof(int));
                int *freqs_tmp = realloc(freqs, vals_alloc * sizeof(int));
                if (!vals_tmp || !freqs_tmp) {
                    free(vals_tmp  ? vals_tmp  : vals);
                    free(freqs_tmp ? freqs_tmp : freqs);
                    return E_HUFFMAN; // Cannot do much else atm
                }
                vals = vals_tmp;
                freqs = freqs_tmp;
            }
            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;
    st->min_val = min_val;
    st->max_val = max_val;
    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.
     */
    if (CRAM_MAJOR_VERS(fd->version) >= 4) {
        // Note, we're assuming integer data here as we don't have the
        // type passed in.  Cram_encoder_init does know the type and
        // will convert to E_CONST_BYTE or E_EXTERNAL as appropriate.
        if (nvals == 1)
            return E_CONST_INT;
        else if (nvals == 0 || min_val < 0)
            return E_VARINT_SIGNED;
        else
            return E_VARINT_UNSIGNED;
    } else {
        return nvals <= 1 ? E_HUFFMAN : E_EXTERNAL;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
xorl %edi, %edi
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %r15d, %r15d
movq %rsi, 0x20(%rsp)
cmpq $0x400, %r14            # imm = 0x400
je 0x4796e
cmpl $0x0, (%rsi,%r14,4)
je 0x47966
cmpl %ebx, %r15d
jl 0x47942
movl %r11d, 0x8(%rsp)
movl %r10d, 0xc(%rsp)
movl %r15d, 0x4(%rsp)
movl %ebp, %r15d
testl %ebx, %ebx
leal (%rbx,%rbx), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %r13, %rbx
movq %rax, %r12
movslq %eax, %r13
shlq $0x2, %r13
movq %rdi, 0x10(%rsp)
movq %r13, %rsi
callq 0x4840
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r13
testq %rbp, %rbp
je 0x47ad0
testq %r13, %r13
je 0x47ad0
movq %rbp, %rdi
movl %r15d, %ebp
movl 0x4(%rsp), %r15d
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r10d
movl 0x8(%rsp), %r11d
movq %r12, %rbx
movslq %r15d, %rax
movl %r14d, (%rdi,%rax,4)
movl (%rsi,%r14,4), %ecx
movl %ecx, (%r13,%rax,4)
addl %ecx, %r10d
cmpl %r14d, %r11d
cmovlel %r14d, %r11d
cmpl %r14d, %ebp
cmovgel %r14d, %ebp
incl %r15d
incq %r14
jmp 0x478a7
movq 0x1000(%rsi), %rax
testq %rax, %rax
je 0x47a71
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl (%rax), %r12d
je 0x47a71
movq 0x10(%rax), %rcx
movl %r12d, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x47a65
cmpl %ebx, %r15d
jl 0x47a35
movl %r11d, 0x8(%rsp)
movl %r10d, 0xc(%rsp)
movl %ebp, 0x4(%rsp)
testl %ebx, %ebx
leal (%rbx,%rbx), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %r13, %rbx
movq %rax, 0x30(%rsp)
movslq %eax, %r13
shlq $0x2, %r13
movq %rdi, 0x10(%rsp)
movq %r13, %rsi
callq 0x4840
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r13
testq %rbp, %rbp
je 0x47ad0
testq %r13, %r13
je 0x47ad0
movq 0x20(%rsp), %rsi
movq 0x1000(%rsi), %rax
movq %rbp, %rdi
movl 0x4(%rsp), %ebp
movl 0xc(%rsp), %r10d
movl 0x8(%rsp), %r11d
movq 0x30(%rsp), %rbx
movq 0x18(%rax), %rcx
movl %r12d, %edx
movl (%rcx,%rdx,8), %ecx
movslq %r15d, %r8
movl %ecx, (%rdi,%r8,4)
movq 0x20(%rax), %r9
movl (%r9,%rdx,4), %edx
movl %edx, (%r13,%r8,4)
addl %edx, %r10d
cmpl %ecx, %r11d
cmovlel %ecx, %r11d
cmpl %ecx, %ebp
cmovgel %ecx, %ebp
incl %r15d
incl %r12d
addl $0x2, %r14d
jmp 0x47984
movl %r15d, 0x100c(%rsi)
movslq %ebp, %rax
movq %rax, 0x1010(%rsi)
movslq %r11d, %rax
movq %rax, 0x1018(%rsi)
cmpl 0x1008(%rsi), %r10d
jne 0x47b19
callq 0x45c0
movq %r13, %rdi
callq 0x45c0
movq 0x28(%rsp), %rax
cmpl $0x400, 0xc(%rax)       # imm = 0x400
jl 0x47abe
cmpl $0x1, %r15d
jne 0x47af7
pushq $0x2c
jmp 0x47af4
xorl %eax, %eax
cmpl $0x2, %r15d
setl %al
leal 0x1(,%rax,2), %eax
jmp 0x47b0a
testq %rbp, %rbp
movq 0x10(%rsp), %rdi
cmovneq %rbp, %rdi
callq 0x45c0
testq %r13, %r13
movq 0x18(%rsp), %rdi
cmovneq %r13, %rdi
callq 0x45c0
pushq $0x3
popq %rax
jmp 0x47b0a
testl %r15d, %r15d
sete %al
testl %ebp, %ebp
sets %cl
orb %al, %cl
movzbl %cl, %eax
addl $0x29, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x35bbd(%rip), %rdi     # 0x7d6dd
leaq 0x35b41(%rip), %rsi     # 0x7d668
leaq 0x35bc1(%rip), %rcx     # 0x7d6ef
movl $0xc7, %edx
callq 0x42f0