// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x28, %rsp
movl %edi, 0x20(%rsp)
movl %esi, 0x1c(%rsp)
movl $0x1, 0x14(%rsp)
leaq 0xc6a5(%rip), %rax      # 0xe3a0
movl 0x10(%rax), %edx
movl $0x8, %eax
movl $0x10, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x18(%rsp)
cmpl $-0x1, 0x1c(%rsp)
jne 0x1d3a
movl $0x0, 0xc675(%rip)      # 0xe398
movl $0x0, 0xc66f(%rip)      # 0xe39c
movl $0x1, 0x24(%rsp)
jmp 0x1f61
cmpl $0x0, 0x1c(%rsp)
jne 0x1de3
cmpl $0x0, 0xc650(%rip)      # 0xe39c
je 0x1da6
jmp 0x1d50
movl 0xc646(%rip), %eax      # 0xe39c
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0xc63b(%rip)      # 0xe39c
cmpl 0x18(%rsp), %eax
jae 0x1d77
movl 0xc62b(%rip), %eax      # 0xe398
shll %eax
movl %eax, 0xc623(%rip)      # 0xe398
jmp 0x1d50
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0x13(%rsp)
je 0x1d99
movl 0xc60e(%rip), %edi      # 0xe398
callq 0x37c0
cmpl $0x0, %eax
setne %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0x12(%rsp)
je 0x1dc9
xorl %edi, %edi
movl $0x1, %esi
callq 0x1b30
cmpl $0x0, %eax
setne %al
movb %al, 0x12(%rsp)
movb 0x12(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x1f61
cmpl $0x0, 0x1c(%rsp)
jle 0x1f4b
jmp 0x1df0
leaq 0xc5a9(%rip), %rax      # 0xe3a0
cmpl $0x0, 0x18(%rax)
jne 0x1e74
movl 0xc599(%rip), %eax      # 0xe39c
cmpl 0x18(%rsp), %eax
jne 0x1e72
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0x11(%rsp)
je 0x1e2b
movl 0xc57c(%rip), %edi      # 0xe398
callq 0x37c0
cmpl $0x0, %eax
setne %al
movb %al, 0x11(%rsp)
movb 0x11(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0x10(%rsp)
je 0x1e5b
xorl %edi, %edi
movl $0x1, %esi
callq 0x1b30
cmpl $0x0, %eax
setne %al
movb %al, 0x10(%rsp)
movb 0x10(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0xc52a(%rip)      # 0xe39c
jmp 0x1e74
movl 0xc51e(%rip), %eax      # 0xe398
shll %eax
movl 0x20(%rsp), %ecx
shrl $0x1f, %ecx
andl $0x1, %ecx
orl %ecx, %eax
movl %eax, 0xc50a(%rip)      # 0xe398
movl 0x20(%rsp), %eax
shll %eax
movl %eax, 0x20(%rsp)
movl 0xc4fe(%rip), %eax      # 0xe39c
addl $0x1, %eax
movl %eax, 0xc4f5(%rip)      # 0xe39c
leaq 0xc4f2(%rip), %rax      # 0xe3a0
cmpl $0x0, 0x18(%rax)
je 0x1f2b
movl 0xc4e2(%rip), %eax      # 0xe39c
cmpl 0x18(%rsp), %eax
jne 0x1f29
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0xf(%rsp)
je 0x1ee2
movl 0xc4c5(%rip), %edi      # 0xe398
callq 0x37c0
cmpl $0x0, %eax
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
movb %al, 0xe(%rsp)
je 0x1f12
xorl %edi, %edi
movl $0x1, %esi
callq 0x1b30
cmpl $0x0, %eax
setne %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0xc473(%rip)      # 0xe39c
jmp 0x1f2b
jmp 0x1f2d
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, %eax
jne 0x1df0
movl 0x14(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x1f61
leaq 0x740c(%rip), %rdi      # 0x935e
movb $0x0, %al
callq 0x1040
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw (%rax,%rax)