// File: karel-brinda[P]ococo/ext/htslib/knetfile.c
// Function: knet_seek
off_t knet_seek(knetFile *fp, off_t off, int whence)
{
	if (whence == SEEK_SET && off == fp->offset) return 0;
	if (fp->type == KNF_TYPE_LOCAL) {
		/* Be aware that lseek() returns the offset after seeking, while fseek() returns zero on success. */
		off_t offset = lseek(fp->fd, off, whence);
		if (offset == -1) return -1;
		fp->offset = offset;
		return fp->offset;
	} else if (fp->type == KNF_TYPE_FTP) {
		if (whence == SEEK_CUR) fp->offset += off;
		else if (whence == SEEK_SET) fp->offset = off;
		else if (whence == SEEK_END) fp->offset = fp->file_size + off;
		else return -1;
		fp->is_ready = 0;
		return fp->offset;
	} else if (fp->type == KNF_TYPE_HTTP) {
		if (whence == SEEK_END) { // FIXME: can we allow SEEK_END in future?
			fprintf(stderr, "[knet_seek] SEEK_END is not supported for HTTP. Offset is unchanged.\n");
			errno = ESPIPE;
			return -1;
		}
		if (whence == SEEK_CUR) fp->offset += off;
		else if (whence == SEEK_SET) fp->offset = off;
		else return -1;
		fp->is_ready = 0;
		return fp->offset;
	}
	errno = EINVAL;
	fprintf(stderr,"[knet_seek] %s\n", strerror(errno));
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x776e4
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x776e4
movq $0x0, -0x8(%rbp)
jmp 0x7788d
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x77735
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x4ad0
movq %rax, -0x28(%rbp)
cmpq $-0x1, -0x28(%rbp)
jne 0x77718
movq $-0x1, -0x8(%rbp)
jmp 0x7788d
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x7788d
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x777b3
cmpl $0x1, -0x1c(%rbp)
jne 0x77756
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x77797
cmpl $0x0, -0x1c(%rbp)
jne 0x7776a
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x77795
cmpl $0x2, -0x1c(%rbp)
jne 0x77786
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
addq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x77793
movq $-0x1, -0x8(%rbp)
jmp 0x7788d
jmp 0x77795
jmp 0x77797
movq -0x10(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x7788d
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x77847
cmpl $0x2, -0x1c(%rbp)
jne 0x777f6
movq 0x12803(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x76a3(%rip), %rsi      # 0x7ee7a
movb $0x0, %al
callq 0x4830
callq 0x4070
movl $0x1d, (%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x7788d
cmpl $0x1, -0x1c(%rbp)
jne 0x7780e
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x7782e
cmpl $0x0, -0x1c(%rbp)
jne 0x77822
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x7782c
movq $-0x1, -0x8(%rbp)
jmp 0x7788d
jmp 0x7782e
movq -0x10(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x7788d
jmp 0x77849
jmp 0x7784b
callq 0x4070
movl $0x16, (%rax)
movq 0x12773(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x30(%rbp), %rdi
movq %rax, %rdx
leaq 0x7642(%rip), %rsi      # 0x7eec0
movb $0x0, %al
callq 0x4830
movq $-0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)