// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %r8, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
movl %edx, 0x1c(%rsp)
movl %esi, %ebx
movl %edi, 0x18(%rsp)
movl %esi, %r14d
cmpl $0x14, %esi
jb 0x4772
leaq 0x4d27(%rip), %rdi      # 0x9490
movl %ebx, %esi
xorl %eax, %eax
callq 0x5bec
cmpb $0x1, 0xb53f(%rip)      # 0xfcb8
leaq (%r14,%r14,8), %r15
leaq 0xaa0c(%rip), %r13      # 0xf190
jne 0x4793
leaq 0x8(,%r15,8), %rax
addq %r13, %rax
jmp 0x47be
movl (%r13,%r15,8), %eax
addl $-0x3, %eax
cmpl $0x3, %eax
jae 0x47af
movl %eax, %eax
leaq 0x9fef(%rip), %rcx      # 0xe798
movq (%rcx,%rax,8), %rax
jmp 0x47be
leaq (,%r15,8), %rax
addq %r13, %rax
addq $0x8, %rax
cmpq $0x0, (%rax)
jne 0x48b8
movl (%r13,%r15,8), %ebp
imulq $0x38, %rbp, %rax
leaq 0x9e38(%rip), %r14      # 0xe610
movq (%rax,%r14), %rdx
testq %rdx, %rdx
je 0x4813
movl %r12d, 0x14(%rsp)
leaq (,%r15,8), %r12
addq %r13, %r12
leaq 0xb4e8(%rip), %rdi      # 0xfce0
cmpl $0xf, %ebx
ja 0x482a
leaq 0x4d51(%rip), %rsi      # 0x9555
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3280
movl (%r12), %ebp
jmp 0x4832
leaq 0x4d11(%rip), %rdi      # 0x952b
movl %ebx, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x5bec
jmp 0x48b8
movq %rdx, %rsi
callq 0x3090
imulq $0x38, %rbp, %rax
movq 0x8(%rax,%r14), %rbp
leaq 0xb49e(%rip), %rdi      # 0xfce0
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x8(%r12)
testq %rax, %rax
jne 0x48aa
leaq 0x4ca9(%rip), %rsi      # 0x9504
movq %rbp, %rdi
callq 0x31b0
testl %eax, %eax
je 0x488d
callq 0x3070
movl (%rax), %edi
callq 0x32b0
leaq 0x4c92(%rip), %rdi      # 0x950c
leaq 0xb45f(%rip), %rsi      # 0xfce0
movq %rax, %rdx
xorl %eax, %eax
callq 0x5bec
jmp 0x48aa
leaq 0xb44c(%rip), %rdi      # 0xfce0
leaq 0x4c6d(%rip), %rsi      # 0x9508
callq 0x3250
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x4867
movq $0x0, 0x10(%r12)
movl 0x14(%rsp), %r12d
cmpl $0x0, 0x18(%r13,%r15,8)
je 0x48ec
incq 0xb3e9(%rip)            # 0xfcb0
movl $0x1, %ebx
movl $0x1, %edi
callq 0x4bf2
callq 0x6db0
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (,%r15,8), %rbp
addq %r13, %rbp
callq 0x6a18
cmpl $0x1, %eax
jne 0x4908
movl $0x1, 0x3c(%rbp)
movl (%rbp), %edx
imulq $0x38, %rdx, %rax
leaq 0x9cfa(%rip), %rcx      # 0xe610
leaq (%rcx,%rax), %r8
movl 0x30(%rax,%rcx), %r15d
testl %r15d, %r15d
movl 0x1c(%rsp), %r9d
movq 0x20(%rsp), %r10
je 0x4996
movl 0x44(%rbp), %eax
movl 0x18(%rsp), %ecx
movl %ecx, 0x1c(%rbp)
movl %r9d, 0x20(%rbp)
movq 0x8(%rsp), %rcx
movq %rcx, 0x28(%rbp)
movq %r10, 0x30(%rbp)
movl %r12d, 0x38(%rbp)
movl 0x34(%r8), %esi
testl %r9d, %r9d
je 0x49bd
movl %edx, %ecx
xorl $0x6, %ecx
movl %r9d, %edi
xorl $0x1, %edi
orl %ecx, %edi
je 0x4a09
movl %edx, %ecx
xorl $0x2, %ecx
xorl $0x1, %eax
orl %ecx, %eax
jne 0x4a89
addl $0x3929, %r15d          # imm = 0x3929
movl $0x0, 0x44(%rbp)
cmpl $0x2, %r9d
ja 0x4a1d
jmp 0x4af6
movl %ebx, %edi
cmpl $0x2, %r9d
je 0x4a57
cmpl $0x1, %r9d
jne 0x4a65
movq 0x8(%rsp), %rsi
movl %r12d, %edx
callq *0x18(%r8)
jmp 0x4a71
movq 0x10(%rbp), %rcx
cmpl $0x2, %edx
je 0x4b0e
cmpl $0x5, %edx
jne 0x4b5d
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 0x4a1d
movl 0x14(%r8), %esi
xorl %r15d, %r15d
movl $0xffffffeb, %edx       # imm = 0xFFFFFFEB
movl %r12d, %edi
callq 0x510a
movl %r15d, 0x18(%rbp)
testl %r15d, %r15d
je 0x4b64
movl 0xb28c(%rip), %eax      # 0xfcbc
testl %eax, %eax
je 0x4a4b
xorl %ebx, %ebx
cmpl %eax, %r15d
jae 0x48db
movl %r15d, 0xb276(%rip)     # 0xfcbc
jmp 0x48db
movl %r15d, 0xb26a(%rip)     # 0xfcbc
jmp 0x4b6b
movq 0x8(%rsp), %rsi
movl %r12d, %edx
callq *0x20(%r8)
jmp 0x4a71
movq 0x8(%rsp), %rsi
movq %r10, %rdx
callq *0x28(%r8)
xorl %ebx, %ebx
cmpl $0x1, 0x3c(%rbp)
jne 0x48db
movl $0x1, 0x40(%rbp)
jmp 0x48db
cmpl $0x3, %r9d
jae 0x4b72
cmpl $0x1, %edx
ja 0x4af6
leaq 0xc291(%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, %r10d
leal 0x40(%r10), %ecx
subl %eax, %ecx
cmpl %eax, %r10d
cmovael %r10d, %ecx
imull %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x49a6(%rip), %xmm0     # 0x9488
movl %r15d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r15
movl 0x14(%r8), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x1, %r9d
je 0x4a10
jmp 0x4a15
movq %r10, %rdx
andq $0x3fffffff, %rdx       # imm = 0x3FFFFFFF
je 0x4b7b
btl $0x1e, %r10d
jb 0x4b8d
addl %ecx, %edx
movl 0x10(%r8), %esi
movl %esi, %edi
subl %ecx, %edi
cmpl %edx, %esi
cmoval %edx, %edi
imull %edi, %r15d
testl %r15d, %r15d
setne %cl
cmpl $0x1, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x4a1d
addl $0x3929, %r15d          # imm = 0x3929
movl $0x0, 0x44(%rbp)
jmp 0x4a1d
movl $0x0, 0x18(%rbp)
movl %ebx, %edi
callq 0x4c75
xorl %ebx, %ebx
jmp 0x48db
movl %r15d, 0x18(%rbp)
jmp 0x4a2a
movl $0x1, 0x44(%rbp)
imull %esi, %ecx
movl %ecx, %r15d
jmp 0x4a1d
cmpq %rcx, %rdx
cmovlq %rdx, %rcx
imull %ecx, %r15d
jmp 0x4a1d