// File: /workspace/github_cpp_makefile_star2_O1/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 0xa514
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 0xb3e5
movq %r14, %rdi
movq %r15, %rsi
callq 0xb4c8
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
cmpq %rsi, %rdi
je 0xa52a
leaq 0x58(%rbx), %rdx
callq 0xb738
movq 0x40(%rbx), %rax
cmpq %rax, 0x48(%rbx)
je 0xa538
movq %rax, 0x48(%rbx)
movq 0x58(%rbx), %r12
movq 0x60(%rbx), %r13
cmpq %r13, %r12
je 0xa587
leaq 0x8(%rbx), %r14
leaq 0x10(%rsp), %r15
movl (%r12), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xb7b8
movq (%rax), %rdi
movl 0x10(%rsp), %esi
movq (%rdi), %rax
callq *0x18(%rax)
testb %al, %al
jne 0xa57e
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0xa718
addq $0x4, %r12
cmpq %r13, %r12
jne 0xa54e
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0xa599
movq %rax, 0x60(%rbx)
jmp 0xa599
pause
movb $0x1, %al
xchgb %al, 0x70(%rbx)
testb %al, %al
jne 0xa597
movups 0x90(%rbx), %xmm0
movq 0xa0(%rbx), %rax
movq 0x78(%rbx), %rcx
movq %rcx, 0x90(%rbx)
movups 0x80(%rbx), %xmm1
movups %xmm1, 0x98(%rbx)
movups %xmm0, 0x78(%rbx)
movq %rax, 0x88(%rbx)
movb $0x0, 0x70(%rbx)
movq 0x90(%rbx), %r14
movq 0x98(%rbx), %r15
cmpq %r15, %r14
je 0xa5fc
movl (%r14), %esi
movq %rbx, %rdi
callq 0xa718
addq $0x4, %r14
jmp 0xa5e6
movq 0x90(%rbx), %rax
cmpq %rax, 0x98(%rbx)
je 0xa613
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 0xa6ed
testl %eax, %eax
jle 0xa6f9
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 0xb7b8
movq (%rax), %r12
movl 0x10(%rsp,%rbp), %eax
testb $0x8, %al
jne 0xa6c0
testb $0x10, %al
je 0xa690
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x28(%rax)
testb %al, %al
je 0xa6d2
testb $0x1, 0x10(%rsp,%rbp)
je 0xa6a9
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
testb %al, %al
je 0xa6d2
testb $0x4, 0x10(%rsp,%rbp)
je 0xa6de
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x20(%rax)
jmp 0xa6ce
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x30(%rax)
testb %al, %al
jne 0xa6de
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xa718
addq $0xc, %rbp
cmpq %rbp, %r13
jne 0xa65c
jmp 0xa6f9
callq 0x7040
movl (%rax), %edi
cmpl $0x4, %edi
jne 0xa70b
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29c41(%rip), %rsi     # 0x34353
callq 0x8dd3
nop