// File: /workspace/github_c_makefile_star2_O2/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 $0x428, %rsp            # imm = 0x428
movq %rsi, %rbx
movl %edi, -0x34(%rbp)
movq 0x10(%rsi), %rdi
callq 0x10e0
movl %eax, -0x30(%rbp)
movq %rbx, -0x40(%rbp)
movq 0x8(%rbx), %rdi
callq 0x10c0
movq %rax, %r14
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1100
movl %eax, %r15d
leaq -0x4c(%rbp), %r13
andq $0x0, 0x4(%r13)
movw $0x2, -0x4(%r13)
movq 0x18(%r14), %rax
movq (%rax), %r12
movslq 0x14(%r14), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x10d0
movl -0x30(%rbp), %eax
rolw $0x8, %ax
movw %ax, -0x2(%r13)
movl (%r12), %eax
movl %eax, (%r13)
leaq -0x50(%rbp), %rsi
pushq $0x10
popq %rdx
movl %r15d, %edi
callq 0x10f0
pushq $0x3
popq %rbx
cmpl -0x34(%rbp), %ebx
movq -0x40(%rbp), %r13
jge 0x144a
movslq %ebx, %r12
movq (%r13,%r12,8), %r14
movq %r14, %rdi
leaq 0xce8(%rip), %rsi       # 0x2004
callq 0x10b0
testl %eax, %eax
je 0x13b3
movq %r14, %rdi
leaq 0xcd5(%rip), %rsi       # 0x2008
callq 0x10b0
testl %eax, %eax
jne 0x1443
movb $0x70, -0x29(%rbp)
movl %r15d, %edi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x120c
movq -0x40(%rbp), %rax
movq 0x8(%rax,%r12,8), %r14
testq %r14, %r14
je 0x144a
cmpq $0x0, 0x10(%rax,%r12,8)
je 0x144a
addl $0x2, %ebx
addq $0x2, %r12
movq %r14, %rdi
movq %rax, %r13
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x120c
movq (%r13,%r12,8), %r14
movq %r14, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x120c
jmp 0x1443
movb $0x67, -0x29(%rbp)
movl %r15d, %edi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x120c
movq 0x8(%r13,%r12,8), %r14
testq %r14, %r14
je 0x144a
incl %ebx
movq %r14, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x120c
movl %r15d, %edi
leaq -0x450(%rbp), %r12
movq %r12, %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x1030
movzbl -0x450(%rbp), %eax
cmpl $0x6e, %eax
je 0x143b
cmpl $0x66, %eax
jne 0x143b
movl %r15d, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10a0
testl %eax, %eax
jle 0x143b
movb -0x450(%rbp), %al
testb %al, %al
je 0x143b
movsbl %al, %edi
callq 0x1040
jmp 0x1415
pushq $0xa
popq %rdi
callq 0x1040
incl %ebx
jmp 0x12fd
pushq $0x1
popq %rsi
movl %r15d, %edi
callq 0x1070
movl %r15d, %edi
callq 0x1090
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq