// File: /workspace/github_cpp_makefile_star2_O3/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 $0x4c08, %rsp           # imm = 0x4C08
movq %rdi, %rbx
xorl %edi, %edi
callq 0x83a0
movl %eax, (%rbx)
testl %eax, %eax
js 0x1030f
movl %eax, %edi
movl $0x64, %r12d
movq 0xe04d(%rip), %r15      # 0x1dfd0
leaq -0xc30(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
movl $0xa, %ecx
callq 0x8150
cmpl $-0x1, %eax
je 0x101b6
movl %r12d, -0x30(%rbp)
movslq %eax, %rcx
addq %rcx, 0xa0(%rbx)
testl %eax, %eax
jle 0x101f0
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r15
xorl %r13d, %r13d
movl $0x0, -0x2c(%rbp)
movslq -0xc2c(%rbp,%r13), %r14
movq 0x18(%rbx), %rax
cmpl 0x30(%rax), %r14d
jne 0xfff3
movq %rbx, %rdi
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x10378
jmp 0x1012d
movl -0xc30(%rbp,%r13), %ecx
testb $0x18, %cl
je 0x10091
testb $0x2, 0x20(%rax)
je 0x10081
movl $0x27, %edx
movq 0xdfba(%rip), %r12      # 0x1dfd0
movq %r12, %rdi
leaq 0x83ca(%rip), %rsi      # 0x183ea
callq 0x8350
movl -0xc30(%rbp,%r13), %esi
movq %r12, %rdi
callq 0x81e0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x83cb(%rip), %rsi      # 0x18412
callq 0x8350
movq %r12, %rdi
movl %r14d, %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
callq 0x8040
movq %rax, %rdi
callq 0x81f0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x10472
jmp 0x1012d
movq 0x50(%rax,%r14,8), %r12
movl 0x38(%r12), %edx
cmpl 0x4(%rbx), %edx
jne 0x100b4
testb $0x1, %cl
jne 0x100d4
testb $0x4, %cl
je 0x10122
movq %r12, %rdi
callq 0x8ac8
jmp 0x10110
testb $0x1, 0x20(%rax)
je 0x1012d
movl $0x2a, %edx
movq 0xdf0a(%rip), %rdi      # 0x1dfd0
leaq 0x8349(%rip), %rsi      # 0x18416
callq 0x8350
jmp 0x1012d
movl $0x4000, %edx           # imm = 0x4000
movl %r14d, %edi
leaq -0x4c30(%rbp), %rsi
xorl %ecx, %ecx
callq 0x80f0
testl %eax, %eax
je 0x10117
js 0x1013f
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x90(%rbx)
movq %r12, %rdi
leaq -0x4c30(%rbp), %rsi
movl %eax, %edx
callq 0x8cd0
cmpl $-0x1, (%r12)
jne 0x10122
movq %rbx, %rdi
movl %r14d, %esi
callq 0x10472
movl -0x2c(%rbp), %eax
orb 0x40(%r12), %al
movl %eax, -0x2c(%rbp)
addq $0xc, %r13
cmpq %r13, %r15
jne 0xffcc
jmp 0x101f7
callq 0x8060
cmpl $0xb, (%rax)
je 0x1012d
jmp 0x102ca
cmpl $0x1, %edx
jne 0x1036f
movq %rax, %rdi
callq 0x8120
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x101a4
movq %rax, %rdi
leaq 0x82d9(%rip), %rsi      # 0x1844c
callq 0xcf80
jmp 0x101a4
cmpl $0x1, %edx
jne 0x1036f
movq %rax, %rdi
callq 0x8120
movq 0x18(%rbx), %rcx
testb $0x2, 0x20(%rcx)
je 0x101a4
movq %rax, %rdi
leaq 0x82c2(%rip), %rsi      # 0x18461
callq 0xcf80
movl $0xffffffff, (%r12)     # imm = 0xFFFFFFFF
callq 0x8450
jmp 0x10110
movq 0x18(%rbx), %rax
testb $0x1, 0x20(%rax)
je 0x102c3
callq 0x163f5
testq %rax, %rax
je 0x1025e
movq %rax, %r14
movq %rax, %rdi
callq 0x8130
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x8350
jmp 0x10276
movl $0x0, -0x2c(%rbp)
cmpl $0x0, 0x4(%rbx)
je 0x1020e
movq 0xddcc(%rip), %r15      # 0x1dfd0
movl -0x30(%rbp), %r12d
movl -0x2c(%rbp), %r14d
jmp 0x10236
movl -0x30(%rbp), %r12d
cmpl $0x1, %r12d
movq 0xddb3(%rip), %r15      # 0x1dfd0
movl -0x2c(%rbp), %r14d
jle 0x10228
decl %r12d
jmp 0x10236
movq %rbx, %rdi
callq 0x1060c
movl $0x64, %r12d
movq %rbx, %rdi
callq 0x10896
testb $0x1, %r14b
je 0x1024c
movq %rbx, %rdi
callq 0x108f8
movq %rbx, %rdi
callq 0x10aa8
movq %rbx, %rdi
callq 0x10bf2
jmp 0x102c3
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 0x8152(%rip), %rsi      # 0x183d7
callq 0x8350
callq 0x8060
movl (%rax), %esi
movq %r15, %rdi
callq 0x8490
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x82f0
movsbl %al, %esi
movq %r14, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x81f0
movl (%rbx), %edi
jmp 0xff83
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0x8163(%rip), %rsi      # 0x18441
leaq 0x7f0d(%rip), %rdx      # 0x181f2
leaq 0x80d8(%rip), %r8       # 0x183c4
movq %rax, %rdi
movl $0x17a, %ecx            # imm = 0x17A
callq 0xcd04
leaq 0xda18(%rip), %rsi      # 0x1dd18
leaq -0x2ff7(%rip), %rdx     # 0xd310
movq %rbx, %rdi
callq 0x8480
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0x8093(%rip), %rsi      # 0x183b6
leaq 0x7ec8(%rip), %rdx      # 0x181f2
leaq 0x8093(%rip), %r8       # 0x183c4
movq %rax, %rdi
movl $0x151, %ecx            # imm = 0x151
callq 0xcd04
jmp 0x102f9
jmp 0x10361
movq %rax, %rbx
callq 0x8450
jmp 0x10354
movq %rax, %rbx
callq 0x8450
movq %rbx, %rax
jmp 0x1036f
movq %rax, %rdi
callq 0xc1cb
movq %rax, %r14
movq %rbx, %rdi
callq 0x8220
movq %r14, %rax
movq %rax, %rdi
callq 0x84a0
nop