// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0xa, -0x24(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0xcc(%rbp)
callq 0x1760
movq %rax, -0xe0(%rbp)
movl $0x18, %edi
callq 0x1120
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rax
movl $0x3, 0x10(%rax)
movq -0xe0(%rbp), %rax
movl $0x100, 0xc(%rax)       # imm = 0x100
movq -0xe0(%rbp), %rdi
callq 0x1900
movq %rax, -0xc0(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x104(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x1070
movl -0x104(%rbp), %edi
movq -0x100(%rbp), %rsi
movq %rax, %rdx
movq -0x18(%rbp), %r8
movl -0x1c(%rbp), %r9d
xorl %ecx, %ecx
callq 0x10b0
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x2635
leaq 0x1b48(%rip), %rdi      # 0x4159
callq 0x1180
movq -0xc0(%rbp), %rdi
callq 0x1030
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x11c0
jmp 0x2637
leaq -0xb8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0xec(%rbp)
movl -0xec(%rbp), %eax
cmpq $0x10, %rax
jae 0x2681
movq -0xf8(%rbp), %rax
movl -0xec(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x264f
jmp 0x2683
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0xc(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xb8(%rbp,%rax,8), %rcx
movq %rcx, -0xb8(%rbp,%rax,8)
movslq -0x24(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x1, -0xc8(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xcc(%rbp)
movb %al, -0x105(%rbp)
jne 0x26f2
cmpl $0xa, -0x20(%rbp)
setl %al
movb %al, -0x105(%rbp)
movb -0x105(%rbp), %al
testb $0x1, %al
jne 0x2701
jmp 0x2866
movl -0xc(%rbp), %edi
addl $0x1, %edi
leaq -0xb8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x38(%rbp), %r8
movq %rcx, %rdx
callq 0x1110
movl %eax, -0xc8(%rbp)
cmpl $0x0, -0xc8(%rbp)
jge 0x2740
leaq 0x1a2a(%rip), %rdi      # 0x415e
callq 0x1180
xorl %edi, %edi
callq 0x11c0
cmpl $0x0, -0xc8(%rbp)
je 0x2858
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb8(%rbp,%rax,8), %rax
movq %rax, -0x110(%rbp)
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x110(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2858
movl -0xc(%rbp), %edi
movq -0xc0(%rbp), %rsi
movq -0x18(%rbp), %r8
movl $0x100, %edx            # imm = 0x100
xorl %ecx, %ecx
leaq -0x1c(%rbp), %r9
callq 0x1140
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x27d0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2856
movq -0xc0(%rbp), %rdi
callq 0x17e0
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
je 0x2854
movq -0xd8(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x2854
movl $0x1, -0xcc(%rbp)
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0xe8(%rbp), %rax
movl %ecx, (%rax)
movslq -0x24(%rbp), %rcx
subq -0x38(%rbp), %rcx
movq -0xe8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0xe8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xe8(%rbp), %rdi
callq 0x1c70
movl $0x1, -0xcc(%rbp)
jmp 0x2856
jmp 0x2858
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x26d4
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb8(%rbp,%rax,8), %rax
movq %rax, -0x118(%rbp)
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x118(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x28e5
movl -0xc(%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 -0xc(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq -0xb8(%rbp,%rax,8), %rcx
movq %rcx, -0xb8(%rbp,%rax,8)
cmpl $0x0, -0xcc(%rbp)
je 0x28fb
movq -0xe8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x290f
movq -0xe8(%rbp), %rdi
callq 0x1030
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)