// File: /workspace/github_c_makefile_star2_O0/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
subq $0x210, %rsp            # imm = 0x210
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1250
movl %eax, 0x3a4f(%rip)      # 0x6624
cmpl $0x0, 0x3a48(%rip)      # 0x6624
jge 0x2bea
leaq 0x15d6(%rip), %rdi      # 0x41bb
callq 0x1180
movl $0x10, 0x3a34(%rip)     # 0x6628
movslq 0x3a2d(%rip), %rdx      # 0x6628
leaq 0x3a2a(%rip), %rdi      # 0x662c
xorl %esi, %esi
callq 0x10a0
movw $0x2, 0x3a1a(%rip)      # 0x662c
movl $0x0, 0x3a14(%rip)      # 0x6630
movl 0x351e(%rip), %eax      # 0x6140
movzwl %ax, %edi
callq 0x1080
movw %ax, 0x39fd(%rip)       # 0x662e
movl $0x10, -0x24(%rbp)
movl 0x39e6(%rip), %edi      # 0x6624
movl 0x39e4(%rip), %edx      # 0x6628
leaq 0x39e1(%rip), %rsi      # 0x662c
callq 0x1150
cmpl $0x0, %eax
jge 0x2c61
leaq 0x156e(%rip), %rdi      # 0x41ca
callq 0x1180
jmp 0x2c63
movl $0x104, %edi            # imm = 0x104
callq 0x1120
movq %rax, 0x39cc(%rip)      # 0x6640
movl $0x10, %edi
callq 0x1120
movq %rax, -0x20(%rbp)
movl 0x399c(%rip), %edi      # 0x6624
movq 0x39b1(%rip), %rsi      # 0x6640
movq -0x20(%rbp), %r8
movl $0x104, %edx            # imm = 0x104
xorl %ecx, %ecx
leaq -0x24(%rbp), %r9
callq 0x1140
movl %eax, 0x3977(%rip)      # 0x6620
cmpl $0x0, 0x3970(%rip)      # 0x6620
jge 0x2cbe
leaq 0x1445(%rip), %rdi      # 0x40fe
callq 0x1180
movq 0x397b(%rip), %rax      # 0x6640
movslq 0x3954(%rip), %rcx      # 0x6620
movb $0x0, (%rax,%rcx)
movq 0x3969(%rip), %rdi      # 0x6640
callq 0x17e0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2cf5
leaq 0x14e4(%rip), %rdi      # 0x41d2
movb $0x0, %al
callq 0x1090
movq 0x3944(%rip), %rdi      # 0x6640
callq 0x1030
cmpq $0x0, -0x10(%rbp)
je 0x306d
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x306d
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1250
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x2d4c
leaq 0x147c(%rip), %rdi      # 0x41c3
callq 0x1180
movl -0x28(%rbp), %edi
movq -0x20(%rbp), %rsi
movl $0x8, %edx
callq 0x11d0
callq 0x1220
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x306b
cmpl $0x0, -0x28(%rbp)
je 0x306b
movq $0x0, -0x30(%rbp)
movl $0x0, -0x148(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x28(%rbp), %edi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x2540
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x2db5
xorl %edi, %edi
callq 0x11c0
jmp 0x2db7
leaq -0x1e0(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movl $0x0, -0x1e4(%rbp)
movl -0x1e4(%rbp), %eax
cmpq $0x10, %rax
jae 0x2e01
movq -0x1f0(%rbp), %rax
movl -0x1e4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e4(%rbp)
jmp 0x2dcf
jmp 0x2e03
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x28(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x1e0(%rbp,%rax,8), %rcx
movq %rcx, -0x1e0(%rbp,%rax,8)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x160(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x158(%rbp)
xorl %eax, %eax
cmpl $0x3, -0x14c(%rbp)
movb %al, -0x1f1(%rbp)
jge 0x2e7b
cmpl $0x0, -0x148(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x1f1(%rbp)
movb -0x1f1(%rbp), %al
testb $0x1, %al
jne 0x2e8a
jmp 0x2f93
movl -0x28(%rbp), %edi
addl $0x1, %edi
leaq -0x1e0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x160(%rbp), %r8
movq %rcx, %rdx
callq 0x1110
movl %eax, -0x144(%rbp)
cmpl $0x0, -0x144(%rbp)
je 0x2f73
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1e0(%rbp,%rax,8), %rax
movq %rax, -0x200(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x200(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2f73
movl -0x28(%rbp), %edi
leaq -0x140(%rbp), %rsi
movq -0x20(%rbp), %r8
movl $0x104, %edx            # imm = 0x104
xorl %ecx, %ecx
leaq -0x24(%rbp), %r9
callq 0x1140
movl %eax, 0x36fb(%rip)      # 0x6620
cmpl $0x0, 0x36f4(%rip)      # 0x6620
jge 0x2f41
leaq 0x11c9(%rip), %rdi      # 0x40fe
callq 0x1180
xorl %edi, %edi
callq 0x11c0
movslq 0x36d8(%rip), %rax      # 0x6620
movb $0x0, -0x140(%rbp,%rax)
leaq -0x140(%rbp), %rdi
callq 0x17e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x2f71
movl $0x1, -0x148(%rbp)
jmp 0x2f8e
leaq -0x160(%rbp), %rdi
callq 0x1750
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x2e58
cmpl $0x0, -0x148(%rbp)
je 0x3053
cmpq $0x0, -0x30(%rbp)
je 0x3053
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1e0(%rbp,%rax,8), %rax
movq %rax, -0x208(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x208(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x302a
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0x28(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq -0x1e0(%rbp,%rax,8), %rcx
movq %rcx, -0x1e0(%rbp,%rax,8)
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %esi
movq -0x30(%rbp), %rdx
movl -0x28(%rbp), %ecx
movq -0x38(%rbp), %r8
movq -0x20(%rbp), %r9
movl -0x24(%rbp), %eax
movl %eax, (%rsp)
callq 0x2920
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
movl -0x28(%rbp), %edi
callq 0x10c0
xorl %edi, %edi
callq 0x11c0
jmp 0x306d
jmp 0x2c63
nopw %cs:(%rax,%rax)