// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/mhmt/mhmt-emit.c
// Function: emit_file
ULONG emit_file(UBYTE * bytes, LONG length)
{
    static UBYTE buffer[EMIT_FILEBUF_SIZE];
    static ULONG position;

    if( length==EMIT_FILE_INIT ) {
        position = 0;
        return 1;

    } else if( length>0 ) {
        while( (position+length) >= EMIT_FILEBUF_SIZE ) { // if we have to flush buffer
            length -= (EMIT_FILEBUF_SIZE-position);

            while( position < EMIT_FILEBUF_SIZE ) { // fill buffer to the end, if possible
                buffer[position++] = *(bytes++);
            }

            if( EMIT_FILEBUF_SIZE!=fwrite(buffer, 1, EMIT_FILEBUF_SIZE, wrk.file_out) ) {
                printf("mhmt-emit.c:emit_file() can't write to file!\n");
                return 0;
            }

            position=0;
        }

        while( length-- ) { // if something left that does not need flushing
            buffer[position++] = *(bytes++);
        }

        return 1;

    } else if( length==EMIT_FILE_FINISH ) {
        if( position>0 ) { // do we have anything to flush?
            if( position!=fwrite(buffer, 1, position, wrk.file_out) ) {
                printf("mhmt-emit.c:emit_file() can't write to file!\n");
                return 0;
            }
        }

        return 1;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $-0x1, %esi
je 0x16e7
movl %esi, %r14d
testl %esi, %esi
jle 0x16f8
movq %rdi, %rbx
movl 0x9bdb(%rip), %r12d     # 0xb200
leal (%r12,%r14), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jb 0x16ab
leaq 0x8bc9(%rip), %r15      # 0xa200
leaq 0x9ce2(%rip), %r13      # 0xb320
movl 0x9bbc(%rip), %eax      # 0xb200
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x1669
movb (%rbx), %cl
incq %rbx
movb %cl, (%rax,%r15)
incq %rax
cmpl $0x1000, %eax           # imm = 0x1000
jne 0x164c
movl $0x1000, 0x9b97(%rip)   # imm = 0x1000
movq 0x38(%r13), %rcx
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
callq 0x1130
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x1737
addl %r12d, %r14d
addl $0xfffff000, %r14d      # imm = 0xFFFFF000
movl $0x0, 0x9b61(%rip)      # 0xb200
xorl %r12d, %r12d
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x163e
movl $0x1, %ebp
testl %r14d, %r14d
je 0x1745
movl 0x9b41(%rip), %eax      # 0xb200
movl %r14d, %edx
xorl %ecx, %ecx
leaq 0x8b35(%rip), %rsi      # 0xa200
leal (%rax,%rcx), %edi
movb (%rbx,%rcx), %r8b
movb %r8b, (%rdi,%rsi)
incq %rcx
cmpl %ecx, %edx
jne 0x16cb
addl %ecx, %eax
movl %eax, 0x9b1b(%rip)      # 0xb200
jmp 0x1745
movl $0x0, 0x9b0f(%rip)      # 0xb200
movl $0x1, %ebp
jmp 0x1745
je 0x1703
leaq 0x4a89(%rip), %rdi      # 0x618a
jmp 0x173e
movl 0x9af7(%rip), %ebx      # 0xb200
movl $0x1, %ebp
testq %rbx, %rbx
je 0x1745
leaq 0x9c06(%rip), %rax      # 0xb320
movq 0x38(%rax), %rcx
leaq 0x8adb(%rip), %rdi      # 0xa200
movl $0x1, %esi
movq %rbx, %rdx
callq 0x1130
cmpq %rbx, %rax
je 0x1745
leaq 0x4a83(%rip), %rdi      # 0x61c1
callq 0x1120
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq