// File: /workspace/github_cpp_makefile_star2_O3/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 0x49d70
testq %rcx, %rcx
je 0x49d4a
leaq (%rax,%rax), %rcx
movq %rcx, 0x28(%rbx)
movq 0x20(%rbx), %rdi
leaq 0x4(,%rax,2), %rsi
callq 0x48d0
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x49d70
jmp 0x49e4e
movq $0x400, 0x28(%rbx)      # imm = 0x400
movq 0x20(%rbx), %rdi
movl $0x404, %esi            # imm = 0x404
callq 0x48d0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x49e4e
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 0x49e17
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), %rsi
movq 0x30(%rbx), %rdi
leaq 0x1(%rdi), %r8
decb %cl
movl $0x1, %edx
shll %cl, %edx
movq %r8, 0x30(%rbx)
movb $0x0, 0x1(%rsi,%rdi)
subl %r14d, %eax
incl %eax
movl 0x38(%rbx), %ecx
testl %ebp, %edx
je 0x49de9
movl $0x1, %esi
shll %cl, %esi
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdi
orb %sil, (%rcx,%rdi)
movl 0x38(%rbx), %ecx
subl $0x1, %ecx
movl %ecx, 0x38(%rbx)
jae 0x49e0d
movl $0x7, 0x38(%rbx)
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, 0x30(%rbx)
movb $0x0, 0x1(%rcx,%rsi)
shrl %edx
incl %eax
jne 0x49dcc
xorl %eax, %eax
jmp 0x49e53
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 0x49e53
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 0x49e53
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r14
popq %rbp
retq