// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/cram/cram_codecs.c
// Function: store_bits_MSB
static int store_bits_MSB(cram_block *block, uint64_t 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+8 >= block->alloc) {
        if (block->byte) {
            block->alloc *= 2;
            block->data = realloc(block->data, block->alloc + 8);
            if (!block->data)
                return -1;
        } else {
            block->alloc = 1024;
            block->data = realloc(block->data, block->alloc + 8);
            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, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x28(%rdi), %rax
movq 0x30(%rdi), %rcx
leaq 0x8(%rcx), %rdx
cmpq %rax, %rdx
jb 0x90085
testq %rcx, %rcx
je 0x9005f
leaq (%rax,%rax), %rcx
movq %rcx, 0x28(%rbx)
movq 0x20(%rbx), %rdi
leaq 0x8(,%rax,2), %rsi
callq 0x4840
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x90085
jmp 0x9015c
movq $0x400, 0x28(%rbx)      # imm = 0x400
movq 0x20(%rbx), %rdi
movl $0x408, %esi            # imm = 0x408
callq 0x4840
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x9015c
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 %ebp, %ecx
jge 0x90125
movl %ebp, %ecx
subl %r8d, %ecx
movq %r14, %r8
shrq %cl, %r8
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 %ebp, %eax
incl %eax
testl %r14d, %edx
je 0x900f9
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 0x9011b
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 0x900de
xorl %eax, %eax
jmp 0x90161
shlq %cl, %r14
orb %r14b, %dil
movb %dil, (%rdx,%rsi)
movl 0x38(%rbx), %ecx
subl %ebp, %ecx
movl %ecx, 0x38(%rbx)
xorl %eax, %eax
cmpl $-0x1, %ecx
jne 0x90161
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 0x90161
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r14
popq %rbp
retq