// File: /workspace/github_cpp_makefile_star2_O1/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 0x4ae32
testq %rcx, %rcx
je 0x4ae0c
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 0x4ae32
jmp 0x4af0b
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 0x4af0b
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 0x4aed4
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
testl %ebp, %edx
je 0x4aea8
movb 0x38(%rbx), %cl
movl $0x1, %esi
shll %cl, %esi
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdi
orb %sil, (%rcx,%rdi)
subl $0x1, 0x38(%rbx)
jae 0x4aeca
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 0x4ae8e
xorl %eax, %eax
jmp 0x4af10
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 0x4af10
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 0x4af10
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r14
popq %rbp
retq