// File: /workspace/github_c_makefile_star2_O0/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
subq $0x4a0, %rsp            # imm = 0x4A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1110
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10f0
movq %rax, -0x38(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1130
movl %eax, -0x14(%rbp)
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10b0
movw $0x2, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
leaq -0x2c(%rbp), %rdi
addq $0x4, %rdi
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x1100
movl -0x18(%rbp), %eax
movzwl %ax, %edi
callq 0x1090
movw %ax, -0x2a(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x14(%rbp), %edi
leaq -0x2c(%rbp), %rsi
movl $0x10, %edx
callq 0x1120
movl $0x3, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1663
leaq -0x44f(%rbp), %rdi
leaq 0xc4f(%rip), %rsi       # 0x2004
callq 0x1050
leaq -0x45e(%rbp), %rdi
leaq 0xc40(%rip), %rsi       # 0x2008
callq 0x1050
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xc24(%rip), %rsi       # 0x2004
callq 0x10e0
cmpl $0x0, %eax
jne 0x153a
movb $0x67, -0x46e(%rbp)
movl -0x14(%rbp), %edi
leaq -0x46e(%rbp), %rsi
movl $0x1, %edx
callq 0x1240
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x1422
jmp 0x1663
movl -0x14(%rbp), %eax
movl %eax, -0x47c(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x478(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1080
movl -0x47c(%rbp), %edi
movq -0x478(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x1240
movl -0x14(%rbp), %edi
leaq -0x440(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movsbl -0x440(%rbp), %eax
cmpl $0x6e, %eax
jne 0x14b0
leaq 0xb71(%rip), %rdi       # 0x200c
movb $0x0, %al
callq 0x10a0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1655
movsbl -0x440(%rbp), %eax
cmpl $0x66, %eax
jne 0x151c
jmp 0x14be
movl -0x14(%rbp), %edi
leaq -0x440(%rbp), %rsi
movl $0x1, %edx
callq 0x10d0
movl %eax, %ecx
movl %ecx, -0x40(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x47d(%rbp)
jle 0x14f7
movsbl -0x440(%rbp), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x47d(%rbp)
movb -0x47d(%rbp), %al
testb $0x1, %al
jne 0x1503
jmp 0x151a
movsbl -0x440(%rbp), %esi
leaq 0xafd(%rip), %rdi       # 0x200e
movb $0x0, %al
callq 0x10a0
jmp 0x14be
jmp 0x151c
jmp 0x151e
leaq 0xae7(%rip), %rdi       # 0x200c
movb $0x0, %al
callq 0x10a0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1655
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xabb(%rip), %rsi       # 0x2008
callq 0x10e0
cmpl $0x0, %eax
jne 0x1653
movb $0x70, -0x46e(%rbp)
movl -0x14(%rbp), %edi
leaq -0x46e(%rbp), %rsi
movl $0x1, %edx
callq 0x1240
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x158f
jmp 0x1663
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x15a8
jmp 0x1663
movl -0x14(%rbp), %eax
movl %eax, -0x49c(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x498(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1080
movl -0x49c(%rbp), %edi
movq -0x498(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x1240
movl -0x14(%rbp), %eax
movl %eax, -0x48c(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x488(%rbp)
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1080
movl -0x48c(%rbp), %edi
movq -0x488(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x1240
movl -0x3c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1655
jmp 0x1655
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x139b
movl -0x14(%rbp), %edi
movl $0x1, %esi
callq 0x1070
movl -0x14(%rbp), %edi
callq 0x10c0
xorl %eax, %eax
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq