// File: /workspace/github_c_makefile_star2_O3/Marinouel[P]Network-Lab/client.c
// Function: main
int main(int argc, char *argv[])
{
	//Define the struct and the variables
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *serv;
    int i, er;
    char buffer[1024];

//-------------------------------------------------------------------------------------------------------------------
    char str1[15];	 		
    char str2[15]; 
    char val1[15]; 			
    
//--------------------------------------------------------------------------------------------------------------------
    portno = atoi(argv[2]);
  	serv = gethostbyname(argv[1]);
	sockfd = socket(AF_INET, SOCK_STREAM, 0); 					// Create a socket 
    bzero((char *) &serv_addr, sizeof(serv_addr)); 				//take the serv_addr and put the right protocol
    serv_addr.sin_family = AF_INET; 							// Address family = Internet 
    bcopy((char *)serv->h_addr,(char *)&serv_addr.sin_addr.s_addr,serv->h_length);
    serv_addr.sin_port = htons(portno); 						// Set port number
    serv_addr.sin_addr = *((struct in_addr *)serv->h_addr);
    connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));

    //input example : ./client diogenis.ceid.upatras.gr 1234 put city patra get city 	
 	
    char prot;
    for( i = 3; i < argc; i++ ) 								//Here we send the right input arguments
    {
		strcpy(str1,"get");
   		strcpy(str2,"put");
    	if ( strcmp(argv[i],"get" ) == 0) 		 				//if argv[i]=get then send g
		{
			prot = 103;
    		writen(sockfd,&prot,1);			    				//send g
					
			if (argv[i+1]==NULL) break;							//if there isn't key, then stop the connection		
			writen(sockfd,argv[i+1],strlen(argv[i+1])+1);		//else, write it to the socket, send it to the server
			recv(sockfd, buffer, 1, 0);							//take the message from the server	
			
			if (buffer[0] == 'n'){								 //if the answer from server is n->110 ascii (didn't found)
				printf("\n");
				i +=1;
				continue;										 //keep searching untill you find it
			} else if (buffer[0] == 'f'){						  //if we found  it, then we recieve f->102 ascii
					while((er = read(sockfd, &buffer, 1))>0 && buffer[0]!='\0') //and we take it from the server
					printf("%c", buffer[0]);
			}
				
			printf("\n");
			i++;
			continue;
    	} 
    	
    	if ( strcmp(argv[i],"put" ) == 0) { 					//if argv[i]=put
    		prot = 112;
    		writen(sockfd,&prot,1);	 							//send p
			
			if (argv[i+1]==NULL) break;							//if we haven't found key or value stop the connection
			if (argv[i+2]==NULL) break;
			
			writen(sockfd,argv[i+1],strlen(argv[i+1])+1);		//else, take both of them
			writen(sockfd,argv[i+2],strlen(argv[i+2])+1);
			i+=2;
			continue;
    	}  
  	}

	shutdown(sockfd, SHUT_WR);
    close(sockfd); 											// close the socket
    return 0;	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %rbx
movl %edi, -0x30(%rbp)
movq 0x10(%rsi), %rdi
callq 0x10e0
movl %eax, %r12d
movq 0x8(%rbx), %rdi
callq 0x10c0
movq %rax, %r13
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, %r15d
leaq -0x3c(%rbp), %rdi
movq $0x0, 0x4(%rdi)
movw $0x2, -0x4(%rdi)
movq 0x18(%r13), %rax
movq (%rax), %r14
movslq 0x14(%r13), %rdx
movq %rdi, %r13
movq %r14, %rsi
callq 0x10d0
rolw $0x8, %r12w
movw %r12w, -0x2(%r13)
movl (%r14), %eax
movl %eax, (%r13)
leaq -0x40(%rbp), %rsi
movl %r15d, %edi
movl $0x10, %edx
callq 0x10f0
cmpl $0x4, -0x30(%rbp)
jl 0x1476
movl $0x3, %r13d
movslq %r13d, %r14
movq (%rbx,%r14,8), %r12
movq %r12, %rdi
leaq 0xceb(%rip), %rsi       # 0x2004
callq 0x10b0
testl %eax, %eax
je 0x13ab
movq %r12, %rdi
leaq 0xcd8(%rip), %rsi       # 0x2008
callq 0x10b0
testl %eax, %eax
jne 0x1469
movb $0x70, -0x29(%rbp)
movl $0x1, %edx
movl %r15d, %edi
leaq -0x29(%rbp), %rsi
callq 0x120c
movq 0x8(%rbx,%r14,8), %r12
testq %r12, %r12
je 0x1476
cmpq $0x0, 0x10(%rbx,%r14,8)
je 0x1476
leal 0x2(%r14), %r13d
addq $0x2, %r14
movq %r12, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x120c
movq (%rbx,%r14,8), %r14
movq %r14, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x120c
jmp 0x1469
movb $0x67, -0x29(%rbp)
movl $0x1, %edx
movl %r15d, %edi
leaq -0x29(%rbp), %rsi
callq 0x120c
movq 0x8(%rbx,%r14,8), %r12
testq %r12, %r12
je 0x1476
incl %r13d
movq %r12, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x120c
movl $0x1, %edx
movl %r15d, %edi
leaq -0x440(%rbp), %r14
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1030
movzbl -0x440(%rbp), %eax
cmpl $0x6e, %eax
je 0x145f
cmpl $0x66, %eax
jne 0x145f
movl $0x1, %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10a0
testl %eax, %eax
setg %cl
movb -0x440(%rbp), %al
testb %al, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x145f
movsbl %al, %edi
callq 0x1040
movl $0x1, %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10a0
testl %eax, %eax
jle 0x145f
movb -0x440(%rbp), %al
testb %al, %al
jne 0x1439
movl $0xa, %edi
callq 0x1040
incl %r13d
cmpl -0x30(%rbp), %r13d
jl 0x1308
movl %r15d, %edi
movl $0x1, %esi
callq 0x1070
movl %r15d, %edi
callq 0x1090
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq