// File: /workspace/github_c_makefile_star2_O2/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 0x70274
testq %rcx, %rcx
je 0x7024e
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 0x70274
jmp 0x7034e
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 0x7034e
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 0x70317
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), %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 %ebp, %eax
incl %eax
movl 0x38(%rbx), %ecx
testl %r14d, %esi
je 0x702e9
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 0x7030d
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 0x702ce
xorl %eax, %eax
jmp 0x70351
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 0x70351
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 0x70351
pushq $-0x1
popq %rax
popq %rbx
popq %r14
popq %rbp
retq