// File: /workspace/github_c_makefile_star2_O2/pahihu[P]mixal/io.c
// Function: do_operation
static unsigned do_operation(Address pc,
                             Byte device, Operation operation,
                             Cell rX, Cell offset, Address buffer)
{
    unsigned clocks = 0;

    /*
    if (true == devices[device].int_pending)
        error("Interrupt pending - %02o", device);
    */

    ensure_open(device);
    if (devices[device].busy) {
        clocks = 1;
        idle_time += clocks;
        do_scheduled_io(clocks);
        set_wait_state();
        return clocks;
    }

    if (control_state == get_internal_state())
        devices[device].int_request = true;

    if (attributes(device)->io_time)
        io_schedule(pc, device, operation, rX, offset, buffer);
    else {
        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); break;
        }
        if (devices[device].int_request == true)
            devices[device].int_pending = true;
    }
    return clocks;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %r14d
movq %r8, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movl %edx, %r13d
movl %esi, %ebx
movl %edi, 0x1c(%rsp)
movl %esi, %r12d
cmpl $0x14, %esi
jb 0x44dd
leaq 0x4fbc(%rip), %rdi      # 0x9490
movl %ebx, %esi
xorl %eax, %eax
callq 0x5700
movl %ebx, %edi
callq 0x4dbe
leaq 0xaca5(%rip), %r15      # 0xf190
testq %rax, %rax
jne 0x45e4
movl %r13d, 0x8(%rsp)
movl %r14d, 0xc(%rsp)
imulq $0x48, %r12, %r13
movl (%r13,%r15), %ebp
imulq $0x38, %rbp, %rax
leaq 0xa0fe(%rip), %r14      # 0xe610
movq (%rax,%r14), %rdx
testq %rdx, %rdx
je 0x4540
addq %r15, %r13
leaq 0xb7bb(%rip), %rdi      # 0xfce0
cmpl $0xf, %ebx
ja 0x4557
leaq 0x5024(%rip), %rsi      # 0x9555
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3280
movl (%r13), %ebp
jmp 0x455f
leaq 0x4fe4(%rip), %rdi      # 0x952b
movl %ebx, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x5700
jmp 0x45da
movq %rdx, %rsi
callq 0x3090
imulq $0x38, %rbp, %rax
movq 0x8(%rax,%r14), %rbp
leaq 0xb771(%rip), %rdi      # 0xfce0
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x8(%r13)
testq %rax, %rax
jne 0x45d5
leaq 0x4f7d(%rip), %rsi      # 0x9504
movq %rbp, %rdi
callq 0x31b0
testl %eax, %eax
je 0x45b9
callq 0x3070
movl (%rax), %edi
callq 0x32b0
leaq 0x4f66(%rip), %rdi      # 0x950c
leaq 0xb733(%rip), %rsi      # 0xfce0
movq %rax, %rdx
xorl %eax, %eax
callq 0x5700
jmp 0x45d5
leaq 0xb720(%rip), %rdi      # 0xfce0
leaq 0x4f41(%rip), %rsi      # 0x9508
callq 0x3250
movq %rax, 0x8(%r13)
testq %rax, %rax
je 0x4593
andq $0x0, 0x10(%r13)
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %r13d
imulq $0x48, %r12, %rax
cmpl $0x0, 0x18(%rax,%r15)
je 0x4617
incq 0xb6b9(%rip)            # 0xfcb0
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x48c7
callq 0x6982
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rax, %r15
callq 0x65f3
cmpl $0x1, %eax
jne 0x462c
movl $0x1, 0x3c(%r15)
movl (%r15), %edi
imulq $0x38, %rdi, %rax
leaq 0x9fd6(%rip), %rdx      # 0xe610
leaq (%rdx,%rax), %rcx
movl 0x30(%rax,%rdx), %ebp
testl %ebp, %ebp
movq 0x20(%rsp), %r9
je 0x46ab
movl 0x44(%r15), %edx
movl 0x1c(%rsp), %eax
movl %eax, 0x1c(%r15)
movl %r13d, 0x20(%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x28(%r15)
movq %r9, 0x30(%r15)
movl %r14d, 0x38(%r15)
movl 0x34(%rcx), %esi
testl %r13d, %r13d
je 0x46d1
movl %edi, %eax
xorl $0x6, %eax
movl %r13d, %r8d
xorl $0x1, %r8d
orl %eax, %r8d
je 0x46fd
movl %edi, %eax
xorl $0x2, %eax
xorl $0x1, %edx
orl %eax, %edx
jne 0x4775
addl $0x3929, %ebp           # imm = 0x3929
andl $0x0, 0x44(%r15)
cmpl $0x2, %r13d
ja 0x470d
jmp 0x47db
movl %ebx, %edi
cmpl $0x2, %r13d
je 0x4743
cmpl $0x1, %r13d
jne 0x4750
movq 0x10(%rsp), %rsi
movl %r14d, %edx
callq *0x18(%rcx)
jmp 0x475b
movq 0x10(%r15), %rax
cmpl $0x2, %edi
je 0x47ef
cmpl $0x5, %edi
jne 0x4839
movl $0x84, %ecx
cqto
idivq %rcx
movl $0x84, %ebp
subl %edx, %ebp
imull %esi, %ebp
jmp 0x470d
movl 0x14(%rcx), %esi
xorl %ebp, %ebp
pushq $-0x15
popq %rdx
movl %r14d, %edi
callq 0x4e0d
movl %ebp, 0x18(%r15)
testl %ebp, %ebp
je 0x483e
movl 0xb59d(%rip), %eax      # 0xfcbc
testl %eax, %eax
je 0x4738
xorl %ebx, %ebx
cmpl %eax, %ebp
jae 0x4606
movl %ebp, 0xb589(%rip)      # 0xfcbc
jmp 0x4606
movl %ebp, 0xb57e(%rip)      # 0xfcbc
jmp 0x4845
movq 0x10(%rsp), %rsi
movl %r14d, %edx
callq *0x20(%rcx)
jmp 0x475b
movq 0x10(%rsp), %rsi
movq %r9, %rdx
callq *0x28(%rcx)
xorl %ebx, %ebx
cmpl $0x1, 0x3c(%r15)
jne 0x4606
movl $0x1, 0x40(%r15)
jmp 0x4606
cmpl $0x3, %r13d
jae 0x484c
cmpl $0x1, %edi
ja 0x47db
leaq 0xc5a5(%rip), %rax      # 0x10d30
movq (%rax), %rax
xorl %edx, %edx
divq %rsi
shlq $0x6, %rdx
cvtsi2sd %rdx, %xmm0
cvtsi2sd %rsi, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andl $0x3f, %r9d
leal 0x40(%r9), %edx
subl %eax, %edx
cmpl %eax, %r9d
cmovael %r9d, %edx
imull %esi, %edx
cvtsi2sd %rdx, %xmm0
mulsd 0x4cbd(%rip), %xmm0     # 0x9488
movl %ebp, %eax
cvtsi2sd %rax, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rbp
movl 0x14(%rcx), %esi
cmpl $0x1, %r13d
je 0x4702
pushq $-0x1
jmp 0x4704
movq %r9, %rdi
andq $0x3fffffff, %rdi       # imm = 0x3FFFFFFF
je 0x4855
btl $0x1e, %r9d
jb 0x4867
addl %eax, %edi
movl 0x10(%rcx), %ecx
movl %ecx, %esi
subl %eax, %esi
cmpl %edi, %ecx
cmoval %edi, %esi
imull %esi, %ebp
testl %ebp, %ebp
setne %al
cmpl $0x1, %edx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x470d
addl $0x3929, %ebp           # imm = 0x3929
andl $0x0, 0x44(%r15)
jmp 0x470d
andl $0x0, 0x18(%r15)
movl %ebx, %edi
callq 0x4944
xorl %ebx, %ebx
jmp 0x4606
movl %ebp, 0x18(%r15)
jmp 0x4719
movl $0x1, 0x44(%r15)
imull %esi, %eax
movl %eax, %ebp
jmp 0x470d
cmpq %rax, %rdi
cmovlq %rdi, %rax
imull %eax, %ebp
jmp 0x470d