// File: karel-brinda[P]ococo/ext/htslib/cram/cram_stats.c
// Function: cram_stats_encoding
enum cram_encoding cram_stats_encoding(cram_fd *fd, cram_stats *st) {
    int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX;
    int *vals = NULL, *freqs = NULL, vals_alloc = 0;

    //cram_stats_dump(st);

    /* 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);
		return E_HUFFMAN; // Cannot do much else atm
	    }
	}
	vals[nvals] = i;
	freqs[nvals] = st->freqs[i];
	ntot += freqs[nvals];
	if (max_val < i) max_val = i;
	if (min_val > i) min_val = i;
	nvals++;
    }
    if (st->h) {
	khint_t k;
	int i;

	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 E_HUFFMAN; // Cannot do much else atm
	    }
	    i = kh_key(st->h, k);
	    vals[nvals]=i;
	    freqs[nvals] = kh_val(st->h, k);
	    ntot += freqs[nvals];
	    if (max_val < i) max_val = i;
	    if (min_val > i) min_val = i;
	    nvals++;
	}
    }

    st->nvals = nvals;
    assert(ntot == st->nsamp);

    free(vals);
    free(freqs);

    /*
     * Simple policy that everything is external unless it can be
     * encoded using zero bits as a unary item huffman table.
     */
    return nvals <= 1 ? E_HUFFMAN : E_EXTERNAL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x7fffffff, -0x2c(%rbp) # imm = 0x7FFFFFFF
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x400, -0x20(%rbp)     # imm = 0x400
jge 0x55624
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x5552a
jmp 0x55616
movl -0x1c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0x555bc
cmpl $0x0, -0x44(%rbp)
je 0x55546
movl -0x44(%rbp), %eax
shll %eax
movl %eax, -0x50(%rbp)
jmp 0x55550
movl $0x400, %eax            # imm = 0x400
movl %eax, -0x50(%rbp)
jmp 0x55550
movl -0x50(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x44(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4990
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x44(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4990
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x5558e
cmpq $0x0, -0x40(%rbp)
jne 0x555ba
cmpq $0x0, -0x38(%rbp)
je 0x5559e
movq -0x38(%rbp), %rdi
callq 0x46a0
cmpq $0x0, -0x40(%rbp)
je 0x555ae
movq -0x40(%rbp), %rdi
callq 0x46a0
movl $0x3, -0x4(%rbp)
jmp 0x557e3
jmp 0x555bc
movl -0x20(%rbp), %edx
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x555ff
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x5560d
movl -0x20(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x5550a
movq -0x18(%rbp), %rax
cmpq $0x0, 0x1000(%rax)
je 0x5577e
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x1000(%rcx), %rcx
cmpl (%rcx), %eax
je 0x5577c
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x48(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x55684
jmp 0x5576e
movl -0x1c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0x556f2
cmpl $0x0, -0x44(%rbp)
je 0x5569c
movl -0x44(%rbp), %eax
shll %eax
movl %eax, -0x54(%rbp)
jmp 0x556a6
movl $0x400, %eax            # imm = 0x400
movl %eax, -0x54(%rbp)
jmp 0x556a6
movl -0x54(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x44(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4990
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x44(%rbp), %rsi
shlq $0x2, %rsi
callq 0x4990
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x556e4
cmpq $0x0, -0x40(%rbp)
jne 0x556f0
movl $0x3, -0x4(%rbp)
jmp 0x557e3
jmp 0x556f2
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x18(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %edx
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x55757
movl -0x4c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jle 0x55765
movl -0x4c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x5563d
jmp 0x5577e
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x100c(%rax)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1008(%rcx), %eax
jne 0x5579c
jmp 0x557bb
leaq 0x27ea4(%rip), %rdi     # 0x7d647
leaq 0x27e2a(%rip), %rsi     # 0x7d5d4
movl $0xb4, %edx
leaq 0x27ea3(%rip), %rcx     # 0x7d659
callq 0x4360
movq -0x38(%rbp), %rdi
callq 0x46a0
movq -0x40(%rbp), %rdi
callq 0x46a0
movl -0x1c(%rbp), %edx
movl $0x1, %eax
movl $0x3, %ecx
cmpl $0x1, %edx
cmovlel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)