// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
callq 0x22ef2
movq %rax, -0x38(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq %rax, %rbx
movq 0x220(%rax), %rax
testq %rax, %rax
movq %rbx, -0x40(%rbp)
je 0x248b4
movq 0x248(%rbx), %rcx
movq -0x38(%rbp), %rdx
cmpq %rdx, (%rcx)
jg 0x248b4
movq 0x8(%rcx), %r15
movq 0x10(%rcx), %r14
movl 0x18(%rcx), %r12d
movl 0x1c(%rcx), %ebx
movl 0x20(%rcx), %eax
movl %eax, -0x30(%rbp)
movq 0x28(%rcx), %r13
callq 0x22c84
testq %r14, %r14
je 0x2483e
movq %r14, %rdi
callq 0x2a1ca
testl %eax, %eax
je 0x24878
leaq 0x360fd(%rip), %rdi     # 0x5a939
jmp 0x2484f
cmpl %r12d, 0x48(%r15)
jne 0x24878
testl %ebx, %ebx
je 0x2489b
leaq 0x360fb(%rip), %rdi     # 0x5a94a
callq 0x4482c
movq %rax, %rdi
movabsq $-0x6000000000000, %rsi # imm = 0xFFFA000000000000
callq 0x4ed39
movq %r15, %rdi
movq %rax, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x2283b
movl -0x30(%rbp), %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x22c3f
movq -0x40(%rbp), %rbx
movq 0x220(%rbx), %rax
testq %rax, %rax
jne 0x247f2
jmp 0x248b4
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
jmp 0x24871
movl 0x234(%rbx), %ecx
cmpl 0x238(%rbx), %ecx
je 0x24b16
leaq 0x1c(%rbx), %rax
movq %rax, -0x30(%rbp)
leaq 0x264(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq 0x298(%rbx), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1a171
testl %eax, %eax
jne 0x24b0f
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %rax, %r14
movslq 0x234(%rbx), %rax
cmpl 0x238(%rbx), %eax
jne 0x24921
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x24955
movq 0x240(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %r12
movq 0x8(%rcx,%rdx,8), %r14
movl 0x10(%rcx,%rdx,8), %r15d
incl %eax
cmpl 0x230(%rbx), %eax
movl 0x14(%rcx,%rdx,8), %r13d
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, 0x234(%rbx)
movl (%r12), %eax
btl $0x11, %eax
jae 0x2496c
movq -0x38(%rbp), %rdi
callq 0x1a158
movl (%r12), %eax
andl $0xfffcffff, %eax       # imm = 0xFFFCFFFF
movl %eax, (%r12)
cmpl 0x48(%r12), %r13d
jne 0x24afd
movq %r12, %rdi
movq %r14, %rsi
leaq -0x50(%rbp), %rdx
movl %r15d, %ecx
callq 0x4e562
movl %eax, %r15d
movq %r12, %rdi
callq 0x2a1ca
testl %eax, %eax
jne 0x249bf
movq %r12, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4ed7e
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x47664
movq 0x68(%r12), %r14
cmpl $0xd, %r15d
ja 0x24a34
movl $0x3008, %eax           # imm = 0x3008
btl %r15d, %eax
jae 0x24a34
orb $0x2, 0x2(%r12)
movq -0x38(%rbp), %rdi
callq 0x1a147
testq %r14, %r14
je 0x24af3
movl 0x10(%r12), %eax
btl %r15d, %eax
jae 0x24af3
movl %r15d, %eax
leaq 0x5fa8a(%rip), %rcx     # 0x84490
movq (%rcx,%rax,8), %rdi
movl 0x50(%r14), %ebx
callq 0x47261
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x4ed39
movq %rax, -0x70(%rbp)
testl %ebx, %ebx
je 0x24a61
movq 0x40(%r12), %rax
jmp 0x24a73
testq %r14, %r14
je 0x24a49
testl %r15d, %r15d
je 0x249fc
movl 0x10(%r12), %eax
btl %r15d, %eax
jb 0x249fc
movq -0x50(%rbp), %rsi
movq %r12, %rdi
leaq 0x47686(%rip), %rdx     # 0x6c0dd
callq 0x20821
jmp 0x24afd
movq %r12, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4ed7e
movq %rax, -0x68(%rbp)
movq 0x28(%r12), %r13
testq %r13, %r13
movq -0x40(%rbp), %rbx
je 0x24ab0
leaq 0x36285(%rip), %rdi     # 0x5ad11
callq 0x47261
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x4ed39
movq %r13, %rdi
movq %rax, %rsi
callq 0x474e0
jmp 0x24abf
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %rax, -0x60(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x3, %esi
callq 0x47fef
movq %rax, %rdi
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
callq 0x4ed39
movq %r14, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x23191
cmpl $0xc, %r15d
je 0x24c09
movl 0x234(%rbx), %eax
cmpl 0x238(%rbx), %eax
jne 0x248e4
movq 0x220(%rbx), %rax
testq %rax, %rax
je 0x24bbd
leaq 0x298(%rbx), %rax
movq %rax, -0x30(%rbp)
movq 0x248(%rbx), %rax
movq (%rax), %r14
movq 0x10(%rax), %r15
movl 0x20(%rax), %r12d
movq 0x28(%rax), %r13
testq %r15, %r15
je 0x24b7a
movq %r15, %rdi
callq 0x2a1ca
testl %eax, %eax
jne 0x24be3
callq 0x22c84
movq %r15, %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x4ed7e
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x47664
jmp 0x24b8b
movq 0x8(%rax), %rcx
movl 0x48(%rcx), %ecx
cmpl 0x18(%rax), %ecx
je 0x24bf5
callq 0x22c84
movl %r12d, %edi
movq %r13, %rsi
movl $0x1, %edx
callq 0x22c3f
cmpq $0x0, 0x220(%rbx)
jne 0x24b2a
xorl %r12d, %r12d
addq $0x264, %rbx            # imm = 0x264
movq %rbx, %rdi
callq 0x1a169
testl %eax, %eax
jne 0x24bfb
jmp 0x24c06
leaq 0x264(%rbx), %rdi
callq 0x1a169
xorl %r12d, %r12d
testl %eax, %eax
je 0x24c09
cmpq $0x0, 0x220(%rbx)
jne 0x24b1f
xorl %r14d, %r14d
jmp 0x24ba8
movl $0x1, %r12d
cmpq $0x0, 0x220(%rbx)
jne 0x24bfb
jmp 0x24ba8
movl $0x1, %r12d
movl %r12d, %edi
movq %r14, %rsi
callq 0x24c1b
xorl %r12d, %r12d
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq