// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rsi, %r14
movl %edi, %r12d
movl %edx, -0x30(%rbp)
movl $0xe0, %edi
callq 0x1110
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0x18, %edi
callq 0x1110
movq %rax, -0x38(%rbp)
movabsq $0x300000100, %rax      # imm = 0x300000100
movq %rax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x1867
movq %rax, %rbx
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
xorl %r15d, %r15d
movl %r12d, %edi
movq %rbx, -0x60(%rbp)
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r14, -0x58(%rbp)
movq %r14, %r8
callq 0x10b0
testl %eax, %eax
js 0x2338
leaq -0xf0(%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)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
leal 0x3f(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r12d, %ecx
subl %edx, %ecx
movq %r12, %rdx
movq %rsi, %r12
movl $0x1, %r13d
shlq %cl, %r13
sarl $0x6, %eax
movslq %eax, %r14
orq %r13, -0xf0(%rbp,%r14,8)
leaq -0x50(%rbp), %rbx
movq $0xa, (%rbx)
movq $0x0, 0x8(%rbx)
movq %rdx, -0x68(%rbp)
leal 0x1(%rdx), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x3c(%rbp), %edi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x1100
testl %eax, %eax
js 0x2333
movq -0x60(%rbp), %rsi
je 0x22d8
testq %r13, -0xf0(%rbp,%r14,8)
je 0x22d8
movq %r14, %rbx
movq %r13, %r14
movq %r12, %r13
movl $0x100, %edx            # imm = 0x100
movq -0x68(%rbp), %rdi
xorl %ecx, %ecx
movq -0x58(%rbp), %r8
leaq -0x30(%rbp), %r9
movq %rsi, %r12
callq 0x1130
testl %eax, %eax
js 0x22c8
movq %r12, %rdi
callq 0x17ae
testq %rax, %rax
je 0x22cb
testb $0x2, 0x10(%rax)
movq %r13, %r12
je 0x22ce
movl 0xc(%rax), %eax
movl $0xa, %ecx
subq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rdi
movl %eax, (%rdi)
movq %rcx, 0x8(%rdi)
movq -0x48(%rbp), %rax
movq %rax, 0x10(%rdi)
callq 0x1b06
movl $0x1, -0x2c(%rbp)
jmp 0x22ce
incl %r15d
movq %r13, %r12
movq %r14, %r13
movq %rbx, %r14
leaq -0x50(%rbp), %rbx
cmpl $0x0, -0x2c(%rbp)
jne 0x22ef
leal 0x1(%r15), %eax
cmpl $0x9, %r15d
movl %eax, %r15d
jl 0x2230
movq -0xf0(%rbp,%r14,8), %rax
testq %r13, %rax
je 0x230a
notq %r13
andq %r13, %rax
movq %rax, -0xf0(%rbp,%r14,8)
cmpl $0x0, -0x2c(%rbp)
je 0x2316
movq -0x38(%rbp), %rax
jmp 0x2321
movq -0x38(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x137f
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x134e