// File: xuchanglong[P]SimpleReactorImplemention-comments/eventDemultiplexer/event_demultiplexer.cc
// Function: reactor::EpollDemultiplexer::WaitEvents(std::map<int, reactor::EventHandler*, std::less<int>, std::allocator<std::pair<int const, reactor::EventHandler*>>>*, int, time_heap*)
int EpollDemultiplexer::WaitEvents(std::map<handle_t, EventHandler *> *handlers,
    int timeout, time_heap *event_timer)
{
    //  申请用于保存已发送事件的 epoll_event 。
    std::vector<epoll_event> ep_evts(m_fd_num);
    //  获取已发生事件的 epoll_event 。
    int num = epoll_wait(m_epoll_fd, &ep_evts[0], ep_evts.size(), timeout);
    if (num > 0) {
        for (int idx = 0; idx < num; ++idx) {
            handle_t handle = ep_evts[idx].data.fd;
            assert(handlers->find(handle) != handlers->end());
            if ((ep_evts[idx].events & EPOLLERR) || (ep_evts[idx].events & EPOLLHUP)) {
                (*handlers)[handle]->HandleError();
            } else {
                /**
                 * 调用该句柄对应的事件处理器的读写函数。
                */
                if (ep_evts[idx].events & EPOLLIN) {
                    (*handlers)[handle]->HandleRead();
                }
                if (ep_evts[idx].events & EPOLLOUT) {
                    (*handlers)[handle]->HandleWrite();
                }
            }
        }
    }
    if (event_timer != nullptr) {
        event_timer->tick();
    }

    return num;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movslq 0xc(%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x4860
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x4880
jmp 0x44aa
leaq -0x39(%rbp), %rdi
callq 0x48f0
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x94(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x4910
movq -0xa0(%rbp), %rdi
movq %rax, -0x90(%rbp)
callq 0x4930
movl -0x94(%rbp), %edi
movq -0x90(%rbp), %rsi
movl %eax, %edx
movl -0x14(%rbp), %ecx
callq 0x3130
movl %eax, -0x84(%rbp)
jmp 0x4506
movl -0x84(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jle 0x46cf
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x46cd
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4910
movl 0x4(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x4990
movq %rax, -0xa8(%rbp)
jmp 0x4555
movq -0xa8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x49c0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4960
testb $0x1, %al
jne 0x4580
jmp 0x4582
jmp 0x45d5
leaq 0x3ca6(%rip), %rdi      # 0x822f
leaq 0x3c43(%rip), %rsi      # 0x81d3
movl $0x1d, %edx
leaq 0x3cbd(%rip), %rcx      # 0x8259
callq 0x31a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x48f0
jmp 0x4704
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4b70
jmp 0x4704
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4910
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x4603
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4910
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x4633
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x49f0
movq %rax, -0xb0(%rbp)
jmp 0x4619
movq -0xb0(%rbp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x462e
jmp 0x46bd
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4910
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4677
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x49f0
movq %rax, -0xb8(%rbp)
jmp 0x4660
movq -0xb8(%rbp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x4675
jmp 0x4677
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4910
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x46bb
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x49f0
movq %rax, -0xc0(%rbp)
jmp 0x46a4
movq -0xc0(%rbp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x46b9
jmp 0x46bb
jmp 0x46bd
jmp 0x46bf
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4520
jmp 0x46cf
cmpq $0x0, -0x20(%rbp)
je 0x46e3
movq -0x20(%rbp), %rdi
callq 0x4ad0
jmp 0x46e1
jmp 0x46e3
movl -0x50(%rbp), %eax
movl %eax, -0xc4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4b70
movl -0xc4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3390
nopl (%rax)