// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x6e, -0x31(%rbp)
movb $0x66, -0x32(%rbp)
movl $0x14, %edi
callq 0x10c0
movq %rax, -0xb8(%rbp)
movl $0x14, %edi
callq 0x10c0
movq %rax, -0xc0(%rbp)
movl $0x0, 0x2be7(%rip)      # 0x40a4
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, -0x14(%rbp)
leaq -0x44(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10a0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1100
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xac(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movw $0x2, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movzwl %ax, %edi
callq 0x1090
movw %ax, -0x42(%rbp)
movl -0x14(%rbp), %edi
leaq -0x44(%rbp), %rsi
movl $0x10, %edx
callq 0x10e0
movl -0x14(%rbp), %edi
movl $0x5, %esi
callq 0x10d0
movl $0x10, -0xcc(%rbp)
movl -0x14(%rbp), %edi
leaq -0x44(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
callq 0x10f0
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edi
movq -0xb8(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movq -0xb8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x67, %eax
jne 0x169a
movl $0x0, -0x64(%rbp)
movl -0x18(%rbp), %edi
leaq -0x33(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0xcd(%rbp)
jle 0x15ce
movsbl -0x33(%rbp), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xcd(%rbp)
movb -0xcd(%rbp), %al
testb $0x1, %al
jne 0x15da
jmp 0x15f7
movb -0x33(%rbp), %dl
movq -0xb8(%rbp), %rax
movl -0x64(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x64(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x159a
movq -0xb8(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0xc8(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x10a0
movq -0xb8(%rbp), %rdi
callq 0x12c0
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x1648
movl -0x18(%rbp), %edi
leaq -0x31(%rbp), %rsi
movl $0x1, %edx
callq 0x1220
jmp 0x1695
movl -0x18(%rbp), %edi
leaq -0x32(%rbp), %rsi
movl $0x1, %edx
callq 0x1220
movl -0x18(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x1080
movl -0xdc(%rbp), %edi
movq -0xd8(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x1220
jmp 0x17ac
movq -0xb8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x70, %eax
jne 0x17a8
movl $0x0, -0x64(%rbp)
movl -0x18(%rbp), %edi
leaq -0x33(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0xdd(%rbp)
jle 0x16e8
movsbl -0x33(%rbp), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xdd(%rbp)
movb -0xdd(%rbp), %al
testb $0x1, %al
jne 0x16f4
jmp 0x1711
movb -0x33(%rbp), %dl
movq -0xb8(%rbp), %rax
movl -0x64(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x64(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x16b4
movq -0xb8(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x64(%rbp)
movl -0x18(%rbp), %edi
leaq -0x33(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0xde(%rbp)
jle 0x175b
movsbl -0x33(%rbp), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xde(%rbp)
movb -0xde(%rbp), %al
testb $0x1, %al
jne 0x1767
jmp 0x1784
movb -0x33(%rbp), %dl
movq -0xc0(%rbp), %rax
movl -0x64(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x64(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1727
movq -0xc0(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x1360
jmp 0x17aa
jmp 0x17b1
jmp 0x17ac
jmp 0x156a
jmp 0x154a