// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-dbuf.c
// Function: buffered_read_internal
static int buffered_read_internal(struct de_bufferedreadctx *brctx,
	dbuf *f, i64 pos1, i64 len, de_buffered_read_cbfn cbfn)
{
	int retval = 0;
	i64 pos = pos1; // Absolute pos of next byte to read from f
	i64 offs_of_first_byte_in_buf; // Relative to pos1, where in f is buf[0]?
	i64 num_unconsumed_bytes_in_buf;
#define BRBUFLEN 4096 // Must be >= DE_BUFFERED_READ_MIN_BLKSIZE
	u8 buf[BRBUFLEN];

	num_unconsumed_bytes_in_buf = 0;
	offs_of_first_byte_in_buf = 0;

	while(1) {
		i64 nbytes_avail_to_read;
		i64 bytestoread;
		int ret;

		nbytes_avail_to_read = pos1+len-pos;
		if(nbytes_avail_to_read<1 && num_unconsumed_bytes_in_buf<1) {
			break;
		}

		// max bytes that will fit in buf:
		bytestoread = BRBUFLEN-num_unconsumed_bytes_in_buf;

		// max bytes available to read:
		if(bytestoread >= nbytes_avail_to_read) {
			bytestoread = nbytes_avail_to_read;
			brctx->eof_flag = 1;
		}
		else {
			brctx->eof_flag = 0;
		}

		dbuf_read(f, &buf[num_unconsumed_bytes_in_buf], pos, bytestoread);
		pos += bytestoread;
		num_unconsumed_bytes_in_buf += bytestoread;

		brctx->offset = offs_of_first_byte_in_buf;
		brctx->bytes_consumed = num_unconsumed_bytes_in_buf;
		ret = cbfn(brctx, buf, num_unconsumed_bytes_in_buf);
		if(!ret) goto done;
		if(brctx->bytes_consumed<1 || brctx->bytes_consumed>num_unconsumed_bytes_in_buf) {
			goto done;
		}

		if(brctx->bytes_consumed < num_unconsumed_bytes_in_buf) {
			// cbfn didn't consume all bytes
			// TODO: For better efficiency, we could leave the buffer as it is until
			// the unconsumed byte count drops below DE_BUFFERED_READ_MIN_BLKSIZE.
			// But that's only useful if some consumers consume only a small number of bytes.
			de_memmove(buf, &buf[brctx->bytes_consumed],
				(size_t)(num_unconsumed_bytes_in_buf-brctx->bytes_consumed));
			num_unconsumed_bytes_in_buf -= brctx->bytes_consumed;
		}
		else {
			num_unconsumed_bytes_in_buf = 0;
		}
		offs_of_first_byte_in_buf += brctx->bytes_consumed;
	}
	retval = 1;
done:
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1070, %rsp           # imm = 0x1070
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x1058(%rbp)
cmpq $0x1, -0x1058(%rbp)
jge 0x169b77
cmpq $0x1, -0x48(%rbp)
jge 0x169b77
jmp 0x169cb7
movl $0x1000, %eax           # imm = 0x1000
subq -0x48(%rbp), %rax
movq %rax, -0x1060(%rbp)
movq -0x1060(%rbp), %rax
cmpq -0x1058(%rbp), %rax
jl 0x169baf
movq -0x1058(%rbp), %rax
movq %rax, -0x1060(%rbp)
movq -0x8(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0x169bb7
movq -0x8(%rbp), %rax
movb $0x0, 0x20(%rax)
movq -0x10(%rbp), %rdi
leaq -0x1050(%rbp), %rsi
addq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x1060(%rbp), %rcx
callq 0x164d10
movq -0x1060(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x1060(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rdi
leaq -0x1050(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq *%rax
movl %eax, -0x1064(%rbp)
cmpl $0x0, -0x1064(%rbp)
jne 0x169c35
jmp 0x169cbe
movq -0x8(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jl 0x169c4e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x48(%rbp), %rax
jle 0x169c50
jmp 0x169cbe
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x48(%rbp), %rax
jge 0x169c9a
leaq -0x1050(%rbp), %rdi
movq -0x8(%rbp), %rax
leaq -0x1050(%rbp), %rsi
addq 0x18(%rax), %rsi
movq -0x48(%rbp), %rdx
movq -0x8(%rbp), %rax
subq 0x18(%rax), %rdx
callq 0x19260
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x169ca2
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x169b4e
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x1070, %rsp           # imm = 0x1070
popq %rbp
retq
nopw (%rax,%rax)