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

	active_connections = 0;
	server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if( server_fd < 0 )
	{
		perror("socket");
		error_state = Error::SOCKET;
	}
    else
    {
	    int optval = 1;
		struct sockaddr_in local_address;
		memset((char *)&local_address, 0, sizeof(local_address));
		local_address.sin_family = AF_INET;
		local_address.sin_addr.s_addr = htonl(INADDR_ANY);
		local_address.sin_port = htons(_port);

	    setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));

		if ( bind(server_fd, (struct sockaddr*)&local_address, sizeof(local_address)) != 0 )
		{
			perror("socket--bind");
			error_state = Error::BIND;
			server_fd = -1;
		}
		else
		{
			error_state = setBlockingMode(Blocking_Mode::NonBlock);
			active_connections = 0;
			if(error_state != Error::NONE)
			{
				perror("server Nonblock failed.");
				server_fd = -1;
			}
			else
			{
				if ( listen(server_fd, MAX_CONNECTIONS) != 0 )
				{
					perror("socket--listen");
					error_state = Error::LISTEN;
					server_fd = -1;
				}
				else
				{
					int epoll_status;
					event_fd = epoll_create1 (0);
					if (event_fd == -1)
					{
						perror ("epoll_create");
						error_state = Error::EPOLL_CREATE;
						server_fd = -1;
					}
					else
					{
						event.data.fd = server_fd;
						event.events = EPOLLIN;

						epoll_status = epoll_ctl (event_fd, EPOLL_CTL_ADD, server_fd, &event);
						if (epoll_status == -1)
						{
							perror ("epoll_create");
							error_state = Error::EPOLL_CTL;
							server_fd = -1;
						}
						else
						{
							//DEBUG("Created server object, bind, setblocking mode, Listening, Server FD: %d\n ",server_fd);
							char port_buffer[32];
							char ip_buffer[32];

							socklen_t len = sizeof( local_address);
							int nameinfo = getnameinfo ((const sockaddr *)&local_address, len, ip_buffer, sizeof(ip_buffer), port_buffer, sizeof(port_buffer), NI_NUMERICHOST | NI_NUMERICSERV);
							if (nameinfo == 0)
							{
								string ip_address = string(ip_buffer);
								port = atoi(port_buffer);
								DEBUG("Server Ip:%s:%d \n ",ip_address.c_str(), port);
							}
						}
					}
				}
			}
		}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movl %esi, %r15d
movq %rdi, %rbx
movq 0x51ef(%rip), %rax      # 0xefa0
addq $0x10, %rax
movq %rax, (%rdi)
leaq 0x10(%rdi), %r14
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x6340
addq $-0x40, %r12
addq $0x40, %r14
cmpq $-0x180, %r12           # imm = 0xFE80
jne 0x9dbf
xorps %xmm0, %xmm0
movups %xmm0, 0x190(%rbx)
movq $0x0, 0x1a0(%rbx)
leaq 0x288(%rbx), %r14
movq %r14, %rdi
callq 0x6430
movq 0x51d5(%rip), %rax      # 0xefd8
addq $0x10, %rax
movq %rax, 0x288(%rbx)
movl $0x0, 0x8(%rbx)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x60f0
movl %eax, 0x1a8(%rbx)
testl %eax, %eax
js 0x9fb3
leaq -0x24(%rbp), %rcx
movl $0x1, (%rcx)
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %r12
movaps %xmm0, (%r12)
movw $0x2, (%r12)
movl $0x0, 0x4(%r12)
rolw $0x8, %r15w
movw %r15w, 0x2(%r12)
movl %eax, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x6690
movl 0x1a8(%rbx), %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x6090
testl %eax, %eax
jne 0x9fcb
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6210
movl %eax, 0x1b4(%rbx)
movl $0x0, 0x8(%rbx)
cmpl $0x2, %eax
jne 0x9fe3
movl 0x1a8(%rbx), %edi
movl $0x6, %esi
callq 0x6440
testl %eax, %eax
jne 0x9ff1
xorl %edi, %edi
callq 0x6500
movl %eax, 0x1b8(%rbx)
cmpl $-0x1, %eax
je 0xa009
movl 0x1a8(%rbx), %edx
leaq 0x1bc(%rbx), %rcx
movl %edx, 0x1c0(%rbx)
movl $0x1, 0x1bc(%rbx)
movl %eax, %edi
movl $0x1, %esi
callq 0x6520
cmpl $-0x1, %eax
je 0xa021
movl $0x3, (%rsp)
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0xa0(%rbp), %r8
movl $0x10, %esi
movl $0x20, %ecx
movl $0x20, %r9d
callq 0x62a0
testl %eax, %eax
jne 0xa041
leaq -0x50(%rbp), %r12
movq %r12, -0x10(%r12)
leaq -0x80(%rbp), %r15
movq %r15, %rdi
callq 0x61e0
leaq (%rax,%rbp), %rdx
addq $-0x80, %rdx
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x61b0
leaq -0xa0(%rbp), %rdi
callq 0x65c0
movl %eax, 0x1ac(%rbx)
movq -0x60(%rbp), %rsi
leaq 0x13ea(%rip), %rdi      # 0xb37d
movl %eax, %edx
xorl %eax, %eax
callq 0x6060
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0xa041
callq 0x63e0
jmp 0xa041
leaq 0x124d(%rip), %rdi      # 0xb207
callq 0x6370
movl $0x7, 0x1b4(%rbx)
jmp 0xa041
leaq 0x1377(%rip), %rdi      # 0xb349
callq 0x6370
movl $0x8, 0x1b4(%rbx)
jmp 0xa037
leaq 0x136c(%rip), %rdi      # 0xb356
callq 0x6370
jmp 0xa037
leaq 0x1376(%rip), %rdi      # 0xb36e
callq 0x6370
movl $0xa, 0x1b4(%rbx)
jmp 0xa037
leaq 0x12b6(%rip), %rdi      # 0xb2c6
callq 0x6370
movl $0xb, 0x1b4(%rbx)
jmp 0xa037
leaq 0x129e(%rip), %rdi      # 0xb2c6
callq 0x6370
movl $0xc, 0x1b4(%rbx)
movl $0xffffffff, 0x1a8(%rbx) # imm = 0xFFFFFFFF
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa055
jmp 0xa055
movq %rax, %r15
movq %r14, %rdi
callq 0x66b0
jmp 0xa065
movq %rax, %r15
leaq 0x190(%rbx), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0xa079
callq 0x63e0
movl $0x150, %r14d           # imm = 0x150
leaq (%rbx,%r14), %rdi
callq 0x60e0
addq $-0x40, %r14
cmpq $-0x30, %r14
jne 0xa07f
jmp 0xa0b2
movq %rax, %r15
testq %r12, %r12
je 0xa0b2
negq %r12
addq $-0x30, %rbx
leaq (%rbx,%r12), %rdi
callq 0x60e0
addq $-0x40, %r12
jne 0xa0a3
movq %r15, %rdi
callq 0x6680