// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %esi, %r12d
movq %rdi, %rbx
movq 0x4a5f(%rip), %rax      # 0xdfa0
addq $0x10, %rax
movq %rax, (%rdi)
leaq 0x10(%rdi), %r14
movq %r14, %rdi
callq 0x6440
xorps %xmm0, %xmm0
movups %xmm0, 0x190(%rbx)
andq $0x0, 0x1a0(%rbx)
leaq 0x288(%rbx), %r15
movq %r15, %rdi
callq 0x60b0
andl $0x0, 0x8(%rbx)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x6120
movl %eax, 0x1a8(%rbx)
testl %eax, %eax
js 0x96d4
leaq -0x30(%rbp), %rcx
leaq -0x40(%rbp), %r13
andq $0x0, 0x8(%r13)
movw $0x2, (%r13)
andl $0x0, 0x4(%r13)
movl $0x1, (%rcx)
rolw $0x8, %r12w
movw %r12w, 0x2(%r13)
pushq $0x1
popq %rsi
pushq $0xf
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x6750
movl 0x1a8(%rbx), %edi
pushq $0x10
popq %rdx
movq %r13, %rsi
callq 0x6090
testl %eax, %eax
jne 0x96ec
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6240
movl %eax, 0x1b4(%rbx)
andl $0x0, 0x8(%rbx)
cmpl $0x2, %eax
jne 0x9704
movl 0x1a8(%rbx), %edi
pushq $0x6
popq %rsi
callq 0x64a0
testl %eax, %eax
jne 0x9712
xorl %edi, %edi
callq 0x6570
movl %eax, 0x1b8(%rbx)
cmpl $-0x1, %eax
je 0x972a
movl 0x1a8(%rbx), %edx
leaq 0x1bc(%rbx), %rcx
movl %edx, 0x1c0(%rbx)
movl $0x1, 0x1bc(%rbx)
pushq $0x1
popq %rsi
movl %eax, %edi
callq 0x65b0
cmpl $-0x1, %eax
je 0x9742
movl $0x3, (%rsp)
leaq -0x40(%rbp), %rdi
pushq $0x10
popq %rsi
leaq -0x60(%rbp), %rdx
leaq -0x80(%rbp), %r8
pushq $0x20
popq %rcx
movl %ecx, %r9d
callq 0x62e0
testl %eax, %eax
jne 0x975f
leaq -0xa0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x29(%rbp), %rdx
callq 0x61e0
leaq -0x80(%rbp), %rdi
callq 0x6660
movl %eax, 0x1ac(%rbx)
leaq -0xa0(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xc95(%rip), %rdi       # 0xa353
movl %eax, %edx
xorl %eax, %eax
callq 0x6050
movq %rbx, %rdi
callq 0x6200
jmp 0x975f
leaq 0xb02(%rip), %rdi       # 0xa1dd
callq 0x63c0
movl $0x7, 0x1b4(%rbx)
jmp 0x975f
leaq 0xc2c(%rip), %rdi       # 0xa31f
callq 0x63c0
movl $0x8, 0x1b4(%rbx)
jmp 0x9758
leaq 0xc21(%rip), %rdi       # 0xa32c
callq 0x63c0
jmp 0x9758
leaq 0xc2b(%rip), %rdi       # 0xa344
callq 0x63c0
movl $0xa, 0x1b4(%rbx)
jmp 0x9758
leaq 0xb6b(%rip), %rdi       # 0xa29c
callq 0x63c0
movl $0xb, 0x1b4(%rbx)
jmp 0x9758
leaq 0xb53(%rip), %rdi       # 0xa29c
callq 0x63c0
movl $0xc, 0x1b4(%rbx)
orl $-0x1, 0x1a8(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9775
jmp 0x9775
movq %rax, %r12
movq %r15, %rdi
callq 0x6770
jmp 0x9785
movq %rax, %r12
addq $0x190, %rbx            # imm = 0x190
movq %rbx, %rdi
callq 0x67c0
movq %r14, %rdi
callq 0x6100
movq %r12, %rdi
callq 0x6740