// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/mhmt/mhmt-emit.c
// Function: emit_byte
ULONG emit_byte(UBYTE byte, ULONG operation)
{
    static UBYTE buffer[EMIT_BYTEBUF_SIZE];
    static ULONG in_pos, out_pos;
    ULONG success;

    switch( operation ) {
    case EMIT_BYTE_INIT:
        in_pos  = 0;
        out_pos = 0;
        return 1;

    case EMIT_BYTE_ADD:
#ifdef DBG
        printf("<%02x>",byte&0x00FF);
#endif
        buffer[in_pos] = byte;
        in_pos = (in_pos+1) & (EMIT_BYTEBUF_SIZE-1);

        if( in_pos==out_pos ) { // overflow!
            printf("mhmt-emit.c:emit_byte() buffer overflow!\n");
            return 0;
        }

        return 1;

    case EMIT_BYTE_FLUSH:
        if( in_pos==out_pos ) // nothing to do?
            return 1;

        else if( in_pos>out_pos ) { // no index wraparound
            success = emit_file( &buffer[out_pos], in_pos-out_pos );

        } else { // in_pos<out_pos - wraparound
            success = emit_file( &buffer[out_pos], EMIT_BYTEBUF_SIZE-out_pos );

            if( in_pos )
                success = success && emit_file( &buffer[0], in_pos );
        }

        out_pos=in_pos;
        return success;

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

// Assembly:
subq $0x18, %rsp
movb %dil, %al
movb %al, 0x13(%rsp)
movl %esi, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x1b6a
jmp 0x1b4d
movl 0x4(%rsp), %eax
subl $0x1, %eax
je 0x1beb
jmp 0x1b5c
movl 0x4(%rsp), %eax
subl $0x2, %eax
je 0x1b8b
jmp 0x1cb8
movl $0x0, 0xc81c(%rip)      # 0xe390
movl $0x0, 0xc816(%rip)      # 0xe394
movl $0x1, 0x14(%rsp)
jmp 0x1cce
movb 0x13(%rsp), %dl
movl 0xc7fb(%rip), %eax      # 0xe390
movl %eax, %ecx
leaq 0xc6f2(%rip), %rax      # 0xe290
movb %dl, (%rax,%rcx)
movl 0xc7e9(%rip), %eax      # 0xe390
addl $0x1, %eax
andl $0xff, %eax
movl %eax, 0xc7db(%rip)      # 0xe390
movl 0xc7d5(%rip), %eax      # 0xe390
cmpl 0xc7d3(%rip), %eax      # 0xe394
jne 0x1bde
leaq 0x7732(%rip), %rdi      # 0x92fc
movb $0x0, %al
callq 0x1040
movl $0x0, 0x14(%rsp)
jmp 0x1cce
movl $0x1, 0x14(%rsp)
jmp 0x1cce
movl 0xc79f(%rip), %eax      # 0xe390
cmpl 0xc79d(%rip), %eax      # 0xe394
jne 0x1c06
movl $0x1, 0x14(%rsp)
jmp 0x1cce
movl 0xc784(%rip), %eax      # 0xe390
cmpl 0xc782(%rip), %eax      # 0xe394
jbe 0x1c3b
movl 0xc77a(%rip), %eax      # 0xe394
leaq 0xc66f(%rip), %rdi      # 0xe290
addq %rax, %rdi
movl 0xc766(%rip), %esi      # 0xe390
subl 0xc764(%rip), %esi      # 0xe394
callq 0x1950
movl %eax, 0x8(%rsp)
jmp 0x1ca0
movl 0xc753(%rip), %eax      # 0xe394
leaq 0xc648(%rip), %rdi      # 0xe290
addq %rax, %rdi
movl $0x100, %esi            # imm = 0x100
subl 0xc73e(%rip), %esi      # 0xe394
callq 0x1950
movl %eax, 0x8(%rsp)
cmpl $0x0, 0xc72a(%rip)      # 0xe390
je 0x1c9e
xorl %eax, %eax
cmpl $0x0, 0x8(%rsp)
movb %al, 0x3(%rsp)
je 0x1c91
movl 0xc715(%rip), %esi      # 0xe390
leaq 0xc60e(%rip), %rdi      # 0xe290
callq 0x1950
cmpl $0x0, %eax
setne %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x8(%rsp)
jmp 0x1ca0
jmp 0x1ca2
movl 0xc6e8(%rip), %eax      # 0xe390
movl %eax, 0xc6e6(%rip)      # 0xe394
movl 0x8(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x1cce
leaq 0x7667(%rip), %rdi      # 0x9326
movb $0x0, %al
callq 0x1040
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
addq $0x18, %rsp
retq
nopw (%rax,%rax)