// File: /workspace/github_c_makefile_star2_O2/ulisses[P]ftp-client-server/clientec2.c
// Function: recebeFicheiro
int recebeFicheiro(FILE *f, int sock, struct sockaddr_in *from, int fromlen){
	int r, try = 0, cksum, n, lastAck, fin = 0;
	Datagrama *receber, *enviar;
	char *buffer, *res = NULL;
	int timeout = 10;
	struct timeval t;
	fd_set fds;
	lastAck = 0;
	
	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(!fin && try < 3){
	
		r = select(sock + 1, &fds, NULL, NULL, &t);
		
		if(r && FD_ISSET(sock, &fds)){
			buffer = (char *)malloc(sizeof(char)*TAM2);
			n = recvfrom(sock, buffer, TAM2, 0, (struct sockaddr *)from, (socklen_t *) &fromlen);
			if(n < 0){
				perror("recvfrom");
				exit(1);
				}
			buffer[n] = '\0';
			receber = demuxDatagrama(buffer);
			if(receber && !(receber->flags & FL_ERRO)){
				fin = receber->flags & FL_FIN;
				cksum = checksum(receber->data); //calcular checksum do segmento rebido
				enviar = initDatagrama();
				enviar->flags = FL_ACK;
			
				if(cksum!=receber->cksum){//Informação foi alterada na tranferência do ficheiro.
					printf("Falha: %s", receber->data);
				}
				else {//Informação livre de erros. Enviar ack com o inicio do proximo cnojunto de bytes a receber
					lastAck = receber->seq + 1;
					if(receber->len > 1 && f!=NULL) fprintf(f, "%s", receber->data);
					else printf("%s", receber->data);
				}
				enviar->ack = lastAck;
				res = muxDatagrama(enviar);
				n = sendto(sock, res, strlen(res), 0, (struct sockaddr *)from, (socklen_t) fromlen);
				if(n < 0){
					perror("sendto");
					exit(1);
					}
				//free(res);
				free(enviar);
				free(receber);
			}else {//Poderá ter ocorrido corrupção dos dados e o demuxDatagrama nao conhecer o formato do segmento
				//enviar ack com a sequencia de bytes esperada
				enviar = initDatagrama();
				enviar->flags = FL_ACK;
				enviar->ack = lastAck;
				res = muxDatagrama(enviar);
				n = sendto(sock, res, strlen(res), 0, (struct sockaddr *)from, (socklen_t) fromlen);
				if(n < 0){
					perror("sendto");
					exit(1);
					}
				free(enviar);
				printf("Falhou: %s", buffer);
				fin = 1;
			}
			free(buffer);
			}
		else {//Não leu nada ao fim de 10 segundos
			try++;
		}
	}
	if(fin) return 1; else return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, -0x50(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, -0x60(%rbp)
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1c21
andq $0x0, -0x100(%rbp,%rax,8)
incq %rax
jmp 0x1c0d
pushq $0x40
popq %rcx
movq -0x48(%rbp), %rsi
movl %esi, %eax
cltd
idivl %ecx
movslq %eax, %rbx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
pushq $0x1
popq %r14
shlq %cl, %r14
orq %r14, -0x100(%rbp,%rbx,8)
leaq -0x80(%rbp), %r12
movq $0xa, (%r12)
andq $0x0, 0x8(%r12)
leal 0x1(%rsi), %r13d
xorl %eax, %eax
movl $0x0, -0x2c(%rbp)
xorl %r15d, %r15d
testl %eax, %eax
sete %al
testb $0x1, %al
je 0x1e61
cmpl $0x2, %r15d
jg 0x1e61
movl %r13d, %edi
leaq -0x100(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1100
testl %eax, %eax
je 0x1ca3
testq %r14, -0x100(%rbp,%rbx,8)
jne 0x1caa
incl %r15d
movb $0x1, %al
jmp 0x1c6d
movl $0x100, %edi            # imm = 0x100
callq 0x1110
movq %rax, %r12
movl $0x100, %edx            # imm = 0x100
movq -0x48(%rbp), %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movq -0x50(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x1130
testl %eax, %eax
js 0x1e7b
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r12,%rax)
movq %r12, -0x68(%rbp)
movq %r12, %rdi
callq 0x15cb
testq %rax, %rax
je 0x1d6a
movq %rax, %r12
movl 0x10(%rax), %eax
testb $0x10, %al
jne 0x1d6a
movl %eax, -0x38(%rbp)
movq %r12, %rdi
addq $0x18, %rdi
movq %rdi, -0x40(%rbp)
callq 0x1349
movl %eax, -0x54(%rbp)
callq 0x15aa
movl $0x2, 0x10(%rax)
movl -0x54(%rbp), %ecx
cmpl 0x14(%r12), %ecx
movq %r12, -0x70(%rbp)
jne 0x1ddc
movq %r12, %rcx
movq -0x40(%rbp), %rsi
movq %rax, %r12
movl (%rcx), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x1dec
cmpl $0x2, 0x8(%rcx)
jl 0x1dec
movq %rsi, %rdi
movq -0x60(%rbp), %rsi
callq 0x1090
jmp 0x1dfa
callq 0x15aa
movq %rax, %r12
movl $0x2, 0x10(%rax)
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%r12)
movq %r12, %rdi
callq 0x1675
movq %rax, -0x38(%rbp)
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x50(%rbp), %r8
callq 0x10a0
testl %eax, %eax
js 0x1e84
movq %r12, %rdi
callq 0x1030
leaq 0x12e1(%rip), %rdi      # 0x30a7
movq -0x68(%rbp), %r12
movq %r12, %rsi
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %rax
movl %eax, -0x38(%rbp)
jmp 0x1e4d
movq %rax, %r12
leaq 0x12b7(%rip), %rdi      # 0x309d
movq -0x40(%rbp), %rsi
jmp 0x1df3
leaq 0x12b1(%rip), %rdi      # 0x30a4
xorl %eax, %eax
callq 0x1080
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%r12)
movq %r12, %rdi
callq 0x1675
movq %rax, -0x40(%rbp)
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x50(%rbp), %r8
callq 0x10a0
testl %eax, %eax
js 0x1e84
andl $0x4, -0x38(%rbp)
movq %r12, %rdi
callq 0x1030
movq -0x70(%rbp), %rdi
callq 0x1030
movq -0x68(%rbp), %r12
movq %r12, %rdi
callq 0x1030
leaq -0x80(%rbp), %r12
movl -0x38(%rbp), %eax
jmp 0x1c68
notb %al
movzbl %al, %eax
andl $0x1, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x120d(%rip), %rdi      # 0x308f
jmp 0x1e8b
leaq 0x11fd(%rip), %rdi      # 0x3088
callq 0x1160
pushq $0x1
popq %rdi
callq 0x11a0