// File: /workspace/github_cpp_makefile_star2_O3/cybozu[P]yrmcds/cybozu/reactor.cpp
// Function: cybozu::reactor::poll()
void reactor::poll() {
    // process readable resources
    std::sort(m_readables.begin(), m_readables.end());
    std::unique_copy(m_readables.begin(), m_readables.end(),
                     std::back_inserter(m_readables_copy));
    m_readables.clear();
    for( int fd: m_readables_copy ) {
        if( ! m_resources[fd]->on_readable(fd) )
            remove_resource(fd);
    }
    m_readables_copy.clear();

    // process drop requests
    {
        lock_guard g(m_lock);
        m_drop_req_copy.swap(m_drop_req);
    }
    for( int fd: m_drop_req_copy )
        remove_resource(fd);
    m_drop_req_copy.clear();

    struct epoll_event events[EPOLL_SIZE];
    int timeout = m_readables.empty() ? POLLING_TIMEOUT : 0;
    int n = epoll_wait(m_fd, events, EPOLL_SIZE, timeout);
    if( n == -1 ) {
        if( errno == EINTR ) return;
        throw_unix_error(errno, "epoll_wait");
    }

    for( int i = 0; i < n; ++i ) {
        const struct epoll_event& ev = events[i];
        const int fd = ev.data.fd;
        resource& r = *(m_resources[fd]);
        if( ev.events & EPOLLERR ) {
            if( ! r.on_error(fd) )
                remove_resource(fd);
            continue;
        }
        if( ev.events & EPOLLHUP ) {
            if( ! r.on_hangup(fd) ) {
                remove_resource(fd);
                continue;
            }
        }
        if( ev.events & EPOLLIN ) {
            if( ! r.on_readable(fd) ) {
                remove_resource(fd);
                continue;
            }
        }
        if( ev.events & EPOLLOUT ) {
            if( ! r.on_writable(fd) )
                remove_resource(fd);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movq %rdi, %rbx
movq 0x40(%rdi), %r14
movq 0x48(%rdi), %r15
cmpq %r15, %r14
je 0xa4f8
movq %r15, %rax
subq %r14, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xb38c
movq %r14, %rdi
movq %r15, %rsi
callq 0xb46f
movq 0x40(%rbx), %r14
movq 0x48(%rbx), %r15
cmpq %r15, %r14
je 0xa514
leaq 0x58(%rbx), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xb6bb
movq 0x40(%rbx), %r14
movq 0x48(%rbx), %r15
cmpq %r14, %r15
je 0xa51d
movq %r14, 0x48(%rbx)
movq 0x58(%rbx), %r12
movq 0x60(%rbx), %r13
cmpq %r13, %r12
je 0xa57e
leaq 0x8(%rbx), %r14
leaq 0x10(%rsp), %r15
movl (%r12), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xb73c
movq (%rax), %rdi
movl 0x10(%rsp), %esi
movq (%rdi), %rax
callq *0x18(%rax)
testb %al, %al
jne 0xa563
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0xa6fe
addq $0x4, %r12
cmpq %r13, %r12
jne 0xa533
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0xa57e
movq %rax, 0x60(%rbx)
jmp 0xa57e
pause
movb $0x1, %al
xchgb %al, 0x70(%rbx)
testb %al, %al
jne 0xa57c
movq 0x78(%rbx), %r14
movq 0x80(%rbx), %r15
movups 0x80(%rbx), %xmm0
movups 0x90(%rbx), %xmm1
movups %xmm1, 0x78(%rbx)
movq 0xa0(%rbx), %xmm1
movq %r14, %xmm2
punpcklqdq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0]
movups %xmm0, 0x98(%rbx)
movdqu %xmm1, 0x88(%rbx)
movb $0x0, 0x70(%rbx)
cmpq %r15, %r14
je 0xa5f8
movl (%r14), %esi
movq %rbx, %rdi
callq 0xa6fe
addq $0x4, %r14
cmpq %r15, %r14
jne 0xa5cd
movq 0x90(%rbx), %rax
cmpq %rax, 0x98(%rbx)
je 0xa5f8
movq %rax, 0x98(%rbx)
movq 0x40(%rbx), %rax
xorl %edx, %edx
cmpq 0x48(%rbx), %rax
movl $0x64, %ecx
cmovnel %edx, %ecx
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x80, %edx
callq 0x71e0
cmpl $-0x1, %eax
je 0xa6d4
testl %eax, %eax
jle 0xa6e0
leaq 0x8(%rbx), %r14
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r13
xorl %ebp, %ebp
leaq 0xc(%rsp), %r15
movl 0x14(%rsp,%rbp), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xb73c
movq (%rax), %r12
movl 0x10(%rsp,%rbp), %eax
testb $0x8, %al
jne 0xa6a7
testb $0x10, %al
je 0xa679
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x28(%rax)
testb %al, %al
je 0xa6b9
movl 0x10(%rsp,%rbp), %eax
testb $0x1, %al
je 0xa693
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
testb %al, %al
je 0xa6b9
movl 0x10(%rsp,%rbp), %eax
testb $0x4, %al
je 0xa6c5
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x20(%rax)
jmp 0xa6b5
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x30(%rax)
testb %al, %al
jne 0xa6c5
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xa6fe
addq $0xc, %rbp
cmpq %rbp, %r13
jne 0xa641
jmp 0xa6e0
callq 0x7040
movl (%rax), %edi
cmpl $0x4, %edi
jne 0xa6f2
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29c5a(%rip), %rsi     # 0x34353
callq 0x8d8d