// File: /workspace/github_c_makefile_star2_O1/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 $0x1d8, %rsp            # imm = 0x1D8
movl %esi, -0x34(%rbp)
movl %edi, -0x30(%rbp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, 0x3032(%rip)      # 0x5614
testl %eax, %eax
jns 0x25f2
leaq 0xb6b(%rip), %rdi       # 0x3158
callq 0x1170
movl $0x10, %eax
movl %eax, 0x301b(%rip)      # 0x5618
xorps %xmm0, %xmm0
movups %xmm0, 0x3015(%rip)     # 0x561c
leaq 0x300e(%rip), %rsi      # 0x561c
movw $0x2, 0x3005(%rip)      # 0x561c
movzwl 0x2b1a(%rip), %ecx      # 0x5138
rolw $0x8, %cx
movl $0x0, 0x2ff4(%rip)      # 0x5620
movw %cx, 0x2feb(%rip)       # 0x561e
movl %eax, -0x2c(%rbp)
movl 0x2fd8(%rip), %edi      # 0x5614
movl $0x10, %edx
callq 0x1140
testl %eax, %eax
jns 0x2656
leaq 0xb16(%rip), %rdi       # 0x3167
callq 0x1170
leaq -0x2c(%rbp), %rbx
leaq 0xa8e(%rip), %r13       # 0x30ef
leaq 0xb9c(%rip), %r14       # 0x3204
movl $0x104, %edi            # imm = 0x104
callq 0x1110
movq %rax, %r12
movq %rax, 0x2fb4(%rip)      # 0x5630
movl $0x10, %edi
callq 0x1110
movq %rax, %r15
movl 0x2f85(%rip), %edi      # 0x5614
movl $0x104, %edx            # imm = 0x104
movq %r12, %rsi
xorl %ecx, %ecx
movq %rax, %r8
movq %rbx, %r9
callq 0x1130
movl %eax, 0x2f66(%rip)      # 0x5610
testl %eax, %eax
js 0x274b
movq 0x2f77(%rip), %rax      # 0x5630
movslq 0x2f50(%rip), %rcx      # 0x5610
movb $0x0, (%rax,%rcx)
movq 0x2f65(%rip), %rdi      # 0x5630
callq 0x17ae
testq %rax, %rax
je 0x2732
movq %rax, %r12
movq 0x2f51(%rip), %rdi      # 0x5630
callq 0x1030
testb $0x1, 0x10(%r12)
je 0x2668
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, %r12d
testl %eax, %eax
js 0x2758
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 0x2668
jmp 0x2766
movq %r14, %rdi
callq 0x1060
movq 0x2eef(%rip), %rdi      # 0x5630
callq 0x1030
jmp 0x2668
movq %r13, %rdi
callq 0x1170
jmp 0x26b2
leaq 0xa01(%rip), %rdi       # 0x3160
callq 0x1170
jmp 0x2708
movl -0x2c(%rbp), %edx
movl %r12d, %edi
movq %r15, %rsi
callq 0x2119
testq %rax, %rax
je 0x2919
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %rdi
movaps %xmm0, 0x70(%rdi)
movaps %xmm0, 0x60(%rdi)
movaps %xmm0, 0x50(%rdi)
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
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
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
sarl $0x6, %esi
movslq %esi, %rcx
movq %rdx, -0x50(%rbp)
movq %rcx, -0x48(%rbp)
orq %rdx, -0xf0(%rbp,%rcx,8)
movq 0x8(%rax), %rcx
leaq -0x68(%rbp), %rdx
movq %rcx, (%rdx)
movq %rax, -0x58(%rbp)
movq 0x10(%rax), %rax
movq %rax, 0x8(%rdx)
movl %r12d, %eax
incl %eax
movl %eax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x1f0(%rbp), %rbx
xorl %r13d, %r13d
xorl %r14d, %r14d
movl -0x38(%rbp), %edi
leaq -0xf0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x68(%rbp), %r8
callq 0x1100
testl %eax, %eax
je 0x2889
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
testq %rax, -0xf0(%rbp,%rcx,8)
je 0x2889
movl $0x104, %edx            # imm = 0x104
movl %r12d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
movq %r15, %r8
leaq -0x2c(%rbp), %r9
callq 0x1130
movl %eax, 0x2db6(%rip)      # 0x5610
testl %eax, %eax
js 0x290d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1f0(%rbp,%rax)
movq %rbx, %rdi
callq 0x17ae
movq %rax, -0x40(%rbp)
testq %rax, %rax
movl $0x1, %eax
cmovnel %eax, %r14d
jmp 0x288c
incl %r13d
cmpl $0x2, %r13d
jg 0x289b
testl %r14d, %r14d
je 0x280e
testl %r14d, %r14d
setne %al
movq -0x40(%rbp), %rbx
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x28fb
movq -0x48(%rbp), %rdx
movq -0xf0(%rbp,%rdx,8), %rax
movq -0x50(%rbp), %rcx
testq %rcx, %rax
je 0x28d5
notq %rcx
andq %rcx, %rax
movq %rax, -0xf0(%rbp,%rdx,8)
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl -0x30(%rbp), %edi
movl -0x34(%rbp), %esi
movq %rbx, %rdx
movl %r12d, %ecx
movq -0x58(%rbp), %r8
movq %r15, %r9
callq 0x2345
movq %rbx, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl %r12d, %edi
callq 0x10c0
jmp 0x2919
leaq 0x7db(%rip), %rdi       # 0x30ef
callq 0x1170
xorl %edi, %edi
callq 0x11b0