// File: /workspace/github_c_makefile_star2_O0/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
subq $0x130, %rsp            # imm = 0x130
movl %edi, -0xc(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x18, %edi
callq 0x1130
movq %rax, -0x30(%rbp)
movl $0xa, -0x34(%rbp)
callq 0x1720
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl $0xc8, 0xc(%rax)
movq -0xd0(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0xd0(%rbp), %rdi
callq 0x18c0
movq %rax, -0x18(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0xf4(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movl -0xf4(%rbp), %edi
movq -0xf0(%rbp), %rsi
movq %rax, %rdx
movl 0x30d3(%rip), %r9d      # 0x5610
xorl %ecx, %ecx
leaq 0x30ba(%rip), %r8       # 0x5600
callq 0x10b0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x256a
leaq 0xbb3(%rip), %rdi       # 0x310e
callq 0x1180
movl $0x1, %edi
callq 0x11c0
jmp 0x256c
leaq -0xc8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpq $0x10, %rax
jae 0x25b6
movq -0xe8(%rbp), %rax
movl -0xdc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x2584
jmp 0x25b8
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 -0xc8(%rbp,%rax,8), %rcx
movq %rcx, -0xc8(%rbp,%rax,8)
movslq -0x34(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
xorl %eax, %eax
cmpl $0xa, -0x24(%rbp)
movb %al, -0xf5(%rbp)
jge 0x261c
cmpl $0x0, -0x28(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0xf5(%rbp)
movb -0xf5(%rbp), %al
testb $0x1, %al
jne 0x262b
jmp 0x28c7
movl -0xc(%rbp), %edi
addl $0x1, %edi
leaq -0xc8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x48(%rbp), %r8
movq %rcx, %rdx
callq 0x1120
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x2664
leaq 0xabd(%rip), %rdi       # 0x3115
callq 0x1180
xorl %edi, %edi
callq 0x11c0
cmpl $0x0, -0x1c(%rbp)
je 0x28b9
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xc8(%rbp,%rax,8), %rax
movq %rax, -0x100(%rbp)
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x100(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x28b9
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
xorl %ecx, %ecx
leaq 0x2f49(%rip), %r8       # 0x5614
leaq 0x2f3e(%rip), %r9       # 0x5610
callq 0x1150
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x275b
leaq 0xa35(%rip), %rdi       # 0x311c
callq 0x1180
movl -0xc(%rbp), %eax
movl %eax, -0x10c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movl -0x10c(%rbp), %edi
movq -0x108(%rbp), %rsi
movq %rax, %rdx
movl 0x2ef0(%rip), %r9d      # 0x5610
xorl %ecx, %ecx
leaq 0x2ed7(%rip), %r8       # 0x5600
callq 0x10b0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x274d
leaq 0x975(%rip), %rdi       # 0x30b3
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x28b7
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x18(%rbp), %rdi
callq 0x17a0
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
je 0x28a0
movq -0xd8(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x28a0
movq -0xd8(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x28a0
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movslq -0x34(%rbp), %rcx
subq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1030
movq -0xd8(%rbp), %rdi
callq 0x1030
movq -0xd0(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0xd0(%rbp), %rax
movl $0xc8, 0xc(%rax)
movq -0xd0(%rbp), %rax
movl $0x2, 0x10(%rax)
movq -0xd0(%rbp), %rdi
callq 0x18c0
movq %rax, -0x18(%rbp)
movl $0x1, %edi
callq 0x11e0
movl -0xc(%rbp), %eax
movl %eax, -0x11c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movl -0x11c(%rbp), %edi
movq -0x118(%rbp), %rsi
movq %rax, %rdx
movl 0x2d9f(%rip), %r9d      # 0x5610
xorl %ecx, %ecx
leaq 0x2d9a(%rip), %r8       # 0x5614
callq 0x10b0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x289e
leaq 0x87f(%rip), %rdi       # 0x310e
callq 0x1180
movl $0x1, %edi
callq 0x11c0
jmp 0x28b5
movq -0x18(%rbp), %rdi
callq 0x1030
movq -0xd8(%rbp), %rdi
callq 0x1030
jmp 0x28b7
jmp 0x28b9
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x25ff
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xc8(%rbp,%rax,8), %rax
movq %rax, -0x128(%rbp)
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x128(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2946
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 -0xc8(%rbp,%rax,8), %rcx
movq %rcx, -0xc8(%rbp,%rax,8)
cmpl $0x0, -0x28(%rbp)
jne 0x2956
movq $0x0, -0x8(%rbp)
jmp 0x295e
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax,%rax)