// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x7fffffff, -0x28(%rbp) # imm = 0x7FFFFFFF
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x400, -0x20(%rbp)     # imm = 0x400
jge 0x7f589
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x7f45d
jmp 0x7f57b
movl -0x1c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x7f521
cmpl $0x0, -0x3c(%rbp)
je 0x7f479
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x6c(%rbp)
jmp 0x7f483
movl $0x400, %eax            # imm = 0x400
movl %eax, -0x6c(%rbp)
jmp 0x7f483
movl -0x6c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x7f4c1
cmpq $0x0, -0x50(%rbp)
jne 0x7f511
cmpq $0x0, -0x48(%rbp)
je 0x7f4d2
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x7f4da
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x45a0
cmpq $0x0, -0x50(%rbp)
je 0x7f4f4
movq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x7f4fc
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x45a0
movl $0x3, -0x4(%rbp)
jmp 0x7f814
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x20(%rbp), %edx
movq -0x30(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x7f564
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x7f572
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x7f43d
movq -0x18(%rbp), %rax
cmpq $0x0, 0x1000(%rax)
je 0x7f755
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x1000(%rcx), %rcx
cmpl (%rcx), %eax
je 0x7f753
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x54(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x7f5e9
jmp 0x7f745
movl -0x1c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x7f6c8
cmpl $0x0, -0x3c(%rbp)
je 0x7f608
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x84(%rbp)
jmp 0x7f615
movl $0x400, %eax            # imm = 0x400
movl %eax, -0x84(%rbp)
jmp 0x7f615
movl -0x84(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4820
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x7f656
cmpq $0x0, -0x68(%rbp)
jne 0x7f6b8
cmpq $0x0, -0x60(%rbp)
je 0x7f66a
movq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x7f675
movq -0x30(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x45a0
cmpq $0x0, -0x68(%rbp)
je 0x7f695
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x7f6a0
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x45a0
movl $0x3, -0x4(%rbp)
jmp 0x7f814
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x18(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %edx
movq -0x30(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x7f72e
movl -0x58(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x7f73c
movl -0x58(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x7f5a2
jmp 0x7f755
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x100c(%rax)
movslq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x1010(%rax)
movslq -0x24(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x1018(%rax)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1008(%rcx), %eax
jne 0x7f791
jmp 0x7f7b0
leaq 0x7691d(%rip), %rdi     # 0xf60b5
leaq 0x768a1(%rip), %rsi     # 0xf6040
movl $0xc7, %edx
leaq 0x7691c(%rip), %rcx     # 0xf60c7
callq 0x42e0
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x38(%rbp), %rdi
callq 0x45a0
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x4, %eax
jl 0x7f7fe
cmpl $0x1, -0x1c(%rbp)
jne 0x7f7e0
movl $0x2c, -0x4(%rbp)
jmp 0x7f814
cmpl $0x0, -0x1c(%rbp)
je 0x7f7ec
cmpl $0x0, -0x28(%rbp)
jge 0x7f7f5
movl $0x2a, -0x4(%rbp)
jmp 0x7f814
movl $0x29, -0x4(%rbp)
jmp 0x7f814
movl -0x1c(%rbp), %edx
movl $0x1, %eax
movl $0x3, %ecx
cmpl $0x1, %edx
cmovlel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq