// File: /workspace/github_c_makefile_star2_O1/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 $0x1c8, %rsp            # imm = 0x1C8
movl %r9d, -0x30(%rbp)
movq %rcx, -0x58(%rbp)
movl %edx, %r15d
movl %esi, -0x3c(%rbp)
movl %edi, -0x38(%rbp)
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
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
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
sarl $0x6, %eax
cltq
movq %rdx, -0x50(%rbp)
movq %rax, -0x48(%rbp)
orq %rdx, -0xf0(%rbp,%rax,8)
movq 0x8(%r8), %rax
incq %rax
leaq -0x68(%rbp), %rcx
movq %rax, (%rcx)
movq 0x10(%r8), %rax
movq %rax, 0x8(%rcx)
leal 0x1(%r15), %eax
movl %eax, -0x34(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq -0x58(%rbp), %rdi
callq 0x11c0
movq %rax, %rbx
movl -0x38(%rbp), %edi
movl -0x3c(%rbp), %esi
movq %rax, %rdx
callq 0x1714
testq %rax, %rax
je 0x1cfe
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 0x1d37
movl -0x34(%rbp), %edi
leaq -0xf0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x68(%rbp), %r8
callq 0x1110
testl %eax, %eax
je 0x1ced
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
testq %rax, -0xf0(%rbp,%rcx,8)
je 0x1ced
movl %r13d, -0x2c(%rbp)
movq %r12, %r13
movl $0x100, %edx            # imm = 0x100
movq %r15, %r12
movl %r15d, %edi
leaq -0x1f0(%rbp), %r15
movq %r15, %rsi
xorl %ecx, %ecx
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %r9
callq 0x1140
testl %eax, %eax
js 0x1d62
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1f0(%rbp,%rax)
movq %r15, %rdi
callq 0x180e
testq %rax, %rax
je 0x1d0c
movl -0x30(%rbp), %ecx
cmpl %ecx, 0x4(%rax)
movq %r12, %r15
jle 0x1d0f
movq %rax, %rdi
callq 0x1030
movl $0x1, %r12d
jmp 0x1d12
incl %r13d
leaq 0x14b0(%rip), %rdi      # 0x31a7
callq 0x1060
jmp 0x1d16
leaq 0x1446(%rip), %rdi      # 0x314b
callq 0x1060
jmp 0x1d1e
movq %r12, %r15
movq %r13, %r12
movl -0x2c(%rbp), %r13d
movq %r14, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
testl %r12d, %r12d
jne 0x1d48
cmpl $0x3, %r13d
jl 0x1c07
jmp 0x1d48
leaq 0x1366(%rip), %rdi      # 0x30a4
callq 0x1170
jmp 0x1c58
xorl %eax, %eax
testl %r12d, %r12d
setne %al
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1220