// File: /workspace/github_c_makefile_star2_O2/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, %ebx
pushq $0x18
popq %rdi
callq 0x1110
movq %rax, -0x40(%rbp)
callq 0x15aa
movq %rax, %rdi
movabsq $0x1000000c8, %rax      # imm = 0x1000000C8
movq %rax, 0xc(%rdi)
movq %rdi, -0x48(%rbp)
callq 0x1675
movq %rax, %r12
movq %rax, %rdi
callq 0x1070
movl 0x3715(%rip), %r9d      # 0x5600
leaq 0x36fe(%rip), %r8       # 0x55f0
movq %rbx, -0x38(%rbp)
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10a0
testl %eax, %eax
js 0x212d
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1f23
andq $0x0, -0xe8(%rbp,%rax,8)
incq %rax
jmp 0x1f0f
pushq $0x40
popq %rcx
movq -0x38(%rbp), %rsi
movl %esi, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rdx, -0x58(%rbp)
movq %rax, -0x50(%rbp)
orq %rax, -0xe8(%rbp,%rdx,8)
leaq -0x68(%rbp), %r14
movq $0xa, (%r14)
andq $0x0, 0x8(%r14)
leal 0x1(%rsi), %r15d
xorl %r13d, %r13d
xorl %ebx, %ebx
movl %r15d, -0x2c(%rbp)
testl %r13d, %r13d
jne 0x20fb
cmpl $0x9, %ebx
jg 0x20fb
movl %r15d, %edi
leaq -0xe8(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x1100
testl %eax, %eax
js 0x211a
xorl %r13d, %r13d
testl %eax, %eax
je 0x20b9
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
testq %rcx, -0xe8(%rbp,%rax,8)
je 0x20b9
movl $0x100, %edx            # imm = 0x100
movq -0x38(%rbp), %rdi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x3632(%rip), %r8       # 0x5604
leaq 0x3627(%rip), %r9       # 0x5600
callq 0x1130
testl %eax, %eax
js 0x20c0
movq %r14, %r15
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x15cb
movq %rax, %r14
testq %rax, %rax
je 0x20a2
movl 0x10(%r14), %eax
notl %eax
testb $0x3, %al
jne 0x20a2
movl 0xc(%r14), %eax
pushq $0xa
popq %rcx
subq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rdx
movl %eax, (%rdx)
movq %rcx, 0x8(%rdx)
movq -0x60(%rbp), %rax
movq %rax, 0x10(%rdx)
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
movabsq $0x2000000c8, %rax      # imm = 0x2000000C8
movq %rax, 0xc(%rdi)
callq 0x1675
movq %rax, %r12
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x11c0
movq %r12, %rdi
callq 0x1070
movl 0x3589(%rip), %r9d      # 0x5600
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x357a(%rip), %r8       # 0x5604
callq 0x10a0
movl %r14d, %r13d
testl %eax, %eax
movq %r15, %r14
movl -0x2c(%rbp), %r15d
jns 0x20b9
jmp 0x212d
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %r15, %r14
movl -0x2c(%rbp), %r15d
incl %ebx
jmp 0x1f6c
leaq 0xff9(%rip), %rdi       # 0x30c0
callq 0x1160
movq %r12, %rdi
callq 0x1070
movl 0x3525(%rip), %r9d      # 0x5600
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x3502(%rip), %r8       # 0x55f0
callq 0x10a0
testl %eax, %eax
js 0x2141
incl %ebx
jmp 0x20b9
xorl %ecx, %ecx
testl %r13d, %r13d
movq -0x40(%rbp), %rax
cmoveq %rcx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf98(%rip), %rdi       # 0x30b9
callq 0x1160
xorl %edi, %edi
callq 0x11a0
leaq 0xf7e(%rip), %rdi       # 0x30b2
callq 0x1160
pushq $0x1
popq %rdi
callq 0x11a0
leaq 0xf40(%rip), %rdi       # 0x3088
jmp 0x2134