// File: /workspace/github_c_makefile_star2_O3/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, 0x30(%rsp)
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %edi, %edi
movq %rsi, 0x28(%rsp)
cmpl $0x0, (%rsi,%rbx,4)
je 0x5642a
cmpl %r12d, %r15d
jl 0x56409
movl %r11d, (%rsp)
movl %r10d, 0x4(%rsp)
movl %r15d, 0x8(%rsp)
movl %r13d, %r15d
testl %r12d, %r12d
leal (%r12,%r12), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %rax, %r12
movslq %eax, %r13
shlq $0x2, %r13
movq %rdi, 0x10(%rsp)
movq %r13, %rsi
callq 0x4840
movq %rax, %rbp
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r14
testq %rbp, %rbp
je 0x565ba
testq %r14, %r14
je 0x565ba
movq %rbp, %rdi
movl %r15d, %r13d
movl 0x8(%rsp), %r15d
movq 0x28(%rsp), %rsi
movl 0x4(%rsp), %r10d
movl (%rsp), %r11d
movslq %r15d, %rax
movl %ebx, (%rdi,%rax,4)
movl (%rsi,%rbx,4), %ecx
movl %ecx, (%r14,%rax,4)
addl %ecx, %r10d
cmpl %ebx, %r11d
cmovlel %ebx, %r11d
cmpl %ebx, %r13d
cmovgel %ebx, %r13d
incl %r15d
incq %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x56382
movq 0x1000(%rsi), %rax
testq %rax, %rax
je 0x56544
movl (%rax), %edx
testl %edx, %edx
je 0x56544
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x10(%rax), %rcx
movl %ebp, %r8d
shrl $0x4, %r8d
movl (%rcx,%r8,4), %r8d
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %r8d
testb $0x3, %r8b
jne 0x56537
cmpl %r12d, %r15d
jge 0x56485
movq %r12, 0x8(%rsp)
jmp 0x56500
movl %r11d, (%rsp)
movl %r10d, 0x4(%rsp)
movl %r13d, 0x24(%rsp)
testl %r12d, %r12d
leal (%r12,%r12), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %rax, 0x8(%rsp)
movslq %eax, %r13
shlq $0x2, %r13
movq %rdi, 0x10(%rsp)
movq %r13, %rsi
callq 0x4840
movq %rax, %r12
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r14
testq %r12, %r12
je 0x565f0
testq %r14, %r14
je 0x565f0
movq 0x28(%rsp), %rsi
movq 0x1000(%rsi), %rax
movq %r12, %rdi
movl 0x24(%rsp), %r13d
movl 0x4(%rsp), %r10d
movl (%rsp), %r11d
movq 0x18(%rax), %rcx
movl %ebp, %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, (%r14,%r8,4)
addl %edx, %r10d
cmpl %ecx, %r11d
cmovlel %ecx, %r11d
cmpl %ecx, %r13d
cmovgel %ecx, %r13d
incl %r15d
movl (%rax), %edx
movq 0x8(%rsp), %r12
incl %ebp
addl $0x2, %ebx
cmpl %edx, %ebp
jne 0x56458
movl %r15d, 0x100c(%rsi)
movslq %r13d, %rax
movq %rax, 0x1010(%rsi)
movslq %r11d, %rax
movq %rax, 0x1018(%rsi)
cmpl 0x1008(%rsi), %r10d
jne 0x565fe
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movq 0x30(%rsp), %rax
cmpl $0x400, 0xc(%rax)       # imm = 0x400
jl 0x565a8
movl $0x2c, %eax
cmpl $0x1, %r15d
je 0x565e1
testl %r15d, %r15d
sete %al
testl %r13d, %r13d
sets %cl
orb %al, %cl
movzbl %cl, %eax
addl $0x29, %eax
jmp 0x565e1
xorl %eax, %eax
cmpl $0x2, %r15d
setl %al
leal 0x1(,%rax,2), %eax
jmp 0x565e1
testq %rbp, %rbp
movq 0x10(%rsp), %rdi
cmovneq %rbp, %rdi
callq 0x45c0
testq %r14, %r14
movq 0x18(%rsp), %rdi
cmovneq %r14, %rdi
callq 0x45c0
movl $0x3, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r12, %r12
movq 0x10(%rsp), %rdi
cmovneq %r12, %rdi
jmp 0x565c6
leaq 0x452dd(%rip), %rdi     # 0x9b8e2
leaq 0x45261(%rip), %rsi     # 0x9b86d
leaq 0x452e1(%rip), %rcx     # 0x9b8f4
movl $0xc7, %edx
callq 0x42f0