// File: /workspace/github_cpp_makefile_star2_O3/luigix25[P]SSLP/client_files/client.cpp
// Function: main
int main(int argc,char **argv){

	if(argc < 3){
		cout<<"[Error] IP and Port are mandatory!"<<endl;
		exit(-1);
	}

	signal(SIGPIPE, SIG_IGN);
	signal (SIGINT,close_handler);

	int status,portServer;
	struct sockaddr_in serverAddress;

	int i,fdmax;
	fd_set read_fds;

	portServer = atoi(argv[2]);

	if(portServer <= 0 || portServer >= USHRT_MAX){
		cout<<"[Error] Invalid Port"<<endl;
		exit(-1);
	}


	socket_tcp = socket(AF_INET,SOCK_STREAM,0);
	if(socket_tcp < 0){
		perror("[Error] socket\n");
		return -1;
	}

	memset(&serverAddress,0,sizeof(serverAddress));
	
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_port = htons(portServer);
	if(inet_pton(AF_INET,argv[1],&serverAddress.sin_addr) <=0){
		cout<<"[Error] invalid IP"<<endl;
		exit(-1);
	}
	

	status = connect(socket_tcp, (struct sockaddr*)&serverAddress,sizeof(serverAddress));
	if(status < 0){
		perror("[Error] connect\n");
		return -1;
	}
	
	cout<<endl<<"Successfully connected to the server "<<argv[1]<<" : "<<portServer<<endl;
	server_socket = NetSocket(socket_tcp);

	if(!initial_protocol(server_socket)){
		cout<<"Something Strange Occurred"<<endl;
		exit(-1);
		//handle error
	}

	cmd_help();

	FD_ZERO(&master);	
	FD_ZERO(&read_fds);


	FD_SET(socket_tcp,&master);
	FD_SET(0,&master);		// 0 è stdin

	fdmax = socket_tcp;

	while(true){

	
		read_fds = master;
		if(select(fdmax+1,&read_fds,NULL,NULL,NULL) <=0){
			perror("[Error] select");
			exit(-1);
		}


		for(i = 0; i <= fdmax; i++){
			if(FD_ISSET(i,&read_fds)){
				if(i == 0){						//stdin
					if(!read_input()){
						KeyManager::destroyKeys();
						server_socket.closeConnection();
						exit(-1);
					}
					//continue;
				} else if(i == socket_tcp) {			//server tcp
					//non dovrei mai arrivare qui	
					cout<<"Protocolar Error!"<<endl;
					KeyManager::destroyKeys();
					server_socket.closeConnection();
					exit(-1);
				} 

			}

		}
		
	}

	
	return 0;

}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x90, %rsp
cmpl $0x2, %edi
jle 0x66ca
movq %rsi, %rbx
movl $0x1, %esi
movl $0xd, %edi
callq 0x4540
leaq -0x1904(%rip), %rsi     # 0x4d0c
movl $0x2, %edi
callq 0x4540
movq 0x10(%rbx), %rdi
callq 0x4700
movl %eax, %r14d
movl $0xffff0001, %eax       # imm = 0xFFFF0001
leal (%r14,%rax), %ecx
cmpl %eax, %ecx
jbe 0x66dd
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x40f0
movl %eax, 0x9ef2(%rip)      # 0x10540
testl %eax, %eax
jns 0x665b
leaq 0x4d3c(%rip), %rdi      # 0xb395
jmp 0x66af
leaq 0x4(%rsp), %rdx
movl $0x0, 0x8(%rdx)
movq $0x0, (%rdx)
movw $0x2, -0x4(%rdx)
movl %r14d, %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdx)
movq 0x8(%rbx), %rsi
movl $0x2, %edi
callq 0x4850
testl %eax, %eax
jle 0x66f0
movl 0x9ea9(%rip), %edi      # 0x10540
movq %rsp, %rsi
movl $0x10, %edx
callq 0x43a0
testl %eax, %eax
jns 0x6703
leaq 0x4d09(%rip), %rdi      # 0xb3b8
callq 0x43b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x98ef(%rip), %rdi      # 0xffc0
leaq 0x4c85(%rip), %rsi      # 0xb35d
jmp 0x684d
movq 0x98dc(%rip), %rdi      # 0xffc0
leaq 0x4c95(%rip), %rsi      # 0xb380
jmp 0x684d
movq 0x98c9(%rip), %rdi      # 0xffc0
leaq 0x4ca7(%rip), %rsi      # 0xb3a5
jmp 0x684d
movq 0x98b6(%rip), %rdi      # 0xffc0
callq 0x41e0
movq %rax, %r15
leaq 0x4cb0(%rip), %rsi      # 0xb3c9
movl $0x25, %edx
movq %rax, %rdi
callq 0x44e0
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x4420
movq %rax, %rbx
leaq 0x4cb3(%rip), %rsi      # 0xb3ef
movl $0x3, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x4790
movq %rax, %rdi
callq 0x41e0
movl 0x9dde(%rip), %esi      # 0x10540
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x6b28
movb 0x4(%rbx), %al
movb %al, 0x9dec(%rip)       # 0x10564
movl (%rbx), %eax
movl %eax, 0x9de0(%rip)      # 0x10560
leaq 0x9dd9(%rip), %rdi      # 0x10560
callq 0x5c7e
testb %al, %al
je 0x683f
callq 0x4e01
xorps %xmm0, %xmm0
movups %xmm0, 0x9d1d(%rip)     # 0x104c0
movups %xmm0, 0x9d26(%rip)     # 0x104d0
movups %xmm0, 0x9d2f(%rip)     # 0x104e0
movups %xmm0, 0x9d38(%rip)     # 0x104f0
movups %xmm0, 0x9d41(%rip)     # 0x10500
movups %xmm0, 0x9d4a(%rip)     # 0x10510
movups %xmm0, 0x9d53(%rip)     # 0x10520
movups %xmm0, 0x9d5c(%rip)     # 0x10530
leaq 0x9ce5(%rip), %rsi      # 0x104c0
movl 0x9d5e(%rip), %r12d     # 0x10540
leal 0x3f(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r12d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, (%rsi,%rax,8)
orb $0x1, 0x9cb0(%rip)      # 0x104c0
leal 0x1(%r12), %ebx
leaq 0x10(%rsp), %rax
movl $0x10, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4400
testl %eax, %eax
jg 0x6864
callq 0x48d0
movq 0x977a(%rip), %rdi      # 0xffc0
leaq 0x4ba6(%rip), %rsi      # 0xb3f3
callq 0x4420
movq %rax, %rdi
callq 0x41e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4560
leaq 0x9c55(%rip), %r14      # 0x104c0
leaq 0x10(%rsp), %r15
testl %r12d, %r12d
js 0x68aa
xorl %r13d, %r13d
movl %r13d, %eax
shrl $0x6, %eax
movq 0x10(%rsp,%rax,8), %rax
btq %r13, %rax
jae 0x68a2
testl %r13d, %r13d
je 0x6899
cmpl 0x9cab(%rip), %r13d     # 0x10540
jne 0x68a2
jmp 0x68d2
callq 0x5be2
testb %al, %al
je 0x68ed
incl %r13d
cmpl %r13d, %ebx
jne 0x6878
movl $0x10, %ecx
movq %r15, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4400
testl %eax, %eax
jg 0x6870
jmp 0x683a
movq 0x96e7(%rip), %rdi      # 0xffc0
leaq 0x4b3d(%rip), %rsi      # 0xb41d
callq 0x4420
movq %rax, %rdi
callq 0x41e0
callq 0xa154
leaq 0x9c67(%rip), %rdi      # 0x10560
callq 0x6b44
jmp 0x685a
nop