// File: /workspace/github_c_makefile_star2_O1/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, %r12
movl $0x880, %edi            # imm = 0x880
callq 0x4680
testq %rax, %rax
je 0x940c9
movq %rbx, 0x20(%rsp)
movq %rax, 0x8(%rsp)
movl $0x3, (%rax)
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r8d, %r8d
xorl %edx, %edx
movq %r12, 0x18(%rsp)
cmpl $0x0, (%r12,%rbp,4)
je 0x93d85
cmpq %r13, %r8
jb 0x93d6b
movq %rdx, (%rsp)
movq %r8, %r12
testq %r13, %r13
leaq (%r13,%r13), %r13
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %r13
leaq (,%r13,4), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x940dd
movq %rax, %r14
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x4840
testq %rax, %rax
je 0x940ce
movq %rax, %rdx
movq %r14, %r15
movq %r12, %r8
movq 0x18(%rsp), %r12
movl %ebp, (%r15,%r8,4)
movl (%r12,%rbp,4), %eax
movl %eax, (%rdx,%r8,4)
cmpl $0x0, (%r12,%rbp,4)
jle 0x941d3
incq %r8
incq %rbp
cmpq $0x400, %rbp            # imm = 0x400
jne 0x93d05
cmpq $0x0, 0x1000(%r12)
je 0x93e9c
movq 0x1000(%r12), %rax
cmpl $0x0, (%rax)
je 0x93e89
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x10(%rax), %rax
movl %r14d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x93e72
cmpq %r13, %r8
jae 0x93de0
movq %rdx, %rax
jmp 0x93e43
movq %rdx, (%rsp)
movq %r8, 0x10(%rsp)
testq %r13, %r13
leaq (%r13,%r13), %r13
movl $0x400, %eax            # imm = 0x400
cmoveq %rax, %r13
leaq (,%r13,4), %r12
movq %r15, %rbp
movq %r15, %rdi
movq %r12, %rsi
callq 0x4840
movq %rax, %r15
movl $0x5, %eax
testq %r15, %r15
je 0x94168
movq (%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4840
testq %rax, %rax
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r12
je 0x94179
movq 0x1000(%r12), %rcx
movq 0x18(%rcx), %rdx
movl %r14d, %esi
movl (%rdx,%rsi,8), %edx
movl %edx, (%r15,%r8,4)
movq 0x20(%rcx), %rcx
movl (%rcx,%rsi,4), %ecx
movl %ecx, (%rax,%r8,4)
testl %ecx, %ecx
jle 0x941f2
incq %r8
movq %rax, %rdx
incl %r14d
movq 0x1000(%r12), %rax
addl $0x2, %ebx
cmpl (%rax), %r14d
jne 0x93dba
xorl %eax, %eax
cmpl $0x5, %eax
je 0x940d9
testl %eax, %eax
jne 0x94123
testq %r8, %r8
je 0x94211
movq %r15, %rbx
leaq (,%r8,8), %rsi
movq %rdx, %r14
movq %rdx, %rdi
movq %r8, %r15
callq 0x4840
testq %rax, %rax
je 0x940d3
movq %rax, %rbp
movq %r15, %r14
leaq (%r15,%r15), %rdi
movl $0x4, %esi
callq 0x4300
testq %rax, %rax
je 0x94135
movq %r14, %r10
movq %rax, %r12
testq %r10, %r10
je 0x93f40
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %r9d
movl %edi, %r8d
movl (%rbp,%rdx,4), %ecx
testl %ecx, %ecx
js 0x93f27
cmpl %ecx, %r9d
jle 0x93f1d
movl %r9d, %r11d
movl %edx, %edi
movl %r8d, %esi
jmp 0x93f2d
cmpl %ecx, %r11d
cmovgel %ecx, %r11d
cmovgl %edx, %esi
movl %r9d, %ecx
movl %r8d, %edi
incq %rdx
cmpq %rdx, %r10
jne 0x93f00
movslq %edi, %rdx
movslq %esi, %rsi
movq %r12, %rax
jmp 0x93f4f
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %esi, %esi
cmpl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
je 0x93f73
addl %r11d, %ecx
movl %ecx, (%rbp,%r10,4)
movl %r10d, (%rax,%rdx,4)
movl %r10d, (%rax,%rsi,4)
negl (%rbp,%rdx,4)
negl (%rbp,%rsi,4)
incq %r10
cmpl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
jne 0x93eea
shrq %r10
xorl %esi, %esi
movl (%rax,%rsi,4), %edx
movl $0x0, %ecx
testl %edx, %edx
je 0x93f9f
xorl %ecx, %ecx
incl %ecx
movslq %edx, %rdx
movl (%rax,%rdx,4), %edx
testl %edx, %edx
jne 0x93f93
negl (%rbp,%rsi,4)
movl %ecx, (%rax,%rsi,4)
leaq 0x1(%rsi), %rcx
cmpq %r10, %rsi
movq %rcx, %rsi
jne 0x93f85
leaq 0x8(,%r10,8), %rax
leaq (%rax,%rax,2), %rdi
movq %r10, %r14
callq 0x4680
testq %rax, %rax
je 0x94138
movq %rax, %r13
movq %r14, %r15
incq %r14
addq $0x10, %rax
xorl %ecx, %ecx
movq %rbx, %rsi
movq %r12, %rdi
movslq (%rsi,%rcx,4), %rdx
movq %rdx, -0x10(%rax)
movl (%rdi,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x18, %rax
cmpq %rcx, %r14
jne 0x93fe4
leaq -0x94d(%rip), %rcx      # 0x936b7
movl $0x18, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x4990
movl 0x10(%r13), %ecx
xorl %eax, %eax
xorl %edx, %edx
movq %r15, %r8
leaq (%rax,%rax,2), %rsi
movl 0x10(%r13,%rsi,8), %edi
cmpl %edi, %ecx
je 0x94036
addl %edx, %edx
incl %ecx
cmpl %ecx, %edi
jne 0x9402c
movl %edi, %ecx
leaq (,%rsi,8), %rsi
addq %r13, %rsi
movl %edx, 0xc(%rsi)
movq (%rsi), %rsi
leaq 0x1(%rsi), %rdi
cmpq $0x80, %rdi
ja 0x9405d
movq 0x8(%rsp), %rdi
movl %eax, 0x70(%rdi,%rsi,4)
incl %edx
leaq 0x1(%rax), %rsi
cmpq %r8, %rax
movq %rsi, %rax
jne 0x9401f
movq %r12, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x45c0
movq 0x8(%rsp), %rcx
movq %r13, 0x60(%rcx)
movl %r14d, 0x68(%rcx)
movq 0x20(%rsp), %rdx
movl %edx, 0x270(%rcx)
leaq -0x765(%rip), %rax      # 0x9393d
movq %rax, 0x20(%rcx)
leal -0x3(%rdx), %eax
cmpl $0x1, %eax
ja 0x9413e
cmpl $0x0, 0x10(%r13)
je 0x94196
leaq -0x9ee(%rip), %rax      # 0x936d6
jmp 0x941b4
xorl %r14d, %r14d
jmp 0x94123
movq %r14, %rbx
jmp 0x940e0
movq %r14, (%rsp)
jmp 0x940e0
movq %rdx, (%rsp)
movq %r15, %rbx
xorl %r12d, %r12d
leaq 0x115fb(%rip), %rsi     # 0xa56e5
leaq 0x6594(%rip), %rdx      # 0x9a685
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movq %rbx, %rdi
callq 0x45c0
movq (%rsp), %rdi
callq 0x45c0
movq %r12, %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 %r12d, %r12d
movq %rbp, (%rsp)
jmp 0x940e3
xorl %r14d, %r14d
decl %edx
cmpl $0x6, %edx
ja 0x94123
leaq 0x10f99(%rip), %rax     # 0xa50e8
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x10(%r13)
je 0x9419f
leaq -0x9cf(%rip), %rax      # 0x93797
jmp 0x941af
movq %rbp, %r15
movq 0x10(%rsp), %r8
movq (%rsp), %rdx
jmp 0x93e8b
movq %rbp, %rdx
movl $0x5, %eax
jmp 0x93e8b
cmpl $0x0, 0x10(%r13)
je 0x941a8
leaq -0x92c(%rip), %rax      # 0x93868
jmp 0x941af
leaq -0xaca(%rip), %rax      # 0x936d3
jmp 0x941b4
leaq -0xa12(%rip), %rax      # 0x93794
jmp 0x941af
leaq -0x94a(%rip), %rax      # 0x93865
movq 0x8(%rsp), %rcx
movq %rax, 0x30(%rcx)
leaq -0x861(%rip), %rax      # 0x9395e
movq %rax, 0x38(%rcx)
movq $0x0, 0x48(%rcx)
movq %rcx, %r14
jmp 0x94123
leaq 0x11464(%rip), %rdi     # 0xa563e
leaq 0x11379(%rip), %rsi     # 0xa555a
leaq 0x11467(%rip), %rcx     # 0xa564f
movl $0xc76, %edx            # imm = 0xC76
callq 0x42f0
leaq 0x114d1(%rip), %rdi     # 0xa56ca
leaq 0x1135a(%rip), %rsi     # 0xa555a
leaq 0x11448(%rip), %rcx     # 0xa564f
movl $0xc8c, %edx            # imm = 0xC8C
callq 0x42f0
leaq 0x114c3(%rip), %rdi     # 0xa56db
leaq 0x1133b(%rip), %rsi     # 0xa555a
leaq 0x11429(%rip), %rcx     # 0xa564f
movl $0xc93, %edx            # imm = 0xC93
callq 0x42f0