// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x120(%rbp)
movl $0x0, -0x14(%rbp)
addq $0x40, %rdi
addq $0x58, %rdi
callq 0x2340
movq -0x120(%rbp), %rdi
movl 0xe0(%rdi), %eax
movl %eax, -0x14(%rbp)
movl 0xe0(%rdi), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rdi)
addq $0x40, %rdi
addq $0x58, %rdi
callq 0x2170
movq -0x120(%rbp), %rcx
movl -0x14(%rbp), %eax
cltd
idivl 0x44(%rcx)
movq -0x120(%rbp), %rax
movl %edx, -0x14(%rbp)
movq 0x50(%rax), %rcx
movslq -0x14(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x18(%rbp)
movq 0xc0(%rax), %rcx
movslq -0x14(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x20(%rbp)
movq 0x68(%rax), %rcx
movslq -0x14(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq %rcx, -0x28(%rbp)
movq 0xc8(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x100000, %edi         # imm = 0x100000
callq 0x22d0
movq %rax, -0x38(%rbp)
movq -0x120(%rbp), %rax
cmpl $0x0, 0x58(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x7df4
jmp 0x8191
movq -0x28(%rbp), %rdi
callq 0x2340
movq -0x120(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0x7e18
movq -0x28(%rbp), %rdi
callq 0x2170
jmp 0x8191
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jg 0x7e44
movq -0x120(%rbp), %rax
movl -0x18(%rbp), %edi
movq -0x20(%rbp), %rsi
movl 0x40(%rax), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x2110
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rcx
movl (%rcx), %eax
movl %eax, %edx
addl $-0x1, %edx
movl %edx, (%rcx)
cmpl $0x0, %eax
jge 0x7e73
movq -0x28(%rbp), %rdi
callq 0x2170
callq 0x2040
cmpl $0x4, (%rax)
jne 0x7e6e
jmp 0x7ddb
jmp 0x8191
movq -0x120(%rbp), %rcx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rdx
movslq (%rdx), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0x4c(%rcx), %eax
jne 0x809b
movq -0x120(%rbp), %rdi
addq $0x40, %rdi
movl -0x3c(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x3330
movq -0x28(%rbp), %rdi
callq 0x2170
movl -0x3c(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x20c0
movl %eax, -0x40(%rbp)
cmpl $0x0, %eax
jl 0x8080
movq -0x120(%rbp), %rdi
addq $0x40, %rdi
addq $0x30, %rdi
callq 0x2340
movq -0x120(%rbp), %rdi
movl 0x5c(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rdi)
movl 0x5c(%rdi), %eax
movl %eax, -0x44(%rbp)
addq $0x40, %rdi
addq $0x30, %rdi
callq 0x2170
movl $0x10, -0x58(%rbp)
movl -0x40(%rbp), %edi
leaq -0x54(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x2160
movq -0x120(%rbp), %rdi
addq $0x40, %rdi
movl -0x40(%rbp), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3250
movq -0x120(%rbp), %rcx
movq %rcx, %rdi
addq $0x40, %rdi
movl -0x40(%rbp), %esi
movq 0x50(%rcx), %rax
movq %rax, -0x148(%rbp)
movl 0xe4(%rcx), %eax
cltd
idivl 0x44(%rcx)
movq -0x148(%rbp), %rax
movslq %edx, %rcx
movl (%rax,%rcx,4), %edx
callq 0x2f90
leaq -0x60(%rbp), %rdi
callq 0x21e0
leaq -0x60(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x2120
leaq -0xb0(%rbp), %rdi
movl $0x12, %esi
leaq 0x1062(%rip), %rdx      # 0x9004
leaq -0x98(%rbp), %rcx
callq 0x2190
movq 0x5023(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x140(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x128(%rbp)
callq 0x21c0
movq %rax, -0x138(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xb8(%rbp), %edi
callq 0x2100
movq %rax, -0x130(%rbp)
movzwl -0x52(%rbp), %edi
callq 0x2400
movq -0x140(%rbp), %rdi
movq -0x138(%rbp), %rcx
movq -0x130(%rbp), %r9
movq -0x120(%rbp), %rsi
movzwl %ax, %ebx
movl -0x40(%rbp), %r11d
movl -0x44(%rbp), %r10d
movl 0xe4(%rsi), %eax
cltd
idivl 0x44(%rsi)
movl %edx, %eax
movq -0x128(%rbp), %rdx
leaq 0x14a0(%rip), %rsi      # 0x94de
leaq 0x1136(%rip), %r8       # 0x917b
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x2310
movl %eax, %ecx
movq -0x120(%rbp), %rax
movl %ecx, -0xb4(%rbp)
movl 0xe4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xe4(%rax)
jmp 0x7ebd
movq -0x120(%rbp), %rdi
addq $0x40, %rdi
movl -0x3c(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x2f90
jmp 0x818c
movq -0x120(%rbp), %rcx
movl -0x3c(%rbp), %eax
movq 0xd0(%rcx), %rcx
movslq -0x14(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x8155
leaq -0xc0(%rbp), %rdi
callq 0x21e0
leaq -0xc0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x2120
leaq -0x110(%rbp), %rdi
movl $0x12, %esi
leaq 0xf19(%rip), %rdx       # 0x9004
leaq -0xf8(%rbp), %rcx
callq 0x2190
movq 0x4eda(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x158(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x150(%rbp)
callq 0x21c0
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rdx
movq %rax, %rcx
leaq 0x13f2(%rip), %rsi      # 0x9525
leaq 0x1041(%rip), %r8       # 0x917b
movb $0x0, %al
callq 0x2310
movl %eax, -0x114(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2170
jmp 0x7ddb
movq -0x120(%rbp), %rdi
addq $0x40, %rdi
movl -0x3c(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x3330
movq -0x28(%rbp), %rdi
callq 0x2170
movq -0x120(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x38(%rbp), %rcx
callq 0x81b0
jmp 0x818c
jmp 0x7ddb
movq -0x38(%rbp), %rdi
callq 0x2270
xorl %eax, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)