// File: /workspace/github_c_makefile_star2_O2/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, %r15
movl $0x880, %edi            # imm = 0x880
callq 0x4680
testq %rax, %rax
je 0x737c4
movq %rbx, 0x20(%rsp)
movq %rax, 0x8(%rsp)
movl $0x3, (%rax)
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %r15, 0x18(%rsp)
cmpq $0x400, %rbp            # imm = 0x400
je 0x736fd
cmpl $0x0, (%r15,%rbp,4)
je 0x736f8
cmpq %r14, %r12
jb 0x736e0
movq %rbx, %r15
testq %r14, %r14
leaq (%r14,%r14), %r14
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %r14
leaq (,%r14,4), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x7388d
movq %rax, %r13
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x7388d
movq %rax, %rbx
movq 0x18(%rsp), %r15
movl %ebp, (%r13,%r12,4)
movl (%r15,%rbp,4), %eax
movl %eax, (%rbx,%r12,4)
testl %eax, %eax
jle 0x73a7e
incq %r12
incq %rbp
jmp 0x7367d
movq 0x1000(%r15), %rdx
testq %rdx, %rdx
je 0x737cc
xorl %esi, %esi
xorl %ebp, %ebp
cmpl (%rdx), %ebp
je 0x737cc
movq 0x10(%rdx), %rax
movl %ebp, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %esi, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x737ba
cmpq %r14, %r12
jb 0x73796
movl %esi, 0x14(%rsp)
movq %rbx, %r15
testq %r14, %r14
leaq (%r14,%r14), %r14
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %r14
leaq (,%r14,4), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x7388d
movq %rax, %r13
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x7388d
movq 0x18(%rsp), %rcx
movq 0x1000(%rcx), %rdx
movq %rax, %rbx
movl 0x14(%rsp), %esi
movq 0x18(%rdx), %rax
movl %ebp, %ecx
movl (%rax,%rcx,8), %eax
movl %eax, (%r13,%r12,4)
movq 0x20(%rdx), %rax
movl (%rax,%rcx,4), %eax
movl %eax, (%rbx,%r12,4)
testl %eax, %eax
jle 0x73a9d
incq %r12
incl %ebp
addl $0x2, %esi
jmp 0x73711
xorl %r12d, %r12d
jmp 0x73a00
testq %r12, %r12
je 0x73abc
leaq (,%r12,8), %rsi
movq %rbx, %rdi
callq 0x4840
testq %rax, %rax
je 0x73890
movq %rax, %rbp
leaq (%r12,%r12), %rdi
pushq $0x4
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x739b8
movq %rax, %rbx
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edx, %edx
cmpq %rax, %r12
je 0x73860
movl (%rbp,%rax,4), %r8d
testl %r8d, %r8d
js 0x73832
cmpl %r8d, %ecx
jle 0x73839
movl %eax, %r9d
jmp 0x73851
movl %ecx, %r8d
movl %edi, %ecx
jmp 0x7384c
cmpl %r8d, %edi
movl %r8d, %r9d
cmovll %edi, %r9d
cmovgl %eax, %edx
movl %ecx, %r8d
movl %r9d, %ecx
movl %esi, %r9d
movl %edx, %esi
movl %esi, %edx
movl %ecx, %edi
incq %rax
movl %r8d, %ecx
movl %r9d, %esi
jmp 0x73819
cmpl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
je 0x73898
addl %ecx, %edi
movl %edi, (%rbp,%r12,4)
movslq %esi, %rax
movl %r12d, (%rbx,%rax,4)
movslq %edx, %rcx
movl %r12d, (%rbx,%rcx,4)
negl (%rbp,%rax,4)
negl (%rbp,%rcx,4)
incq %r12
jmp 0x73809
movq %r15, %rbx
xorl %r14d, %r14d
jmp 0x739c3
shrq %r12
incq %r12
xorl %eax, %eax
cmpq %r12, %rax
je 0x738c9
leaq (%rbx,%rax,4), %rcx
xorl %edx, %edx
movq %rcx, %rsi
movslq (%rsi), %rsi
testq %rsi, %rsi
je 0x738be
incl %edx
leaq (%rbx,%rsi,4), %rsi
jmp 0x738ae
movl %edx, (%rcx)
negl (%rbp,%rax,4)
incq %rax
jmp 0x738a0
imulq $0x18, %r12, %rdi
callq 0x4680
testq %rax, %rax
je 0x739bd
movq %rax, %r14
addq $0x10, %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x73900
movslq (%r13,%rcx,4), %rdx
movq %rdx, -0x10(%rax)
movl (%rbx,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x18, %rax
jmp 0x738e4
leaq -0x789(%rip), %rcx      # 0x7317e
pushq $0x18
popq %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x4990
movl 0x10(%r14), %edx
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %r12, %rax
je 0x7395e
imulq $0x18, %rax, %rsi
leaq (%r14,%rsi), %rdi
movl 0x10(%r14,%rsi), %esi
cmpl %edx, %esi
je 0x73939
addl %ecx, %ecx
incl %edx
jmp 0x7392f
movl %ecx, 0xc(%rdi)
movq (%rdi), %rdx
leaq 0x1(%rdx), %rdi
cmpq $0x80, %rdi
ja 0x73955
movq 0x8(%rsp), %rdi
movl %eax, 0x70(%rdi,%rdx,4)
incl %ecx
incq %rax
movl %esi, %edx
jmp 0x7391d
movq %rbx, %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
movq 0x8(%rsp), %rdx
movq %r14, 0x60(%rdx)
movl %r12d, 0x68(%rdx)
movq 0x20(%rsp), %rcx
movl %ecx, 0x270(%rdx)
leaq -0x5aa(%rip), %rax      # 0x733eb
movq %rax, 0x20(%rdx)
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x73a12
cmpl $0x0, 0x10(%r14)
je 0x73a4c
leaq -0x818(%rip), %rax      # 0x7319b
jmp 0x73a65
xorl %r14d, %r14d
jmp 0x739c0
movq %rbx, %r14
movq %rbp, %rbx
leaq 0x10d8b(%rip), %rsi     # 0x84755
leaq 0x5c14(%rip), %rdx      # 0x795e5
xorl %r12d, %r12d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %r13, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movq 0x8(%rsp), %rdi
callq 0x45c0
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
decl %ecx
cmpl $0x6, %ecx
ja 0x73a00
leaq 0x10735(%rip), %rax     # 0x84158
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x10(%r14)
je 0x73a55
leaq -0x7e3(%rip), %rax      # 0x73257
jmp 0x73a65
cmpl $0x0, 0x10(%r14)
je 0x73a5e
leaq -0x72e(%rip), %rax      # 0x7331c
jmp 0x73a65
leaq -0x8bb(%rip), %rax      # 0x73198
jmp 0x73a65
leaq -0x808(%rip), %rax      # 0x73254
jmp 0x73a65
leaq -0x74c(%rip), %rax      # 0x73319
movq %rax, 0x30(%rdx)
leaq -0x669(%rip), %rax      # 0x73407
movq %rax, 0x38(%rdx)
andq $0x0, 0x48(%rdx)
movq %rdx, %r12
jmp 0x73a00
leaq 0x10c29(%rip), %rdi     # 0x846ae
leaq 0x10b3e(%rip), %rsi     # 0x845ca
leaq 0x10c2c(%rip), %rcx     # 0x846bf
movl $0xc76, %edx            # imm = 0xC76
callq 0x42f0
leaq 0x10c96(%rip), %rdi     # 0x8473a
leaq 0x10b1f(%rip), %rsi     # 0x845ca
leaq 0x10c0d(%rip), %rcx     # 0x846bf
movl $0xc8c, %edx            # imm = 0xC8C
callq 0x42f0
leaq 0x10c88(%rip), %rdi     # 0x8474b
leaq 0x10b00(%rip), %rsi     # 0x845ca
leaq 0x10bee(%rip), %rcx     # 0x846bf
movl $0xc93, %edx            # imm = 0xC93
callq 0x42f0