// File: /workspace/github_cpp_makefile_star2_O1/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 0x83a0
movl %eax, (%rbx)
testl %eax, %eax
js 0x103f8
movl $0x64, -0x30(%rbp)
leaq -0xc40(%rbp), %r14
movq 0xdf51(%rip), %r15      # 0x1dfd0
movl (%rbx), %edi
movq %r14, %rsi
movl $0x100, %edx            # imm = 0x100
movl $0xa, %ecx
callq 0x8150
cmpl $-0x1, %eax
jne 0x10135
movq 0x18(%rbx), %rax
testb $0x1, 0x20(%rax)
je 0x1007f
callq 0x16531
testq %rax, %rax
je 0x100cb
movq %rax, %r12
movq %rax, %rdi
callq 0x8130
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8350
jmp 0x100e3
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x8470
movl $0x12, %edx
movq %r15, %rdi
leaq 0x82e5(%rip), %rsi      # 0x183d7
callq 0x8350
callq 0x8060
movl (%rax), %esi
movq %r15, %rdi
callq 0x8490
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x82f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x81f0
jmp 0x1007f
movslq %eax, %rcx
addq %rcx, 0xa0(%rbx)
testl %eax, %eax
jle 0x10356
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r12
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
movq %r12, -0x38(%rbp)
movslq -0xc3c(%rbp,%r14), %r13
movq 0x18(%rbx), %rax
cmpl 0x30(%rax), %r13d
jne 0x10186
movq %rbx, %rdi
movl %r13d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x10460
jmp 0x102c9
movl -0xc40(%rbp,%r14), %ecx
testb $0x18, %cl
je 0x10221
testb $0x2, 0x20(%rax)
je 0x10211
movl $0x27, %edx
movq %r15, %rdi
leaq 0x823e(%rip), %rsi      # 0x183ea
callq 0x8350
movl -0xc40(%rbp,%r14), %esi
movq %r15, %rdi
callq 0x81e0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x823f(%rip), %rsi      # 0x18412
callq 0x8350
movq %r12, %rdi
movl %r13d, %esi
callq 0x8490
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x82f0
movsbl %al, %esi
movq %r12, %rdi
movq -0x38(%rbp), %r12
callq 0x8040
movq %rax, %rdi
callq 0x81f0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x10550
jmp 0x102c9
movq 0x50(%rax,%r13,8), %r12
movl 0x38(%r12), %edx
cmpl 0x4(%rbx), %edx
jne 0x10244
testb $0x1, %cl
jne 0x10260
testb $0x4, %cl
je 0x102ae
movq %r12, %rdi
callq 0x8ac8
jmp 0x1029c
testb $0x1, 0x20(%rax)
je 0x102c5
movl $0x2a, %edx
movq %r15, %rdi
leaq 0x81bd(%rip), %rsi      # 0x18416
callq 0x8350
jmp 0x102c5
movl $0x4000, %edx           # imm = 0x4000
movl %r13d, %edi
leaq -0x4c40(%rbp), %rsi
xorl %ecx, %ecx
callq 0x80f0
testl %eax, %eax
je 0x102a3
js 0x102db
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x90(%rbx)
movq %r12, %rdi
leaq -0x4c40(%rbp), %rsi
movl %eax, %edx
callq 0x8cea
cmpl $-0x1, (%r12)
jne 0x102ae
movq %rbx, %rdi
movl %r13d, %esi
callq 0x10550
movl -0x2c(%rbp), %ecx
movzbl %cl, %ecx
cmpb $0x0, 0x40(%r12)
movl $0x1, %eax
cmovnel %eax, %ecx
movl %ecx, -0x2c(%rbp)
movq -0x38(%rbp), %r12
addq $0xc, %r14
cmpq %r14, %r12
jne 0x1015f
jmp 0x1035d
callq 0x8060
cmpl $0xb, (%rax)
movq -0x38(%rbp), %r12
je 0x102c9
jmp 0x103b3
cmpl $0x1, %edx
jne 0x10458
movq %rax, %rdi
callq 0x8120
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x10344
movq %rax, %rdi
leaq 0x8139(%rip), %rsi      # 0x1844c
callq 0xcf80
jmp 0x10344
cmpl $0x1, %edx
jne 0x10458
movq %rax, %rdi
callq 0x8120
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x10344
movq %rax, %rdi
leaq 0x8122(%rip), %rsi      # 0x18461
callq 0xcf80
movl $0xffffffff, (%r12)     # imm = 0xFFFFFFFF
callq 0x8450
jmp 0x1029c
movl $0x0, -0x2c(%rbp)
cmpl $0x0, 0x4(%rbx)
jne 0x10381
movl -0x30(%rbp), %eax
cmpl $0x1, %eax
jle 0x10372
decl %eax
movl %eax, -0x30(%rbp)
jmp 0x10381
movq %rbx, %rdi
callq 0x106ea
movl $0x64, -0x30(%rbp)
leaq -0xc40(%rbp), %r14
movq %rbx, %rdi
callq 0x10970
testb $0x1, -0x2c(%rbp)
je 0x1039e
movq %rbx, %rdi
callq 0x109d2
movq %rbx, %rdi
callq 0x10b82
movq %rbx, %rdi
callq 0x10cc6
jmp 0x1007f
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0x807a(%rip), %rsi      # 0x18441
leaq 0x7e24(%rip), %rdx      # 0x181f2
leaq 0x7fef(%rip), %r8       # 0x183c4
movq %rax, %rdi
movl $0x17a, %ecx            # imm = 0x17A
callq 0xcd04
leaq 0xd92f(%rip), %rsi      # 0x1dd18
leaq -0x30e0(%rip), %rdx     # 0xd310
movq %rbx, %rdi
callq 0x8480
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0x7faa(%rip), %rsi      # 0x183b6
leaq 0x7ddf(%rip), %rdx      # 0x181f2
leaq 0x7faa(%rip), %r8       # 0x183c4
movq %rax, %rdi
movl $0x151, %ecx            # imm = 0x151
callq 0xcd04
jmp 0x103e2
jmp 0x1044a
movq %rax, %rbx
callq 0x8450
jmp 0x1043d
movq %rax, %rbx
callq 0x8450
movq %rbx, %rax
jmp 0x10458
movq %rax, %rdi
callq 0xc171
movq %rax, %r14
movq %rbx, %rdi
callq 0x8220
movq %r14, %rax
movq %rax, %rdi
callq 0x84a0