// File: /workspace/github_c_makefile_star2_O3/salextpuru[P]speccy-toochain/src/mhmt/mhmt-emit.c
// Function: emit_bits
ULONG emit_bits(ULONG msb_aligned_bits, LONG length)
{
    static ULONG bit_store;
    static ULONG bit_count;
    ULONG max_bits;
    ULONG success = 1;
    max_bits = wrk.wordbit ? 16 : 8;

    if( length==EMIT_BITS_INIT ) {
        bit_store = 0;
        bit_count = 0;
        return 1;

    } else if( length==EMIT_BITS_FINISH ) {
        if( bit_count ) { // some bits to flush
            while( (bit_count++)<max_bits )
                bit_store <<= 1;

            success = success && emit_bits_flush(bit_store);
        }

        success = success && emit_byte(0, EMIT_BYTE_FLUSH);
        return success;

    } else if( length>0 ) { // add bits
        do {
            if( !wrk.fullbits ) { // empty bits - check for flushing before shiftin
                if( bit_count==max_bits ) {
                    success = success && emit_bits_flush(bit_store);
                    success = success && emit_byte(0, EMIT_BYTE_FLUSH);
                    bit_count = 0;
                }
            }

#ifdef DBG
            printf("%d",1&(msb_aligned_bits>>31));
#endif
            bit_store = (bit_store<<1) | ( 1 & (msb_aligned_bits>>31) );
            msb_aligned_bits <<= 1;
            bit_count++;

            if( wrk.fullbits ) { // full bits - check for flushing after bit shiftin
                if( bit_count==max_bits ) {
                    success = success && emit_bits_flush(bit_store);
                    success = success && emit_byte(0, EMIT_BYTE_FLUSH);
                    bit_count = 0;
                }
            }
        } while( --length );

        return success;

    } else {
        printf("mhmt-emit.c:emit_bits() encountered invalid arguments!\n");
        return 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq 0x9b06(%rip), %r15      # 0xb320
xorl %eax, %eax
cmpl $0x0, 0x10(%r15)
setne %al
leal 0x8(,%rax,8), %r14d
testl %esi, %esi
je 0x184f
movl %esi, %ebx
cmpl $-0x1, %esi
jne 0x1883
xorl %eax, %eax
movl %eax, 0x9ad9(%rip)      # 0xb318
movl %eax, 0x9ad7(%rip)      # 0xb31c
movl $0x1, %eax
jmp 0x1972
movl 0x9ac7(%rip), %ecx      # 0xb31c
testl %ecx, %ecx
je 0x195c
movl 0x9ab5(%rip), %edi      # 0xb318
movl %r14d, %eax
subl %ecx, %eax
jbe 0x1947
orl $0x1, %r14d
addl %edi, %edi
decl %eax
jne 0x1872
movl %edi, 0x9a9a(%rip)      # 0xb318
jmp 0x194c
testl %ebx, %ebx
jle 0x1939
movl %edi, %ebp
movl $0x1, %eax
movl 0x9a83(%rip), %r12d     # 0xb31c
movl 0x18(%r15), %ecx
testl %ecx, %ecx
jne 0x18d8
cmpl %r14d, %r12d
jne 0x18d8
xorl %r12d, %r12d
testl %eax, %eax
movl $0x0, %eax
je 0x18d4
movl 0x9a60(%rip), %edi      # 0xb318
callq 0x26e1
movl %eax, %ecx
movl $0x0, %eax
testl %ecx, %ecx
je 0x18d4
xorl %edi, %edi
movl $0x1, %esi
callq 0x173f
movl 0x18(%r15), %ecx
movl 0x9a3a(%rip), %edi      # 0xb318
shldl $0x1, %ebp, %edi
movl %edi, 0x9a30(%rip)      # 0xb318
incl %r12d
movl %r12d, 0x9a2a(%rip)     # 0xb31c
testl %ecx, %ecx
je 0x192d
cmpl %r14d, %r12d
jne 0x192d
xorl %r12d, %r12d
testl %eax, %eax
movl $0x0, %eax
je 0x1923
callq 0x26e1
movl %eax, %ecx
movl $0x0, %eax
testl %ecx, %ecx
je 0x1923
xorl %edi, %edi
movl $0x1, %esi
callq 0x173f
movl $0x0, 0x99ef(%rip)      # 0xb31c
addl %ebp, %ebp
decl %ebx
jne 0x1899
jmp 0x1972
leaq 0x490e(%rip), %rdi      # 0x624e
callq 0x1120
jmp 0x1970
incl %ecx
movl %ecx, %r14d
movl %r14d, 0x99c9(%rip)     # 0xb31c
callq 0x26e1
testl %eax, %eax
je 0x1970
xorl %edi, %edi
movl $0x1, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x173f
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq