// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_codecs.c
// Function: cram_huffman_encode_init
cram_codec *cram_huffman_encode_init(cram_stats *st,
                                     enum cram_encoding codec,
                                     enum cram_external_type option,
                                     void *dat,
                                     int version, varint_vec *vv) {
    int *vals = NULL, *freqs = NULL, *lens = NULL, code, len;
    int *new_vals, *new_freqs;
    int i, max_val = 0, min_val = INT_MAX, k;
    size_t nvals, vals_alloc = 0;
    cram_codec *c;
    cram_huffman_code *codes;

    c = malloc(sizeof(*c));
    if (!c)
        return NULL;
    c->codec = E_HUFFMAN;

    /* 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;
            new_vals  = realloc(vals,  vals_alloc * sizeof(int));
            if (!new_vals) goto nomem;
            vals = new_vals;
            new_freqs = realloc(freqs, vals_alloc * sizeof(int));
            if (!new_freqs) goto nomem;
            freqs = new_freqs;
        }
        vals[nvals] = i;
        freqs[nvals] = st->freqs[i];
        assert(st->freqs[i] > 0);
        if (max_val < i) max_val = i;
        if (min_val > i) min_val = i;
        nvals++;
    }
    if (st->h) {
        khint_t k;

        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;
                new_vals  = realloc(vals,  vals_alloc * sizeof(int));
                if (!new_vals) goto nomem;
                vals = new_vals;
                new_freqs = realloc(freqs, vals_alloc * sizeof(int));
                if (!new_freqs) goto nomem;
                freqs = new_freqs;
            }
            vals[nvals]= kh_key(st->h, k);
            freqs[nvals] = kh_val(st->h, k);
            assert(freqs[nvals] > 0);
            if (max_val < i) max_val = i;
            if (min_val > i) min_val = i;
            nvals++;
        }
    }

    assert(nvals > 0);

    new_freqs = realloc(freqs, 2*nvals*sizeof(*freqs));
    if (!new_freqs) goto nomem;
    freqs = new_freqs;
    lens = calloc(2*nvals, sizeof(*lens));
    if (!lens) goto nomem;

    /* Inefficient, use pointers to form chain so we can insert and maintain
     * a sorted list? This is currently O(nvals^2) complexity.
     */
    for (;;) {
        int low1 = INT_MAX, low2 = INT_MAX;
        int ind1 = 0, ind2 = 0;
        for (i = 0; i < nvals; i++) {
            if (freqs[i] < 0)
                continue;
            if (low1 > freqs[i])
                low2 = low1, ind2 = ind1, low1 = freqs[i], ind1 = i;
            else if (low2 > freqs[i])
                low2 = freqs[i], ind2 = i;
        }
        if (low2 == INT_MAX)
            break;

        freqs[nvals] = low1 + low2;
        lens[ind1] = nvals;
        lens[ind2] = nvals;
        freqs[ind1] *= -1;
        freqs[ind2] *= -1;
        nvals++;
    }
    nvals = nvals/2+1;

    /* Assign lengths */
    for (i = 0; i < nvals; i++) {
        int code_len = 0;
        for (k = lens[i]; k; k = lens[k])
            code_len++;
        lens[i] = code_len;
        freqs[i] *= -1;
        //fprintf(stderr, "%d / %d => %d\n", vals[i], freqs[i], lens[i]);
    }


    /* Sort, need in a struct */
    if (!(codes = malloc(nvals * sizeof(*codes))))
        goto nomem;
    for (i = 0; i < nvals; i++) {
        codes[i].symbol = vals[i];
        codes[i].len = lens[i];
    }
    qsort(codes, nvals, sizeof(*codes), code_sort);

    /*
     * Generate canonical codes from lengths.
     * Sort by length.
     * Start with 0.
     * Every new code of same length is +1.
     * Every new code of new length is +1 then <<1 per extra length.
     *
     * /\
     * a/\
     * /\/\
     * bcd/\
     *    ef
     *
     * a 1  0
     * b 3  4 (0+1)<<2
     * c 3  5
     * d 3  6
     * e 4  14  (6+1)<<1
     * f 5  15
     */
    code = 0; len = codes[0].len;
    for (i = 0; i < nvals; i++) {
        while (len != codes[i].len) {
            code<<=1;
            len++;
        }
        codes[i].code = code++;

        if (codes[i].symbol >= -1 && codes[i].symbol < MAX_HUFF)
            c->u.e_huffman.val2code[codes[i].symbol+1] = i;

        //fprintf(stderr, "sym %d, code %d, len %d\n",
        //      codes[i].symbol, codes[i].code, codes[i].len);
    }

    free(lens);
    free(vals);
    free(freqs);

    c->u.e_huffman.codes = codes;
    c->u.e_huffman.nvals = nvals;
    c->u.e_huffman.option = option;

    c->free = cram_huffman_encode_free;
    if (option == E_BYTE || option == E_BYTE_ARRAY) {
        if (c->u.e_huffman.codes[0].len == 0)
            c->encode = cram_huffman_encode_char0;
        else
            c->encode = cram_huffman_encode_char;
    } else if (option == E_INT || option == E_SINT) {
        if (c->u.e_huffman.codes[0].len == 0)
            c->encode = cram_huffman_encode_int0;
        else
            c->encode = cram_huffman_encode_int;
    } else if (option == E_LONG || option == E_SLONG) {
        if (c->u.e_huffman.codes[0].len == 0)
            c->encode = cram_huffman_encode_long0;
        else
            c->encode = cram_huffman_encode_long;
    } else {
        return NULL;
    }
    c->store = cram_huffman_encode_store;
    c->flush = NULL;

    return c;

 nomem:
    hts_log_error("Out of memory");
    free(vals);
    free(freqs);
    free(lens);
    free(c);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebx
movq %rdi, %r14
movl $0x880, %edi            # imm = 0x880
callq 0x4680
testq %rax, %rax
je 0x9157a
movq %rbx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movl $0x3, (%rax)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r8d, %r8d
movq %r14, 0x10(%rsp)
cmpl $0x0, (%r14,%r12,4)
je 0x91482
cmpq %rbp, %r13
jb 0x9146b
movq %r8, (%rsp)
testq %rbp, %rbp
leaq (%rbp,%rbp), %rbp
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %rbp
leaq (,%rbp,4), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x9178a
movq %rax, %r14
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x91787
movq %rax, %r8
movq %r14, %r15
movq 0x10(%rsp), %r14
movl %r12d, (%r15,%r13,4)
movl (%r14,%r12,4), %eax
movl %eax, (%r8,%r13,4)
testl %eax, %eax
jle 0x9185f
incq %r13
incq %r12
cmpq $0x400, %r12            # imm = 0x400
jne 0x9140b
movq 0x1000(%r14), %rdx
testq %rdx, %rdx
je 0x91582
movl (%rdx), %eax
testl %eax, %eax
je 0x91582
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rdx), %rcx
movl %ebx, %esi
shrl $0x4, %esi
movl (%rcx,%rsi,4), %esi
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %esi
testb $0x3, %sil
je 0x914d3
movq %r15, %rsi
jmp 0x9156a
cmpq %rbp, %r13
jae 0x914dd
movq %r15, %rsi
jmp 0x91542
movq %r13, 0x20(%rsp)
movq %r8, (%rsp)
testq %rbp, %rbp
leaq (%rbp,%rbp), %rbp
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %rbp
leaq (,%rbp,4), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x9178a
movq %rax, %r15
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x9178a
movq 0x10(%rsp), %rcx
movq 0x1000(%rcx), %rdx
movq %rax, %r8
movq %r15, %rsi
movq 0x20(%rsp), %r13
movq 0x18(%rdx), %rax
movl %ebx, %ecx
movl (%rax,%rcx,8), %eax
movl %eax, (%rsi,%r13,4)
movq 0x20(%rdx), %rax
movl (%rax,%rcx,4), %eax
movl %eax, (%r8,%r13,4)
testl %eax, %eax
jle 0x9187e
incq %r13
movl (%rdx), %eax
movq %rsi, %r15
incl %ebx
addl $0x2, %r14d
cmpl %eax, %ebx
jne 0x914b1
jmp 0x91585
xorl %r14d, %r14d
jmp 0x917e0
movq %r15, %rsi
testq %r13, %r13
je 0x9189d
movq %rsi, %r14
leaq (,%r13,8), %rsi
movq %r8, %rbx
movq %r8, %rdi
callq 0x4840
testq %rax, %rax
je 0x9177f
movq %rax, %rbp
leaq (,%r13,2), %rdi
movl $0x4, %esi
callq 0x4300
testq %rax, %rax
je 0x9178e
movq %rax, %r15
cmpq $0x1, %r13
movq %r13, %rcx
adcq $0x0, %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl %edi, %r10d
movl %r8d, %r9d
movl (%rbp,%rsi,4), %edi
testl %edi, %edi
js 0x91610
cmpl %edi, %r10d
jle 0x91608
movl %r10d, %eax
movl %esi, %r8d
movl %r9d, %edx
jmp 0x91616
cmpl %edi, %eax
cmovgel %edi, %eax
cmovgl %esi, %edx
movl %r10d, %edi
movl %r9d, %r8d
incq %rsi
cmpq %rsi, %rcx
jne 0x915ea
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x91647
addl %edi, %eax
movl %eax, (%rbp,%r13,4)
movslq %r8d, %rax
movl %r13d, (%r15,%rax,4)
movslq %edx, %rcx
movl %r13d, (%r15,%rcx,4)
negl (%rbp,%rax,4)
negl (%rbp,%rcx,4)
incq %r13
jmp 0x915ce
shrq %r13
leaq 0x1(%r13), %r12
xorl %eax, %eax
movl (%r15,%rax,4), %edx
movl $0x0, %ecx
testl %edx, %edx
je 0x9166c
xorl %ecx, %ecx
incl %ecx
movslq %edx, %rdx
movl (%r15,%rdx,4), %edx
testl %edx, %edx
jne 0x9165f
negl (%rbp,%rax,4)
movl %ecx, (%r15,%rax,4)
leaq 0x1(%rax), %rcx
cmpq %r13, %rax
movq %rcx, %rax
jne 0x91650
leaq (,%r12,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4680
testq %rax, %rax
je 0x91796
movq %rax, %rbx
addq $0x10, %rax
xorl %ecx, %ecx
movq %r14, %rsi
movslq (%rsi,%rcx,4), %rdx
movq %rdx, -0x10(%rax)
movl (%r15,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x18, %rax
cmpq %rcx, %r12
jne 0x916a6
leaq -0x911(%rip), %rcx      # 0x90db6
movl $0x18, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4990
movl 0x10(%rbx), %ecx
xorl %eax, %eax
xorl %edx, %edx
leaq (%rax,%rax,2), %rsi
movl 0x10(%rbx,%rsi,8), %edi
cmpl %edi, %ecx
je 0x916f4
addl %edx, %edx
incl %ecx
cmpl %ecx, %edi
jne 0x916ea
movl %edi, %ecx
leaq (%rbx,%rsi,8), %rsi
movl %edx, 0xc(%rsi)
movq (%rsi), %rsi
leaq 0x1(%rsi), %rdi
cmpq $0x80, %rdi
ja 0x91714
movq 0x8(%rsp), %rdi
movl %eax, 0x70(%rdi,%rsi,4)
incl %edx
leaq 0x1(%rax), %rsi
cmpq %r13, %rax
movq %rsi, %rax
jne 0x916de
movq %r15, %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
movq 0x8(%rsp), %rdx
movq %rbx, 0x60(%rdx)
movl %r12d, 0x68(%rdx)
movq 0x18(%rsp), %rcx
movl %ecx, 0x270(%rdx)
leaq -0x71c(%rip), %rax      # 0x9103d
movq %rax, 0x20(%rdx)
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x917f2
cmpl $0x0, 0x10(%rbx)
je 0x9182a
leaq -0x9a5(%rip), %rax      # 0x90dd5
jmp 0x91843
movq %rbx, (%rsp)
xorl %ebx, %ebx
jmp 0x9179d
movq %r14, %r15
xorl %ebx, %ebx
jmp 0x917a0
xorl %ebx, %ebx
movq %rbp, (%rsp)
jmp 0x9179d
movq %rbp, (%rsp)
movq %r15, %rbx
movq %r14, %r15
leaq 0x116be(%rip), %rsi     # 0xa2e65
leaq 0x5ee7(%rip), %rdx      # 0x97695
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %r15, %rdi
callq 0x45c0
movq (%rsp), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
movq 0x8(%rsp), %rdi
callq 0x45c0
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
decl %ecx
cmpl $0x6, %ecx
ja 0x917e0
leaq 0x11065(%rip), %rax     # 0xa2868
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x10(%rbx)
je 0x91833
leaq -0x97f(%rip), %rax      # 0x90e9a
jmp 0x91843
cmpl $0x0, 0x10(%rbx)
je 0x9183c
leaq -0x8c0(%rip), %rax      # 0x90f68
jmp 0x91843
leaq -0xa5f(%rip), %rax      # 0x90dd2
jmp 0x91843
leaq -0x9a3(%rip), %rax      # 0x90e97
jmp 0x91843
leaq -0x8de(%rip), %rax      # 0x90f65
movq %rax, 0x30(%rdx)
leaq -0x7f0(%rip), %rax      # 0x9105e
movq %rax, 0x38(%rdx)
movq $0x0, 0x48(%rdx)
movq %rdx, %r14
jmp 0x917e0
leaq 0x11558(%rip), %rdi     # 0xa2dbe
leaq 0x1146d(%rip), %rsi     # 0xa2cda
leaq 0x1155b(%rip), %rcx     # 0xa2dcf
movl $0xc76, %edx            # imm = 0xC76
callq 0x42f0
leaq 0x115c5(%rip), %rdi     # 0xa2e4a
leaq 0x1144e(%rip), %rsi     # 0xa2cda
leaq 0x1153c(%rip), %rcx     # 0xa2dcf
movl $0xc8c, %edx            # imm = 0xC8C
callq 0x42f0
leaq 0x115b7(%rip), %rdi     # 0xa2e5b
leaq 0x1142f(%rip), %rsi     # 0xa2cda
leaq 0x1151d(%rip), %rcx     # 0xa2dcf
movl $0xc93, %edx            # imm = 0xC93
callq 0x42f0