// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movl %r9d, %r13d
movq %r8, 0x10(%rsp)
movq %rcx, (%rsp)
movl %edx, %r12d
movl %esi, %ebx
movl %edi, 0xc(%rsp)
movl %esi, %r14d
cmpl $0x14, %esi
jb 0x4790
leaq 0x4d09(%rip), %rdi      # 0x9490
movl %ebx, %esi
xorl %eax, %eax
callq 0x5bbc
cmpb $0x1, 0xb521(%rip)      # 0xfcb8
leaq (%r14,%r14,8), %r15
leaq 0xa9ee(%rip), %r14      # 0xf190
jne 0x47ae
leaq (%r14,%r15,8), %rax
addq $0x8, %rax
jmp 0x47d1
movl (%r14,%r15,8), %eax
addl $-0x3, %eax
cmpl $0x3, %eax
jae 0x47c9
movl %eax, %eax
leaq 0x9fd5(%rip), %rcx      # 0xe798
movq (%rcx,%rax,8), %rax
jmp 0x47d1
leaq (%r14,%r15,8), %rax
addq $0x8, %rax
cmpq $0x0, (%rax)
jne 0x48c0
movl (%r14,%r15,8), %eax
imulq $0x38, %rax, %rcx
leaq 0x9e26(%rip), %rbp      # 0xe610
movq (%rcx,%rbp), %rdx
testq %rdx, %rdx
je 0x481a
movl %r13d, 0x8(%rsp)
leaq (%r14,%r15,8), %r13
leaq 0xb4dd(%rip), %rdi      # 0xfce0
cmpl $0xf, %ebx
ja 0x4831
leaq 0x4d46(%rip), %rsi      # 0x9555
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3280
jmp 0x4839
leaq 0x4d0a(%rip), %rdi      # 0x952b
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x5bbc
jmp 0x48c0
movq %rdx, %rsi
callq 0x3090
movl (%r13), %eax
imulq $0x38, %rax, %rax
movq 0x8(%rax,%rbp), %rbp
leaq 0xb493(%rip), %rdi      # 0xfce0
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x8(%r13)
testq %rax, %rax
jne 0x48b3
leaq 0x4c9f(%rip), %rsi      # 0x9504
movq %rbp, %rdi
callq 0x31b0
testl %eax, %eax
jne 0x4888
leaq 0xb468(%rip), %rdi      # 0xfce0
leaq 0x4c89(%rip), %rsi      # 0x9508
callq 0x3250
movq %rax, 0x8(%r13)
cmpq $0x0, 0x8(%r13)
jne 0x48b3
callq 0x3070
movl (%rax), %edi
callq 0x32b0
leaq 0x4c6a(%rip), %rdi      # 0x950c
leaq 0xb437(%rip), %rsi      # 0xfce0
movq %rax, %rdx
xorl %eax, %eax
callq 0x5bbc
movq $0x0, 0x10(%r13)
movl 0x8(%rsp), %r13d
cmpl $0x0, 0x18(%r14,%r15,8)
je 0x48f4
incq 0xb3e1(%rip)            # 0xfcb0
movl $0x1, %ebx
movl $0x1, %edi
callq 0x4baa
callq 0x6d76
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%r14,%r15,8), %rbp
callq 0x6a04
cmpl $0x1, %eax
jne 0x4909
movl $0x1, 0x3c(%rbp)
movl (%rbp), %edi
imulq $0x38, %rdi, %rax
leaq 0x9cf9(%rip), %rcx      # 0xe610
leaq (%rcx,%rax), %r8
movl 0x30(%rax,%rcx), %r15d
testl %r15d, %r15d
movq 0x10(%rsp), %r11
je 0x4987
movl 0x44(%rbp), %eax
movl 0xc(%rsp), %ecx
movl %ecx, 0x1c(%rbp)
movl %r12d, 0x20(%rbp)
movq (%rsp), %rcx
movq %rcx, 0x28(%rbp)
movq %r11, 0x30(%rbp)
movl %r13d, 0x38(%rbp)
movl 0x34(%r8), %esi
testl %r12d, %r12d
je 0x49a6
movl %edi, %ecx
xorl $0x6, %ecx
movl %r12d, %edx
xorl $0x1, %edx
orl %ecx, %edx
je 0x4a05
movl %edi, %ecx
xorl $0x2, %ecx
xorl $0x1, %eax
orl %ecx, %eax
jne 0x4a8d
addl $0x3929, %r15d          # imm = 0x3929
movl $0x0, 0x44(%rbp)
jmp 0x4a8d
movl %ebx, %edi
cmpl $0x2, %r12d
je 0x4a0d
cmpl $0x1, %r12d
jne 0x4a1a
movq (%rsp), %rsi
movl %r13d, %edx
callq *0x18(%r8)
jmp 0x4a25
movq 0x10(%rbp), %rcx
cmpl $0x5, %edi
je 0x4a3d
cmpl $0x2, %edi
jne 0x4a05
movq %r11, %rdx
andq $0x3fffffff, %rdx       # imm = 0x3FFFFFFF
je 0x4a73
btl $0x1e, %r11d
jb 0x4a82
addl %ecx, %edx
movl 0x10(%r8), %r9d
movl %r9d, %r10d
subl %ecx, %r10d
cmpl %edx, %r9d
cmoval %edx, %r10d
imull %r10d, %r15d
testl %r15d, %r15d
setne %cl
cmpl $0x1, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
je 0x4974
jmp 0x4a8d
xorl %r15d, %r15d
jmp 0x4a8d
movq (%rsp), %rsi
movl %r13d, %edx
callq *0x20(%r8)
jmp 0x4a25
movq (%rsp), %rsi
movq %r11, %rdx
callq *0x28(%r8)
xorl %ebx, %ebx
cmpl $0x1, 0x3c(%rbp)
jne 0x48e3
movl $0x1, 0x40(%rbp)
jmp 0x48e3
movabsq $0xf83e0f83e0f83e1, %rdx # imm = 0xF83E0F83E0F83E1
movq %rcx, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x3, %rdx
addl %eax, %edx
movl %edx, %eax
shll $0x7, %eax
leal (%rax,%rdx,4), %r15d
subl %ecx, %r15d
addl $0x84, %r15d
imull %esi, %r15d
jmp 0x4a8d
movl $0x1, 0x44(%rbp)
imull %esi, %ecx
movl %ecx, %r15d
jmp 0x4a8d
cmpq %rcx, %rdx
cmovlq %rdx, %rcx
imull %ecx, %r15d
leal -0x1(%r12), %eax
cmpl $0x1, %eax
ja 0x4b14
cmpl $0x1, %edi
ja 0x4afa
leaq 0xc28d(%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, %r11d
leal 0x40(%r11), %ecx
subl %eax, %ecx
cmpl %eax, %r11d
cmovael %r11d, %ecx
imull %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x49a2(%rip), %xmm0     # 0x9488
movl %r15d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r15
xorl %edx, %edx
cmpl $0x1, %r12d
setne %dl
movl 0x14(%r8), %esi
negl %edx
orl $-0x15, %edx
movl %r13d, %edi
callq 0x50ba
movl %r15d, 0x18(%rbp)
testl %r15d, %r15d
je 0x4b3e
movl 0xb199(%rip), %eax      # 0xfcbc
testl %eax, %eax
je 0x4b47
xorl %ebx, %ebx
cmpl %eax, %r15d
jae 0x48e3
movl %r15d, 0xb183(%rip)     # 0xfcbc
jmp 0x48e3
movl %ebx, %edi
callq 0x4c2d
jmp 0x4b4e
movl %r15d, 0xb16e(%rip)     # 0xfcbc
xorl %ebx, %ebx
jmp 0x48e3