// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movl %edi, %r15d
movl $0x18, %edi
callq 0x1120
movq %rax, -0x38(%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, -0x50(%rbp)
movq %rax, %rdi
callq 0x18c7
movq %rax, %r12
movq %rax, %rdi
callq 0x1070
movl 0x3459(%rip), %r9d      # 0x5600
leaq 0x3442(%rip), %r8       # 0x55f0
xorl %r13d, %r13d
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10b0
testl %eax, %eax
js 0x23f7
leaq -0xe0(%rbp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
leal 0x3f(%r15), %eax
testl %r15d, %r15d
cmovnsl %r15d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r15d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
sarl $0x6, %eax
cltq
movq %rdx, -0x48(%rbp)
movq %rax, -0x40(%rbp)
orq %rdx, -0xe0(%rbp,%rax,8)
leaq -0x60(%rbp), %rax
movq $0xa, (%rax)
movq $0x0, 0x8(%rax)
leal 0x1(%r15), %eax
movl %eax, -0x2c(%rbp)
xorl %ebx, %ebx
movl -0x2c(%rbp), %edi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x1110
testl %eax, %eax
js 0x23ed
je 0x2381
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
testq %rax, -0xe0(%rbp,%rcx,8)
je 0x2381
movl $0x100, %edx            # imm = 0x100
movl %r15d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x3371(%rip), %r8       # 0x5604
leaq 0x3366(%rip), %r9       # 0x5600
callq 0x1140
testl %eax, %eax
js 0x2394
movq %r15, %r14
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x180e
movq %rax, %r15
testq %rax, %rax
je 0x2367
movl 0x10(%r15), %eax
notl %eax
testb $0x3, %al
jne 0x2367
movl 0xc(%r15), %eax
movl $0xa, %ecx
subq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl %eax, (%rdx)
movq %rcx, 0x8(%rdx)
movq -0x58(%rbp), %rax
movq %rax, 0x10(%rdx)
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq -0x50(%rbp), %rdi
movabsq $0x2000000c8, %rax      # imm = 0x2000000C8
movq %rax, 0xc(%rdi)
callq 0x18c7
movq %rax, %r12
movl $0x1, %ebx
movl $0x1, %edi
callq 0x11d0
movq %r12, %rdi
callq 0x1070
movl 0x32c3(%rip), %r9d      # 0x5600
movq %r14, %r15
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x32b2(%rip), %r8       # 0x5604
callq 0x10b0
testl %eax, %eax
leaq -0xe0(%rbp), %r14
jns 0x2381
jmp 0x23f2
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r14, %r15
leaq -0xe0(%rbp), %r14
cmpl $0x8, %r13d
jg 0x23ac
incl %r13d
testl %ebx, %ebx
je 0x2248
jmp 0x23ac
movq %r12, %rdi
movl %r15d, %esi
movl %r13d, %edx
leaq -0x30(%rbp), %rcx
callq 0x1326
movl -0x30(%rbp), %r13d
jmp 0x2381
movq -0x40(%rbp), %rdx
movq -0xe0(%rbp,%rdx,8), %rax
movq -0x48(%rbp), %rcx
testq %rcx, %rax
je 0x23cf
notq %rcx
andq %rcx, %rax
movq %rax, -0xe0(%rbp,%rdx,8)
xorl %ecx, %ecx
testl %ebx, %ebx
movq -0x38(%rbp), %rax
cmoveq %rcx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x13ae
callq 0x1396
callq 0x130e