// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r14
pushq %rbx
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
movq 0x28(%rdi), %rax
movq 0x30(%rdi), %rcx
leaq 0x4(%rcx), %rdx
cmpq %rax, %rdx
jb 0x3cb0b
testq %rcx, %rcx
je 0x3cae5
leaq (%rax,%rax), %rcx
movq %rcx, 0x28(%rbx)
movq 0x20(%rbx), %rdi
leaq 0x4(,%rax,2), %rsi
callq 0x48c0
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x3cb0b
jmp 0x3cbe7
movq $0x400, 0x28(%rbx)      # imm = 0x400
movq 0x20(%rbx), %rdi
movl $0x404, %esi            # imm = 0x404
callq 0x48c0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x3cbe7
movb $0x0, (%rax)
movl 0x38(%rbx), %eax
leal 0x1(%rax), %r8d
movl %r8d, %ecx
movq 0x20(%rbx), %rdx
movq 0x30(%rbx), %rsi
movb (%rdx,%rsi), %dil
subl %r14d, %ecx
jge 0x3cbb0
movl %r14d, %ecx
subl %r8d, %ecx
movl %ebp, %r8d
shrl %cl, %r8d
orb %r8b, %dil
movb %dil, (%rdx,%rsi)
movl $0x7, 0x38(%rbx)
movq 0x20(%rbx), %rdi
movq 0x30(%rbx), %r8
leaq 0x1(%r8), %r9
decb %cl
pushq $0x1
popq %rdx
movl %edx, %esi
shll %cl, %esi
movq %r9, 0x30(%rbx)
movb $0x0, 0x1(%rdi,%r8)
subl %r14d, %eax
incl %eax
movl 0x38(%rbx), %ecx
testl %ebp, %esi
je 0x3cb82
movl %edx, %edi
shll %cl, %edi
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %r8
orb %dil, (%rcx,%r8)
movl 0x38(%rbx), %ecx
subl $0x1, %ecx
movl %ecx, 0x38(%rbx)
jae 0x3cba6
movl $0x7, 0x38(%rbx)
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, 0x30(%rbx)
movb $0x0, 0x1(%rcx,%rdi)
shrl %esi
incl %eax
jne 0x3cb68
xorl %eax, %eax
jmp 0x3cbea
shll %cl, %ebp
orb %bpl, %dil
movb %dil, (%rdx,%rsi)
movl 0x38(%rbx), %ecx
subl %r14d, %ecx
movl %ecx, 0x38(%rbx)
xorl %eax, %eax
cmpl $-0x1, %ecx
jne 0x3cbea
movl $0x7, 0x38(%rbx)
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x30(%rbx)
movb $0x0, 0x1(%rcx,%rdx)
jmp 0x3cbea
pushq $-0x1
popq %rax
popq %rbx
popq %r14
popq %rbp
retq