// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x8bdb(%rip), %r12      # 0xa320
xorl %eax, %eax
cmpl $0x0, 0x10(%r12)
setne %al
leal 0x8(,%rax,8), %r15d
testl %esi, %esi
je 0x1779
movl %esi, %ebx
cmpl $-0x1, %esi
jne 0x17ab
andl $0x0, 0x8bae(%rip)      # 0xa318
andl $0x0, 0x8bab(%rip)      # 0xa31c
pushq $0x1
popq %rax
jmp 0x1895
movl 0x8b9d(%rip), %eax      # 0xa31c
testl %eax, %eax
je 0x186f
movl 0x8b8b(%rip), %edi      # 0xa318
cmpl %eax, %r15d
movl %eax, %ecx
cmoval %r15d, %ecx
cmpl %r15d, %eax
jae 0x185e
incl %eax
addl %edi, %edi
movl %edi, 0x8b6f(%rip)      # 0xa318
jmp 0x1796
testl %ebx, %ebx
jle 0x1887
movl %edi, %ebp
pushq $0x1
popq %r14
movl 0x8b5c(%rip), %r13d     # 0xa31c
movl %r14d, %eax
movl 0x18(%r12), %ecx
testl %ecx, %ecx
jne 0x1802
cmpl %r15d, %r13d
jne 0x1802
xorl %r13d, %r13d
testl %eax, %eax
movl $0x0, %eax
je 0x17fd
movl 0x8b35(%rip), %edi      # 0xa318
callq 0x227d
movl %eax, %ecx
movl $0x0, %eax
testl %ecx, %ecx
je 0x17fd
xorl %edi, %edi
movl %r14d, %esi
callq 0x1678
movl 0x18(%r12), %ecx
movl 0x8b10(%rip), %edi      # 0xa318
shldl $0x1, %ebp, %edi
movl %edi, 0x8b06(%rip)      # 0xa318
incl %r13d
movl %r13d, 0x8b00(%rip)     # 0xa31c
testl %ecx, %ecx
je 0x1852
cmpl %r15d, %r13d
jne 0x1852
xorl %r13d, %r13d
testl %eax, %eax
movl $0x0, %eax
je 0x184b
callq 0x227d
movl %eax, %ecx
movl $0x0, %eax
testl %ecx, %ecx
je 0x184b
xorl %edi, %edi
movl %r14d, %esi
callq 0x1678
andl $0x0, 0x8aca(%rip)      # 0xa31c
addl %ebp, %ebp
decl %ebx
jne 0x17c3
jmp 0x1895
incl %ecx
movl %ecx, 0x8ab6(%rip)      # 0xa31c
callq 0x227d
testl %eax, %eax
je 0x1893
pushq $0x1
popq %rsi
xorl %edi, %edi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1678
leaq 0x39c0(%rip), %rdi      # 0x524e
callq 0x1110
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq