// File: /workspace/github_c_makefile_star2_O3/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 0x75ed(%rip), %rdi      # 0xa47a
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6c74
movl 0xc310(%rip), %edi      # 0xf1b0
cmpb $0x0, 0xc411(%rip)      # 0xf2b8
movq 0xc412(%rip), %rsi      # 0xf2c0
movl $0x2710, %edx           # imm = 0x2710
cmovel %r14d, %edx
movl %ebx, %ecx
callq 0x2120
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x2ece
callq 0x2030
movl (%rax), %r14d
testb $0x1, %r15b
je 0x2ed9
callq 0x89f9
testl %r14d, %r14d
je 0x2eef
cmpl $0x4, %r14d
jne 0x30d7
xorl %eax, %eax
jmp 0x30ef
testl %r12d, %r12d
je 0x30fe
jle 0x2ee8
movslq %r12d, %r14
movl $0x4, %ebx
movq 0xc3b7(%rip), %rax      # 0xf2c0
movq (%rax,%rbx), %rcx
movq %rcx, %r12
andq $-0x2, %r12
movl 0x18(%r12), %esi
cmpl $-0x1, %esi
je 0x30a8
movl %ecx, %r13d
andl $0x1, %r13d
movq 0x8(%r12), %rdi
btl $0x2, 0x8(%rdi)
movl %r13d, %edx
sbbl $0x0, %edx
jne 0x30a8
movq %rdi, -0x30(%rbp)
movl -0x4(%rax,%rbx), %r15d
leaq 0x7595(%rip), %rdi      # 0xa4e4
movl %r15d, %edx
xorl %eax, %eax
callq 0x6c74
testb $0x18, %r15b
je 0x2f79
movl 0x18(%r12), %esi
leaq 0x759c(%rip), %rdi      # 0xa507
movl %r15d, %edx
xorl %eax, %eax
callq 0x6c74
orl $0x5, %r15d
testb $0x1, %r15b
movq -0x30(%rbp), %rdx
je 0x302e
movl 0x8(%rdx), %eax
testb $0x8, %al
je 0x302e
btl $0xd, %r15d
jae 0x2fba
movl 0x18(%r12), %esi
leaq 0x7587(%rip), %rdi      # 0xa52c
xorl %eax, %eax
callq 0x6c74
movq -0x30(%rbp), %rdx
movl 0x8(%rdx), %eax
movl $0x4000, %ecx           # imm = 0x4000
orl %ecx, %eax
orl $0x100000, %eax         # imm = 0x100000
movl %eax, 0x8(%rdx)
movl 0x18(%r12), %esi
leaq 0x7572(%rip), %rdi      # 0xa540
xorl %eax, %eax
callq 0x6c74
movq -0x30(%rbp), %rsi
movl 0x8(%rsi), %eax
movl %eax, %ecx
orl $0x20, %ecx
movl %ecx, 0x8(%rsi)
leaq 0x757e(%rip), %rdi      # 0xa569
testw %ax, %ax
js 0x3027
leaq 0xc2d1(%rip), %rcx      # 0xf2c8
testb $0x2, %al
jne 0x3002
leaq 0xc2d6(%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 0x752d(%rip), %rdi      # 0xa554
xorl %eax, %eax
callq 0x6c74
testb $0x4, %r15b
je 0x30b9
movq 0x10(%r12), %r15
movl 0x8(%r15), %eax
testb $0x8, %al
je 0x30b9
movl 0x18(%r12), %esi
cmpl $-0x1, %esi
je 0x30a8
btl $0x2, %eax
sbbl $0x0, %r13d
jne 0x30a8
leaq 0x7520(%rip), %rdi      # 0xa580
xorl %eax, %eax
callq 0x6c74
movl 0x8(%r15), %eax
movl %eax, %ecx
orl $0x20, %ecx
movl %ecx, 0x8(%r15)
testw %ax, %ax
js 0x30cb
orl $0x8020, %eax           # imm = 0x8020
movl %eax, 0x8(%r15)
leaq 0xc24f(%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 0x74ae(%rip), %rdi      # 0xa554
jmp 0x30d2
leaq 0x741f(%rip), %rdi      # 0xa4ce
movq %r12, %rsi
xorl %eax, %eax
callq 0x6c74
addq $0xc, %rbx
decq %r14
jne 0x2f02
jmp 0x2ee8
leaq 0x7497(%rip), %rdi      # 0xa569
movq %r15, %rsi
jmp 0x30b2
leaq 0x73ac(%rip), %rsi      # 0xa48a
movl %r14d, %edi
xorl %eax, %eax
callq 0x6b27
movq $-0x1, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $-0x1, %rbx
jne 0x2ee8
leaq 0x738f(%rip), %rsi      # 0xa49e
xorl %edi, %edi
jmp 0x30e1
nop