// File: karel-brinda[P]ococo/ext/htslib/cram/cram_codecs.c
// Function: store_bits_MSB
static int store_bits_MSB(cram_block *block, unsigned int val, int nbits) {
    /* fprintf(stderr, " store_bits: %02x %d\n", val, nbits); */

    /*
     * Use slow mode until we tweak the huffman generator to never generate
     * codes longer than 24-bits.
     */
    unsigned int mask;

    if (block->byte+4 >= block->alloc) {
	if (block->byte) {
	    block->alloc *= 2;
	    block->data = realloc(block->data, block->alloc + 4);
	    if (!block->data)
		return -1;
	} else {
	    block->alloc = 1024;
	    block->data = realloc(block->data, block->alloc + 4);
	    if (!block->data)
		return -1;
	    block->data[0] = 0; // initialise first byte of buffer
	}
    }

    /* fits in current bit-field */
    if (nbits <= block->bit+1) {
	block->data[block->byte] |= (val << (block->bit+1-nbits));
	if ((block->bit-=nbits) == -1) {
	    block->bit = 7;
	    block->byte++;
	    block->data[block->byte] = 0;
	}
	return 0;
    }

    block->data[block->byte] |= (val >> (nbits -= block->bit+1));
    block->bit = 7;
    block->byte++;
    block->data[block->byte] = 0;
				 
    mask = 1<<(nbits-1);
    do {
	if (val & mask)
	    block->data[block->byte] |= (1 << block->bit);
	if (--block->bit == -1) {
	    block->bit = 7;
	    block->byte++;
	    block->data[block->byte] = 0;
	}
	mask >>= 1;
    } while(--nbits);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
addq $0x4, %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jb 0x6fd17
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x6fcc3
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
shlq %rcx
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
addq $0x4, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x6fcc1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6fea5
jmp 0x6fd15
movq -0x10(%rbp), %rax
movq $0x400, 0x28(%rax)      # imm = 0x400
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
addq $0x4, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x6fd0a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6fea5
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movb $0x0, (%rax)
jmp 0x6fd17
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x6fda2
movl -0x14(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
subl -0x18(%rbp), %ecx
shll %cl, %esi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %eax
subl %edx, %eax
movl %eax, 0x38(%rcx)
cmpl $-0x1, %eax
jne 0x6fd96
movq -0x10(%rbp), %rax
movl $0x7, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x4(%rbp)
jmp 0x6fea5
movl -0x14(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
addl $0x1, %eax
movl -0x18(%rbp), %ecx
subl %eax, %ecx
movl %ecx, -0x18(%rbp)
shrl %cl, %esi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl $0x7, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movl -0x18(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %eax
andl -0x1c(%rbp), %eax
cmpl $0x0, %eax
je 0x6fe43
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rcx)
cmpl $-0x1, %eax
jne 0x6fe84
movq -0x10(%rbp), %rax
movl $0x7, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movl -0x1c(%rbp), %eax
shrl %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0x6fe11
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nop