// File: /workspace/github_c_makefile_star2_O1/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, %ebx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r11d, %r11d
xorl %r12d, %r12d
xorl %edi, %edi
movq %rsi, 0x20(%rsp)
cmpl $0x0, (%rsi,%r14,4)
je 0x58e26
cmpl %r11d, %r15d
jl 0x58e03
movl %r9d, 0x8(%rsp)
movl %r8d, 0xc(%rsp)
movl %r15d, 0x14(%rsp)
movl %ebx, 0x10(%rsp)
testl %r11d, %r11d
leal (%r11,%r11), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %rax, 0x28(%rsp)
movslq %eax, %r13
shlq $0x2, %r13
movq %r13, %rsi
movq %rdi, %r15
callq 0x4840
movq %rax, %rbp
movq %r12, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r13
testq %rbp, %rbp
setne %al
testq %r13, %r13
movq %r12, %rbx
setne %r12b
andb %al, %r12b
je 0x58db6
movq %rbp, %rdi
movq %r13, %rbp
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
jmp 0x58de9
testq %rbp, %rbp
cmoveq %r15, %rbp
movq %rbp, %rdi
callq 0x45c0
testq %r13, %r13
movq %rbx, %rbp
cmoveq %rbx, %r13
movq %r13, %rdi
callq 0x45c0
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
movq %r15, %rdi
testb %r12b, %r12b
movl 0x10(%rsp), %ebx
movl 0x14(%rsp), %r15d
movq %rbp, %r12
movq 0x28(%rsp), %r11
je 0x59022
movslq %r15d, %rax
movl %r14d, (%rdi,%rax,4)
movl (%rsi,%r14,4), %ecx
movl %ecx, (%r12,%rax,4)
addl %ecx, %r9d
cmpl %r14d, %r8d
cmovlel %r14d, %r8d
cmpl %r14d, %ebx
cmovgel %r14d, %ebx
incl %r15d
incq %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x58d2c
cmpq $0x0, 0x1000(%rsi)
je 0x58fb1
movq %r12, 0x18(%rsp)
movq 0x1000(%rsi), %rax
cmpl $0x0, (%rax)
sete %dl
je 0x58fa2
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rax), %rax
movl %r12d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x58f88
cmpl %r11d, %r15d
jl 0x58f4e
movb %dl, 0x7(%rsp)
movl %r9d, 0x8(%rsp)
movl %r8d, 0xc(%rsp)
movl %r15d, 0x14(%rsp)
movl %ebx, 0x10(%rsp)
testl %r11d, %r11d
leal (%r11,%r11), %eax
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %eax
movq %rax, 0x28(%rsp)
movslq %eax, %r13
shlq $0x2, %r13
movq %r13, %rsi
movq %rdi, %rbx
callq 0x4840
movq %rax, %rbp
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x4840
movq %rax, %r13
testq %rbp, %rbp
setne %al
testq %r13, %r13
setne %cl
andb %al, %cl
je 0x58f04
movq %rbp, %rdi
movq %r13, 0x18(%rsp)
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
jmp 0x58f38
testq %rbp, %rbp
cmoveq %rbx, %rbp
movq %rbp, %rdi
movl %ecx, %ebp
callq 0x45c0
testq %r13, %r13
cmoveq %r15, %r13
movq %r13, %rdi
callq 0x45c0
movl %ebp, %ecx
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
movq %rbx, %rdi
movb 0x7(%rsp), %dl
testb %cl, %cl
movl 0x10(%rsp), %ebx
movl 0x14(%rsp), %r15d
movq 0x28(%rsp), %r11
je 0x58fa2
movq 0x1000(%rsi), %rax
movq 0x18(%rax), %rcx
movl %r12d, %edx
movl (%rcx,%rdx,8), %ecx
movslq %r15d, %r10
movl %ecx, (%rdi,%r10,4)
movq 0x20(%rax), %rax
movl (%rax,%rdx,4), %eax
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%r10,4)
addl %eax, %r9d
cmpl %ecx, %r8d
cmovlel %ecx, %r8d
cmpl %ecx, %ebx
cmovgel %ecx, %ebx
incl %r15d
incl %r12d
movq 0x1000(%rsi), %rax
addl $0x2, %r14d
cmpl (%rax), %r12d
sete %dl
jne 0x58e62
movl $0x3, %eax
testb $0x1, %dl
movq 0x18(%rsp), %r12
je 0x59027
movl %r15d, 0x100c(%rsi)
movslq %ebx, %rax
movq %rax, 0x1010(%rsi)
movslq %r8d, %rax
movq %rax, 0x1018(%rsi)
cmpl 0x1008(%rsi), %r9d
jne 0x59036
callq 0x45c0
movq %r12, %rdi
callq 0x45c0
movq 0x30(%rsp), %rax
cmpl $0x400, 0xc(%rax)       # imm = 0x400
jl 0x59010
movl $0x2c, %eax
cmpl $0x1, %r15d
je 0x59027
testl %r15d, %r15d
sete %al
testl %ebx, %ebx
sets %cl
orb %al, %cl
movzbl %cl, %eax
addl $0x29, %eax
jmp 0x59027
xorl %eax, %eax
cmpl $0x2, %r15d
setl %al
leal 0x1(,%rax,2), %eax
jmp 0x59027
movl $0x3, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x456ad(%rip), %rdi     # 0x9e6ea
leaq 0x45631(%rip), %rsi     # 0x9e675
leaq 0x456b1(%rip), %rcx     # 0x9e6fc
movl $0xc7, %edx
callq 0x42f0