// File: Neuromancer2701[P]OpenTool/lib/sockets/Client.cpp
// Function: Client::EventRead(std::vector<char, std::allocator<char>>&)
Error Client::EventRead(vector<char>& buffer)
{
	Error error_state = Error::UNKNOWN;
	int event_fd, epoll_status;
	struct epoll_event event;
	struct epoll_event events[MAX_EVENTS];

	event_fd = epoll_create1(0);
	if (event_fd == -1)
	{
		perror ("epoll_create");
		error_state = Error::EPOLL_CREATE;
	}
	else
	{
		event.data.fd = client_fd;
		event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;

		epoll_status = epoll_ctl (event_fd, EPOLL_CTL_ADD, client_fd, &event);
		if (epoll_status == -1)
		{
			perror ("epoll_create");
			error_state = Error::EPOLL_CTL;
		}
		else
		{
		    int epoll_events = epoll_wait (event_fd, events, MAX_EVENTS, 1000);
		    for (int i = 0; i < epoll_events; i++)
			{
				if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN ))) /*Epoll error occured*/
				{
				  perror("epoll wait error\n");
				  error_state = Error::EPOLL_WAIT;
				  close (events[i].data.fd);
				}
				else if(events[i].events & EPOLLRDHUP)
				{
					perror("epoll event Client has hung up.\n");
					error_state = Error::EPOLL_CLOSE;
				}
				else if(client_fd == events[i].data.fd)
				{
					error_state = Read(buffer);
				}
			}
		}
	}

	return error_state;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movl $0x0, -0x14(%rbp)
xorl %edi, %edi
callq 0xaaa0
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x1062d
leaq 0x2c52(%rip), %rdi      # 0x1326e
callq 0xa720
movl $0xb, -0x14(%rbp)
jmp 0x107cb
movq -0x100(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0x24(%rbp)
movl $0x80002001, -0x28(%rbp) # imm = 0x80002001
movl -0x18(%rbp), %edi
movl 0xc(%rax), %edx
movl $0x1, %esi
leaq -0x28(%rbp), %rcx
callq 0xab60
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x10676
leaq 0x2c09(%rip), %rdi      # 0x1326e
callq 0xa720
movl $0xc, -0x14(%rbp)
jmp 0x107c9
movl -0x18(%rbp), %edi
leaq -0xf0(%rbp), %rsi
movl $0x10, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0xa3e0
movl %eax, -0xf4(%rbp)
movl $0x0, -0xf8(%rbp)
movl -0xf8(%rbp), %eax
cmpl -0xf4(%rbp), %eax
jge 0x107c7
movslq -0xf8(%rbp), %rcx
leaq -0xf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x1070e
movslq -0xf8(%rbp), %rcx
leaq -0xf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x1070e
movslq -0xf8(%rbp), %rcx
leaq -0xf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x10740
leaq 0x2b66(%rip), %rdi      # 0x1327b
callq 0xa720
movl $0xd, -0x14(%rbp)
movslq -0xf8(%rbp), %rcx
leaq -0xf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0xb030
jmp 0x107b1
movslq -0xf8(%rbp), %rcx
leaq -0xf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x10776
leaq 0x2b25(%rip), %rdi      # 0x1328d
callq 0xa720
movl $0xf, -0x14(%rbp)
jmp 0x107af
movq -0x100(%rbp), %rax
movl 0xc(%rax), %eax
movslq -0xf8(%rbp), %rdx
leaq -0xf0(%rbp), %rcx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x107ad
movq -0x100(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xa440
movl %eax, -0x14(%rbp)
jmp 0x107af
jmp 0x107b1
jmp 0x107b3
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x1069f
jmp 0x107c9
jmp 0x107cb
movl -0x14(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)