// File: Neuromancer2701[P]OpenTool/lib/sockets/Server.cpp
// Function: Server::Available()
int Server::Available() {

	Client client;

	if(error_state == Error::NONE)
	{
		int epoll_events = epoll_wait (event_fd, events, MAX_EVENTS, -1);
		DEBUG("Epoll events, %d\n ",epoll_events);

		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(server_fd == events[i].data.fd)
			{
				if(active_connections >= MAX_CONNECTIONS)
				{
					break;
				}

				while (error_state != Error::ACCEPT_DONE)
				{
					error_state = client_list[active_connections].Accept(server_fd);
					if(error_state == Error::NONE)
					{
						AddClientToEvent(client_list[active_connections]);
						active_connections++;
					}
				}
				error_state = Error::NONE;
			}
			else if(isClient(events[i].data.fd, client))
			{
				client.Read(read_buffer);
			}
			else
			{
				perror ("Unknown epoll event");
				error_state = Error::EPOLL_UNKNOWN;
			}
		}
	}
	else
	{
	  perror ("Setup did not work so can't wait for a connection.");
	}

	DEBUG("Active Connections:%d and Error State:%d\n", active_connections, error_state);
	return active_connections;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xa700
movq -0xa8(%rbp), %rax
cmpl $0x2, 0x1b4(%rax)
jne 0x11d45
movq -0xa8(%rbp), %rsi
movl 0x1b8(%rsi), %edi
addq $0x1c8, %rsi            # imm = 0x1C8
movl $0x10, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0xa3e0
movl %eax, -0xac(%rbp)
jmp 0x11ab2
movl -0xac(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %esi
leaq 0x1873(%rip), %rdi      # 0x13338
xorl %eax, %eax
callq 0xa070
jmp 0x11ace
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x11d43
movq -0xa8(%rbp), %rax
addq $0x1c8, %rax            # imm = 0x1C8
movslq -0x60(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x11b47
movq -0xa8(%rbp), %rax
addq $0x1c8, %rax            # imm = 0x1C8
movslq -0x60(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x11b47
movq -0xa8(%rbp), %rax
addq $0x1c8, %rax            # imm = 0x1C8
movslq -0x60(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x11b92
leaq 0x172d(%rip), %rdi      # 0x1327b
callq 0xa720
jmp 0x11b55
movq -0xa8(%rbp), %rax
movl $0xd, 0x1b4(%rax)
movslq -0x60(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x1cc(%rax,%rcx,4), %edi
callq 0xb030
jmp 0x11b7c
jmp 0x11d33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x11d9d
movq -0xa8(%rbp), %rcx
movl 0x1a8(%rcx), %eax
addq $0x1c8, %rcx            # imm = 0x1C8
movslq -0x60(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x11cc2
movq -0xa8(%rbp), %rax
cmpl $0x6, 0x8(%rax)
jl 0x11bcc
jmp 0x11d43
jmp 0x11bce
movq -0xa8(%rbp), %rax
cmpl $0x10, 0x1b4(%rax)
je 0x11caf
movq -0xa8(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xaac0
movq %rax, %rdi
movq -0xa8(%rbp), %rax
movl 0x1a8(%rax), %esi
callq 0xaf00
movl %eax, -0xb0(%rbp)
jmp 0x11c16
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl %ecx, 0x1b4(%rax)
cmpl $0x2, 0x1b4(%rax)
jne 0x11caa
movq -0xa8(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xaac0
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0xa0b0
jmp 0x11c5a
movq -0xa8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xab80
jmp 0x11c6f
leaq -0xa0(%rbp), %rdi
callq 0xa170
movq -0xa8(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x11caa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xa170
jmp 0x11d9d
jmp 0x11bce
movq -0xa8(%rbp), %rax
movl $0x2, 0x1b4(%rax)
jmp 0x11d31
movq -0xa8(%rbp), %rdi
movslq -0x60(%rbp), %rax
leaq (%rax,%rax,2), %rax
movl 0x1cc(%rdi,%rax,4), %esi
leaq -0x48(%rbp), %rdx
callq 0xa750
movb %al, -0xb1(%rbp)
jmp 0x11ce9
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x11cf5
jmp 0x11d10
movq -0xa8(%rbp), %rsi
addq $0x190, %rsi            # imm = 0x190
leaq -0x48(%rbp), %rdi
callq 0xa440
jmp 0x11d0e
jmp 0x11d2f
leaq 0x1634(%rip), %rdi      # 0x1334b
callq 0xa720
jmp 0x11d1e
movq -0xa8(%rbp), %rax
movl $0xe, 0x1b4(%rax)
jmp 0x11d31
jmp 0x11d33
jmp 0x11d35
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11ad5
jmp 0x11d55
leaq 0x1613(%rip), %rdi      # 0x1335f
callq 0xa720
jmp 0x11d53
jmp 0x11d55
movq -0xa8(%rbp), %rax
movl 0x8(%rax), %esi
movl 0x1b4(%rax), %edx
leaq 0x1626(%rip), %rdi      # 0x13392
xorl %eax, %eax
callq 0xa070
jmp 0x11d75
movq -0xa8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0xb8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xa170
movl -0xb8(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0xa170
movq -0x58(%rbp), %rdi
callq 0xae90
nop