// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/servidorc2.c
// Function: servidor
void servidor(int te, int tp) {
	Datagrama *novo;
	int pid, size;
	struct sockaddr_in *from;
	int fromlen, newSock;
        sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (sock < 0)
                perror("Opening socket");
        length = sizeof(serv_addr);
        
        memset(&serv_addr, '\0', length);
        
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = htons(portno);
        
        fromlen = sizeof(struct sockaddr_in);
        
        if (bind(sock,(struct sockaddr *)&serv_addr,length)<0)
                perror("binding");
        
        while(1) {
                buf = (char *)malloc(sizeof(char)*TAM2 +4);
                from = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in));
                
                n = recvfrom(sock,buf,TAM2 + 4,0, (struct sockaddr *)from,(socklen_t*) &fromlen);
                if (n < 0){
                        
                        perror("recvfrom");
                        }
                
                buf[n]='\0';
                
                /*Passar os dados que recebeu para um datagrama para os poder interpretar*/
                novo = demuxDatagrama(buf);
                if(!novo){
                printf("Formato de datagrama nao conhecido!!!\n");
                }
                free(buf);
                if(novo && novo->flags & FL_SYN){ //Tentativa de ligação
               		size = novo->len;
                		/*Filtra mensagens UDP que sejam destinadas a esta conexão*/
               		newSock =  socket(PF_INET, SOCK_DGRAM, 0);
                	if(newSock < 0) perror("socket");
                	
                	connect(newSock, (struct sockaddr *) from, sizeof(from));
                		
                		pid = fork(); //Cria processo para a ligação
                		if(pid == 0 && newSock){//processo que trata a ligação estabelecida
                			Datagrama *dados = NULL;
                			ConState *con;
                			char buffer[TAM2];
                			int r, pedido = 0, try;
					struct timeval t;
					fd_set fds;	
					try = 0;
                			if((con = aceitaLigacao(newSock, from, fromlen))==NULL) exit(0);
                			//Usar um select para não ficar eternamente á espera de pedidos
                			FD_ZERO(&fds); FD_SET(newSock, &fds); //inicializa fds e coloca sock em fds
					t.tv_sec = con->rtt.tv_sec; t.tv_usec = con->rtt.tv_usec; //inicializa o timeval
                			
                			while(try < 3 && !pedido){
		        			
		        			r = select(newSock + 1, &fds, NULL, NULL, &t);
			
						if(r && FD_ISSET(newSock, &fds)){
							n = recvfrom(newSock,buffer,TAM2+4,0, (struct sockaddr *)from,(socklen_t*) &fromlen);
							if(n < 0){
								perror("recvfrom");
								exit(0);
				  			}
				  			buffer[n] = '\0';
				  			dados = demuxDatagrama(buffer);
				  			if(dados) pedido = 1;
			  			}
			  			else {
			  				aumentaTimeout(&t);
			  				try++;
			  				}
		  			}
		        		if(pedido && dados){
		        			if(FD_ISSET(newSock, &fds)) FD_CLR(newSock, &fds);
						trataPedido(te, tp, dados, newSock, con, from, fromlen);
						free(dados);
						}	//envia pedido
		        		free(from);
		        		close(newSock);
		        		exit(0);
                		}
                	}
               /* n = sendto(sock,"Recebido\n",17, 0,(struct sockaddr *)&from,fromlen);
                if (n  < 0)
                        perror("sendto");*/
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movl %esi, -0x34(%rbp)
movl %edi, -0x30(%rbp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, 0x3085(%rip)      # 0x5614
testl %eax, %eax
jns 0x25a5
leaq 0xbbe(%rip), %rdi       # 0x3158
callq 0x1170
movl 0x306f(%rip), %eax      # 0x5614
movl $0x10, %ecx
movl %ecx, 0x3068(%rip)      # 0x5618
movq $0x0, 0x3069(%rip) # 0x5624
leaq 0x305a(%rip), %rsi      # 0x561c
movw $0x2, 0x3051(%rip)      # 0x561c
movl $0x0, 0x304b(%rip)      # 0x5620
movzwl 0x2b5c(%rip), %edx      # 0x5138
rolw $0x8, %dx
movw %dx, 0x3037(%rip)       # 0x561e
movl %ecx, -0x2c(%rbp)
movl %eax, %edi
movl $0x10, %edx
callq 0x1140
testl %eax, %eax
jns 0x2606
leaq 0xb66(%rip), %rdi       # 0x3167
callq 0x1170
leaq -0x2c(%rbp), %rbx
leaq 0xade(%rip), %r13       # 0x30ef
leaq 0xbec(%rip), %r14       # 0x3204
movl $0x104, %edi            # imm = 0x104
callq 0x1110
movq %rax, %r12
movq %rax, 0x3004(%rip)      # 0x5630
movl $0x10, %edi
callq 0x1110
movq %rax, %r15
movl 0x2fd5(%rip), %edi      # 0x5614
movl $0x104, %edx            # imm = 0x104
movq %r12, %rsi
xorl %ecx, %ecx
movq %rax, %r8
movq %rbx, %r9
callq 0x1130
movl %eax, 0x2fb6(%rip)      # 0x5610
testl %eax, %eax
js 0x26f2
movq 0x2fc7(%rip), %rcx      # 0x5630
cltq
movb $0x0, (%rcx,%rax)
movq 0x2fba(%rip), %rdi      # 0x5630
callq 0x17d7
testq %rax, %rax
je 0x26d9
movq %rax, %r12
movq 0x2fa6(%rip), %rdi      # 0x5630
callq 0x1030
testb $0x1, 0x10(%r12)
je 0x2618
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, %r12d
testl %eax, %eax
js 0x2705
movl %r12d, %edi
movq %r15, %rsi
movl $0x8, %edx
callq 0x11c0
callq 0x1210
testl %eax, %eax
setne %al
testl %r12d, %r12d
sete %cl
orb %al, %cl
jne 0x2618
jmp 0x2713
movq %r14, %rdi
callq 0x1060
movq 0x2f48(%rip), %rdi      # 0x5630
callq 0x1030
jmp 0x2618
movq %r13, %rdi
callq 0x1170
movl 0x2f10(%rip), %eax      # 0x5610
jmp 0x2662
leaq 0xa54(%rip), %rdi       # 0x3160
callq 0x1170
jmp 0x26af
movl -0x2c(%rbp), %edx
movl %r12d, %edi
movq %r15, %rsi
callq 0x2130
testq %rax, %rax
je 0x28a9
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %rcx
movaps %xmm0, 0x70(%rcx)
movaps %xmm0, 0x60(%rcx)
movaps %xmm0, 0x50(%rcx)
movaps %xmm0, 0x40(%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, (%rcx)
leal 0x3f(%r12), %esi
testl %r12d, %r12d
cmovnsl %r12d, %esi
movl %esi, %edx
andl $-0x40, %edx
movl %r12d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %esi
movslq %esi, %rcx
movq %rdx, -0x48(%rbp)
movq %rcx, -0x40(%rbp)
orq %rdx, -0xe0(%rbp,%rcx,8)
movq 0x8(%rax), %rcx
leaq -0x60(%rbp), %rdx
movq %rcx, (%rdx)
movq %rax, -0x50(%rbp)
movq 0x10(%rax), %rax
movq %rax, 0x8(%rdx)
movl %r12d, %ebx
incl %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %ebx, %edi
leaq -0xe0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x1100
testl %eax, %eax
je 0x2823
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
testq %rax, -0xe0(%rbp,%rcx,8)
je 0x2823
movl $0x104, %edx            # imm = 0x104
movl %r12d, %edi
leaq -0x1e0(%rbp), %r13
movq %r13, %rsi
xorl %ecx, %ecx
movq %r15, %r8
leaq -0x2c(%rbp), %r9
callq 0x1130
movl %eax, 0x2e15(%rip)      # 0x5610
testl %eax, %eax
js 0x289d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1e0(%rbp,%rax)
movq %r13, %rdi
callq 0x17d7
movq %rax, %r13
testq %rax, %rax
setne %al
jmp 0x2828
incl %r14d
xorl %eax, %eax
cmpl $0x2, %r14d
jg 0x2836
testb %al, %al
je 0x27a9
testq %r13, %r13
setne %cl
andb %cl, %al
cmpb $0x1, %al
jne 0x288b
movq -0x40(%rbp), %rdx
movq -0xe0(%rbp,%rdx,8), %rax
movq -0x48(%rbp), %rcx
testq %rcx, %rax
je 0x2865
notq %rcx
andq %rcx, %rax
movq %rax, -0xe0(%rbp,%rdx,8)
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl -0x30(%rbp), %edi
movl -0x34(%rbp), %esi
movq %r13, %rdx
movl %r12d, %ecx
movq -0x50(%rbp), %r8
movq %r15, %r9
callq 0x230b
movq %r13, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl %r12d, %edi
callq 0x10c0
jmp 0x28a9
leaq 0x84b(%rip), %rdi       # 0x30ef
callq 0x1170
xorl %edi, %edi
callq 0x11b0