// File: /workspace/github_c_makefile_star2_O1/Marinouel[P]Network-Lab/serv4.c
// Function: main
int main(int argc, char *argv[])
{

    int sockfd, newsockfd, portno;
    int status,socket_fd, client_fd,num;
    socklen_t size;
    char  n_found = 110, found = 102;
    char ch;
    struct sockaddr_in serv_addr, cli_addr;
    int n, n1;
    int k,i;
    char val[50];
    char *ptr;
    int on; //new
    char * bfr = malloc(20*sizeof(char  )); //new
    char * bfr2 = malloc(20*sizeof(char  )); //new
    char * my_buffer;

    netlab.next=0; 

    //Here i create the socket from server's side
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)); //new
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)); 
     listen(sockfd,5);
     int len;

     while(1) { 
        len= sizeof(struct sockaddr_in);  
        newsockfd = accept(sockfd, (struct sockaddr *)&serv_addr, &len);
        
       while(1) {
          recv(newsockfd, bfr, 1,0);
     
        if (bfr[0] == 'g')      //if the inptu is then
        {
            i=0; 
            while( recv(newsockfd, &ch, 1,0)>0 && (ch!='\0')){ //while we recieve arg from client
                bfr[i++] = ch;                //store them
            } 
            bfr[i] = '\0';                    //store 0 before the arg
        
          bzero(my_buffer, 0); 
          my_buffer = get(bfr);               //store the result from get function
        
          if ( my_buffer == NULL )            //if we took null
            writen(newsockfd, &n_found, 1);   //return n
          else 
          {
            writen(newsockfd, &found, 1);     //else f for found
            writen(newsockfd, my_buffer, strlen(my_buffer)+1); //return the result from get
          }
          
     //---------------------------------------------------------------------------------------------------   
      } else if (bfr[0] == 'p')           //if the inptu is then
      {      
         i=0; 
         while( recv(newsockfd, &ch, 1,0)>0 && (ch!='\0')){   //while we recieve arg from client
            bfr[i++] = ch;        //store them
         }
        bfr[i] = '\0';            //store 0 before the arg
        
        i=0; 
        while( recv(newsockfd, &ch, 1,0)>0 && (ch!='\0')){  //while we recieve arg from client
          bfr2[i++] = ch;         //store them
        }
        bfr2[i] = '\0';           //store 0 before the arg
        put(bfr, bfr2);           //send key,value to put function
        
     } else 
       break;                     //if we recieved no p or g then stop the connection
  }
 }
  
  close(newsockfd);  
  close(sockfd);  
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
leaq -0x2b(%rbp), %rax
movb $0x6e, (%rax)
leaq -0x2a(%rbp), %rax
movb $0x66, (%rax)
movl $0x14, %edi
callq 0x10a0
movq %rax, %r15
movl $0x14, %edi
callq 0x10a0
movq %rax, %r12
xorl %eax, %eax
movl %eax, 0x2ccc(%rip)      # 0x4094
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x10f0
movl %eax, %r13d
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %r14
movaps %xmm0, (%r14)
movq 0x8(%rbx), %rdi
callq 0x10e0
movl %eax, %ebx
leaq -0x38(%rbp), %rcx
movl %r13d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1060
movw $0x2, (%r14)
xorl %eax, %eax
movl %eax, 0x4(%r14)
rolw $0x8, %bx
movw %bx, 0x2(%r14)
movl %r13d, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x10c0
movl %r13d, -0x30(%rbp)
movl %r13d, %edi
movl $0x5, %esi
callq 0x10b0
leaq -0x29(%rbp), %r13
movl $0x10, -0x34(%rbp)
movl -0x30(%rbp), %edi
leaq -0x50(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x10d0
movl %eax, %ebx
movl $0x1, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x1030
movzbl (%r15), %eax
cmpl $0x67, %eax
je 0x14d9
cmpl $0x70, %eax
jne 0x1448
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
setg %cl
movb -0x29(%rbp), %al
testb %al, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1571
xorl %r14d, %r14d
movb %al, (%r15,%r14)
incq %r14
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x14d1
movb -0x29(%rbp), %al
testb %al, %al
jne 0x14ad
movl %r14d, %eax
jmp 0x1573
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
setg %cl
movb -0x29(%rbp), %al
testb %al, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x152b
xorl %r14d, %r14d
movb %al, (%r15,%r14)
incq %r14
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x1526
movb -0x29(%rbp), %al
testb %al, %al
jne 0x1502
movl %r14d, %eax
jmp 0x152d
xorl %eax, %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x1254
testq %rax, %rax
je 0x15e1
movq %rax, %r14
movl $0x1, %edx
movl %ebx, %edi
leaq -0x2a(%rbp), %rsi
callq 0x11fc
movq %r14, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x11fc
jmp 0x1461
xorl %eax, %eax
movb $0x0, (%r15,%rax)
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
setg %cl
movb -0x29(%rbp), %al
testb %al, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x15ca
xorl %r14d, %r14d
movb %al, (%r12,%r14)
incq %r14
movl $0x1, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x15c5
movb -0x29(%rbp), %al
testb %al, %al
jne 0x15a1
movl %r14d, %eax
jmp 0x15cc
xorl %eax, %eax
movb $0x0, (%r12,%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x12c8
jmp 0x1461
movl $0x1, %edx
movl %ebx, %edi
leaq -0x2b(%rbp), %rsi
callq 0x11fc
jmp 0x1461