// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_read
void dbuf_read(dbuf *f, u8 *buf, i64 pos, i64 len)
{
	i64 bytes_read = 0;
	i64 bytes_to_read;
	deark *c;

	c = f->c;

	if(len <= 0) goto done_read;
	if(len > DE_MAX_MALLOC) {
		de_fatalerror(c);
		return;
	}

	if(pos < 0) {
		if(pos <= (-len)) {
			// All requested bytes are before the beginning of the file
			de_zeromem(buf, (size_t)len);
			return;
		}
		// Some requested bytes are before the beginning of the file.
		// Zero out the ones that are:
		de_zeromem(buf, (size_t)(-pos));
		// And adjust the parameters:
		buf += (-pos);
		len -= (-pos);
		pos = 0;
	}

	bytes_to_read = len;
	if(pos >= f->len) {
		bytes_to_read = 0;
	}
	else if(pos + bytes_to_read > f->len) {
		bytes_to_read = f->len - pos;
	}

	if(bytes_to_read<1) {
		goto done_read;
	}

	// If the data we need is all cached, get it from cache.
	if(f->rcache &&
		pos >= 0 &&
		pos + bytes_to_read <= f->rcache_bytes_used)
	{
		de_memcpy(buf, &f->rcache[pos], (size_t)bytes_to_read);
		bytes_read = bytes_to_read;
		goto done_read;
	}

	switch(f->btype) {
	case DBUF_TYPE_IFILE:
		if(!f->fp) {
			de_internal_err_fatal(c, "File not open");
			goto done_read;
		}

		// For performance reasons, don't call fseek if we're already at the
		// right position.
		if(!f->file_pos_known || f->file_pos!=pos) {
			de_fseek(f->fp, pos, SEEK_SET);
		}

		bytes_read = fread(buf, 1, (size_t)bytes_to_read, f->fp);

		f->file_pos = pos + bytes_read;
		f->file_pos_known = 1;
		break;

	case DBUF_TYPE_IDBUF:
		// Recursive call to the parent dbuf.
		dbuf_read(f->parent_dbuf, buf, f->offset_into_parent_dbuf+pos, bytes_to_read);

		// The parent dbuf always writes 'bytes_to_read' bytes.
		bytes_read = bytes_to_read;
		break;

	case DBUF_TYPE_MEMBUF:
		de_memcpy(buf, &f->membuf_buf[pos], (size_t)bytes_to_read);
		bytes_read = bytes_to_read;
		break;

	default:
		de_internal_err_fatal(c, "getbytes from this I/O type not implemented");
		goto done_read;
	}

done_read:
	// Zero out any requested bytes that were not read.
	if(bytes_read < len) {
		de_zeromem(buf+bytes_read, (size_t)(len - bytes_read));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rsi, %r14
testq %rcx, %rcx
jle 0xef007
movq %rdi, %r13
movq 0x8(%rdi), %rbp
cmpq $0x1dcd6501, %rbx       # imm = 0x1DCD6501
jl 0xeef28
movq %rbp, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea084
movq %rdx, %r12
testq %rdx, %rdx
jns 0xeef58
movq %rbx, %rax
negq %rax
cmpq %r12, %rax
jge 0xef012
movq %r12, %rdx
negq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x18140
subq %r12, %r14
addq %r12, %rbx
xorl %r12d, %r12d
movq 0x18(%r13), %rax
movq %rax, %r15
subq %r12, %r15
leaq (%rbx,%r12), %rcx
cmpq %rax, %rcx
cmovleq %rbx, %r15
xorl %ecx, %ecx
cmpq %r12, %rax
cmovleq %rcx, %r15
testq %r15, %r15
jle 0xef007
movq 0xc8(%r13), %rsi
testq %rsi, %rsi
je 0xeef9c
leaq (%r15,%r12), %rax
cmpq 0xc0(%r13), %rax
jle 0xef03c
movl (%r13), %eax
cmpl $0x4, %eax
je 0xef058
cmpl $0x3, %eax
je 0xef044
cmpl $0x1, %eax
jne 0xef070
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0xef079
cmpl $0x0, 0x34(%r13)
je 0xeefd5
cmpq %r12, 0x38(%r13)
je 0xeefdf
movq %r12, %rsi
xorl %edx, %edx
callq 0x109acc
movq 0x10(%r13), %rcx
movl $0x1, %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x18080
movq %rax, %r15
addq %rax, %r12
movq %r12, 0x38(%r13)
movl $0x1, 0x34(%r13)
jmp 0xef00a
xorl %r15d, %r15d
subq %r15, %rbx
jle 0xef02d
addq %r15, %r14
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18140
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %r12, %rsi
movq %r14, %rdi
jmp 0xef04e
addq 0x78(%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x181e0
jmp 0xef00a
movq 0x50(%r13), %rdi
addq 0x58(%r13), %r12
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xeeee8
jmp 0xef00a
leaq 0x59bdf(%rip), %rsi     # 0x148c56
jmp 0xef080
leaq 0x59bc8(%rip), %rsi     # 0x148c48
xorl %r15d, %r15d
movq %rbp, %rdi
xorl %eax, %eax
callq 0xea0a6
jmp 0xef00a