// File: /workspace/github_cpp_makefile_star2_O2/SeTriones[P]Anubis/receiver.cpp
// Function: receiver::svc()
int receiver::svc() {
	int local_epoll_idx = 0;
	pthread_mutex_lock(&epoll_idx_mutex);
	local_epoll_idx = epoll_thread_cnt;
	epoll_thread_cnt++;
	pthread_mutex_unlock(&epoll_idx_mutex);
	local_epoll_idx = local_epoll_idx % epoll_cnt;

	int epoll_fd = epoll_fds[local_epoll_idx];
	epoll_event* es = epoll_ready_events[local_epoll_idx];
	pthread_mutex_t* epoll_mutex = &epoll_mutexes[local_epoll_idx];
	int* event_num = &epoll_ready_event_num[local_epoll_idx];

	char* recv_buf = (char*)malloc(max_recv_buf_len);
	int fd, new_fd;
	int tmp_conn;
	sockaddr_in sa;
	socklen_t len;

	while (!stop_task) {
		pthread_mutex_lock(epoll_mutex);
		if (stop_task) {
			pthread_mutex_unlock(epoll_mutex);
			break;
		}

		if ((*event_num) <= 0) {
			*event_num = epoll_wait(epoll_fd, es, max_conn, -1);
		}
		if ((*event_num)-- < 0) {
			pthread_mutex_unlock(epoll_mutex);
			if (errno == EINTR) {
				continue;
			}
			else {
				break;
			}
		}

		fd = es[(*event_num)].data.fd;

		if (fd == listen_fd) {
			del_input_fd(fd, epoll_fd);
			pthread_mutex_unlock(epoll_mutex);
			while ((new_fd = accept(fd, NULL, NULL)) >= 0) {
				pthread_mutex_lock(&conn_mutex);
				conn_num++;
				tmp_conn = conn_num;
				pthread_mutex_unlock(&conn_mutex);
				len = sizeof(sa);
				getpeername(new_fd, (struct sockaddr *)&sa, &len);
				set_socket(new_fd, O_NONBLOCK);
				add_input_fd(new_fd, epoll_fds[incoming_fd_cnt % epoll_cnt]);
				_INFO("new conn on %s:%d,fd=%d,conn_num=%d,epollno=%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port), new_fd, tmp_conn, incoming_fd_cnt % epoll_cnt);
				incoming_fd_cnt++;
			}
			add_input_fd(fd, epoll_fd);
		} else if (fd == pipe_read_fds[local_epoll_idx]) {
			_INFO("get a stop signal");
			pthread_mutex_unlock(epoll_mutex);
			continue;
		} else {
			del_input_fd(fd, epoll_fd);
			pthread_mutex_unlock(epoll_mutex);
			handle_request(fd, epoll_fd, recv_buf);
		}
	}

	free(recv_buf);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
leaq 0x40(%rdi), %r14
leaq 0x98(%rdi), %r15
movq %r15, %rdi
callq 0x2310
movl 0xe0(%rbx), %r12d
leal 0x1(%r12), %eax
movl %eax, 0xe0(%rbx)
movq %r15, %rdi
callq 0x2160
movl %r12d, %eax
cltd
idivl 0x44(%rbx)
movslq %edx, %r12
movq 0x50(%rbx), %rax
movq 0xc0(%rbx), %rcx
movl (%rax,%r12,4), %r13d
movq (%rcx,%r12,8), %rax
movq %rax, 0x20(%rsp)
imulq $0x28, %r12, %r15
addq 0x68(%rbx), %r15
movq 0xc8(%rbx), %rbp
movl $0x100000, %edi         # imm = 0x100000
callq 0x22a0
movq %rax, 0x18(%rsp)
leaq 0x70(%rbx), %rax
movq %rax, 0x48(%rsp)
cmpl $0x0, 0x58(%rbx)
jne 0x4eec
movq %r15, %rdi
callq 0x2310
cmpl $0x0, 0x58(%rbx)
jne 0x4ee4
movl (%rbp,%r12,4), %ecx
testl %ecx, %ecx
jg 0x4c97
movl (%r14), %edx
movl %r13d, %edi
movq 0x20(%rsp), %rsi
pushq $-0x1
popq %rcx
callq 0x20f0
leal -0x1(%rax), %ecx
movl %ecx, (%rbp,%r12,4)
testl %eax, %eax
jns 0x4c9e
movq %r15, %rdi
callq 0x2160
callq 0x2040
cmpl $0x4, (%rax)
je 0x4c3c
jmp 0x4eec
decl %ecx
movl %ecx, (%rbp,%r12,4)
movslq %ecx, %rax
imulq $0xc, %rax, %rax
movq 0x20(%rsp), %rcx
movl 0x4(%rcx,%rax), %ecx
cmpl 0x4c(%rbx), %ecx
movl %ecx, 0x4(%rsp)
jne 0x4e15
movq %r14, %rdi
movl %ecx, %esi
movl %r13d, (%rsp)
movl %r13d, %edx
callq 0x2c4e
movq %r15, %rdi
callq 0x2160
movq %r14, 0x28(%rsp)
movl 0x4(%rsp), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20a0
testl %eax, %eax
js 0x4e50
movl %eax, %r13d
movq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x2310
movl 0x5c(%rbx), %eax
incl %eax
movq %rax, 0x40(%rsp)
movl %eax, 0x5c(%rbx)
movq %r14, %rdi
movq 0x28(%rsp), %r14
callq 0x2160
movl $0x10, 0x14(%rsp)
movl %r13d, %edi
leaq 0x70(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x2150
movq %r14, %rdi
movl %r13d, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x2ba4
movq 0x50(%rbx), %rcx
movl 0xe4(%rbx), %eax
cltd
idivl 0x44(%rbx)
movslq %edx, %rax
movl (%rcx,%rax,4), %edx
movq %r14, %rdi
movl %r13d, %esi
callq 0x29c8
leaq 0x8(%rsp), %rdi
callq 0x21d0
leaq 0x8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2100
leaq 0x50(%rsp), %rdi
pushq $0x12
popq %rsi
leaq 0x1278(%rip), %rdx      # 0x6004
leaq 0x80(%rsp), %rcx
callq 0x2180
movq 0x5238(%rip), %rax      # 0x9fd8
movq (%rax), %rax
movq %rax, 0x38(%rsp)
callq 0x21b0
movq %rax, 0x30(%rsp)
movl 0x74(%rsp), %edi
callq 0x20e0
movzwl 0x72(%rsp), %ecx
rolw $0x8, %cx
movq %rax, %r9
movzwl %cx, %r11d
movl 0xe4(%rbx), %eax
cltd
idivl 0x44(%rbx)
movl %edx, %r10d
movq 0x38(%rsp), %rdi
leaq 0x1706(%rip), %rsi      # 0x64ea
leaq 0x50(%rsp), %rdx
movq 0x30(%rsp), %rcx
leaq 0x1386(%rip), %r8       # 0x617b
xorl %eax, %eax
pushq %r10
pushq 0x48(%rsp)
pushq %r13
pushq %r11
callq 0x22e0
addq $0x20, %rsp
incl 0xe4(%rbx)
jmp 0x4cd9
movq 0xd0(%rbx), %rax
cmpl (%rax,%r12,4), %ecx
je 0x4e68
movq %r14, %rdi
movl %ecx, %esi
movl %r13d, %edx
callq 0x2c4e
movq %r15, %rdi
callq 0x2160
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
movq 0x18(%rsp), %rcx
callq 0x4f0a
jmp 0x4c3c
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl (%rsp), %r13d
movl %r13d, %edx
callq 0x29c8
jmp 0x4c3c
leaq 0x8(%rsp), %rdi
callq 0x21d0
leaq 0x8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2100
leaq 0x50(%rsp), %rdi
pushq $0x12
popq %rsi
leaq 0x1171(%rip), %rdx      # 0x6004
leaq 0x80(%rsp), %rcx
callq 0x2180
movl %r13d, (%rsp)
movq 0x512d(%rip), %rax      # 0x9fd8
movq (%rax), %r13
callq 0x21b0
movq %r13, %rdi
movl (%rsp), %r13d
leaq 0x1670(%rip), %rsi      # 0x6531
leaq 0x50(%rsp), %rdx
movq %rax, %rcx
leaq 0x12ab(%rip), %r8       # 0x617b
xorl %eax, %eax
callq 0x22e0
movq %r15, %rdi
callq 0x2160
jmp 0x4c3c
movq %r15, %rdi
callq 0x2160
movq 0x18(%rsp), %rdi
callq 0x2250
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq