// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl $0x240, %edi            # imm = 0x240
callq 0x69d0
xorl %r14d, %r14d
testq %rax, %rax
je 0x78cd5
movl %ebx, 0x14(%rsp)
movq %rax, 0x18(%rsp)
movl $0x3, (%rax)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %edi, %edi
movq %r13, 0x8(%rsp)
cmpq $0x400, %r12            # imm = 0x400
je 0x78aa0
cmpl $0x0, (%r13,%r12,4)
je 0x78a98
cmpl %r14d, %ebx
jl 0x78a7e
testl %r14d, %r14d
leal (%r14,%r14), %eax
movl %eax, %r14d
movl $0x400, %eax            # imm = 0x400
cmovel %eax, %r14d
movslq %r14d, %r13
shlq $0x2, %r13
movq %r13, %rsi
callq 0x6c80
movq %rax, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x6c80
movq %rbp, %rdi
movq %rax, %r15
testq %rbp, %rbp
je 0x78cbe
testq %r15, %r15
je 0x78cbe
movq 0x8(%rsp), %r13
movslq %ebx, %rax
movl %r12d, (%rdi,%rax,4)
movl (%r13,%r12,4), %ecx
movl %ecx, (%r15,%rax,4)
testl %ecx, %ecx
jle 0x78dcc
incl %ebx
incq %r12
jmp 0x78a1b
movq 0x1000(%r13), %rax
testq %rax, %rax
je 0x78b73
movq $0x0, (%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpl (%rax), %r12d
je 0x78b73
movq 0x10(%rax), %rcx
movl %r12d, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
movl %r13d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x78b67
cmpl %r14d, %ebx
jl 0x78b42
testl %r14d, %r14d
leal (%r14,%r14), %eax
movl %eax, %r14d
movl $0x400, %eax            # imm = 0x400
cmovel %eax, %r14d
movslq %r14d, %rbp
shlq $0x2, %rbp
movq %rbp, %rsi
callq 0x6c80
movq %rax, 0x20(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6c80
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x78cdd
movq %rax, %r15
testq %rax, %rax
je 0x78cdd
movq 0x8(%rsp), %rax
movq 0x1000(%rax), %rax
movq 0x18(%rax), %rcx
movl %r12d, %edx
movl (%rcx,%rdx,4), %ecx
movslq %ebx, %rsi
movl %ecx, (%rdi,%rsi,4)
movq 0x20(%rax), %rcx
movl (%rcx,%rdx,4), %ecx
movl %ecx, (%r15,%rsi,4)
testl %ecx, %ecx
jle 0x78deb
incl %ebx
incl %r12d
addl $0x2, %r13d
jmp 0x78abe
testl %ebx, %ebx
jle 0x78e0a
movq %rdi, %r14
leal (%rbx,%rbx), %r12d
leaq (,%r12,4), %rsi
movq %r15, %rdi
callq 0x6c80
movq %rax, %r15
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x6470
movq $0x0, (%rsp)
testq %r15, %r15
je 0x78cdd
movq %rax, %r12
testq %rax, %rax
je 0x78cdd
movl %ebx, %ebx
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %edi, %edi
xorl %esi, %esi
cmpq %rax, %rcx
je 0x78c22
movl (%r15,%rax,4), %r9d
testl %r9d, %r9d
js 0x78bf2
cmpl %r9d, %edx
jle 0x78bfa
movl %eax, %r10d
jmp 0x78c12
movl %edx, %r9d
movl %r8d, %edx
jmp 0x78c0d
cmpl %r9d, %r8d
movl %r9d, %r10d
cmovll %r8d, %r10d
cmovgl %eax, %esi
movl %edx, %r9d
movl %r10d, %edx
movl %edi, %r10d
movl %esi, %edi
movl %edi, %esi
movl %edx, %r8d
incq %rax
movl %r9d, %edx
movl %r10d, %edi
jmp 0x78bda
cmpl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
je 0x78c50
addl %edx, %r8d
movl %r8d, (%r15,%rbx,4)
movslq %edi, %rax
movl %ebx, (%r12,%rax,4)
movslq %esi, %rcx
movl %ebx, (%r12,%rcx,4)
negl (%r15,%rax,4)
negl (%r15,%rcx,4)
incq %rbx
jmp 0x78bbf
shrl %ebx
xorl %eax, %eax
cmpq %rbx, %rax
ja 0x78c7d
leaq (%r12,%rax,4), %rcx
xorl %edx, %edx
movq %rcx, %rsi
movslq (%rsi), %rsi
testq %rsi, %rsi
je 0x78c72
incl %edx
leaq (%r12,%rsi,4), %rsi
jmp 0x78c62
movl %edx, (%rcx)
negl (%r15,%rax,4)
incq %rax
jmp 0x78c54
leaq 0x1(%rbx), %rbp
movq %rbp, %rdi
shlq $0x4, %rdi
callq 0x69d0
testq %rax, %rax
je 0x78cd5
movq %rax, %r13
leal 0x1(%rbx), %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
movq %r13, %rcx
movq %r14, %rsi
cmpq %rbx, %rax
ja 0x78cf0
movl (%rsi,%rax,4), %edx
movl %edx, (%rcx)
movl (%r12,%rax,4), %edx
movl %edx, 0xc(%rcx)
incq %rax
addq $0x10, %rcx
jmp 0x78ca4
callq 0x68a0
movq %r15, %rdi
callq 0x68a0
movq 0x18(%rsp), %rdi
callq 0x68a0
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x9f4(%rip), %rcx      # 0x78303
pushq $0x10
popq %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6e40
movl 0xc(%r13), %esi
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %rbp
cmpq %rbx, %rax
ja 0x78d4f
movq %rax, %rdx
shlq $0x4, %rdx
leaq (%rdx,%r13), %rdi
movl 0xc(%r13,%rdx), %edx
cmpl %esi, %edx
je 0x78d31
addl %ecx, %ecx
incl %esi
jmp 0x78d27
movl %ecx, 0x8(%rdi)
movl (%rdi), %esi
incl %esi
cmpl $0x80, %esi
ja 0x78d46
movl %esi, %esi
movl %eax, 0x3c(%rbp,%rsi,4)
incl %ecx
incq %rax
movl %edx, %esi
jmp 0x78d12
movq %r12, %rdi
callq 0x68a0
movq %r14, %rdi
callq 0x68a0
movq %r15, %rdi
callq 0x68a0
movq %r13, 0x30(%rbp)
movl 0x8(%rsp), %eax
movl %eax, 0x38(%rbp)
movl 0x14(%rsp), %esi
addl $-0x3, %esi
cmpl $0x0, 0xc(%r13)
leaq -0xa75(%rip), %rax      # 0x78310
leaq -0xa79(%rip), %rcx      # 0x78313
cmoveq %rax, %rcx
leaq -0x9cd(%rip), %rax      # 0x783ca
leaq -0x9d1(%rip), %rdx      # 0x783cd
cmoveq %rax, %rdx
leaq -0x91c(%rip), %rax      # 0x7848d
movq %rax, 0x10(%rbp)
cmpl $0x2, %esi
cmovbq %rcx, %rdx
movq %rdx, 0x20(%rbp)
leaq -0x916(%rip), %rax      # 0x784a9
movq %rax, 0x28(%rbp)
movq %rbp, (%rsp)
jmp 0x78cdd
leaq 0x11bd4(%rip), %rdi     # 0x8a9a7
leaq 0x11a55(%rip), %rsi     # 0x8a82f
leaq 0x11bd7(%rip), %rcx     # 0x8a9b8
movl $0x4c5, %edx            # imm = 0x4C5
callq 0x6460
leaq 0x11c1f(%rip), %rdi     # 0x8aa11
leaq 0x11a36(%rip), %rsi     # 0x8a82f
leaq 0x11bb8(%rip), %rcx     # 0x8a9b8
movl $0x4da, %edx            # imm = 0x4DA
callq 0x6460
leaq 0x11c11(%rip), %rdi     # 0x8aa22
leaq 0x11a17(%rip), %rsi     # 0x8a82f
leaq 0x11b99(%rip), %rcx     # 0x8a9b8
movl $0x4e2, %edx            # imm = 0x4E2
callq 0x6460