// File: /workspace/github_c_makefile_star2_O0/pahihu[P]mixal/io.c
// Function: do_io
static void do_io(Byte device)
{
    Cell rX, offset;
    Address buffer;
    Operation operation;

    operation = devices[device].operation;
    rX        = devices[device].rX;
    offset    = devices[device].offset;
    buffer    = devices[device].buffer;

    if (dbg > 1) {
        fflush(stdout);
        fprintf(stderr,"do_io(%s, %d, %s, %s%lo, %ld, %d, @%ld)\n",
                address_to_string(devices[device].pc),
                device, operations[operation],
                is_negative(rX) ? "-" : "", magnitude(rX),
                offset, buffer,
                devices[device].position);
    }

    current_device_type = devices[device].type;
    switch (operation) {
    case control:
        attributes(device)->ioc_handler(device, rX, offset); break;
    case input:
        attributes(device)->in_handler(device, rX, buffer); break;
    case output:
        attributes(device)->out_handler(device, rX, buffer);
        if (current_device_type == printer)
            devices[device].position++;
    }
    
    if (operation != control)
        io_mark_incomplete(buffer,
                           attributes(device)->block_size, 
                           operation == input ? -WRITE : -READ);
        
    if (devices[device].int_request == true)
        devices[device].int_pending = true;

    devices[device].busy = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x58, %rsp
movl %edi, -0xc(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xcaa8(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xca8f(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rax), %rax
movq %rax, -0x18(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xca74(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xca59(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x38(%rax), %eax
movl %eax, -0x24(%rbp)
cmpl $0x1, 0xd5b1(%rip)      # 0x12cdc
jle 0x57ef
movq 0xc898(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x31c0
movq 0xc8b1(%rip), %rax      # 0x11ff8
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xca16(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %edi
callq 0x4670
movq -0x30(%rbp), %rdi
movq %rax, %rdx
movl -0xc(%rbp), %ecx
movl -0x28(%rbp), %eax
movl %eax, %esi
leaq 0xcf91(%rip), %rax      # 0x12710
movq (%rax,%rsi,8), %r8
movq -0x18(%rbp), %rsi
andq $0x40000000, %rsi       # imm = 0x40000000
leaq 0x6b55(%rip), %r9       # 0xc2ea
leaq 0x6bb5(%rip), %rax      # 0xc351
cmpq $0x0, %rsi
cmovneq %rax, %r9
movq -0x18(%rbp), %rbx
andq $0x3fffffff, %rbx       # imm = 0x3FFFFFFF
movq -0x20(%rbp), %r11
movl -0x24(%rbp), %r10d
movl -0xc(%rbp), %eax
movl %eax, %esi
leaq 0xc9ad(%rip), %rax      # 0x12170
imulq $0x48, %rsi, %rsi
addq %rsi, %rax
movq 0x10(%rax), %rax
leaq 0x6ebd(%rip), %rsi      # 0xc692
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, %al
callq 0x3170
movl -0xc(%rbp), %eax
leaq (%rax,%rax,8), %rcx
leaq 0xc973(%rip), %rax      # 0x12170
movl (%rax,%rcx,8), %eax
movl %eax, 0xd4f2(%rip)      # 0x12cf8
movl -0x28(%rbp), %eax
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x5826
jmp 0x5812
movl -0x34(%rbp), %eax
subl $0x1, %eax
je 0x5841
jmp 0x581c
movl -0x34(%rbp), %eax
subl $0x2, %eax
je 0x585b
jmp 0x589d
movl -0xc(%rbp), %edi
callq 0x6140
movq 0x28(%rax), %rax
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq *%rax
jmp 0x589d
movl -0xc(%rbp), %edi
callq 0x6140
movq 0x18(%rax), %rax
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movl -0x24(%rbp), %edx
callq *%rax
jmp 0x589d
movl -0xc(%rbp), %edi
callq 0x6140
movq 0x20(%rax), %rax
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movl -0x24(%rbp), %edx
callq *%rax
cmpl $0x5, 0xd47e(%rip)      # 0x12cf8
jne 0x589b
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xc8e8(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0x589d
cmpl $0x0, -0x28(%rbp)
je 0x58cf
movl -0x24(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0xc(%rbp), %edi
callq 0x6140
movl -0x38(%rbp), %edi
movl 0x14(%rax), %esi
movl -0x28(%rbp), %ecx
movl $0x1, %edx
movl $0x15, %eax
cmpl $0x1, %ecx
cmovel %eax, %edx
callq 0x6740
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xc895(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x3c(%rax)
jne 0x5902
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xc87c(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x40(%rax)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xc862(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x18(%rax)
addq $0x58, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)