// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, %eax
movq %rdx, %r14
movq %rdi, -0x50(%rbp)
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %rbx
movaps %xmm0, 0x70(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
leal 0x3f(%rsi), %edx
testl %esi, %esi
cmovnsl %esi, %edx
movl %edx, %edi
andl $-0x40, %edi
movl %esi, %ecx
subl %edi, %ecx
movl $0x1, %edi
shlq %cl, %rdi
sarl $0x6, %edx
movslq %edx, %rcx
movq %rdi, -0x68(%rbp)
movq %rcx, -0x60(%rbp)
orq %rdi, -0x100(%rbp,%rcx,8)
leaq -0x30(%rbp), %rcx
movl %eax, (%rcx)
leaq -0x78(%rbp), %r15
movq $0xa, (%r15)
movq $0x0, 0x8(%r15)
movq %rsi, -0x40(%rbp)
leal 0x1(%rsi), %r12d
xorl %eax, %eax
movl $0x0, -0x2c(%rbp)
xorl %r13d, %r13d
movq %r14, -0x58(%rbp)
movl %r12d, -0x34(%rbp)
movl %eax, -0x38(%rbp)
movl %r12d, %edi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x1110
testl %eax, %eax
je 0x1ff0
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
testq %rax, -0x100(%rbp,%rcx,8)
je 0x1ff0
movl $0x100, %edi            # imm = 0x100
callq 0x1120
movq %rax, %r15
movl $0x100, %edx            # imm = 0x100
movq -0x40(%rbp), %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x30(%rbp), %r9
callq 0x1140
testl %eax, %eax
js 0x2129
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x180e
testq %rax, %rax
je 0x1ffa
movq %rax, %r12
movl 0x10(%rax), %r13d
testb $0x10, %r13b
jne 0x1ffa
movq %r12, %r14
addq $0x18, %r14
movq %r14, %rdi
callq 0x1539
movl %eax, -0x44(%rbp)
movl $0xe0, %edi
callq 0x1120
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0x2, 0x10(%rax)
movl -0x44(%rbp), %eax
cmpl 0x14(%r12), %eax
jne 0x2080
movl (%r12), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x2089
cmpl $0x2, 0x8(%r12)
jl 0x2089
movq %r14, %rdi
movq -0x50(%rbp), %rsi
callq 0x1090
jmp 0x209a
movl -0x38(%rbp), %eax
incl %eax
jmp 0x2101
movl $0xe0, %edi
callq 0x1120
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movw $0x63, 0x18(%rax)
movl $0x2, 0x10(%rax)
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
callq 0x18c7
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x58(%rbp), %r14
movq %r14, %r8
callq 0x10b0
testl %eax, %eax
js 0x212e
movq %rbx, %rdi
callq 0x1030
leaq 0x1055(%rip), %rdi      # 0x30c3
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
movl $0x1, %r13d
jmp 0x20e7
leaq 0x1032(%rip), %rdi      # 0x30b9
jmp 0x2090
leaq 0x1030(%rip), %rdi      # 0x30c0
movq %r14, %rsi
xorl %eax, %eax
callq 0x1080
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
callq 0x18c7
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x58(%rbp), %r14
movq %r14, %r8
callq 0x10b0
testl %eax, %eax
js 0x2133
andl $0x4, %r13d
movq %rbx, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movl -0x34(%rbp), %r12d
movq %r15, %rdi
callq 0x1030
leaq -0x100(%rbp), %rbx
leaq -0x78(%rbp), %r15
movl -0x38(%rbp), %eax
testl %r13d, %r13d
jne 0x210f
cmpl $0x3, %eax
jl 0x1ee9
xorl %eax, %eax
testl %r13d, %r13d
setne %al
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12c6
callq 0x12f6
callq 0x12de