// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x20(%rsp)
movq %r8, 0x18(%rsp)
cmpq $-0x4000, %rsi          # imm = 0xC000
jge 0xef64a
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 *0x8(%rsp)
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movl %ecx, %eax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r15
testq %rdx, %rdx
jle 0xef611
movq %rsi, %r14
movq 0x18(%rdi), %rax
leaq 0x4000(%rax), %rcx
cmpq %rsi, %rcx
jl 0xef611
cmpq %r15, %rcx
jl 0xef611
leaq (%r15,%r14), %rdx
movq %rdx, 0x10(%rsp)
cmpq %rcx, %rdx
jg 0xef611
movq 0xc8(%rdi), %r13
testq %r13, %r13
sete %cl
testq %r14, %r14
sets %dl
orb %cl, %dl
jne 0xef6a1
movq 0x10(%rsp), %rcx
cmpq 0xc0(%rdi), %rcx
jle 0xef7c5
testq %r14, %r14
setns %cl
cmpl $0x3, (%rdi)
sete %dl
andb %cl, %dl
cmpq %rax, 0x10(%rsp)
setle %al
andb %dl, %al
cmpb $0x1, %al
jne 0xef716
addq 0x78(%rdi), %r14
xorl %ebp, %ebp
leaq 0x18(%rsp), %r13
movq %r15, %rbx
subq %rbp, %rbx
jle 0xef7b4
movq %rbx, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movb $0x1, 0x38(%rsp)
leaq (%r14,%rbp), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq *0x8(%rsp)
xorl %ecx, %ecx
testl %eax, %eax
je 0xef636
movq 0x30(%rsp), %rax
testq %rax, %rax
jle 0xef636
addq %rax, %rbp
cmpq %rbx, %rax
jle 0xef6c8
jmp 0xef636
xorl %ebp, %ebp
xorl %r12d, %r12d
movl $0x1000, %ebx           # imm = 0x1000
subq %rbp, %rbx
cmpq %r15, %rbx
cmovgeq %r15, %rbx
setge 0x38(%rsp)
leaq (%rsp,%rbp), %rsi
addq $0x40, %rsi
movq %rdi, %r13
movq %r14, %rdx
movq %rbx, %rcx
callq 0xee8d4
addq %rbx, %rbp
movq %r12, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq *0x8(%rsp)
testl %eax, %eax
je 0xef7be
movq 0x30(%rsp), %r15
testq %r15, %r15
jle 0xef7be
addq %rbx, %r14
subq %r15, %rbp
jl 0xef7be
jle 0xef792
leaq (%rsp,%r15), %rsi
addq $0x40, %rsi
leaq 0x40(%rsp), %rdi
movq %rbp, %rdx
callq 0x18270
jmp 0xef794
xorl %ebp, %ebp
movq %r13, %rdi
addq %r15, %r12
movq 0x10(%rsp), %r15
subq %r14, %r15
testq %r15, %r15
jg 0xef71b
testq %rbp, %rbp
jg 0xef71b
movl $0x1, %ecx
jmp 0xef636
xorl %ecx, %ecx
jmp 0xef636
addq %r14, %r13
xorl %ebp, %ebp
leaq 0x18(%rsp), %r14
movq %r15, %rbx
subq %rbp, %rbx
jle 0xef7b4
movq %rbx, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movb $0x1, 0x38(%rsp)
movq %r13, %rsi
addq %rbp, %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq *0x8(%rsp)
xorl %ecx, %ecx
testl %eax, %eax
je 0xef636
movq 0x30(%rsp), %rax
testq %rax, %rax
jle 0xef636
addq %rax, %rbp
cmpq %rbx, %rax
jle 0xef7cf
jmp 0xef636