// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_buffered_read
int dbuf_buffered_read(dbuf *f, i64 pos1, i64 len,
	de_buffered_read_cbfn cbfn, void *userdata)
{
	struct de_bufferedreadctx brctx;

	brctx.c = f->c;
	brctx.userdata = userdata;

	if((pos1 < -DE_ALLOWED_VIRTUAL_BYTES) ||
		(pos1 > f->len+DE_ALLOWED_VIRTUAL_BYTES) ||
		(len > f->len+DE_ALLOWED_VIRTUAL_BYTES) ||
		(pos1+len > f->len+DE_ALLOWED_VIRTUAL_BYTES))
	{
		len = 0;
	}

	if(len<=0) { // Get this special case out of the way.
		return buffered_read_zero_len(&brctx, cbfn);
	}

	// Use an optimized routine if all the data we need to read is already in memory.
	if(f->rcache && (pos1>=0) && (pos1+len<=f->rcache_bytes_used)) {
		return buffered_read_from_mem(&brctx, f, f->rcache, pos1, len, cbfn);
	}

	// Not an "optimization", since we promise this behavior for MEMBUFs.
	if(f->btype==DBUF_TYPE_MEMBUF && (pos1>=0) && (pos1+len<=f->len)) {
		return buffered_read_from_mem(&brctx, f, f->membuf_buf, pos1, len, cbfn);
	}

	// The general case:
	return buffered_read_internal(&brctx, f, pos1, len, cbfn);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1048, %rsp           # imm = 0x1048
movq %rcx, %rbx
movq 0x8(%rdi), %rax
movq %rax, 0x20(%rsp)
movq %r8, 0x18(%rsp)
cmpq $-0x4000, %rsi          # imm = 0xC000
jge 0xe47a7
leaq 0x40(%rsp), %rsi
movb $0x0, (%rsi)
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %rdi
movups %xmm0, 0x10(%rdi)
movb $0x1, 0x20(%rdi)
xorl %edx, %edx
callq *%rbx
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
setne %al
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rcx
testq %rdx, %rdx
jle 0xe4770
movq %rsi, %r14
movq %rdi, %r13
movq 0x18(%rdi), %rax
leaq 0x4000(%rax), %rdx
cmpq %rsi, %rdx
jl 0xe4770
cmpq %rcx, %rdx
jl 0xe4770
leaq (%rcx,%r14), %rsi
movq %rsi, 0x8(%rsp)
cmpq %rdx, %rsi
jg 0xe4770
movq 0xc8(%r13), %rsi
testq %rsi, %rsi
sete %dl
testq %r14, %r14
sets %dil
orb %dl, %dil
jne 0xe47ff
movq 0x8(%rsp), %rdx
cmpq 0xc0(%r13), %rdx
jle 0xe4824
testq %r14, %r14
setns %dl
cmpl $0x3, (%r13)
sete %sil
andb %dl, %sil
cmpq %rax, 0x8(%rsp)
setle %al
andb %sil, %al
cmpb $0x1, %al
jne 0xe4839
movq 0x78(%r13), %rsi
leaq 0x18(%rsp), %rdi
movq %r14, %rdx
movq %rbx, %r8
callq 0xe6646
jmp 0xe4795
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
subq %r14, %rax
testq %rax, %rax
jg 0xe485a
testq %rbp, %rbp
jle 0xe48f7
movl $0x1000, %r12d          # imm = 0x1000
subq %rbp, %r12
cmpq %rax, %r12
cmovgeq %rax, %r12
setge 0x38(%rsp)
leaq (%rsp,%rbp), %rsi
addq $0x40, %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0xe3cb7
addq %r12, %rbp
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq *%rbx
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0xe4795
movq %rbx, %r15
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
jle 0xe4795
addq %r12, %r14
subq %rbx, %rbp
jl 0xe4795
jle 0xe48e8
leaq (%rsp,%rbx), %rsi
addq $0x40, %rsi
leaq 0x40(%rsp), %rdi
movq %rbp, %rdx
callq 0x18270
jmp 0xe48ea
xorl %ebp, %ebp
addq %rbx, 0x10(%rsp)
movq %r15, %rbx
jmp 0xe4844
pushq $0x1
popq %rax
jmp 0xe4795