// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/cram/cram_codecs.c
// Function: cram_huffman_encode_init
cram_codec *cram_huffman_encode_init(cram_stats *st,
				     enum cram_external_type option,
				     void *dat,
				     int version) {
    int *vals = NULL, *freqs = NULL, vals_alloc = 0, *lens, code, len;
    int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX, k;
    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;
	    vals  = realloc(vals,  vals_alloc * sizeof(int));
	    freqs = realloc(freqs, vals_alloc * sizeof(int));
	    if (!vals || !freqs) {
		if (vals)  free(vals);
		if (freqs) free(freqs);
		free(c);
		return NULL;
	    }
	}
	vals[nvals] = i;
	freqs[nvals] = st->freqs[i];
	assert(st->freqs[i] > 0);
	ntot += freqs[nvals];
	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;
		vals  = realloc(vals,  vals_alloc * sizeof(int));
		freqs = realloc(freqs, vals_alloc * sizeof(int));
		if (!vals || !freqs)
		    return NULL;
	    }
	    vals[nvals]= kh_key(st->h, k);
	    freqs[nvals] = kh_val(st->h, k);
	    assert(freqs[nvals] > 0);
	    ntot += freqs[nvals];
	    if (max_val < i) max_val = i;
	    if (min_val > i) min_val = i;
	    nvals++;
	}
    }

    assert(nvals > 0);

    freqs = realloc(freqs, 2*nvals*sizeof(*freqs));
    lens = calloc(2*nvals, sizeof(*lens));
    if (!lens || !freqs)
	return NULL;

    /* 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))))
	return NULL;
    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->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->e_huffman.codes = codes;
    c->e_huffman.nvals = nvals;

    c->free = cram_huffman_encode_free;
    if (option == E_BYTE || option == E_BYTE_ARRAY) {
	if (c->e_huffman.codes[0].len == 0)
	    c->encode = cram_huffman_encode_char0;
	else
	    c->encode = cram_huffman_encode_char;
    } else {
	if (c->e_huffman.codes[0].len == 0)
	    c->encode = cram_huffman_encode_int0;
	else
	    c->encode = cram_huffman_encode_int;
    }
    c->store = cram_huffman_encode_store;

    return c;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, %r12
movl $0x240, %edi            # imm = 0x240
callq 0x69d0
xorl %r13d, %r13d
testq %rax, %rax
je 0x8a8dd
movl %ebx, 0x14(%rsp)
movq %rax, 0x18(%rsp)
movl $0x3, (%rax)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
xorl %ebp, %ebp
cmpl $0x0, (%r12,%r14,4)
je 0x8a58b
cmpl %ebp, %r13d
jl 0x8a56f
movq %r13, %r15
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl %ecx, %ebp
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %ebp
movslq %ebp, %r13
shlq $0x2, %r13
movq %r13, %rsi
movq %rax, 0x8(%rsp)
callq 0x6c80
movq %rax, %rbx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x6c80
movq %rbx, %rdi
testq %rbx, %rbx
je 0x8a8b3
testq %rax, %rax
je 0x8a8b3
movq %r15, %r13
movslq %r13d, %rdx
movl %r14d, (%rdi,%rdx,4)
movl (%r12,%r14,4), %ecx
movl %ecx, (%rax,%rdx,4)
cmpl $0x0, (%r12,%r14,4)
jle 0x8a8f8
incl %r13d
incq %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x8a514
cmpq $0x0, 0x1000(%r12)
je 0x8a687
movq 0x1000(%r12), %rcx
cmpl $0x0, (%rcx)
sete %r15b
je 0x8a67d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rcx), %rdx
movl %ebx, %ecx
shrl $0x4, %ecx
movl (%rdx,%rcx,4), %edx
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x8a663
cmpl %ebp, %r13d
jl 0x8a637
movq %r13, 0x20(%rsp)
testl %ebp, %ebp
leal (%rbp,%rbp), %ecx
movl %ecx, %ebp
movl $0x400, %ecx            # imm = 0x400
cmovel %ecx, %ebp
movslq %ebp, %r13
shlq $0x2, %r13
movq %r13, %rsi
movq %rax, 0x8(%rsp)
callq 0x6c80
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x6c80
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8a8ee
testq %rax, %rax
movq 0x20(%rsp), %r13
je 0x8a67d
movq 0x1000(%r12), %r8
movq 0x18(%r8), %rcx
movl %ebx, %edx
movl (%rcx,%rdx,4), %ecx
movslq %r13d, %rsi
movl %ecx, (%rdi,%rsi,4)
movq 0x20(%r8), %rcx
movl (%rcx,%rdx,4), %ecx
movl %ecx, (%rax,%rsi,4)
testl %ecx, %ecx
jle 0x8a917
incl %r13d
incl %ebx
movq 0x1000(%r12), %rcx
addl $0x2, %r14d
cmpl (%rcx), %ebx
sete %r15b
jne 0x8a5c4
testb $0x1, %r15b
je 0x8a8dd
movq %rdi, (%rsp)
testl %r13d, %r13d
jle 0x8a936
leal (,%r13,2), %r12d
leaq (,%r12,4), %rsi
movq %rax, %rdi
callq 0x6c80
movq %rax, %r15
movl $0x4, %esi
movq %r12, %rdi
callq 0x6470
movq %rax, %r12
xorl %eax, %eax
testq %r15, %r15
movq 0x18(%rsp), %rbx
je 0x8a8df
testq %r12, %r12
je 0x8a8df
movl %r13d, %ecx
testl %r13d, %r13d
jle 0x8a72d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, %r10d
movl %r8d, %r9d
movl (%r15,%rsi,4), %edx
testl %edx, %edx
js 0x8a717
cmpl %edx, %r10d
jle 0x8a70f
movl %r10d, %eax
movl %esi, %r8d
movl %r9d, %edi
jmp 0x8a71d
cmpl %edx, %eax
cmovgel %edx, %eax
cmovgl %esi, %edi
movl %r10d, %edx
movl %r9d, %r8d
incq %rsi
cmpq %rsi, %rcx
jne 0x8a6f1
movslq %r8d, %rsi
movslq %edi, %rdi
jmp 0x8a73b
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x8a75b
addl %eax, %edx
movl %edx, (%r15,%rcx,4)
movl %r13d, (%r12,%rsi,4)
movl %r13d, (%r12,%rdi,4)
negl (%r15,%rsi,4)
negl (%r15,%rdi,4)
incl %r13d
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x8a6d8
shrl %r13d
leal 0x1(%r13), %ebp
xorl %eax, %eax
movl (%r12,%rax,4), %edx
movl $0x0, %ecx
testl %edx, %edx
je 0x8a78b
xorl %ecx, %ecx
incl %ecx
movslq %edx, %rdx
movl (%r12,%rdx,4), %edx
testl %edx, %edx
jne 0x8a77e
negl (%r15,%rax,4)
movl %ecx, (%r12,%rax,4)
incq %rax
cmpq %rbp, %rax
jne 0x8a76f
movl %r13d, %edi
incq %rdi
movq %rdi, %r14
shlq $0x4, %rdi
callq 0x69d0
testq %rax, %rax
je 0x8a8dd
movq %r13, %rcx
movq %rax, %r13
leaq 0x4(,%rcx,4), %rax
xorl %ecx, %ecx
movq (%rsp), %rsi
movl (%rsi,%rcx), %edx
movl %edx, (%r13,%rcx,4)
movl (%r12,%rcx), %edx
movl %edx, 0xc(%r13,%rcx,4)
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x8a7ca
leaq -0x9d1(%rip), %rcx      # 0x89e1a
movl $0x10, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x6e40
movl 0xc(%r13), %ecx
xorl %eax, %eax
xorl %edx, %edx
movq %rax, %rsi
shlq $0x4, %rsi
movl 0xc(%r13,%rsi), %edi
cmpl %edi, %ecx
je 0x8a81d
addl %edx, %edx
incl %ecx
cmpl %ecx, %edi
jne 0x8a813
movl %edi, %ecx
addq %r13, %rsi
movl %edx, 0x8(%rsi)
movl (%rsi), %esi
incl %esi
cmpl $0x80, %esi
ja 0x8a833
movl %eax, 0x3c(%rbx,%rsi,4)
incl %edx
incq %rax
cmpq %rbp, %rax
jne 0x8a803
movq %r12, %rdi
callq 0x68a0
movq (%rsp), %rdi
callq 0x68a0
movq %r15, %rdi
callq 0x68a0
movq %r13, 0x30(%rbx)
movl %ebp, 0x38(%rbx)
movl 0x14(%rsp), %esi
addl $-0x3, %esi
cmpl $0x0, 0xc(%r13)
leaq -0xa49(%rip), %rax      # 0x89e27
leaq -0xa4d(%rip), %rcx      # 0x89e2a
cmoveq %rax, %rcx
leaq -0x9a2(%rip), %rax      # 0x89ee0
leaq -0x9a6(%rip), %rdx      # 0x89ee3
cmoveq %rax, %rdx
leaq -0x8f3(%rip), %rax      # 0x89fa1
movq %rax, 0x10(%rbx)
cmpl $0x2, %esi
cmovbq %rcx, %rdx
movq %rdx, 0x20(%rbx)
leaq -0x8e8(%rip), %rax      # 0x89fc2
movq %rax, 0x28(%rbx)
movq %rbx, %rax
jmp 0x8a8df
testq %rdi, %rdi
je 0x8a8c3
movq %rax, %rbx
callq 0x68a0
movq %rbx, %rax
testq %rax, %rax
movq 0x18(%rsp), %rbx
je 0x8a8d5
movq %rax, %rdi
callq 0x68a0
movq %rbx, %rdi
callq 0x68a0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %r13
jmp 0x8a67d
leaq 0x110f8(%rip), %rdi     # 0x9b9f7
leaq 0x10f79(%rip), %rsi     # 0x9b87f
leaq 0x110fb(%rip), %rcx     # 0x9ba08
movl $0x4c5, %edx            # imm = 0x4C5
callq 0x6460
leaq 0x11143(%rip), %rdi     # 0x9ba61
leaq 0x10f5a(%rip), %rsi     # 0x9b87f
leaq 0x110dc(%rip), %rcx     # 0x9ba08
movl $0x4da, %edx            # imm = 0x4DA
callq 0x6460
leaq 0x11135(%rip), %rdi     # 0x9ba72
leaq 0x10f3b(%rip), %rsi     # 0x9b87f
leaq 0x110bd(%rip), %rcx     # 0x9ba08
movl $0x4e2, %edx            # imm = 0x4E2
callq 0x6460