// File: /workspace/github_c_makefile_star2_O0/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
subq $0x370, %rsp            # imm = 0x370
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
cmpl $0x0, 0x340(%rax)
jne 0x2e791
cmpl $0x0, -0x4(%rbp)
je 0x2e7fa
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x12420
cmpl $0x0, -0x4(%rbp)
je 0x2e7d2
movq -0x10(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
movq %rax, -0x18(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movl 0x33c(%rax), %edi
movl $0x1, %esi
leaq -0x30(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x129f0
movl -0x4(%rbp), %eax
movl %eax, -0x36c(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movl -0x36c(%rbp), %ecx
movl %ecx, 0x340(%rax)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movl 0x338(%rax), %edi
leaq -0x330(%rbp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x128a0
movl %eax, -0x334(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0x334(%rbp)
movb %al, -0x36d(%rbp)
jne 0x2e873
callq 0x12120
cmpl $0x4, (%rax)
sete %al
movb %al, -0x36d(%rbp)
movb -0x36d(%rbp), %al
testb $0x1, %al
jne 0x2e81f
cmpl $-0x1, -0x334(%rbp)
jne 0x2e8b8
jmp 0x2e888
movq 0x81761(%rip), %rax     # 0xafff0
movq (%rax), %rdi
leaq 0x5277b(%rip), %rsi     # 0x81014
movl $0x6da, %edx            # imm = 0x6DA
leaq 0x58fd3(%rip), %rcx     # 0x87878
leaq 0x590b6(%rip), %r8      # 0x87962
movb $0x0, %al
callq 0x125f0
callq 0x12100
movl $0x0, -0x338(%rbp)
movl -0x338(%rbp), %eax
cmpl -0x334(%rbp), %eax
jge 0x2eb1c
movslq -0x338(%rbp), %rcx
leaq -0x330(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq 0x4(%rax), %rax
movq %rax, -0x340(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
addq $0x33c, %rax            # imm = 0x33C
cmpq -0x340(%rbp), %rax
jne 0x2e918
jmp 0x2eb06
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
addq $0x330, %rax            # imm = 0x330
cmpq -0x340(%rbp), %rax
jne 0x2e941
callq 0x2ed60
jmp 0x2eb04
movq -0x340(%rbp), %rax
movq %rax, -0x348(%rbp)
movslq -0x338(%rbp), %rcx
leaq -0x330(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x34c(%rbp)
movl -0x34c(%rbp), %eax
andl $0x8, %eax
movl %eax, -0x350(%rbp)
movl -0x34c(%rbp), %eax
andl $0x10, %eax
movl %eax, -0x354(%rbp)
movq -0x348(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x360(%rbp)
movq -0x348(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x368(%rbp)
cmpq $0x0, -0x360(%rbp)
je 0x2ea53
movq -0x360(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2e9f1
movl -0x34c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2e9f1
movq -0x360(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x360(%rbp), %rdi
movl $0x6, %esi
callq *%rax
movq -0x360(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2ea21
cmpl $0x0, -0x350(%rbp)
je 0x2ea21
movq -0x360(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x360(%rbp), %rdi
movl $0x4, %esi
callq *%rax
movq -0x360(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2ea51
cmpl $0x0, -0x354(%rbp)
je 0x2ea51
movq -0x360(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x360(%rbp), %rdi
movl $0x5, %esi
callq *%rax
jmp 0x2ea53
cmpq $0x0, -0x368(%rbp)
je 0x2eaf8
movq -0x368(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2ea96
movl -0x34c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x2ea96
movq -0x368(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x368(%rbp), %rdi
movl $0x7, %esi
callq *%rax
movq -0x368(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2eac6
cmpl $0x0, -0x350(%rbp)
je 0x2eac6
movq -0x368(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x368(%rbp), %rdi
movl $0x4, %esi
callq *%rax
movq -0x368(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2eaf6
cmpl $0x0, -0x354(%rbp)
je 0x2eaf6
movq -0x368(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x368(%rbp), %rdi
movl $0x5, %esi
callq *%rax
jmp 0x2eaf8
movq -0x348(%rbp), %rdi
callq 0x2ee00
jmp 0x2eb06
jmp 0x2eb08
movl -0x338(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x338(%rbp)
jmp 0x2e8c2
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
nopw %cs:(%rax,%rax)