// File: karel-brinda[P]ococo/ext/htslib/cram/cram_codecs.c
// Function: cram_subexp_decode
int cram_subexp_decode(cram_slice *slice, cram_codec *c, cram_block *in, char *out, int *out_size) {
    int32_t *out_i = (int32_t *)out;
    int n, count;
    int k = c->subexp.k;

    for (count = 0, n = *out_size; count < n; count++) {
	int i = 0, tail;
	int val;

	/* Get number of 1s */
	//while (get_bit_MSB(in) == 1) i++;
	i = get_one_bits_MSB(in);
        if (i < 0 || cram_not_enough_bits(in, i > 0 ? i + k - 1 : k))
            return -1;
	/*
	 * Val is
	 * i > 0:  2^(k+i-1) + k+i-1 bits
	 * i = 0:  k bits
	 */
	if (i) {
	    tail = i + k-1;
	    val = 0;
	    while (tail) {
		//val = val<<1; val |= get_bit_MSB(in);
		GET_BIT_MSB(in, val);
		tail--;
	    }
	    val += 1 << (i + k-1);
	} else {
	    tail = k;
	    val = 0;
	    while (tail) {
		//val = val<<1; val |= get_bit_MSB(in);
		GET_BIT_MSB(in, val);
		tail--;
	    }
	}

	out_i[count] = val - c->subexp.offset;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x70362
movl $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rdi
callq 0x70380
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x70206
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x48(%rbp)
jle 0x701ef
movl -0x48(%rbp), %eax
addl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x701f5
movl -0x44(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x6eec0
cmpl $0x0, %eax
je 0x70212
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x70369
cmpl $0x0, -0x48(%rbp)
je 0x702bd
movl -0x48(%rbp), %eax
addl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x702a2
movl -0x50(%rbp), %eax
shll %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl (%rax,%rcx), %eax
movq -0x20(%rbp), %rcx
movl 0x38(%rcx), %ecx
sarl %cl, %eax
andl $0x1, %eax
orl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rcx
movl 0x38(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rcx)
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
movslq %eax, %rcx
movq -0x20(%rbp), %rax
addq 0x30(%rax), %rcx
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
movl 0x38(%rax), %ecx
andl $0x7, %ecx
movl %ecx, 0x38(%rax)
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x7022f
movl -0x48(%rbp), %ecx
addl -0x44(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x7033f
movl -0x44(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x7033d
movl -0x50(%rbp), %eax
shll %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl (%rax,%rcx), %eax
movq -0x20(%rbp), %rcx
movl 0x38(%rcx), %ecx
sarl %cl, %eax
andl $0x1, %eax
orl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rcx
movl 0x38(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rcx)
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
movslq %eax, %rcx
movq -0x20(%rbp), %rax
addq 0x30(%rax), %rcx
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
movl 0x38(%rax), %ecx
andl $0x7, %ecx
movl %ecx, 0x38(%rax)
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x702ca
jmp 0x7033f
movl -0x50(%rbp), %edx
movq -0x18(%rbp), %rax
subl 0x30(%rax), %edx
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x701ae
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)