// File: /workspace/github_c_makefile_star2_O1/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 0x9aef(%rip), %r12      # 0xb320
xorl %eax, %eax
cmpl $0x0, 0x10(%r12)
setne %al
leal 0x8(,%rax,8), %r15d
testl %esi, %esi
je 0x1867
movl %esi, %ebx
cmpl $-0x1, %esi
jne 0x18a0
xorl %eax, %eax
movl %eax, 0x9ac1(%rip)      # 0xb318
movl %eax, 0x9abf(%rip)      # 0xb31c
movl $0x1, %eax
jmp 0x199d
cmpl $0x0, 0x9aae(%rip)      # 0xb31c
je 0x1987
movl 0x9aa2(%rip), %ecx      # 0xb31c
movl %r15d, %eax
subl %ecx, %eax
jbe 0x196c
movl 0x9a8d(%rip), %ecx      # 0xb318
orl $0x1, %r15d
addl %ecx, %ecx
decl %eax
jne 0x188f
movl %ecx, 0x9a7d(%rip)      # 0xb318
jmp 0x1971
testl %ebx, %ebx
jle 0x195e
movl %edi, %ebp
movl $0x1, %eax
cmpl $0x0, 0x18(%r12)
jne 0x18f2
cmpl %r15d, 0x9a5e(%rip)     # 0xb31c
jne 0x18f2
xorl %r14d, %r14d
testl %eax, %eax
je 0x18e5
movl 0x9a4b(%rip), %edi      # 0xb318
callq 0x2691
testl %eax, %eax
je 0x18e5
xorl %edi, %edi
movl $0x1, %esi
callq 0x1756
movl %eax, %r14d
movl $0x0, 0x9a2d(%rip)      # 0xb31c
movl %r14d, %eax
movl 0x9a20(%rip), %edi      # 0xb318
shldl $0x1, %ebp, %edi
movl %edi, 0x9a16(%rip)      # 0xb318
movl 0x9a14(%rip), %ecx      # 0xb31c
incl %ecx
movl %ecx, 0x9a0c(%rip)      # 0xb31c
cmpl $0x0, 0x18(%r12)
setne %dl
cmpl %r15d, %ecx
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x1952
xorl %r14d, %r14d
testl %eax, %eax
je 0x1945
callq 0x2691
testl %eax, %eax
je 0x1945
xorl %edi, %edi
movl $0x1, %esi
callq 0x1756
movl %eax, %r14d
movl $0x0, 0x99cd(%rip)      # 0xb31c
movl %r14d, %eax
addl %ebp, %ebp
decl %ebx
jne 0x18af
jmp 0x199d
leaq 0x48e9(%rip), %rdi      # 0x624e
callq 0x1120
jmp 0x199b
incl %ecx
movl %ecx, %r15d
movl %r15d, 0x99a4(%rip)     # 0xb31c
movl 0x999a(%rip), %edi      # 0xb318
callq 0x2691
testl %eax, %eax
je 0x199b
xorl %edi, %edi
movl $0x1, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1756
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq