// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/servidorc2.c
// Function: aceitaLigacao
ConState* aceitaLigacao(int s, struct sockaddr_in *client, int clientLen){
	int try, timeout = 10;
	struct timeval t;
	fd_set fds;	
	try = 0;
	
	char *buffer;
	int n, r, conected = 0;
	Datagrama *recebido, *enviar = initDatagrama();
	ConState *con = (ConState *)malloc(sizeof(struct con_estado));
	
	/*Construir um segmento com um SYN ACK para enviar ao cliente*/
	/*Coloca tambem no campo win_size o tamanho do buffer*/
	enviar->flags = (FL_SYN | FL_ACK);
	enviar->win_size = TAM2;
	buffer = muxDatagrama(enviar);
	
	n = sendto(s,buffer,strlen(buffer), 0, (struct sockaddr *)client, clientLen);
	if (n  < 0) {
		perror("send");
		free(buffer);
		free(client);
		exit(1);
		}
	/*Esperar por um ack usando o select de forma a que o recvfrom nao bloqueie
	Desta forma pode-se obter o valor do RTT uma vez que quando o select retorna 
	devido á escrita em um dos fds ele coloca no timeval o tempo restante*/
	
	
	FD_ZERO(&fds); FD_SET(s, &fds); //inicializa fds e coloca sock em fds
	t.tv_sec = timeout; t.tv_usec = 0; //inicializa o timeval
	
        r = 1;
        
	while(!conected && try < 10 ){
	
	r = select(s + 1, &fds, NULL, NULL, &t);
	if(r < 0) {
	perror("select");
	exit(0);
	}
	if(r && FD_ISSET(s, &fds)){
		n = recvfrom(s,buffer,TAM2,0, (struct sockaddr *) client, (socklen_t*) &clientLen);
		if(n < 0){ //Nao foi lido nada
			try++;
		}
		else {
			recebido = demuxDatagrama(buffer);
			if(recebido && recebido->flags & FL_ACK){
			conected = 1;
			/*three way hand shake completo*/
			/*retiro neste ultimo segmento recevido o tamanho do buffer do cliente e tambem o rtt*/
			con->clientBuffer = recebido->win_size;
			con->rtt.tv_sec = (timeout - t.tv_sec);
			con->rtt.tv_usec = t.tv_usec;
			printConState(con);
			conected = 1;
			}
			}
		}
	try++;
	}
	if(FD_ISSET(s, &fds)) FD_CLR(s, &fds);
	if(conected) return con; else{free(con); return NULL;} 
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %edx, -0x30(%rbp)
movq %rsi, %r12
movl %edi, %r13d
movl %edx, -0x34(%rbp)
movl $0xe0, %edi
callq 0x1110
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0x18, %edi
callq 0x1110
movq %rax, -0x40(%rbp)
movabsq $0x300000100, %rax      # imm = 0x300000100
movq %rax, 0xc(%r14)
movq %r14, %rdi
callq 0x1891
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
xorl %r15d, %r15d
movl %r13d, %edi
movq %r14, %rbx
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r12, -0x48(%rbp)
movq %r12, %r8
movl -0x30(%rbp), %r9d
callq 0x10b0
testl %eax, %eax
js 0x22ff
leaq -0xe0(%rbp), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
leal 0x3f(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r13d, %ecx
subl %edx, %ecx
movl $0x1, %r12d
shlq %cl, %r12
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
sarl $0x6, %eax
cltq
movq %rax, -0x30(%rbp)
orq %r12, -0xe0(%rbp,%rax,8)
leaq -0x60(%rbp), %rax
movq $0xa, (%rax)
movq $0x0, 0x8(%rax)
movq %r13, -0x50(%rbp)
leal 0x1(%r13), %r14d
movl %r14d, %edi
leaq -0xe0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x1100
testl %eax, %eax
js 0x22fa
je 0x22a5
movq -0x30(%rbp), %rax
testq %r12, -0xe0(%rbp,%rax,8)
je 0x22a5
movq %rbx, %r13
movl $0x100, %edx            # imm = 0x100
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
movq -0x48(%rbp), %r8
leaq -0x34(%rbp), %r9
callq 0x1130
testl %eax, %eax
js 0x22a2
movq %r13, %rdi
callq 0x17d7
testq %rax, %rax
je 0x22a5
testb $0x2, 0x10(%rax)
je 0x22a5
jmp 0x22d2
incl %r15d
leal 0x1(%r15), %eax
cmpl $0x9, %r15d
movl %eax, %r15d
jl 0x223c
movq -0x40(%rbp), %rdi
callq 0x1030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rax), %eax
movl $0xa, %ecx
subq -0x60(%rbp), %rcx
movq -0x40(%rbp), %rbx
movl %eax, (%rbx)
movq %rcx, 0x8(%rbx)
movq -0x58(%rbp), %rax
movq %rax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x1b3a
jmp 0x22bd
callq 0x137e
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x134d