// File: /workspace/github_c_makefile_star2_O0/pahihu[P]mixal/io.c
// Function: io_schedule
static void io_schedule(Address pc,
                        Byte device, Operation operation,
                        Cell rX, Cell offset, Address buffer)
{
    unsigned busy, seek_time, io_time, u;
    enum DeviceType device_type;
    long position;
    Flag rewound;

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

    rewound = devices[device].rewound;

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

    position      = devices[device].position;
    device_type   = devices[device].type;
    seek_time     = attributes(device)->seek_time;
    io_time       = attributes(device)->io_time;

    busy = 0;
    if (operation == control) {
        switch (device_type) {
        case tape:
            u = magnitude(offset);
            if (u == 0) {                       /* --- rewind --- */
                devices[device].rewound = true;
                busy = position * seek_time;
            }
            else if (is_negative(offset)) {     /* --- skip backward --- */
                busy = u < position ? u : position;
                busy *= io_time;
            }
            else {                              /* --- skip forward --- */
                busy = position + u;
                if (busy >= attributes(device)->size)
                    busy = attributes(device)->size - position;
                busy *= io_time;
                if (busy != 0 && rewound == true) {
                    busy += MT_START_LP - MT_START;
                    devices[device].rewound = false;
                }
            }
            break;
        case disk:
            // head/track
            busy = 0;
            // 1 head/surface
            // busy = labs(position - magnitude(offset)) / 64;
            break;
        case printer:
            busy = (132 - (position % 132)) * seek_time;
            break;
        default:
            break;
        }
    }
    else {
        if ((device_type != console) || (operation != input)) {
            busy = io_time;
            if (device_type == tape && rewound == true) {
                busy += (MT_START_LP - MT_START);
                devices[device].rewound = false;
            }
        }
    }

    if (operation == input || operation == output) {
        /* rotational delay */
        if (device_type == disk || device_type == drum) {
            unsigned sector = 64 * (elapsed_time % seek_time) / (double)seek_time;
            u = magnitude(offset) % 64;
            if (u >= sector)
                offset -= sector;
            else
                u += 64 - sector;
            busy += seek_time * u / 64.0;
        }
        io_mark_incomplete(buffer,
                           attributes(device)->block_size,
                           operation == input ? WRITE : READ);
    }
    if (dbg > 2) {
        fflush(stdout);
        fprintf(stderr,"io_schedule: busy = %d next_scheduled_io = %u\n", busy, next_scheduled_io);
    }
    devices[device].busy = busy;

    if (0 == busy) {
        do_io (device);
        return;
    }

    if (next_scheduled_io) {
        if (busy < next_scheduled_io)
            next_scheduled_io = busy;
    } else
        next_scheduled_io = busy;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
cmpl $0x2, 0xcb45(%rip)      # 0x12cdc
jle 0x622a
movq 0xbe2c(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x31c0
movq 0xbe45(%rip), %rax      # 0x11ff8
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movl -0x4(%rbp), %edi
callq 0x4670
movq -0x50(%rbp), %rdi
movq %rax, %rdx
movl -0x8(%rbp), %ecx
movl -0xc(%rbp), %eax
movl %eax, %esi
leaq 0xc538(%rip), %rax      # 0x12710
movq (%rax,%rsi,8), %r8
movq -0x18(%rbp), %rsi
andq $0x40000000, %rsi       # imm = 0x40000000
leaq 0x60fc(%rip), %r9       # 0xc2ea
leaq 0x615c(%rip), %rax      # 0xc351
cmpq $0x0, %rsi
cmovneq %rax, %r9
movq -0x18(%rbp), %r11
andq $0x3fffffff, %r11       # imm = 0x3FFFFFFF
movq -0x20(%rbp), %r10
movl -0x24(%rbp), %eax
leaq 0x635b(%rip), %rsi      # 0xc571
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x3170
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbf3a(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x44(%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x4(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbf1e(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movl -0xc(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbf05(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rcx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbeeb(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rcx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbed0(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x30(%rax)
movl -0x24(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbeb6(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x38(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbea0(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbe85(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movl -0x8(%rbp), %edi
callq 0x6140
movl 0x34(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x8(%rbp), %edi
callq 0x6140
movl 0x30(%rax), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x6475
movl -0x38(%rbp), %eax
movl %eax, -0x54(%rbp)
subl $0x1, %eax
je 0x6446
jmp 0x6335
movl -0x54(%rbp), %eax
subl $0x2, %eax
je 0x6350
jmp 0x633f
movl -0x54(%rbp), %eax
subl $0x5, %eax
je 0x644f
jmp 0x6471
movq -0x20(%rbp), %rax
andq $0x3fffffff, %rax       # imm = 0x3FFFFFFF
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x6390
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbe01(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x44(%rax)
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq %rcx, %rax
movl %eax, -0x28(%rbp)
jmp 0x6444
movq -0x20(%rbp), %rax
andq $0x40000000, %rax       # imm = 0x40000000
cmpq $0x0, %rax
je 0x63cd
movl -0x34(%rbp), %eax
cmpq -0x40(%rbp), %rax
jge 0x63b2
movl -0x34(%rbp), %eax
movq %rax, -0x60(%rbp)
jmp 0x63ba
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0x28(%rbp)
movl -0x30(%rbp), %eax
imull -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x6442
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x8(%rbp), %edi
callq 0x6140
movq %rax, %rcx
movl -0x64(%rbp), %eax
cmpl 0x10(%rcx), %eax
jb 0x6405
movl -0x8(%rbp), %edi
callq 0x6140
movl 0x10(%rax), %eax
subq -0x40(%rbp), %rax
movl %eax, -0x28(%rbp)
movl -0x30(%rbp), %eax
imull -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x6440
cmpl $0x1, -0x44(%rbp)
jne 0x6440
movl -0x28(%rbp), %eax
addl $0x3929, %eax           # imm = 0x3929
movl %eax, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbd3e(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x44(%rax)
jmp 0x6442
jmp 0x6444
jmp 0x6473
movl $0x0, -0x28(%rbp)
jmp 0x6473
movq -0x40(%rbp), %rax
movl $0x84, %ecx
cqto
idivq %rcx
movl $0x84, %eax
subq %rdx, %rax
movl -0x2c(%rbp), %ecx
imulq %rcx, %rax
movl %eax, -0x28(%rbp)
jmp 0x6473
jmp 0x6473
jmp 0x64bc
cmpl $0x6, -0x38(%rbp)
jne 0x6481
cmpl $0x1, -0xc(%rbp)
je 0x64ba
movl -0x30(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x2, -0x38(%rbp)
jne 0x64b8
cmpl $0x1, -0x44(%rbp)
jne 0x64b8
movl -0x28(%rbp), %eax
addl $0x3929, %eax           # imm = 0x3929
movl %eax, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0xbcc6(%rip), %rax      # 0x12170
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x44(%rax)
jmp 0x64ba
jmp 0x64bc
cmpl $0x1, -0xc(%rbp)
je 0x64cc
cmpl $0x2, -0xc(%rbp)
jne 0x65ab
cmpl $0x1, -0x38(%rbp)
je 0x64dc
cmpl $0x0, -0x38(%rbp)
jne 0x657f
leaq 0xd86d(%rip), %rax      # 0x13d50
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movq %rcx, -0x70(%rbp)
xorl %edx, %edx
divq %rcx
movq -0x70(%rbp), %rax
shlq $0x6, %rdx
cvtsi2sd %rdx, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x48(%rbp)
movq -0x20(%rbp), %rax
andq $0x3fffffff, %rax       # imm = 0x3FFFFFFF
movl $0x40, %ecx
cqto
idivq %rcx
movl %edx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x48(%rbp), %eax
jb 0x6543
movl -0x48(%rbp), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
jmp 0x6551
movl $0x40, %eax
subl -0x48(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %eax
movl -0x34(%rbp), %ecx
imull %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd 0x5f21(%rip), %xmm0     # 0xc488
mulsd %xmm0, %xmm1
movl -0x28(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x8(%rbp), %edi
callq 0x6140
movl -0x74(%rbp), %edi
movl 0x14(%rax), %esi
movl -0xc(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffeb, %eax       # imm = 0xFFFFFFEB
cmpl $0x1, %ecx
cmovel %eax, %edx
callq 0x6740
cmpl $0x2, 0xc72a(%rip)      # 0x12cdc
jle 0x65e4
movq 0xba15(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x31c0
movq 0xba2e(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
movl 0xc70a(%rip), %ecx      # 0x12ce0
leaq 0x5fbd(%rip), %rsi      # 0xc59a
movb $0x0, %al
callq 0x3170
movl -0x28(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0xbb7d(%rip), %rax      # 0x12170
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
xorl %eax, %eax
cmpl -0x28(%rbp), %eax
jne 0x660e
movl -0x8(%rbp), %edi
callq 0x56b0
jmp 0x6636
cmpl $0x0, 0xc6cb(%rip)      # 0x12ce0
je 0x662d
movl -0x28(%rbp), %eax
cmpl 0xc6c0(%rip), %eax      # 0x12ce0
jae 0x662b
movl -0x28(%rbp), %eax
movl %eax, 0xc6b5(%rip)      # 0x12ce0
jmp 0x6636
movl -0x28(%rbp), %eax
movl %eax, 0xc6aa(%rip)      # 0x12ce0
addq $0x90, %rsp
popq %rbp
retq
nop