// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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, %ebp
movq %rdi, %r13
movl $0x240, %edi            # imm = 0x240
callq 0x4700
xorl %ebx, %ebx
testq %rax, %rax
je 0x4b967
movl %ebp, 0x1c(%rsp)
movq %rax, 0x20(%rsp)
movl $0x3, (%rax)
xorl %r12d, %r12d
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r13, 0x10(%rsp)
cmpl $0x0, (%r13,%r12,4)
je 0x4b642
cmpl %r14d, %ebx
jl 0x4b628
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 0x48d0
movq %rax, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x48d0
movq %rbp, %rdi
movq %rax, %r15
testq %rbp, %rbp
je 0x4b943
testq %r15, %r15
je 0x4b943
movq 0x10(%rsp), %r13
movslq %ebx, %rax
movl %r12d, (%rdi,%rax,4)
movl (%r13,%r12,4), %ecx
movl %ecx, (%r15,%rax,4)
testl %ecx, %ecx
jle 0x4b978
incl %ebx
incq %r12
cmpq $0x400, %r12            # imm = 0x400
jne 0x4b5ce
movq 0x1000(%r13), %rax
testq %rax, %rax
je 0x4b726
movl (%rax), %edx
testl %edx, %edx
je 0x4b726
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rax), %rcx
movl %r12d, %esi
shrl $0x4, %esi
movl (%rcx,%rsi,4), %esi
movl %r13d, %ecx
andb $0x1e, %cl
shrl %cl, %esi
testb $0x3, %sil
jne 0x4b716
cmpl %r14d, %ebx
jl 0x4b6ef
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 0x48d0
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x48d0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4b967
movq %rax, %r15
testq %rax, %rax
movl $0x0, %eax
je 0x4b969
movq 0x10(%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 0x4b997
incl %ebx
movl (%rax), %edx
incl %r12d
addl $0x2, %r13d
cmpl %edx, %r12d
jne 0x4b66e
movq %rdi, 0x8(%rsp)
testl %ebx, %ebx
jle 0x4b9b6
leal (%rbx,%rbx), %r12d
leaq (,%r12,4), %rsi
movq %r15, %rdi
callq 0x48d0
movq %rax, %r15
movl $0x4, %esi
movq %r12, %rdi
callq 0x4360
movq %rax, %r12
xorl %eax, %eax
testq %r15, %r15
je 0x4b969
testq %r12, %r12
je 0x4b969
movl %ebx, %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %ecx, %ecx
xorl %edi, %edi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, %r9d
movl %edi, %r8d
movl (%r15,%rdx,4), %esi
testl %esi, %esi
js 0x4b7a6
cmpl %esi, %r9d
jle 0x4b79e
movl %r9d, %eax
movl %edx, %edi
movl %r8d, %ecx
jmp 0x4b7ac
cmpl %esi, %eax
cmovgel %esi, %eax
cmovgl %edx, %ecx
movl %r9d, %esi
movl %r8d, %edi
incq %rdx
cmpq %r14, %rdx
jb 0x4b781
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x4b7dc
addl %esi, %eax
movl %eax, (%r15,%r14,4)
movslq %edi, %rax
movl %r14d, (%r12,%rax,4)
movslq %ecx, %rcx
movl %r14d, (%r12,%rcx,4)
negl (%r15,%rax,4)
negl (%r15,%rcx,4)
incq %r14
jmp 0x4b771
movl %r14d, %ebp
shrl %ebp
shrl %r14d
leal 0x1(%r14), %ebx
xorl %eax, %eax
movl (%r12,%rax,4), %edx
movl $0x0, %ecx
testl %edx, %edx
je 0x4b806
xorl %ecx, %ecx
incl %ecx
movslq %edx, %rdx
movl (%r12,%rdx,4), %edx
testl %edx, %edx
jne 0x4b7f9
negl (%r15,%rax,4)
movl %ecx, (%r12,%rax,4)
incq %rax
cmpq %rbx, %rax
jne 0x4b7ea
movl %ebp, %edi
incq %rdi
movq %rdi, 0x10(%rsp)
shlq $0x4, %rdi
callq 0x4700
testq %rax, %rax
je 0x4b967
movq %rax, %r13
incl %ebp
movl %ebp, 0x18(%rsp)
leaq 0x4(,%r14,4), %rax
xorl %ecx, %ecx
movq 0x8(%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 0x4b84a
leaq -0x98c(%rip), %rcx      # 0x4aedf
movl $0x10, %edx
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0x4a10
movl 0xc(%r13), %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x20(%rsp), %r14
movl 0x1c(%rsp), %ebp
movq %rax, %rsi
shlq $0x4, %rsi
movl 0xc(%r13,%rsi), %edi
cmpl %edi, %ecx
je 0x4b8a8
addl %edx, %edx
incl %ecx
cmpl %ecx, %edi
jne 0x4b89e
movl %edi, %ecx
addq %r13, %rsi
movl %edx, 0x8(%rsi)
movl (%rsi), %esi
incl %esi
cmpl $0x80, %esi
ja 0x4b8c1
movl %esi, %esi
movl %eax, 0x3c(%r14,%rsi,4)
incl %edx
incq %rax
cmpq %rbx, %rax
jne 0x4b88e
movq %r12, %rdi
callq 0x4610
movq 0x8(%rsp), %rdi
callq 0x4610
movq %r15, %rdi
callq 0x4610
movq %r13, 0x30(%r14)
movl 0x18(%rsp), %eax
movl %eax, 0x38(%r14)
addl $-0x3, %ebp
cmpl $0x0, 0xc(%r13)
leaq -0xa14(%rip), %rax      # 0x4aeec
leaq -0xa18(%rip), %rcx      # 0x4aeef
cmoveq %rax, %rcx
leaq -0x96d(%rip), %rax      # 0x4afa5
leaq -0x971(%rip), %rdx      # 0x4afa8
cmoveq %rax, %rdx
leaq -0x8b9(%rip), %rax      # 0x4b06b
movq %rax, 0x10(%r14)
cmpl $0x2, %ebp
cmovbq %rcx, %rdx
movq %rdx, 0x20(%r14)
leaq -0x8ae(%rip), %rax      # 0x4b08c
movq %rax, 0x28(%r14)
movq %r14, %rax
jmp 0x4b969
testq %rdi, %rdi
je 0x4b94d
callq 0x4610
testq %r15, %r15
movq 0x20(%rsp), %rbx
je 0x4b95f
movq %r15, %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4610
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x905c(%rip), %rdi      # 0x549db
leaq 0x8edd(%rip), %rsi      # 0x54863
leaq 0x905f(%rip), %rcx      # 0x549ec
movl $0x4bd, %edx            # imm = 0x4BD
callq 0x4350
leaq 0x90a7(%rip), %rdi      # 0x54a45
leaq 0x8ebe(%rip), %rsi      # 0x54863
leaq 0x9040(%rip), %rcx      # 0x549ec
movl $0x4d2, %edx            # imm = 0x4D2
callq 0x4350
leaq 0x9099(%rip), %rdi      # 0x54a56
leaq 0x8e9f(%rip), %rsi      # 0x54863
leaq 0x9021(%rip), %rcx      # 0x549ec
movl $0x4da, %edx            # imm = 0x4DA
callq 0x4350