// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_copy_slice_convert_to_utf8
void dbuf_copy_slice_convert_to_utf8(dbuf *inf, i64 pos, i64 len,
	de_ext_encoding input_ee, dbuf *outf, UI flags)
{
	deark *c = inf->c;
	de_encoding enc =  DE_EXTENC_GET_BASE(input_ee);
	int prepend_BOM = 0;
	int already_has_BOM = 0;
	struct textconvctx_struct tcctx;

	de_zeromem(&tcctx, sizeof(struct textconvctx_struct));
	dbuf_constrain_length(inf, pos, &len);

	if((flags & 0x4) && (enc==DE_ENCODING_UNKNOWN || enc==DE_ENCODING_ASCII)) {
		dbuf_copy(inf, pos, len, outf);
		goto done;
	}

	tcctx.outf = outf;
	de_encconv_init(&tcctx.es, input_ee);
	tcctx.tmpstr = ucstring_create(c);

	if(c->write_bom && outf->len==0) {
		if((flags & 0x3)!=0) {
			already_has_BOM = slice_has_BOM(inf, pos, len, enc);
		}

		if(flags & 0x1) {
			prepend_BOM = !already_has_BOM;
		}
		else if(flags & 0x2) {
			if(!already_has_BOM) {
				prepend_BOM = !slice_is_ascii_compatible(inf, pos, len, input_ee);
			}
		}
	}

	if(prepend_BOM) {
		dbuf_write_uchar_as_utf8(outf, 0xfeff);
	}

	dbuf_buffered_read(inf, pos, len, text2utf8_cbfn, (void*)&tcctx);

done:
	ucstring_destroy(tcctx.tmpstr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rdi), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rdx, %rax
orq %rsi, %rax
js 0xf1f63
movq 0x18(%r15), %rax
subq %r14, %rax
jge 0xf1f67
xorl %ebx, %ebx
jmp 0xf1f71
movq %rdx, %rbx
cmpq %rdx, %rax
cmovlq %rax, %rbx
testb $0x4, %r9b
sete %al
testb $-0x2, %cl
setne %dl
orb %al, %dl
jne 0xf1f95
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r8, %rcx
callq 0xefabc
jmp 0xf1ff0
movl %r9d, %r13d
leaq 0x20(%rsp), %rbp
movq %r8, -0x10(%rbp)
movq %rbp, %rdi
movq %rcx, 0x8(%rsp)
movl %ecx, %esi
movq %r8, (%rsp)
callq 0xec207
movq %r12, %rdi
callq 0xf6870
movq (%rsp), %rdi
movq %rax, -0x8(%rbp)
cmpb $0x0, 0xcc(%r12)
je 0xf1fd6
cmpq $0x0, 0x18(%rdi)
je 0xf2009
leaq 0x161(%rip), %rcx       # 0xf213e
leaq 0x10(%rsp), %r8
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xefc28
movq 0x18(%rsp), %rdi
callq 0xf6bb0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x3, %r13b
je 0xf2081
movb $0x0, 0x42(%rsp)
movw $0x0, 0x40(%rsp)
movq 0x8(%rsp), %rax
addl $-0x2, %eax
cmpb $0x2, %al
ja 0xf2081
movzbl %al, %eax
leaq 0x56bff(%rip), %rcx     # 0x148c30
movq (%rcx,%rax,8), %rcx
cmpq %rbx, %rcx
jg 0xf2081
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xeeee8
movq 0x8(%rsp), %rax
movb (%r12), %dl
movb 0x1(%r12), %cl
cmpb $0x3, %al
je 0xf2072
movzbl %al, %eax
cmpl $0x4, %eax
jne 0xf2116
xorb $-0x2, %dl
notb %cl
jmp 0xf2077
notb %dl
xorb $-0x2, %cl
movb $0x1, %al
orb %dl, %cl
movq (%rsp), %rdi
je 0xf2083
xorl %eax, %eax
testb $0x1, %r13b
jne 0xf20fd
testb $0x2, %r13b
sete %cl
orb %al, %cl
jne 0xf1fd6
movq 0x8(%r15), %r12
leaq 0x50(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movaps %xmm0, -0x10(%rbp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xec207
movq %r12, %rdi
callq 0xf6870
movq %rax, -0x8(%rbp)
leaq 0x2a3(%rip), %rcx       # 0xf2373
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %r8
callq 0xefc28
movl %eax, %ebp
movq 0x8(%r12), %rdi
callq 0xf6bb0
movq (%rsp), %rdi
testl %ebp, %ebp
sete %al
jmp 0xf20ff
xorb $0x1, %al
testb %al, %al
je 0xf1fd6
movl $0xfeff, %esi           # imm = 0xFEFF
callq 0xec81c
jmp 0xf1fd6
cmpb $-0x11, %dl
movq (%rsp), %rdi
jne 0xf2081
cmpb $-0x45, %cl
jne 0xf2081
movb $0x1, %al
cmpb $-0x41, 0x42(%rsp)
je 0xf2083
jmp 0xf2081