// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_read_to_ucstring_ex
void dbuf_read_to_ucstring_ex(dbuf *f, i64 pos1, i64 len,
	de_ucstring *s, unsigned int conv_flags, struct de_encconv_state *es)
{
	i64 nbytes_remaining;
	i64 pos = pos1;
	int stop_at_nul = 0;
#define READTOUCSTRING_BUFLEN 256
	u8 buf[READTOUCSTRING_BUFLEN];

	if(conv_flags & DE_CONVFLAG_STOP_AT_NUL) {
		stop_at_nul = 1;
		// We handle STOP_AT_NUL ourselves, so don't pass it on.
		conv_flags -= DE_CONVFLAG_STOP_AT_NUL;
	}

	// Note: It might be sensible to use dbuf_buffered_read() here, but I've
	// decided against it for now.
	nbytes_remaining = len;
	do {
		i64 nbytes_to_read;
		i64 nbytes_in_buf;
		unsigned int conv_flags_to_use_this_time;

		// Lack of DE_CONVFLAG_PARTIAL_DATA flag signals end of data, which
		// isn't necessarily a no-op even with len=0.
		// That's why we always do this loop at least once.

		nbytes_to_read = de_min_int(nbytes_remaining, READTOUCSTRING_BUFLEN);
		dbuf_read(f, buf, pos, nbytes_to_read);
		pos += nbytes_to_read;
		nbytes_in_buf = nbytes_to_read;
		nbytes_remaining -= nbytes_to_read;

		if(stop_at_nul) {
			char *tmpp;

			tmpp = de_memchr(buf, 0x00, (size_t)nbytes_in_buf);
			if(tmpp) {
				nbytes_in_buf = (const u8*)tmpp - buf;
				nbytes_remaining = 0;
			}
		}

		conv_flags_to_use_this_time = conv_flags;
		if(nbytes_remaining>0) {
			// The caller may have already set this flag, in which case we will use
			// it every time.
			// If not, we still use it for all but the final call to ucstring_append_bytes_ex().
			conv_flags_to_use_this_time |= DE_CONVFLAG_PARTIAL_DATA;
		}

		ucstring_append_bytes_ex(s, buf, nbytes_in_buf, conv_flags_to_use_this_time, es);
	} while(nbytes_remaining>0);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1668dd
movl $0x1, -0x44(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x15ccf0
movq %rax, -0x158(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x158(%rbp), %rcx
callq 0x164d10
movq -0x158(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x158(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x166992
leaq -0x150(%rbp), %rdi
movq -0x160(%rbp), %rdx
xorl %esi, %esi
callq 0x19150
movq %rax, -0x170(%rbp)
cmpq $0x0, -0x170(%rbp)
je 0x166990
movq -0x170(%rbp), %rax
leaq -0x150(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x160(%rbp)
movq $0x0, -0x38(%rbp)
jmp 0x166992
movl -0x24(%rbp), %eax
movl %eax, -0x164(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x1669b1
movl -0x164(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x164(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movq -0x160(%rbp), %rdx
movl -0x164(%rbp), %ecx
movq -0x30(%rbp), %r8
callq 0x171820
cmpq $0x0, -0x38(%rbp)
jg 0x1668e5
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)