// File: /workspace/github_c_makefile_star2_O1/qq-smart[P]qq_sh_server/event/qq_epoll.c
// Function: qq_epoll_process_events
qq_int_t
qq_epoll_process_events(qq_msec_t timer, qq_uint_t flags)
{
    int                events;
    uint32_t           revents;
    qq_int_t           instance, i;
    qq_err_t           err;
    qq_event_t        *rev, *wev;
    qq_connection_t   *c;
    qq_queue_t        *queue;

    qq_log_debug("epoll timer: %d", timer);

    events = epoll_wait(ep, event_list, (int) nevents, timer);

    err = (events == -1) ? errno : 0;

    if (flags & QQ_UPDATE_TIME) {
        qq_time_update();
    }

    if (err) {
        if (err == QQ_EINTR) {
            return QQ_OK;
        }

        qq_log_error(err, "epoll_wait() failed");
        return QQ_ERROR;
    }

    if (events == 0) {
        if (timer != QQ_TIMER_INFINITE) {
            return QQ_OK;
        }

        qq_log_error(err, "epoll_wait() returned no events without timeout");
        return QQ_ERROR;
    }

    for (i = 0; i < events; i++) {
        c = event_list[i].data.ptr;

        instance = (uintptr_t) c & 1;
        c = (qq_connection_t *) ((uintptr_t) c & (uintptr_t) ~1);

        rev = c->read;

        if (c->fd == -1 || rev->instance != instance) {
            qq_log_debug("epoll: stale event %p", c);
            continue;
        }

        revents = event_list[i].events;

        qq_log_debug("epoll process: fd:%d ev:%04XD d:%p", c->fd, revents, event_list[i].data.ptr);

        if (revents & (EPOLLERR|EPOLLHUP)) {
            qq_log_debug("epoll_wait() error on fd:%d ev:%04XD", c->fd, revents);
            revents |= EPOLLIN|EPOLLOUT;
        }

        if ((revents & EPOLLIN) && rev->active) {
#if (QQ_HAVE_EPOLLRDHUP)
            if (revents & EPOLLRDHUP) {
                qq_log_debug("epollrdhup on fd:%d", c->fd);
                rev->pending_eof = 1;
            }
            rev->available = 1;
#endif

            qq_log_debug("epollin event fd:%d", c->fd);
            rev->ready = 1;

            queue = rev->accept ? &qq_posted_accept_events
                                : &qq_posted_events;
            qq_post_event(rev, queue);
        }

        wev = c->write;
        if ((revents & EPOLLOUT) && wev->active) {
            if (c->fd == -1 || wev->instance != instance) {
                qq_log_debug("epoll: stale event %p", c);
                continue;
            }

            qq_log_debug("epollout event fd:%d", c->fd);
            wev->ready = 1;
            qq_post_event(wev, &qq_posted_events);
        }
    }

    return QQ_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x75d9(%rip), %rdi      # 0xa47a
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6daa
movl 0xc2fc(%rip), %edi      # 0xf1b0
cmpb $0x0, 0xc3fd(%rip)      # 0xf2b8
movq 0xc3fe(%rip), %rsi      # 0xf2c0
movl $0x2710, %edx           # imm = 0x2710
cmovel %r14d, %edx
movl %ebx, %ecx
callq 0x2120
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x2ee2
callq 0x2030
movl (%rax), %r14d
testb $0x1, %r15b
je 0x2eed
callq 0x8b85
testl %r14d, %r14d
je 0x2f03
cmpl $0x4, %r14d
jne 0x30e0
xorl %eax, %eax
jmp 0x30f8
testl %r12d, %r12d
je 0x3107
jle 0x2efc
movslq %r12d, %r14
movl $0x4, %ebx
movq 0xc3a3(%rip), %rax      # 0xf2c0
movq (%rax,%rbx), %rcx
movq %rcx, %r12
andq $-0x2, %r12
movl 0x18(%r12), %esi
cmpl $-0x1, %esi
je 0x30b1
movl %ecx, %r13d
andl $0x1, %r13d
movq 0x8(%r12), %rdi
btl $0x2, 0x8(%rdi)
movl %r13d, %edx
sbbl $0x0, %edx
jne 0x30b1
movq %rdi, -0x30(%rbp)
movl -0x4(%rax,%rbx), %r15d
leaq 0x7581(%rip), %rdi      # 0xa4e4
movl %r15d, %edx
xorl %eax, %eax
callq 0x6daa
testb $0x18, %r15b
je 0x2f8d
movl 0x18(%r12), %esi
leaq 0x7588(%rip), %rdi      # 0xa507
movl %r15d, %edx
xorl %eax, %eax
callq 0x6daa
orl $0x5, %r15d
testb $0x1, %r15b
movq -0x30(%rbp), %rax
je 0x3037
testb $0x8, 0x8(%rax)
je 0x3037
btl $0xd, %r15d
jae 0x2fc7
movl 0x18(%r12), %esi
leaq 0x7574(%rip), %rdi      # 0xa52c
xorl %eax, %eax
callq 0x6daa
movq -0x30(%rbp), %rax
orb $0x40, 0x9(%rax)
orb $0x10, 0xa(%rax)
movl 0x18(%r12), %esi
leaq 0x7569(%rip), %rdi      # 0xa540
xorl %eax, %eax
callq 0x6daa
movq -0x30(%rbp), %rsi
movl 0x8(%rsi), %eax
movl %eax, %ecx
orl $0x20, %ecx
movl %ecx, 0x8(%rsi)
leaq 0x7575(%rip), %rdi      # 0xa569
testw %ax, %ax
js 0x3030
leaq 0xc2c8(%rip), %rcx      # 0xf2c8
testb $0x2, %al
jne 0x300b
leaq 0xc2cd(%rip), %rcx      # 0xf2d8
orl $0x8020, %eax           # imm = 0x8020
movl %eax, 0x8(%rsi)
movq (%rcx), %rax
leaq 0x48(%rsi), %rdx
movq %rax, 0x48(%rsi)
movq %rdx, 0x8(%rax)
movq %rcx, 0x50(%rsi)
movq %rdx, (%rcx)
leaq 0x7524(%rip), %rdi      # 0xa554
xorl %eax, %eax
callq 0x6daa
testb $0x4, %r15b
je 0x30c2
movq 0x10(%r12), %r15
movl 0x8(%r15), %eax
testb $0x8, %al
je 0x30c2
movl 0x18(%r12), %esi
cmpl $-0x1, %esi
je 0x30b1
btl $0x2, %eax
sbbl $0x0, %r13d
jne 0x30b1
leaq 0x7517(%rip), %rdi      # 0xa580
xorl %eax, %eax
callq 0x6daa
movl 0x8(%r15), %eax
movl %eax, %ecx
orl $0x20, %ecx
movl %ecx, 0x8(%r15)
testw %ax, %ax
js 0x30d4
orl $0x8020, %eax           # imm = 0x8020
movl %eax, 0x8(%r15)
leaq 0xc246(%rip), %rdx      # 0xf2d8
movq (%rdx), %rax
leaq 0x48(%r15), %rcx
movq %rax, 0x48(%r15)
movq %rcx, 0x8(%rax)
movq %rdx, 0x50(%r15)
movq %rcx, (%rdx)
leaq 0x74a5(%rip), %rdi      # 0xa554
jmp 0x30db
leaq 0x7416(%rip), %rdi      # 0xa4ce
movq %r12, %rsi
xorl %eax, %eax
callq 0x6daa
addq $0xc, %rbx
decq %r14
jne 0x2f16
jmp 0x2efc
leaq 0x748e(%rip), %rdi      # 0xa569
movq %r15, %rsi
jmp 0x30bb
leaq 0x73a3(%rip), %rsi      # 0xa48a
movl %r14d, %edi
xorl %eax, %eax
callq 0x6c5d
movq $-0x1, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $-0x1, %rbx
jne 0x2efc
leaq 0x7386(%rip), %rsi      # 0xa49e
xorl %edi, %edi
jmp 0x30ea