// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %rbx
leaq -0x2b(%rbp), %rax
movb $0x6e, (%rax)
leaq -0x2a(%rbp), %rax
movb $0x66, (%rax)
pushq $0x14
popq %r14
movq %r14, %rdi
callq 0x10a0
movq %rax, %r15
movq %r14, %rdi
callq 0x10a0
movq %rax, -0x40(%rbp)
andl $0x0, 0x2cda(%rip)      # 0x4094
pushq $0x2
popq %rdi
pushq $0x1
popq %r13
movl %r13d, %esi
xorl %edx, %edx
callq 0x10f0
movl %eax, %r12d
leaq -0x54(%rbp), %r14
andq $0x0, 0x8(%r14)
movq 0x8(%rbx), %rdi
callq 0x10e0
movl %eax, %ebx
leaq -0x44(%rbp), %rcx
pushq $0x4
popq %r8
movl %r12d, %edi
movl %r13d, %esi
pushq $0x2
popq %rdx
callq 0x1060
movw $0x2, (%r14)
andl $0x0, 0x4(%r14)
rolw $0x8, %bx
movw %bx, 0x2(%r14)
pushq $0x10
popq %rdx
movl %r12d, %edi
movq %r14, %rsi
callq 0x10c0
pushq $0x5
popq %rsi
movl %r12d, -0x30(%rbp)
movl %r12d, %edi
callq 0x10b0
pushq $0x1
popq %r13
leaq -0x29(%rbp), %rbx
movl $0x10, -0x34(%rbp)
movl -0x30(%rbp), %edi
leaq -0x54(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x10d0
movl %eax, %r14d
movl %r14d, %edi
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x1030
movzbl (%r15), %eax
cmpl $0x67, %eax
je 0x1491
cmpl $0x70, %eax
jne 0x1431
xorl %r12d, %r12d
movl %r14d, %edi
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x14fc
movb -0x29(%rbp), %al
testb %al, %al
je 0x14fc
movb %al, (%r15,%r12)
incq %r12
jmp 0x146c
xorl %r12d, %r12d
movl %r14d, %edi
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x14b9
movb -0x29(%rbp), %al
testb %al, %al
je 0x14b9
movb %al, (%r15,%r12)
incq %r12
jmp 0x1494
movl %r12d, %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x125b
testq %rax, %rax
je 0x1548
movq %rax, %r12
movl %r14d, %edi
leaq -0x2a(%rbp), %rsi
movq %r13, %rdx
callq 0x11fc
movq %r12, %rdi
callq 0x1080
leaq 0x1(%rax), %rdx
movl %r14d, %edi
movq %r12, %rsi
callq 0x11fc
jmp 0x144b
movl %r12d, %eax
movb $0x0, (%r15,%rax)
xorl %r12d, %r12d
movl %r14d, %edi
movq %rbx, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x1030
testq %rax, %rax
jle 0x1530
movb -0x29(%rbp), %al
testb %al, %al
je 0x1530
movq -0x40(%rbp), %rcx
movb %al, (%rcx,%r12)
incq %r12
jmp 0x1507
movl %r12d, %eax
movq -0x40(%rbp), %rsi
movb $0x0, (%rsi,%rax)
movq %r15, %rdi
callq 0x12ab
jmp 0x144b
movl %r14d, %edi
leaq -0x2b(%rbp), %rsi
movq %r13, %rdx
callq 0x11fc
jmp 0x144b