// File: /workspace/github_c_makefile_star2_O0/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
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x60(%rbp)
movl $0xa, -0x64(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0xf8(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpq $0x10, %rax
jae 0x2127
movq -0x108(%rbp), %rax
movl -0xfc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x20f5
jmp 0x2129
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xf8(%rbp,%rax,8), %rcx
movq %rcx, -0xf8(%rbp,%rax,8)
movslq -0x64(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0x109(%rbp)
jne 0x218b
cmpl $0x3, -0x2c(%rbp)
setl %al
movb %al, -0x109(%rbp)
movb -0x109(%rbp), %al
testb $0x1, %al
jne 0x219a
jmp 0x247b
movl -0x14(%rbp), %edi
addl $0x1, %edi
leaq -0xf8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x78(%rbp), %r8
movq %rcx, %rdx
callq 0x1120
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x246d
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xf8(%rbp,%rax,8), %rax
movq %rax, -0x118(%rbp)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x118(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x246d
movl $0x100, %edi            # imm = 0x100
callq 0x1130
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %edi
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %r8
movl $0x100, %edx            # imm = 0x100
xorl %ecx, %ecx
leaq -0x24(%rbp), %r9
callq 0x1150
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x2254
leaq 0xe75(%rip), %rdi       # 0x30ba
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x58(%rbp), %rdi
callq 0x17a0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x23ba
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x23ba
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rdi
addq $0x18, %rdi
callq 0x1380
movl %eax, -0x30(%rbp)
callq 0x1720
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x2, 0x10(%rax)
movl -0x30(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x22e0
movq -0x48(%rbp), %rsi
addq $0x18, %rsi
leaq 0xe22(%rip), %rdi       # 0x30f9
movb $0x0, %al
callq 0x1090
jmp 0x2331
movq -0x48(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jle 0x2319
cmpq $0x0, -0x10(%rbp)
je 0x2319
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rdx
addq $0x18, %rdx
leaq 0xdf0(%rip), %rsi       # 0x3100
movb $0x0, %al
callq 0x1100
jmp 0x232f
movq -0x48(%rbp), %rsi
addq $0x18, %rsi
leaq 0xdd8(%rip), %rdi       # 0x3100
movb $0x0, %al
callq 0x1090
jmp 0x2331
movl -0x38(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x50(%rbp), %rdi
callq 0x18c0
movq %rax, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x124(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1070
movl -0x124(%rbp), %edi
movq -0x120(%rbp), %rsi
movq %rax, %rdx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %r9d
xorl %ecx, %ecx
callq 0x10b0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x23a3
leaq 0xd1f(%rip), %rdi       # 0x30b3
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movq -0x50(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
jmp 0x2462
callq 0x1720
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x2, 0x10(%rax)
movl -0x38(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x50(%rbp), %rdi
callq 0x18c0
movq %rax, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x134(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x60(%rbp), %rdi
callq 0x1070
movl -0x134(%rbp), %edi
movq -0x130(%rbp), %rsi
movq %rax, %rdx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %r9d
xorl %ecx, %ecx
callq 0x10b0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x2440
leaq 0xc82(%rip), %rdi       # 0x30b3
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movq -0x50(%rbp), %rdi
callq 0x1030
movq -0x58(%rbp), %rsi
leaq 0xcaf(%rip), %rdi       # 0x3103
movb $0x0, %al
callq 0x1090
movl $0x1, -0x3c(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1030
jmp 0x2476
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2170
cmpl $0x0, -0x3c(%rbp)
je 0x248a
movl $0x1, -0x4(%rbp)
jmp 0x2491
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)