// File: /workspace/github_cpp_makefile_star2_O2/lega911[P]ijson/src/server.cpp
// Function: Loop::_loop()
void Loop::_loop() {
    epollfd = epoll_create1(0);
    if(epollfd < 0) THROW("epoll_create1");

    eitem events[MAX_EVENTS];
    char buf[BUF_SIZE];
    int countdown = 100;
    while(true) {
        int nready = epoll_wait(epollfd, events, MAX_EVENTS, 10);
        if(nready == -1) {
            if(server->log & 1) std::cout << ltime() << "epoll_wait error: " << errno << std::endl;
            continue;
        }
        stat_ioevent += nready;

        bool need_to_migrate = false;
        for (int i = 0; i < nready; i++) {
            int fd = events[i].data.fd;
            if(fd == server->fake_fd) {
                set_poll_mode(fd, -1);
                continue;
            }

            if(events[i].events & EPOLLERR || events[i].events & EPOLLHUP) {
                if(server->log & 2) std::cout << "epoll_wait returned EPOLLERR/EPOLLHUP (" << events[i].events << "): " << fd << std::endl;
                _close(fd);
                continue;
            }

            Connect* conn = server->connections[fd];
            if(conn->nloop != _nloop) {
                if(server->log & 1) std::cout << "loop warning: connection is in wrong loop\n";
                continue;
            }
            if(events[i].events & EPOLLIN) {
                int size = recv(fd, buf, BUF_SIZE, 0);
                if(size == 0) {
                    _close(fd);
                } else if(size < 0) {
                    if(errno == EAGAIN || errno == EWOULDBLOCK) {
                        // data is not ready yet
                        continue;
                    } else {
                        THROW("recv error");
                    }
                } else {
                    stat_recv += size;
                    try {
                        conn->on_recv(buf, size);
                    } catch (const Exception &e) {
                        if(server->log & 2) e.print("Exception in on_recv");
                        conn->close();
                    }
                    if(conn->is_closed()) _close(fd);
                }
            } else if(events[i].events & EPOLLOUT) {
                try {
                    conn->on_send();
                } catch (const Exception &e) {
                    if(server->log & 2) e.print("Exception in on_send");
                    conn->close();
                }
                if(conn->is_closed()) _close(fd);
            }

            if(conn->go_loop) need_to_migrate = true;
        }

        if(!_nloop) {
            countdown--;
            if(countdown <= 0) {
                countdown = 100;
                _check_timeout();
            }
        }

        _perform_to_send();
        if(need_to_migrate) _migrate_send();
        _delete_connections();
        _migrate_recv();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c18, %rsp           # imm = 0x4C18
movq %rdi, %rbx
xorl %edi, %edi
callq 0xa370
movl %eax, (%rbx)
testl %eax, %eax
js 0x10d8c
movl %eax, %edi
pushq $0x64
popq %r15
pushq $-0x1
popq %r14
leaq -0xc40(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
pushq $0xa
popq %rcx
callq 0xa150
cmpl $-0x1, %eax
je 0x10cb0
movl %r15d, -0x2c(%rbp)
movslq %eax, %rcx
addq %rcx, 0xa0(%rbx)
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0xc, %rax, %r12
movq $0x0, -0x38(%rbp)
cmpq %r13, %r12
je 0x10ca4
movslq -0xc3c(%rbp,%r13), %r15
movq 0x18(%rbx), %rax
cmpl 0x30(%rax), %r15d
jne 0x10b1e
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x10df2
jmp 0x10c24
movl -0xc40(%rbp,%r13), %ecx
testb $0x18, %cl
je 0x10b86
testb $0x2, 0x20(%rax)
je 0x10b76
movq 0xf498(%rip), %rdi      # 0x1ffd0
leaq 0x88af(%rip), %rsi      # 0x193ee
callq 0xa2e0
movl -0xc40(%rbp,%r13), %esi
movq %rax, %rdi
callq 0xa1e0
movq %rax, %rdi
leaq 0x88b8(%rip), %rsi      # 0x19416
callq 0xa2e0
movq %rax, %rdi
movl %r15d, %esi
callq 0xa460
movq %rax, %rdi
callq 0xa160
movq %rbx, %rdi
movl %r15d, %esi
callq 0x10edc
jmp 0x10c24
movq 0x50(%rax,%r15,8), %r14
movl 0x38(%r14), %edx
cmpl 0x4(%rbx), %edx
jne 0x10ba8
testb $0x1, %cl
jne 0x10bc7
testb $0x4, %cl
je 0x10c14
movq %r14, %rdi
callq 0xa984
jmp 0x10c03
testb $0x1, 0x20(%rax)
pushq $-0x1
popq %r14
je 0x10c24
movq 0xf417(%rip), %rdi      # 0x1ffd0
leaq 0x885a(%rip), %rsi      # 0x1941a
callq 0xa2e0
jmp 0x10c24
movl $0x4000, %edx           # imm = 0x4000
movl %r15d, %edi
leaq -0x4c40(%rbp), %rsi
xorl %ecx, %ecx
callq 0xa0e0
testl %eax, %eax
je 0x10c09
js 0x10c2d
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x90(%rbx)
movq %r14, %rdi
leaq -0x4c40(%rbp), %rsi
movl %eax, %edx
callq 0xab22
cmpl $-0x1, (%r14)
jne 0x10c14
movq %rbx, %rdi
movl %r15d, %esi
callq 0x10edc
movq -0x38(%rbp), %rax
orb 0x40(%r14), %al
movq %rax, -0x38(%rbp)
pushq $-0x1
popq %r14
addq $0xc, %r13
jmp 0x10af0
callq 0xa050
cmpl $0xb, (%rax)
pushq $-0x1
popq %r14
je 0x10c24
jmp 0x10d49
cmpl $0x1, %edx
jne 0x10dea
movq %rax, %rdi
callq 0xa110
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x10c96
movq %rax, %rdi
leaq 0x87eb(%rip), %rsi      # 0x19450
callq 0xe38e
jmp 0x10c96
cmpl $0x1, %edx
jne 0x10dea
movq %rax, %rdi
callq 0xa110
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x10c96
movq %rax, %rdi
leaq 0x87d4(%rip), %rsi      # 0x19465
callq 0xe38e
orl $-0x1, (%r14)
callq 0xa430
jmp 0x10c03
cmpl $0x0, 0x4(%rbx)
je 0x10d01
movl -0x2c(%rbp), %r15d
jmp 0x10d1c
movq 0x18(%rbx), %rax
testb $0x1, 0x20(%rax)
je 0x10d42
callq 0x16a89
movq 0xf306(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x86ff(%rip), %rsi      # 0x193db
callq 0xa2e0
movq %rax, %r14
callq 0xa050
movl (%rax), %esi
movq %r14, %rdi
pushq $-0x1
popq %r14
callq 0xa460
movq %rax, %rdi
callq 0xa160
jmp 0x10d42
movl -0x2c(%rbp), %r15d
cmpl $0x1, %r15d
jle 0x10d10
decl %r15d
jmp 0x10d1c
movq %rbx, %rdi
callq 0x10fe0
pushq $0x64
popq %r15
movq %rbx, %rdi
callq 0x111fc
testb $0x1, -0x38(%rbp)
je 0x10d32
movq %rbx, %rdi
callq 0x11242
movq %rbx, %rdi
callq 0x113a8
movq %rbx, %rdi
callq 0x1147c
movl (%rbx), %edi
jmp 0x10ab0
pushq $0x30
popq %rdi
callq 0xa170
movq %rax, %rbx
leaq 0x86ea(%rip), %rsi      # 0x19445
leaq 0x8494(%rip), %rdx      # 0x191f6
leaq 0x865f(%rip), %r8       # 0x193c8
movq %rax, %rdi
movl $0x17a, %ecx            # imm = 0x17A
callq 0xe15c
leaq 0xef9b(%rip), %rsi      # 0x1fd18
leaq -0x286e(%rip), %rdx     # 0xe516
movq %rbx, %rdi
callq 0xa450
pushq $0x30
popq %rdi
callq 0xa170
movq %rax, %rbx
leaq 0x861c(%rip), %rsi      # 0x193ba
leaq 0x8451(%rip), %rdx      # 0x191f6
leaq 0x861c(%rip), %r8       # 0x193c8
movq %rax, %rdi
movl $0x151, %ecx            # imm = 0x151
callq 0xe15c
jmp 0x10d76
jmp 0x10ddc
movq %rax, %rbx
callq 0xa430
jmp 0x10dcf
movq %rax, %rbx
callq 0xa430
movq %rbx, %rax
jmp 0x10dea
movq %rax, %rdi
callq 0xd334
movq %rax, %r14
movq %rbx, %rdi
callq 0xa200
movq %r14, %rax
movq %rax, %rdi
callq 0xa470