// File: /workspace/github_c_makefile_star2_O3/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 0x24cb0
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
testl %r14d, %r14d
je 0x24c9a
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 0x129e0
movl %r14d, 0x340(%rbx)
leaq -0x3a0(%rbp), %r14
movl 0x338(%rbx), %edi
movq %r14, %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x12890
cmpl $-0x1, %eax
jne 0x24cea
callq 0x12120
cmpl $0x4, (%rax)
je 0x24cbe
callq 0x13560
testl %eax, %eax
jle 0x24ea2
leaq 0x33c(%rbx), %r13
leaq 0x330(%rbx), %r12
addq $0x264, %rbx            # imm = 0x264
movq %rbx, -0x40(%rbp)
movl %eax, %eax
movq %rax, -0x50(%rbp)
xorl %ebx, %ebx
leaq -0x98(%rbp), %r14
movq %r13, -0x48(%rbp)
leaq (%rbx,%rbx,2), %rax
movq -0x39c(%rbp,%rax,4), %r15
cmpq %r15, %r13
je 0x24e95
cmpq %r15, %r12
je 0x24e41
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 0x24dc0
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x24d82
movl %edx, %ecx
andl $0x1, %ecx
je 0x24d82
movq %r13, %rdi
movl $0x6, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x24da3
movl %edx, %ecx
andl $0x8, %ecx
je 0x24da3
movq %r13, %rdi
movl $0x4, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
movq 0x50(%r13), %rax
testq %rax, %rax
je 0x24dc0
movl %edx, %ecx
andl $0x10, %ecx
je 0x24dc0
movq %r13, %rdi
movl $0x5, %esi
callq *%rax
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
movq -0x48(%rbp), %r13
je 0x24e22
movq 0x50(%rdi), %rax
testq %rax, %rax
je 0x24def
movl %edx, %ecx
andl $0x4, %ecx
je 0x24def
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 0x24e11
movl %edx, %ecx
andl $0x8, %ecx
je 0x24e11
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 0x24e22
andl $0x10, %edx
je 0x24e22
movl $0x5, %esi
callq *%rax
testb $0x1, 0x6(%r15)
je 0x24e95
cmpq $0x0, 0x10(%r15)
jne 0x24e95
cmpq $0x0, 0x18(%r15)
jne 0x24e95
movq %r15, %rdi
callq 0x2261b
jmp 0x24e95
movq -0x40(%rbp), %r15
movl (%r12), %edi
movl $0x28, %edx
movq %r14, %rsi
callq 0x12520
cmpl $-0x1, %eax
je 0x24e8b
testl %eax, %eax
jle 0x24e95
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x24e45
movups -0x98(%rbp), %xmm0
movups -0x88(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq *%rax
movq %r15, %rdi
callq 0x1a158
jmp 0x24e45
callq 0x12120
cmpl $0x4, (%rax)
je 0x24e45
incq %rbx
cmpq -0x50(%rbp), %rbx
jne 0x24d1e
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq