// File: /workspace/github_c_makefile_star2_O2/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, %r12d
movl %edi, -0x40(%rbp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1230
movl %eax, 0x32b9(%rip)      # 0x5614
testl %eax, %eax
js 0x263a
andq $0x0, 0x32b9(%rip)      # 0x5624
pushq $0x10
popq %rdx
movl %edx, 0x32a4(%rip)      # 0x5618
leaq 0x32a1(%rip), %rsi      # 0x561c
movw $0x2, 0x3298(%rip)      # 0x561c
andl $0x0, 0x3295(%rip)      # 0x5620
movzwl 0x2d9e(%rip), %ecx      # 0x5130
rolw $0x8, %cx
movw %cx, 0x3281(%rip)       # 0x561e
movl %edx, -0x2c(%rbp)
movl %eax, %edi
callq 0x1130
testl %eax, %eax
js 0x2651
pushq $0x10
popq %r13
leaq -0x2c(%rbp), %rbx
pushq $0x2
popq %r14
movl $0x104, %edi            # imm = 0x104
callq 0x1100
movq %rax, %r15
movq %rax, 0x3261(%rip)      # 0x5630
movq %r13, %rdi
callq 0x1100
movl 0x3237(%rip), %edi      # 0x5614
movl $0x104, %edx            # imm = 0x104
movq %r15, %rsi
xorl %ecx, %ecx
movq %rax, -0x38(%rbp)
movq %rax, %r8
movq %rbx, %r9
callq 0x1120
movl %eax, 0x3214(%rip)      # 0x5610
testl %eax, %eax
js 0x2492
movq 0x3225(%rip), %rcx      # 0x5630
cltq
movb $0x0, (%rcx,%rax)
movq 0x3218(%rip), %rdi      # 0x5630
callq 0x160b
testq %rax, %rax
je 0x2475
movq %rax, %r15
movq 0x3204(%rip), %rdi      # 0x5630
callq 0x1030
testb $0x1, 0x10(%r15)
je 0x23bb
movl %r14d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x1230
movl %eax, %r15d
testl %eax, %eax
js 0x24a9
movl %r15d, %edi
movq -0x38(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x11b0
callq 0x1200
testl %eax, %eax
setne %al
testl %r15d, %r15d
sete %cl
orb %al, %cl
jne 0x23bb
jmp 0x24b7
leaq 0xd6c(%rip), %rdi       # 0x31e8
callq 0x1060
movq 0x31a8(%rip), %rdi      # 0x5630
callq 0x1030
jmp 0x23bb
leaq 0xc3a(%rip), %rdi       # 0x30d3
callq 0x1160
movl 0x316c(%rip), %eax      # 0x5610
jmp 0x2404
leaq 0xc94(%rip), %rdi       # 0x3144
callq 0x1160
jmp 0x244c
movl -0x2c(%rbp), %edx
movl %r15d, %edi
movq -0x38(%rbp), %rsi
callq 0x1f19
testq %rax, %rax
je 0x2633
movq %rax, %rsi
xorl %eax, %eax
cmpq $0x10, %rax
je 0x24e8
andq $0x0, -0xe0(%rbp,%rax,8)
incq %rax
jmp 0x24d4
movl %r12d, -0x3c(%rbp)
pushq $0x40
popq %rcx
movl %r15d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %r15d, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rdx, %r13
movq %rax, -0x48(%rbp)
orq %rax, -0xe0(%rbp,%rdx,8)
movq 0x8(%rsi), %rax
leaq -0x60(%rbp), %rcx
movq %rax, (%rcx)
movq %rsi, -0x50(%rbp)
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rcx)
movl %r15d, -0x30(%rbp)
movl %r15d, %r14d
incl %r14d
xorl %ebx, %ebx
leaq -0x1e0(%rbp), %r12
xorl %r15d, %r15d
xorl %eax, %eax
cmpl $0x2, %ebx
ja 0x25c2
testb $0x1, %al
jne 0x25c2
movl %r14d, %edi
leaq -0xe0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x10f0
testl %eax, %eax
je 0x25be
movq -0x48(%rbp), %rax
testq %rax, -0xe0(%rbp,%r13,8)
je 0x25be
movl $0x104, %edx            # imm = 0x104
movl -0x30(%rbp), %edi
movq %r12, %rsi
xorl %ecx, %ecx
movq -0x38(%rbp), %r8
leaq -0x2c(%rbp), %r9
callq 0x1120
movl %eax, 0x307a(%rip)      # 0x5610
testl %eax, %eax
js 0x2627
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1e0(%rbp,%rax)
movq %r12, %rdi
callq 0x160b
movq %rax, %r15
testq %rax, %rax
setne %al
jmp 0x2544
incl %ebx
jmp 0x2542
testq %r15, %r15
setne %cl
andb %cl, %al
cmpb $0x1, %al
jne 0x2614
movq -0xe0(%rbp,%r13,8), %rax
movq -0x48(%rbp), %rcx
testq %rcx, %rax
je 0x25ed
notq %rcx
andq %rcx, %rax
movq %rax, -0xe0(%rbp,%r13,8)
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl -0x40(%rbp), %edi
movl -0x3c(%rbp), %esi
movq %r15, %rdx
movl -0x30(%rbp), %ecx
movq -0x50(%rbp), %r8
movq -0x38(%rbp), %r9
callq 0x2124
movq %r15, %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movl -0x30(%rbp), %edi
callq 0x10b0
jmp 0x2633
leaq 0xaa5(%rip), %rdi       # 0x30d3
callq 0x1160
xorl %edi, %edi
callq 0x11a0
leaq 0xafb(%rip), %rdi       # 0x313c
callq 0x1160
movl 0x2fc8(%rip), %eax      # 0x5614
jmp 0x2363
leaq 0xaf3(%rip), %rdi       # 0x314b
callq 0x1160
jmp 0x23af