// File: /workspace/github_c_makefile_star2_O3/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 0xeea05
movq %rdi, %r12
movq 0x8(%rdi), %rdi
cmpq $0x1dcd6501, %rbx       # imm = 0x1DCD6501
jb 0xee911
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9522
movq %rdx, %r15
testq %rdx, %rdx
jns 0xee947
movq %rbx, %rax
negq %rax
cmpq %r15, %rax
jae 0xeea10
movq %r15, %rdx
negq %rdx
movq %rdi, %r13
movq %r14, %rdi
xorl %esi, %esi
callq 0x18140
movq %r13, %rdi
subq %r15, %r14
addq %r15, %rbx
xorl %r15d, %r15d
movq 0x18(%r12), %rax
movq %rax, %r13
subq %r15, %r13
leaq (%rbx,%r15), %rcx
cmpq %rax, %rcx
cmovleq %rbx, %r13
cmpq %r15, %rax
movl $0x0, %ebp
cmovgq %r13, %rbp
testq %rbp, %rbp
jle 0xeea05
movq 0xc8(%r12), %rsi
testq %rsi, %rsi
je 0xee991
leaq (%r15,%rbp), %rax
cmpq 0xc0(%r12), %rax
jle 0xeea3a
movl (%r12), %eax
cmpl $0x4, %eax
je 0xeea57
cmpl $0x3, %eax
je 0xeea42
cmpl $0x1, %eax
jne 0xeea71
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0xeea7a
cmpl $0x0, 0x34(%r12)
je 0xee9cd
cmpq %r15, 0x38(%r12)
je 0xee9df
movq %rcx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1095b4
movq 0x10(%r12), %rcx
movl $0x1, %esi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x18080
movq %rax, %r13
addq %rax, %r15
movq %r15, 0x38(%r12)
movl $0x1, 0x34(%r12)
jmp 0xeea08
xorl %r13d, %r13d
subq %r13, %rbx
jle 0xeea2b
addq %r13, %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 %r15, %rsi
movq %r14, %rdi
jmp 0xeea4d
addq 0x78(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x181e0
jmp 0xeea08
movq 0x50(%r12), %rdi
addq 0x58(%r12), %r15
movq %r14, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xee8d4
jmp 0xeea08
leaq 0x5a776(%rip), %rsi     # 0x1491ee
jmp 0xeea81
leaq 0x5a75f(%rip), %rsi     # 0x1491e0
xorl %r13d, %r13d
xorl %eax, %eax
callq 0xe9544
jmp 0xeea08