// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rdi, -0x50(%rbp)
leaq -0x30(%rbp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %r15
movaps %xmm0, 0x70(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
leal 0x3f(%rsi), %eax
testl %esi, %esi
cmovnsl %esi, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %esi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
sarl $0x6, %eax
cltq
movq %rdx, -0x68(%rbp)
movq %rax, -0x60(%rbp)
orq %rdx, -0x100(%rbp,%rax,8)
leaq -0x78(%rbp), %r13
movq $0xa, (%r13)
movq $0x0, 0x8(%r13)
movq %rsi, -0x38(%rbp)
leal 0x1(%rsi), %eax
movl %eax, -0x44(%rbp)
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movq %r14, -0x58(%rbp)
movl -0x44(%rbp), %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r13, %r8
callq 0x1110
testl %eax, %eax
je 0x2011
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
testq %rax, -0x100(%rbp,%rcx,8)
je 0x2011
movl $0x100, %edi            # imm = 0x100
callq 0x1120
movq %rax, %r15
movl $0x100, %edx            # imm = 0x100
movq -0x38(%rbp), %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x30(%rbp), %r9
callq 0x1140
testl %eax, %eax
js 0x2140
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x1837
testq %rax, %rax
je 0x201b
movq %rax, %r13
movl 0x10(%rax), %eax
testb $0x10, %al
jne 0x201b
movl %eax, -0x3c(%rbp)
movq %r13, %r14
addq $0x18, %r14
movq %r14, %rdi
callq 0x1539
movl %eax, -0x40(%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 -0x40(%rbp), %eax
cmpl 0x14(%r13), %eax
jne 0x209d
movl (%r13), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x20a6
cmpl $0x2, 0x8(%r13)
jl 0x20a6
movq %r14, %rdi
movq -0x50(%rbp), %rsi
callq 0x1090
jmp 0x20b7
incl %r12d
movb $0x1, %bl
jmp 0x211a
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 0x18f1
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x58(%rbp), %r14
movq %r14, %r8
callq 0x10b0
testl %eax, %eax
js 0x2145
movq %rbx, %rdi
callq 0x1030
xorl %ebx, %ebx
leaq 0x1032(%rip), %rdi      # 0x30c3
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
jmp 0x2107
leaq 0x1015(%rip), %rdi      # 0x30b9
jmp 0x20ad
leaq 0x1013(%rip), %rdi      # 0x30c0
movq %r14, %rsi
xorl %eax, %eax
callq 0x1080
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
callq 0x18f1
movq %rax, %r14
movq %rax, %rdi
callq 0x1070
movl -0x30(%rbp), %r9d
movq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq -0x58(%rbp), %r14
movq %r14, %r8
callq 0x10b0
testl %eax, %eax
js 0x214a
movq %rbx, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
testb $0x4, -0x3c(%rbp)
sete %bl
leaq -0x78(%rbp), %r13
movq %r15, %rdi
callq 0x1030
leaq -0x100(%rbp), %r15
testb %bl, %bl
je 0x2128
cmpl $0x3, %r12d
jl 0x1f0f
xorb $0x1, %bl
movzbl %bl, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12c5
callq 0x12f5
callq 0x12dd