// File: /workspace/github_c_makefile_star2_O3/ulisses[P]ftp-client-server/clientec2.c
// Function: sendMsg
int sendMsg(int te, int tp, int sock, char *buffer, ConState *con, int lastSeq, struct sockaddr_in *from, int *fromlen){
	int r, n, send = 0, try = 0;
	char *msg, *cpy;
	char rec[TAM2];
	Datagrama *recebido;
	struct timeval t;
	fd_set fds;	
	
	FD_ZERO(&fds); FD_SET(sock, &fds); //inicializa fds e coloca sock em fds
	t.tv_sec = con->rtt.tv_sec +1 ; t.tv_usec = con->rtt.tv_usec; //inicializa o timeval
	
	while(!send && try < 3){
		cpy = strdup(buffer); //cria uma cópia para k nao se perca o segmento sem erros
		msg = insereErro(te, tp, cpy); //retorna NULL em caso de perda ou uma mensagem com/sem erros
		
		
		if(msg){//Pode conter erros mas não existe perda
			
			n = sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)from, *fromlen);
			if(n < 0){
				perror("sendto");
			}
			
		/*Espera um ACK*/
			r = select(sock + 1, &fds, NULL, NULL, &t);
			
			if(r && FD_ISSET(sock, &fds)){ //Foi escrito algo no socket
				n = recvfrom(sock, rec, TAM2,0 , (struct sockaddr *)from, (socklen_t*) fromlen);
				if(n < 0){
					perror("recvfrom");
					exit(0);
				}
				rec[n] = '\0';
				/*Verificar ack recebido e tomar decisao*/
				recebido = demuxDatagrama(rec);
				if(recebido && recebido->ack > lastSeq){ //segmento foi enviado sem erros
					send = 1;
					free(recebido);
				}
			}
			else{//Ocorreu uma situação de timeout
			 	try++;
			 	printf("Ocorreu um erro\n");
			 	}
			free(msg);
		}
		else {
			printf("Ocorreu uma perda merda!!!....\n");
		}
		free(cpy);
	}
	if(!send) return 0;
	else return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movl %r9d, -0x2c(%rbp)
movq %rcx, -0x50(%rbp)
movl %edx, %r15d
movl %esi, -0x38(%rbp)
movl %edi, -0x34(%rbp)
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %rax
movaps %xmm0, 0x70(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leal 0x3f(%r15), %eax
testl %edx, %edx
cmovnsl %edx, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r15d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
movq %rdx, -0x48(%rbp)
movq %rax, -0x40(%rbp)
orq %rdx, -0xe0(%rbp,%rax,8)
movq 0x8(%r8), %rax
incq %rax
leaq -0x60(%rbp), %rcx
movq %rax, (%rcx)
movq 0x10(%r8), %rax
movq %rax, 0x8(%rcx)
leal 0x1(%r15), %eax
movl %eax, -0x30(%rbp)
xorl %r12d, %r12d
movq -0x50(%rbp), %rdi
callq 0x11c0
movq %rax, %rbx
movl -0x34(%rbp), %edi
movl -0x38(%rbp), %esi
movq %rax, %rdx
callq 0x1724
testq %rax, %rax
je 0x1d2d
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movq 0x18(%rbp), %rcx
movl (%rcx), %r9d
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq 0x10(%rbp), %r8
callq 0x10b0
testl %eax, %eax
js 0x1d55
movl -0x30(%rbp), %edi
leaq -0xe0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x1110
testl %eax, %eax
je 0x1d11
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
testq %rax, -0xe0(%rbp,%rcx,8)
je 0x1d11
movl $0x100, %edx            # imm = 0x100
movl %r15d, %edi
leaq -0x1e0(%rbp), %r13
movq %r13, %rsi
xorl %ecx, %ecx
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %r9
callq 0x1140
testl %eax, %eax
js 0x1d80
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1e0(%rbp,%rax)
movq %r13, %rdi
callq 0x1837
movb $0x1, %r13b
testq %rax, %rax
je 0x1d23
movl -0x2c(%rbp), %ecx
cmpl %ecx, 0x4(%rax)
jle 0x1d23
movq %rax, %rdi
callq 0x1030
xorl %r13d, %r13d
jmp 0x1d23
incl %r12d
leaq 0x148c(%rip), %rdi      # 0x31a7
callq 0x1060
movb $0x1, %r13b
movq %r14, %rdi
callq 0x1030
jmp 0x1d3c
leaq 0x1417(%rip), %rdi      # 0x314b
callq 0x1060
movb $0x1, %r13b
movq %rbx, %rdi
callq 0x1030
testb %r13b, %r13b
je 0x1d66
cmpl $0x3, %r12d
jl 0x1c38
jmp 0x1d66
leaq 0x1348(%rip), %rdi      # 0x30a4
callq 0x1170
jmp 0x1c89
xorb $0x1, %r13b
movzbl %r13b, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1220