// File: karel-brinda[P]ococo/ext/htslib/cram/cram_codecs.c
// Function: cram_beta_encode_init
cram_codec *cram_beta_encode_init(cram_stats *st,
				  enum cram_external_type option,
				  void *dat,
				  int version) {
    cram_codec *c;
    int min_val, max_val, len = 0;

    c = malloc(sizeof(*c));
    if (!c)
	return NULL;
    c->codec  = E_BETA;
    c->free   = cram_beta_encode_free;
    if (option == E_INT)
	c->encode = cram_beta_encode_int;
    else
	c->encode = cram_beta_encode_char;
    c->store  = cram_beta_encode_store;

    if (dat) {
	min_val = ((int *)dat)[0];
	max_val = ((int *)dat)[1];
    } else {
	min_val = INT_MAX;
	max_val = INT_MIN;
	int i;
	for (i = 0; i < MAX_STAT_VAL; i++) {
	    if (!st->freqs[i])
		continue;
	    if (min_val > i)
		min_val = i;
	    max_val = i;
	}
	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;
	    
		i = kh_key(st->h, k);
		if (min_val > i)
		    min_val = i;
		if (max_val < i)
		    max_val = i;
	    }
	}
    }

    assert(max_val >= min_val);
    c->e_beta.offset = -min_val;
    max_val -= min_val;
    while (max_val) {
	len++;
	max_val >>= 1;
    }
    c->e_beta.nbits = len;

    return c;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x240, %edi            # imm = 0x240
callq 0x47a0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x6ff9f
movq $0x0, -0x8(%rbp)
jmp 0x70165
movq -0x30(%rbp), %rax
movl $0x6, (%rax)
movq -0x30(%rbp), %rax
leaq -0x84(%rip), %rcx       # 0x6ff30
movq %rcx, 0x10(%rax)
cmpl $0x1, -0x14(%rbp)
jne 0x6ffcf
movq -0x30(%rbp), %rax
leaq -0x409(%rip), %rcx      # 0x6fbc0
movq %rcx, 0x20(%rax)
jmp 0x6ffde
movq -0x30(%rbp), %rax
leaq -0x12a(%rip), %rcx      # 0x6feb0
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
leaq -0x6e9(%rip), %rcx      # 0x6f900
movq %rcx, 0x28(%rax)
cmpq $0x0, -0x20(%rbp)
je 0x7000c
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
jmp 0x700fa
movl $0x7fffffff, -0x34(%rbp) # imm = 0x7FFFFFFF
movl $0x80000000, -0x38(%rbp) # imm = 0x80000000
movl $0x0, -0x40(%rbp)
cmpl $0x400, -0x40(%rbp)     # imm = 0x400
jge 0x70059
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x7003a
jmp 0x7004e
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x70048
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x70021
movq -0x10(%rbp), %rax
cmpq $0x0, 0x1000(%rax)
je 0x700f8
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x1000(%rcx), %rcx
cmpl (%rcx), %eax
je 0x700f6
movq -0x10(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x44(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x700b2
jmp 0x700e8
movq -0x10(%rbp), %rax
movq 0x1000(%rax), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x700d8
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x700e6
movl -0x40(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x700e8
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x70072
jmp 0x700f8
jmp 0x700fa
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x70104
jmp 0x70123
leaq 0xe96d(%rip), %rdi      # 0x7ea78
leaq 0xe979(%rip), %rsi      # 0x7ea8b
movl $0x288, %edx            # imm = 0x288
leaq 0xe980(%rip), %rcx      # 0x7ea9e
callq 0x4360
xorl %ecx, %ecx
subl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x30(%rax)
movl -0x34(%rbp), %ecx
movl -0x38(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x70153
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
sarl %eax
movl %eax, -0x38(%rbp)
jmp 0x7013a
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop