// File: /workspace/github_c_makefile_star2_O2/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, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rdx, %rax
orq %rsi, %rax
js 0xe6727
movq 0x18(%r15), %rax
subq %r14, %rax
jge 0xe672b
xorl %ebx, %ebx
jmp 0xe6735
movq %rdx, %rbx
cmpq %rdx, %rax
cmovlq %rax, %rbx
testb $0x4, %r9b
sete %al
testb $-0x2, %cl
setne %dl
orb %al, %dl
jne 0xe675b
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r8, %rcx
callq 0xe45f4
xorl %edi, %edi
jmp 0xe67be
movl %r9d, %r13d
leaq 0x50(%rsp), %rbp
movq %r8, -0x10(%rbp)
movq %rbp, %rdi
movq %rcx, 0x8(%rsp)
movl %ecx, %esi
movq %r8, (%rsp)
callq 0xe11fc
movq %r12, %rdi
callq 0xeac74
movq (%rsp), %rdi
movq %rax, -0x8(%rbp)
cmpb $0x0, 0xcc(%r12)
je 0xe679c
cmpq $0x0, 0x18(%rdi)
je 0xe67d2
leaq 0x178(%rip), %rcx       # 0xe691b
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %r8
callq 0xe4745
movq 0x8(%r12), %rdi
callq 0xeaf3c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x3, %r13b
je 0xe6814
andw $0x0, 0x10(%rsp)
movb $0x0, 0x12(%rsp)
movq 0x8(%rsp), %rax
addl $-0x2, %eax
cmpb $0x2, %al
ja 0xe6806
movzbl %al, %eax
leaq 0x554ff(%rip), %rcx     # 0x13bcf8
movq (%rcx,%rax,8), %rcx
cmpq %rbx, %rcx
jle 0xe689a
xorl %eax, %eax
testb $0x1, %r13b
je 0xe6816
testb %al, %al
jne 0xe679c
jmp 0xe688b
xorl %eax, %eax
testb $0x2, %r13b
sete %cl
orb %al, %cl
jne 0xe679c
movq 0x8(%r15), %r12
leaq 0x20(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movaps %xmm0, -0x10(%rbp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xe11fc
movq %r12, %rdi
callq 0xeac74
movq %rax, -0x8(%rbp)
leaq 0x2db(%rip), %rcx       # 0xe6b38
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %r8
callq 0xe4745
movl %eax, %ebp
movq 0x8(%r12), %rdi
callq 0xeaf3c
movq (%rsp), %rdi
testl %ebp, %ebp
jne 0xe679c
movl $0xfeff, %esi           # imm = 0xFEFF
callq 0xe1790
jmp 0xe679c
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xe3cb7
movq 0x8(%rsp), %rax
movb (%r12), %dl
movb 0x1(%r12), %cl
cmpb $0x3, %al
je 0xe68df
movzbl %al, %eax
cmpl $0x4, %eax
movq (%rsp), %rdi
jne 0xe68f7
xorb $-0x2, %dl
notb %cl
movb $0x1, %al
orb %dl, %cl
jne 0xe6806
jmp 0xe6808
notb %dl
xorb $-0x2, %cl
movb $0x1, %al
orb %dl, %cl
movq (%rsp), %rdi
jne 0xe6806
jmp 0xe6808
cmpb $-0x11, %dl
jne 0xe6806
cmpb $-0x45, %cl
jne 0xe6806
movb $0x1, %al
cmpb $-0x41, 0x12(%rsp)
je 0xe6808
jmp 0xe6806