// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/ev.c
// Function: janet_loop1
JanetFiber *janet_loop1(void) {
    /* Schedule expired timers */
    JanetTimeout to;
    JanetTimestamp now = ts_now();
    while (peek_timeout(&to) && to.when <= now) {
        pop_timeout(0);
        if (to.curr_fiber != NULL) {
            if (janet_fiber_can_resume(to.curr_fiber)) {
                janet_cancel(to.fiber, janet_cstringv("deadline expired"));
            }
        } else {
            /* This is a timeout (for a function call, not a whole fiber) */
            if (to.fiber->sched_id == to.sched_id) {
                if (to.is_error) {
                    janet_cancel(to.fiber, janet_cstringv("timeout"));
                } else {
                    janet_schedule(to.fiber, janet_wrap_nil());
                }
            }
        }
        handle_timeout_worker(to, 0);
    }

    /* Run scheduled fibers unless interrupts need to be handled. */
    while (janet_vm.spawn.head != janet_vm.spawn.tail) {
        /* Don't run until all interrupts have been marked as handled by calling janet_interpreter_interrupt_handled */
        if (janet_atomic_load_relaxed(&janet_vm.auto_suspend)) break;
        JanetTask task = {NULL, janet_wrap_nil(), JANET_SIGNAL_OK, 0};
        janet_q_pop(&janet_vm.spawn, &task, sizeof(task));
        if (task.fiber->gc.flags & JANET_FIBER_EV_FLAG_SUSPENDED) janet_ev_dec_refcount();
        task.fiber->gc.flags &= ~(JANET_FIBER_EV_FLAG_CANCELED | JANET_FIBER_EV_FLAG_SUSPENDED);
        if (task.expected_sched_id != task.fiber->sched_id) continue;
        Janet res;
        JanetSignal sig = janet_continue_signal(task.fiber, task.value, &res, task.sig);
        if (!janet_fiber_can_resume(task.fiber)) {
            janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(task.fiber));
        }
        void *sv = task.fiber->supervisor_channel;
        int is_suspended = sig == JANET_SIGNAL_EVENT || sig == JANET_SIGNAL_YIELD || sig == JANET_SIGNAL_INTERRUPT;
        if (is_suspended) {
            task.fiber->gc.flags |= JANET_FIBER_EV_FLAG_SUSPENDED;
            janet_ev_inc_refcount();
        }
        if (NULL == sv) {
            if (!is_suspended) {
                janet_stacktrace_ext(task.fiber, res, "");
            }
        } else if (sig == JANET_SIGNAL_OK || (task.fiber->flags & (1 << sig))) {
            JanetChannel *chan = janet_channel_unwrap(sv);
            janet_channel_push(chan, make_supervisor_event(janet_signal_names[sig],
                               task.fiber, chan->is_threaded), 2);
        } else if (!is_suspended) {
            janet_stacktrace_ext(task.fiber, res, "");
        }
        if (sig == JANET_SIGNAL_INTERRUPT) {
            return task.fiber;
        }
    }

    /* Poll for events */
    if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) {
        JanetTimeout to;
        memset(&to, 0, sizeof(to));
        int has_timeout;
        /* Drop timeouts that are no longer needed */
        while ((has_timeout = peek_timeout(&to))) {
            if (to.curr_fiber != NULL) {
                if (!janet_fiber_can_resume(to.curr_fiber)) {
                    pop_timeout(0);
                    janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(to.curr_fiber));
                    handle_timeout_worker(to, 1);
                    continue;
                }
            } else if (to.fiber->sched_id != to.sched_id) {
                pop_timeout(0);
                handle_timeout_worker(to, 1);
                continue;
            }
            break;
        }
        /* Run polling implementation only if pending timeouts or pending events */
        if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) {
            janet_loop1_impl(has_timeout, to.when);
        }
    }

    /* No fiber was interrupted */
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movabsq $-0x6000000000000, %r14 # imm = 0xFFFA000000000000
callq 0x20f0a
movq %rax, %r15
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq %rax, %rbx
leaq 0x342ea(%rip), %r13     # 0x56939
cmpq $0x0, 0x220(%rbx)
je 0x22741
movq 0x248(%rbx), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movaps %xmm0, -0xd0(%rbp)
movaps %xmm2, -0xb0(%rbp)
movaps %xmm1, -0xc0(%rbp)
cmpq %r15, -0xd0(%rbp)
jg 0x22741
callq 0x20cfe
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x226b7
callq 0x27d86
testl %eax, %eax
je 0x226f6
movq -0xc8(%rbp), %r12
movq %r13, %rdi
jmp 0x226db
movq -0xc8(%rbp), %r12
movl 0x48(%r12), %eax
cmpl -0xb8(%rbp), %eax
jne 0x226f6
cmpl $0x0, -0xb4(%rbp)
je 0x22725
leaq 0x3426f(%rip), %rdi     # 0x5694a
callq 0x411ea
movq %rax, %rdi
movq %r14, %rsi
callq 0x4b366
movq %r12, %rdi
movq %rax, %rsi
callq 0x209bb
movaps -0xd0(%rbp), %xmm0
movaps -0xc0(%rbp), %xmm1
movaps -0xb0(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
xorl %edi, %edi
callq 0x20c69
jmp 0x2264f
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4b313
movq %r12, %rdi
movq %rax, %rsi
callq 0x209ca
jmp 0x226f6
leaq 0x230(%rbx), %r12
leaq 0x1c(%rbx), %r13
leaq 0x298(%rbx), %rax
movq %rax, -0x40(%rbp)
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
movq %r12, -0x38(%rbp)
movq %r13, -0x30(%rbp)
movl 0x234(%rbx), %eax
cmpl 0x238(%rbx), %eax
je 0x22999
movq %r13, %rdi
callq 0x186d7
testl %eax, %eax
jne 0x22999
andq $0x0, -0x98(%rbp)
movq %r15, %rdi
callq 0x4b313
movq %rax, -0x90(%rbp)
andq $0x0, -0x88(%rbp)
movq %r12, %rdi
leaq -0x98(%rbp), %rsi
pushq $0x18
popq %rdx
callq 0x21fc7
movq -0x98(%rbp), %r14
movl (%r14), %eax
btl $0x11, %eax
jae 0x227d4
callq 0x2046b
movl (%r14), %eax
andl $0xfffcffff, %eax       # imm = 0xFFFCFFFF
movl %eax, (%r14)
movl -0x84(%rbp), %eax
cmpl 0x48(%r14), %eax
jne 0x22769
movl -0x88(%rbp), %ecx
movq -0x90(%rbp), %rsi
movq %r14, %rdi
leaq -0x48(%rbp), %rdx
callq 0x4ab8a
movl %eax, %r12d
movq %r14, %rdi
callq 0x27d86
testl %eax, %eax
jne 0x2282e
movq %r14, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4b3ab
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x43d98
movq 0x68(%r14), %r13
cmpl $0xd, %r12d
ja 0x2289a
movl $0x3008, %eax           # imm = 0x3008
btl %r12d, %eax
jae 0x2289a
orb $0x2, 0x2(%r14)
callq 0x2052f
movb $0x1, %al
testq %r13, %r13
je 0x22962
movl 0x10(%r14), %ecx
btl %r12d, %ecx
jae 0x228ac
movl %r12d, %eax
leaq 0x5cc24(%rip), %rcx     # 0x7f490
movq (%rcx,%rax,8), %rdi
movl 0x50(%r13), %r15d
callq 0x439c6
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x4b366
movq %rax, -0x80(%rbp)
testl %r15d, %r15d
je 0x228cc
movq 0x40(%r14), %rax
jmp 0x228de
testq %r13, %r13
je 0x22979
testl %r12d, %r12d
je 0x22862
xorl %eax, %eax
jmp 0x22858
testb %al, %al
jne 0x22962
movq -0x48(%rbp), %rsi
movq %r14, %rdi
leaq 0x4583b(%rip), %rdx     # 0x680fd
callq 0x1e979
jmp 0x22962
movq %r14, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4b3ab
movq %rax, -0x78(%rbp)
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x22920
leaq 0x3441f(%rip), %rdi     # 0x56d11
callq 0x439c6
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x4b366
movq %r15, %rdi
movq %rax, %rsi
callq 0x43c35
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
jmp 0x22932
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
movq %r15, %rdi
callq 0x4b313
movq %rax, -0x70(%rbp)
leaq -0x80(%rbp), %rdi
pushq $0x3
popq %rsi
callq 0x44692
movq %rax, %rdi
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
callq 0x4b366
movq %r13, %rdi
movq %rax, %rsi
pushq $0x2
popq %rdx
callq 0x21174
cmpl $0xc, %r12d
movq -0x38(%rbp), %r12
movq -0x30(%rbp), %r13
jne 0x22769
jmp 0x22a9d
movq -0x48(%rbp), %rsi
movq %r14, %rdi
leaq 0x45776(%rip), %rdx     # 0x680fd
callq 0x1e979
movq -0x38(%rbp), %r12
movq -0x30(%rbp), %r13
jmp 0x22769
cmpq $0x0, 0x220(%rbx)
jne 0x229b7
leaq 0x264(%rbx), %rdi
callq 0x186cf
testl %eax, %eax
je 0x22a9a
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
pushq $0x1
popq %r14
cmpq $0x0, 0x220(%rbx)
je 0x22a68
movq 0x248(%rbx), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movaps %xmm1, -0x70(%rbp)
movaps %xmm2, -0x60(%rbp)
movaps %xmm0, -0x80(%rbp)
movq -0x70(%rbp), %r15
testq %r15, %r15
je 0x22a30
movq %r15, %rdi
callq 0x27d86
testl %eax, %eax
jne 0x22a80
callq 0x20cfe
movq %r15, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4b3ab
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x43d98
jmp 0x22a41
movq -0x78(%rbp), %rax
movl 0x48(%rax), %eax
cmpl -0x68(%rbp), %eax
je 0x22a8e
callq 0x20cfe
movaps -0x80(%rbp), %xmm0
movaps -0x70(%rbp), %xmm1
movaps -0x60(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl %r14d, %edi
callq 0x20c69
jmp 0x229ca
xorl %r14d, %r14d
addq $0x264, %rbx            # imm = 0x264
movq %rbx, %rdi
callq 0x186cf
testl %eax, %eax
jne 0x22a8e
jmp 0x22a9a
pushq $0x1
popq %r14
cmpq $0x0, 0x220(%rbx)
je 0x22a6b
movq -0x80(%rbp), %rsi
movl %r14d, %edi
callq 0x22ab2
xorl %r14d, %r14d
movq %r14, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq