// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x164d48
jmp 0x164f84
cmpq $0x1dcd6500, -0x20(%rbp) # imm = 0x1DCD6500
jle 0x164d60
movq -0x38(%rbp), %rdi
callq 0x15c250
jmp 0x164fa5
cmpq $0x0, -0x18(%rbp)
jge 0x164dc6
movq -0x18(%rbp), %rax
xorl %ecx, %ecx
subq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jg 0x164d8a
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rdx
xorl %esi, %esi
callq 0x19140
jmp 0x164fa5
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
subq -0x18(%rbp), %rdx
xorl %esi, %esi
callq 0x19140
xorl %eax, %eax
subq -0x18(%rbp), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x10(%rbp)
xorl %eax, %eax
movl %eax, %ecx
subq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jl 0x164de6
movq $0x0, -0x30(%rbp)
jmp 0x164e0a
movq -0x18(%rbp), %rax
addq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jle 0x164e08
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x164e0a
cmpq $0x1, -0x30(%rbp)
jge 0x164e16
jmp 0x164f84
movq -0x8(%rbp), %rax
cmpq $0x0, 0xc8(%rax)
je 0x164e69
cmpq $0x0, -0x18(%rbp)
jl 0x164e69
movq -0x18(%rbp), %rax
addq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0xc0(%rcx), %rax
jg 0x164e69
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rsi
addq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x191e0
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x164f84
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
subl $0x1, %eax
je 0x164e98
jmp 0x164e79
movl -0x3c(%rbp), %eax
subl $0x3, %eax
je 0x164f4b
jmp 0x164e87
movl -0x3c(%rbp), %eax
subl $0x4, %eax
je 0x164f20
jmp 0x164f6e
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x164eba
movq -0x38(%rbp), %rdi
leaq 0x6fe76(%rip), %rsi     # 0x1d4d24
movb $0x0, %al
callq 0x15c2a0
jmp 0x164f84
movq -0x8(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x164ed2
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x18(%rbp), %rax
je 0x164ee5
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x18fdf0
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x1, %esi
callq 0x19080
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x34(%rax)
jmp 0x164f82
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rdx
addq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x164d10
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x164f82
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x78(%rax), %rsi
addq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x191e0
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x164f82
movq -0x38(%rbp), %rdi
leaq 0x6fdb9(%rip), %rsi     # 0x1d4d32
movb $0x0, %al
callq 0x15c2a0
jmp 0x164f84
jmp 0x164f84
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x164fa5
movq -0x10(%rbp), %rdi
addq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rdx
subq -0x28(%rbp), %rdx
xorl %esi, %esi
callq 0x19140
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)