// File: /workspace/github_c_makefile_star2_O0/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
subq $0x220, %rsp            # imm = 0x220
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
leaq -0x1e8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movl $0x0, -0x1ec(%rbp)
movl -0x1ec(%rbp), %eax
cmpq $0x10, %rax
jae 0x1cb0
movq -0x1f8(%rbp), %rax
movl -0x1ec(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
jmp 0x1c7e
jmp 0x1cb2
movl -0x10(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x10(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x1e8(%rbp,%rax,8), %rcx
movq %rcx, -0x1e8(%rbp,%rax,8)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x1, %rax
movq %rax, -0x168(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x160(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x30(%rbp)
movb %al, -0x1f9(%rbp)
jne 0x1d26
cmpl $0x3, -0x34(%rbp)
setl %al
movb %al, -0x1f9(%rbp)
movb -0x1f9(%rbp), %al
testb $0x1, %al
jne 0x1d35
jmp 0x1eee
movq -0x18(%rbp), %rdi
callq 0x11d0
movq %rax, -0x48(%rbp)
movl -0x8(%rbp), %edi
movl -0xc(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x15d0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x1ed2
movl -0x10(%rbp), %eax
movl %eax, -0x20c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x208(%rbp)
movq -0x40(%rbp), %rdi
callq 0x1070
movl -0x20c(%rbp), %edi
movq -0x208(%rbp), %rsi
movq %rax, %rdx
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %rax
movl (%rax), %r9d
xorl %ecx, %ecx
callq 0x10b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x1db4
leaq 0x1304(%rip), %rdi      # 0x30b3
callq 0x1180
movl -0x10(%rbp), %edi
addl $0x1, %edi
leaq -0x1e8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x168(%rbp), %r8
movq %rcx, %rdx
callq 0x1120
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x1eb0
movl -0x10(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1e8(%rbp,%rax,8), %rax
movq %rax, -0x218(%rbp)
movl -0x10(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x218(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x1eb0
movl -0x10(%rbp), %edi
leaq -0x150(%rbp), %rsi
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %r9
movl $0x100, %edx            # imm = 0x100
xorl %ecx, %ecx
callq 0x1150
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x1e63
leaq 0x1263(%rip), %rdi      # 0x30ba
callq 0x1180
xorl %edi, %edi
callq 0x11c0
movslq -0x2c(%rbp), %rax
movb $0x0, -0x150(%rbp,%rax)
leaq -0x150(%rbp), %rdi
callq 0x17a0
movq %rax, -0x158(%rbp)
cmpq $0x0, -0x158(%rbp)
je 0x1eae
movq -0x158(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x24(%rbp), %eax
jle 0x1eae
movl $0x1, -0x30(%rbp)
movq -0x158(%rbp), %rdi
callq 0x1030
jmp 0x1ec7
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
leaq 0x1203(%rip), %rdi      # 0x30c3
movb $0x0, %al
callq 0x1090
movq -0x40(%rbp), %rdi
callq 0x1030
jmp 0x1ee0
leaq 0x11fb(%rip), %rdi      # 0x30d4
movb $0x0, %al
callq 0x1090
movq -0x48(%rbp), %rdi
callq 0x1030
jmp 0x1d0b
cmpl $0x0, -0x30(%rbp)
jne 0x1efd
movl $0x0, -0x4(%rbp)
jmp 0x1f04
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq