// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq %rax, 0x20(%rsp)
movq %r8, 0x18(%rsp)
cmpq $-0x4000, %rsi          # imm = 0xC000
jl 0xefc84
movq %rdx, %r14
movq 0x18(%rbx), %rax
addq $0x4000, %rax           # imm = 0x4000
cmpq %r15, %rax
setl %cl
cmpq %rdx, %rax
setl %dl
orb %cl, %dl
leaq (%r14,%r15), %rcx
cmpq %rax, %rcx
setg %al
orb %dl, %al
cmpb $0x1, %al
jne 0xefc87
xorl %r14d, %r14d
testq %r14, %r14
jle 0xefdb1
movq 0xc8(%rbx), %rsi
testq %rsi, %rsi
sete %al
testq %r15, %r15
sets %cl
orb %al, %cl
jne 0xefcb8
leaq (%r14,%r15), %rax
cmpq 0xc0(%rbx), %rax
jle 0xefde1
testq %r15, %r15
js 0xefcd0
cmpl $0x3, (%rbx)
jne 0xefcd0
leaq (%r14,%r15), %rax
cmpq 0x18(%rbx), %rax
jle 0xefddd
addq %r15, %r14
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movq %r14, 0x8(%rsp)
movq %r14, %rax
subq %r15, %rax
testq %rax, %rax
jg 0xefcfd
movl $0x3, %ecx
testq %r13, %r13
jle 0xefd9d
movl $0x1000, %ebp           # imm = 0x1000
subq %r13, %rbp
cmpq %rax, %rbp
cmovgeq %rax, %rbp
setge 0x38(%rsp)
leaq (%rsp,%r13), %rsi
addq $0x40, %rsi
movq %rbx, %rdi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xeeee8
addq %rbp, %r15
addq %rbp, %r13
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, 0x30(%rsp)
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
callq *%r12
movl $0x4, %ecx
testl %eax, %eax
je 0xefd9d
movq %rbx, %r14
movq %r12, %rbx
movq 0x30(%rsp), %r12
testq %r12, %r12
jle 0xefd92
movq %r13, %rbp
subq %r12, %rbp
jl 0xefd92
jle 0xefd86
leaq (%rsp,%r12), %rsi
addq $0x40, %rsi
leaq 0x40(%rsp), %rdi
movq %rbp, %rdx
callq 0x18270
jmp 0xefd88
xorl %ebp, %ebp
addq %r12, 0x10(%rsp)
xorl %ecx, %ecx
movq %rbp, %r13
movq %rbx, %r12
movq %r14, %rbx
movq 0x8(%rsp), %r14
testl %ecx, %ecx
je 0xefce4
cmpl $0x3, %ecx
jne 0xefdd9
movl $0x1, %eax
jmp 0xefdf4
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 *%r12
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
setne %al
jmp 0xefdf4
xorl %eax, %eax
jmp 0xefdf4
movq 0x78(%rbx), %rsi
leaq 0x18(%rsp), %rdi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0xf1e4a
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq