// File: /workspace/github_cpp_makefile_star2_O2/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), %rdi
movq 0x48(%rbx), %rsi
callq 0xaafe
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
leaq 0x58(%rbx), %rdx
callq 0x9f89
movq 0x40(%rbx), %rax
cmpq %rax, 0x48(%rbx)
je 0x9a78
movq %rax, 0x48(%rbx)
movq 0x58(%rbx), %r12
movq 0x60(%rbx), %r13
leaq 0x8(%rbx), %r14
leaq 0x10(%rsp), %r15
cmpq %r13, %r12
je 0x9ac4
movl (%r12), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf40
movq (%rax), %rdi
movl 0x10(%rsp), %esi
movq (%rdi), %rax
callq *0x18(%rax)
testb %al, %al
jne 0x9abe
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0x9c2a
addq $0x4, %r12
jmp 0x9a89
movq 0x58(%rbx), %rax
cmpq %rax, 0x60(%rbx)
je 0x9ad2
movq %rax, 0x60(%rbx)
leaq 0x70(%rbx), %rdi
callq 0xa22c
leaq 0x90(%rbx), %rdi
leaq 0x78(%rbx), %rsi
callq 0xb010
movb $0x0, 0x70(%rbx)
movq 0x90(%rbx), %r15
movq 0x98(%rbx), %r12
cmpq %r12, %r15
je 0x9b13
movl (%r15), %esi
movq %rbx, %rdi
callq 0x9c2a
addq $0x4, %r15
jmp 0x9afd
movq 0x90(%rbx), %rax
cmpq %rax, 0x98(%rbx)
je 0x9b2a
movq %rax, 0x98(%rbx)
movq 0x40(%rbx), %rax
xorl %r13d, %r13d
cmpq 0x48(%rbx), %rax
pushq $0x64
popq %rcx
cmovnel %r13d, %ecx
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x80, %edx
callq 0x7200
cmpl $-0x1, %eax
je 0x9bff
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0xc, %rax, %rbp
leaq 0xc(%rsp), %r15
cmpq %r13, %rbp
je 0x9c0b
movl 0x14(%rsp,%r13), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf40
movq (%rax), %r12
movl 0x10(%rsp,%r13), %eax
testb $0x8, %al
jne 0x9bd8
testb $0x10, %al
je 0x9ba9
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x28(%rax)
testb %al, %al
je 0x9bea
movl 0x10(%rsp,%r13), %eax
testb $0x1, %al
je 0x9bc4
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
testb %al, %al
je 0x9bea
movl 0x10(%rsp,%r13), %eax
testb $0x4, %al
je 0x9bf6
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x20(%rax)
jmp 0x9be6
movl 0xc(%rsp), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x30(%rax)
testb %al, %al
jne 0x9bf6
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x9c2a
addq $0xc, %r13
jmp 0x9b65
callq 0x7030
movl (%rax), %edi
cmpl $0x4, %edi
jne 0x9c1d
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c74f(%rip), %rsi     # 0x26373
callq 0x8513
nop