// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/clientec2.c
// Function: conecta
ConState* conecta(int sock){
	char *buffer;
	int res, n, try, synACK = 0;
	try = 0;
	ConState *con = (ConState *)malloc(sizeof(struct con_estado));
	//Variaveis necessarias para o select
	int timeout = 10;
	struct timeval t;
	fd_set fds;
	
	Datagrama *novo = initDatagrama();
	Datagrama *recebido;
	novo->win_size = TAM; //Aquando da conexão o campo win_size irá indicar ao servidor o tamanho do buffer
	novo->flags = FL_SYN;
	buffer = muxDatagrama(novo); //buffer contem o pedido de ligação
	
        
	//Usar o select para usar uma estratégia não bloqueante	
	//Envia pedido de ligação ao servidor
	n = sendto(sock,buffer, strlen(buffer),0,(struct sockaddr *)&serv_addr,(socklen_t)length);
        if (n < 0){
        	perror("Sendto");
        	exit(1);
        	}
        
        FD_ZERO(&fds); FD_SET(sock, &fds); //inicializa fds e coloca sock em fds
	t.tv_sec = timeout; t.tv_usec = 0; //inicializa o timeval
        

        while(try<10 && !synACK){
        	
		res = select(sock + 1, &fds, NULL, NULL, &t);
        	if(res < 0){
        		perror("select");
        		exit(0);
        	}
        	
        	if(res && FD_ISSET(sock, &fds)){ //Verifica se o sock está em fds 
        		n = recvfrom(sock,buffer,TAM2,0,(struct sockaddr *)&from,(socklen_t*) &length);
       	 		if (n < 0){ //Não foi lido nada
       	 		perror("recvFrom");
       	 		//envia novamente buffer (pedido de ligação)
       	 		n = sendto(sock,buffer, strlen(buffer),0,(struct sockaddr *)&serv_addr,(socklen_t)length);
        		if (n < 0){
        			perror("sendto");
        			exit(1);
        		}
        		try++;
       	 		}
        		else {
        		//Obteve resposta
        		buffer[n] = '\0';
        		recebido = demuxDatagrama(buffer);
        		if(recebido && (recebido->flags & FL_SYN) && (recebido->flags & FL_ACK)){
        			/*Recebeu a confirmação para a ligação*/
        			con->clientBuffer = recebido->win_size;
				con->rtt.tv_sec = (timeout - t.tv_sec);
				con->rtt.tv_usec = t.tv_usec;
        			synACK = 1;
        			free(buffer);
        			free(recebido);
        			//envia ack para confirmar ligação e tamanho do buffer em novo->len
        			novo->flags = 0;
        			novo->win_size = TAM;
        			novo->flags = (FL_ACK);
        			buffer = muxDatagrama(novo);
        			sleep(1);
        			n = sendto(sock,buffer, strlen(buffer),0,(struct sockaddr *)&from,(socklen_t)length);
        			if (n < 0){
					perror("Sendto");
					exit(1);
        			}
        			}
        		else {
        			//Recebeu confirmação mas não aceita ligação
        			free(buffer);
        			free(recebido);
        			}
        		}
        	}
        	try++;
        }
	if(FD_ISSET(sock, &fds)) FD_CLR(sock, &fds);
	if(!synACK) return NULL;
	else return con;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edi, %r13d
movl $0x18, %edi
callq 0x1120
movq %rax, -0x48(%rbp)
movl $0xe0, %edi
callq 0x1120
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0xc8, 0xc(%rax)
movl $0x1, 0x10(%rax)
movq %rax, -0x40(%rbp)
movq %rax, %rdi
callq 0x18f1
movq %rax, %r12
movq %rax, %rdi
callq 0x1070
movl 0x3442(%rip), %r9d      # 0x5600
leaq 0x342b(%rip), %r8       # 0x55f0
movl %r13d, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10b0
testl %eax, %eax
js 0x23b1
leaq -0xf0(%rbp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
leal 0x3f(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r13d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
movq %rdx, -0x58(%rbp)
movq %rax, -0x50(%rbp)
orq %rdx, -0xf0(%rbp,%rax,8)
leaq -0x68(%rbp), %rax
movq $0xa, (%rax)
movq $0x0, 0x8(%rax)
leal 0x1(%r13), %r15d
xorl %r14d, %r14d
movq %r13, -0x38(%rbp)
movl %r15d, %edi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x68(%rbp), %r8
callq 0x1110
testl %eax, %eax
js 0x23ac
je 0x22e1
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
testq %rax, -0xf0(%rbp,%rcx,8)
je 0x22e1
movl $0x100, %edx            # imm = 0x100
movl %r13d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x336b(%rip), %r8       # 0x5604
leaq 0x3360(%rip), %r9       # 0x5600
callq 0x1140
testl %eax, %eax
js 0x22f4
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x1837
movq %rax, %r13
testq %rax, %rax
je 0x22cd
movl 0x10(%r13), %eax
notl %eax
testb $0x3, %al
je 0x2324
movq %r12, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq -0x38(%rbp), %r13
leal 0x1(%r14), %eax
cmpl $0x9, %r14d
movl %eax, %r14d
jl 0x2256
jmp 0x230c
movq %r12, %rdi
movl %r13d, %esi
movl %r14d, %edx
leaq -0x2c(%rbp), %rcx
callq 0x1325
movl -0x2c(%rbp), %r14d
jmp 0x22e1
xorl %r14d, %r14d
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r13), %eax
movl $0xa, %ecx
subq -0x68(%rbp), %rcx
movq -0x48(%rbp), %r14
movl %eax, (%r14)
movq %rcx, 0x8(%r14)
movq -0x60(%rbp), %rax
movq %rax, 0x10(%r14)
movq %r12, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movabsq $0x2000000c8, %rax      # imm = 0x2000000C8
movq -0x40(%rbp), %rdi
movq %rax, 0xc(%rdi)
callq 0x18f1
movq %rax, %r15
movl $0x1, %edi
callq 0x11d0
movq %r15, %rdi
callq 0x1070
movl 0x3279(%rip), %r9d      # 0x5600
leaq 0x3276(%rip), %r8       # 0x5604
movq -0x38(%rbp), %rdi
movq %r15, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10b0
testl %eax, %eax
jns 0x230f
callq 0x1395
callq 0x13ad
callq 0x130d