// File: /workspace/github_cpp_makefile_star2_O1/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 0x66c1
movq %rsi, %rbx
movl $0x1, %esi
movl $0xd, %edi
callq 0x4540
leaq -0x1902(%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 0x66d4
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x40f0
movl %eax, 0x9ef4(%rip)      # 0x10540
testl %eax, %eax
jns 0x6659
leaq 0x4d3e(%rip), %rdi      # 0xb395
jmp 0x66a6
xorps %xmm0, %xmm0
leaq 0x4(%rsp), %rdx
movaps %xmm0, -0x4(%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 0x66e7
movl 0x9eb2(%rip), %edi      # 0x10540
movq %rsp, %rsi
movl $0x10, %edx
callq 0x43a0
testl %eax, %eax
jns 0x66fa
leaq 0x4d12(%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 0x98f8(%rip), %rdi      # 0xffc0
leaq 0x4c8e(%rip), %rsi      # 0xb35d
jmp 0x6864
movq 0x98e5(%rip), %rdi      # 0xffc0
leaq 0x4c9e(%rip), %rsi      # 0xb380
jmp 0x6864
movq 0x98d2(%rip), %rdi      # 0xffc0
leaq 0x4cb0(%rip), %rsi      # 0xb3a5
jmp 0x6864
movq 0x98bf(%rip), %rdi      # 0xffc0
callq 0x41e0
movq %rax, %r15
leaq 0x4cb9(%rip), %rsi      # 0xb3c9
movl $0x25, %edx
movq %rax, %rdi
callq 0x44e0
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x4420
movq %rax, %rbx
leaq 0x4cbc(%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 0x9de7(%rip), %esi      # 0x10540
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x6b30
movb 0x4(%rbx), %al
movb %al, 0x9df5(%rip)       # 0x10564
movl (%rbx), %eax
movl %eax, 0x9de9(%rip)      # 0x10560
leaq 0x9de2(%rip), %rdi      # 0x10560
callq 0x5c77
testb %al, %al
je 0x6856
callq 0x4dfa
xorps %xmm0, %xmm0
movups %xmm0, 0x9d26(%rip)     # 0x104c0
movups %xmm0, 0x9d2f(%rip)     # 0x104d0
movups %xmm0, 0x9d38(%rip)     # 0x104e0
movups %xmm0, 0x9d41(%rip)     # 0x104f0
movups %xmm0, 0x9d4a(%rip)     # 0x10500
movups %xmm0, 0x9d53(%rip)     # 0x10510
movups %xmm0, 0x9d5c(%rip)     # 0x10520
movups %xmm0, 0x9d65(%rip)     # 0x10530
leaq 0x9cee(%rip), %rsi      # 0x104c0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x70(%rax)
movl 0x9d43(%rip), %r12d     # 0x10540
leal 0x3f(%r12), %edx
testl %r12d, %r12d
cmovnsl %r12d, %edx
movl %edx, %edi
andl $-0x40, %edi
movl %r12d, %ecx
subl %edi, %ecx
movl $0x1, %edi
shlq %cl, %rdi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rdi, (%rsi,%rcx,8)
orb $0x1, 0x9c94(%rip)      # 0x104c0
leal 0x1(%r12), %ebx
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 0x687b
callq 0x48d0
movq 0x9763(%rip), %rdi      # 0xffc0
leaq 0x4b8f(%rip), %rsi      # 0xb3f3
callq 0x4420
movq %rax, %rdi
callq 0x41e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4560
leaq 0x9c3e(%rip), %r14      # 0x104c0
leaq 0x10(%rsp), %r15
testl %r12d, %r12d
js 0x68c1
xorl %r13d, %r13d
movl %r13d, %eax
shrl $0x6, %eax
movq 0x10(%rsp,%rax,8), %rax
btq %r13, %rax
jae 0x68b9
testl %r13d, %r13d
je 0x68b0
cmpl 0x9c94(%rip), %r13d     # 0x10540
jne 0x68b9
jmp 0x68e9
callq 0x5bdb
testb %al, %al
je 0x6904
incl %r13d
cmpl %r13d, %ebx
jne 0x688f
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 0x6887
jmp 0x6851
movq 0x96d0(%rip), %rdi      # 0xffc0
leaq 0x4b26(%rip), %rsi      # 0xb41d
callq 0x4420
movq %rax, %rdi
callq 0x41e0
callq 0xa1f4
leaq 0x9c50(%rip), %rdi      # 0x10560
callq 0x6b4c
jmp 0x6871