// File: /workspace/github_c_makefile_star2_O1/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 $0x398, %rsp            # imm = 0x398
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 0x2510d
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
testl %r14d, %r14d
je 0x250f7
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %r15, %rax
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %rdx, -0x60(%rbp)
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
subq %rax, %r15
imulq $0xf4240, %r15, %rax    # imm = 0xF4240
movq %rax, -0x58(%rbp)
movl 0x33c(%rbx), %edi
leaq -0x70(%rbp), %rdx
movl $0x1, %esi
xorl %ecx, %ecx
callq 0x129f0
movl %r14d, 0x340(%rbx)
leaq -0x3a0(%rbp), %r14
movl 0x338(%rbx), %edi
movq %r14, %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x128a0
cmpl $-0x1, %eax
jne 0x25147
callq 0x12120
cmpl $0x4, (%rax)
je 0x2511b
callq 0x13570
testl %eax, %eax
jle 0x252f8
leaq 0x33c(%rbx), %r13
leaq 0x330(%rbx), %r12
addq $0x264, %rbx            # imm = 0x264
movl %eax, %eax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq %r13, -0x40(%rbp)
leaq (%r14,%r14,2), %rax
movq -0x39c(%rbp,%rax,4), %r15
cmpq %r15, %r13
je 0x252eb
cmpq %r15, %r12
je 0x252be
leaq -0x3a0(,%rax,4), %rax
addq %rbp, %rax
movl (%rax), %edx
movq 0x10(%r15), %r13
movq 0x18(%r15), %rdi
testq %r13, %r13
movq %rdi, -0x38(%rbp)
movl %edx, -0x2c(%rbp)
je 0x25213
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x251d1
movl %edx, %ecx
andl $0x1, %ecx
je 0x251d1
movq %r13, %rdi
movl $0x6, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x251f2
movl %edx, %ecx
andl $0x8, %ecx
je 0x251f2
movq %r13, %rdi
movl $0x4, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x25213
movl %edx, %ecx
andl $0x10, %ecx
je 0x25213
movq %r13, %rdi
movl $0x5, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
movq -0x40(%rbp), %r13
je 0x25275
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x2523e
movl %edx, %ecx
andl $0x4, %ecx
je 0x2523e
movq -0x38(%rbp), %rdi
movl $0x7, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x25260
movl %edx, %ecx
andl $0x8, %ecx
je 0x25260
movq -0x38(%rbp), %rdi
movl $0x4, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x25275
andl $0x10, %edx
je 0x25275
movl $0x5, %esi
callq *%rax
testb $0x1, 0x6(%r15)
je 0x252eb
cmpq $0x0, 0x10(%r15)
jne 0x252eb
cmpq $0x0, 0x18(%r15)
jne 0x252eb
movq %r15, %rdi
callq 0x2297f
jmp 0x252eb
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x252be
movups -0x98(%rbp), %xmm0
movups -0x88(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq *%rax
movq %rbx, %rdi
callq 0x1a24d
movl (%r12), %edi
movl $0x28, %edx
leaq -0x98(%rbp), %rsi
callq 0x12520
movq %rax, %r15
cmpl $-0x1, %r15d
jne 0x252e6
callq 0x12120
cmpl $0x4, (%rax)
je 0x252be
testl %r15d, %r15d
jg 0x25294
incq %r14
cmpq -0x48(%rbp), %r14
jne 0x25171
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq