// File: /workspace/github_c_makefile_star2_O2/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, -0x34(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0x30(%rbp)
movl %esi, -0x3c(%rbp)
movl %edi, %r13d
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1905
andq $0x0, -0xe8(%rbp,%rax,8)
incq %rax
jmp 0x18f1
pushq $0x40
popq %rcx
movq -0x30(%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, -0x50(%rbp)
movq %rax, -0x48(%rbp)
orq %rax, -0xe8(%rbp,%rdx,8)
movq 0x8(%r8), %rax
incq %rax
leaq -0x68(%rbp), %rcx
movq %rax, (%rcx)
movq 0x10(%r8), %rax
movq %rax, 0x8(%rcx)
leal 0x1(%rsi), %eax
movl %eax, -0x38(%rbp)
xorl %r14d, %r14d
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x1a8b
cmpl $0x2, %r14d
jg 0x1a8b
movq -0x58(%rbp), %rdi
callq 0x11b0
movq %rax, %rbx
movl %r13d, %edi
movl -0x3c(%rbp), %esi
movq %rax, %rdx
callq 0x14c5
testq %rax, %rax
je 0x1a5e
movq %rax, %r15
movq %rax, %rdi
callq 0x1070
movq 0x18(%rbp), %rcx
movl (%rcx), %r9d
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq 0x10(%rbp), %r8
callq 0x10a0
testl %eax, %eax
js 0x1a7a
movl -0x38(%rbp), %edi
leaq -0xe8(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x68(%rbp), %r8
callq 0x1100
testl %eax, %eax
je 0x1a42
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
testq %rcx, -0xe8(%rbp,%rax,8)
je 0x1a42
movl $0x100, %edx            # imm = 0x100
movq -0x30(%rbp), %rdi
leaq -0x1f0(%rbp), %r12
movq %r12, %rsi
xorl %ecx, %ecx
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %r9
callq 0x1130
testl %eax, %eax
js 0x1aa5
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, -0x1f0(%rbp,%rax)
movq %r12, %rdi
callq 0x15cb
xorl %r12d, %r12d
testq %rax, %rax
je 0x1a54
movl -0x34(%rbp), %ecx
cmpl %ecx, 0x4(%rax)
jle 0x1a54
movq %rax, %rdi
callq 0x1030
pushq $0x1
popq %r12
jmp 0x1a54
incl %r14d
leaq 0x173f(%rip), %rdi      # 0x318b
callq 0x1060
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x1030
jmp 0x1a6d
leaq 0x16ca(%rip), %rdi      # 0x312f
callq 0x1060
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x1030
jmp 0x1953
leaq 0x1607(%rip), %rdi      # 0x3088
callq 0x1160
jmp 0x19b8
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
leaq 0x15e3(%rip), %rdi      # 0x308f
callq 0x1160
xorl %edi, %edi
callq 0x11a0