// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/ev.c
// Function: janet_loop1_impl
void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
    struct itimerspec its;
    if (janet_vm.timer_enabled || has_timeout) {
        memset(&its, 0, sizeof(its));
        if (has_timeout) {
            its.it_value.tv_sec = timeout / 1000;
            its.it_value.tv_nsec = (timeout % 1000) * 1000000;
        }
        timerfd_settime(janet_vm.timerfd, TFD_TIMER_ABSTIME, &its, NULL);
    }
    janet_vm.timer_enabled = has_timeout;

    /* Poll for events */
    struct epoll_event events[JANET_EPOLL_MAX_EVENTS];
    int ready;
    do {
        ready = epoll_wait(janet_vm.epoll, events, JANET_EPOLL_MAX_EVENTS, -1);
    } while (ready == -1 && errno == EINTR);
    if (ready == -1) {
        JANET_EXIT("failed to poll events");
    }

    /* Step state machines */
    for (int i = 0; i < ready; i++) {
        void *p = events[i].data.ptr;
        if (&janet_vm.timerfd == p) {
            /* Timer expired, ignore */;
        } else if (janet_vm.selfpipe == p) {
            /* Self-pipe handling */
            janet_ev_handle_selfpipe();
        } else {
            JanetStream *stream = p;
            int mask = events[i].events;
            int has_err = mask & EPOLLERR;
            int has_hup = mask & EPOLLHUP;
            JanetFiber *rf = stream->read_fiber;
            JanetFiber *wf = stream->write_fiber;
            if (rf) {
                if (rf->ev_callback && (mask & EPOLLIN)) {
                    rf->ev_callback(rf, JANET_ASYNC_EVENT_READ);
                }
                if (rf->ev_callback && has_err) {
                    rf->ev_callback(rf, JANET_ASYNC_EVENT_ERR);
                }
                if (rf->ev_callback && has_hup) {
                    rf->ev_callback(rf, JANET_ASYNC_EVENT_HUP);
                }
            }
            if (wf) {
                if (wf->ev_callback && (mask & EPOLLOUT)) {
                    wf->ev_callback(wf, JANET_ASYNC_EVENT_WRITE);
                }
                if (wf->ev_callback && has_err) {
                    wf->ev_callback(wf, JANET_ASYNC_EVENT_ERR);
                }
                if (wf->ev_callback && has_hup) {
                    wf->ev_callback(wf, JANET_ASYNC_EVENT_HUP);
                }
            }
            janet_stream_checktoclose(stream);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movq %rsi, %r15
movl %edi, %r14d
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq %rax, %rbx
movl 0x340(%rax), %eax
orl %r14d, %eax
je 0x22b2a
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
testl %r14d, %r14d
je 0x22b16
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r15, %rax
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
movq %rax, -0x48(%rbp)
movl 0x33c(%rbx), %edi
pushq $0x1
popq %rsi
leaq -0x60(%rbp), %rdx
xorl %ecx, %ecx
callq 0x129e0
movl %r14d, 0x340(%rbx)
leaq -0x390(%rbp), %r15
pushq $0x40
popq %r12
pushq $-0x1
popq %r13
movl 0x338(%rbx), %edi
movq %r15, %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x12890
cmpl $-0x1, %eax
jne 0x22b93
callq 0x12120
cmpl $0x4, (%rax)
je 0x22b40
movq 0x5d486(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x2d49c(%rip), %rsi     # 0x50010
leaq 0x33ced(%rip), %rcx     # 0x56868
leaq 0x33dd0(%rip), %r8      # 0x56952
movl $0x6db, %edx            # imm = 0x6DB
xorl %eax, %eax
callq 0x125e0
callq 0x12100
movl %eax, %r14d
leaq 0x33c(%rbx), %r15
addq $0x330, %rbx            # imm = 0x330
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %r14d
movq %r15, -0x40(%rbp)
cmpq %r14, %r12
je 0x22d24
imulq $0xc, %r12, %rax
movq -0x38c(%rbp,%rax), %r13
cmpq %r13, %r15
je 0x22d1c
cmpq %r13, %rbx
je 0x22ccf
addq %rbp, %rax
addq $-0x390, %rax           # imm = 0xFC70
movl (%rax), %edx
movq 0x10(%r13), %r15
movq 0x18(%r13), %rdi
testq %r15, %r15
movq %rdi, -0x38(%rbp)
movl %edx, -0x2c(%rbp)
je 0x22c54
movq 0x50(%r15), %rax
testq %rax, %rax
je 0x22c1a
movl %edx, %ecx
andl $0x1, %ecx
je 0x22c1a
movq %r15, %rdi
pushq $0x6
popq %rsi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r15), %rax
testq %rax, %rax
je 0x22c39
movl %edx, %ecx
andl $0x8, %ecx
je 0x22c39
movq %r15, %rdi
pushq $0x4
popq %rsi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r15), %rax
testq %rax, %rax
je 0x22c54
movl %edx, %ecx
andl $0x10, %ecx
je 0x22c54
movq %r15, %rdi
pushq $0x5
popq %rsi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
movq -0x40(%rbp), %r15
je 0x22cb0
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x22c81
movl %edx, %ecx
andl $0x4, %ecx
je 0x22c81
movq -0x38(%rbp), %rdi
pushq $0x7
popq %rsi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x22ca1
movl %edx, %ecx
andl $0x8, %ecx
je 0x22ca1
movq -0x38(%rbp), %rdi
pushq $0x4
popq %rsi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x22cb0
andl $0x10, %edx
je 0x22cb0
pushq $0x5
popq %rsi
callq *%rax
testb $0x1, 0x6(%r13)
je 0x22d1c
cmpq $0x0, 0x10(%r13)
jne 0x22d1c
cmpq $0x0, 0x18(%r13)
jne 0x22d1c
movq %r13, %rdi
callq 0x2066b
jmp 0x22d1c
leaq -0x88(%rbp), %r13
movl (%rbx), %edi
movq %r13, %rsi
pushq $0x28
popq %rdx
callq 0x12520
cmpl $-0x1, %eax
je 0x22d12
testl %eax, %eax
jle 0x22d1c
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x22cd6
movups -0x88(%rbp), %xmm0
movups -0x78(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq *%rax
callq 0x2046b
jmp 0x22cd6
callq 0x12120
cmpl $0x4, (%rax)
je 0x22cd6
incq %r12
jmp 0x22bb1
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq