// File: /workspace/github_c_makefile_star2_O3/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 0xf18be
movq 0x18(%r15), %rax
subq %r14, %rax
jge 0xf18c2
xorl %ebx, %ebx
jmp 0xf18cc
movq %rdx, %rbx
cmpq %rdx, %rax
cmovlq %rax, %rbx
testb $0x4, %r9b
sete %al
testb $-0x2, %cl
setne %dl
orb %al, %dl
jne 0xf18f2
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r8, %rcx
callq 0xef481
xorl %edi, %edi
jmp 0xf1955
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 0xebbf7
movq %r12, %rdi
callq 0xf6410
movq (%rsp), %rdi
movq %rax, -0x8(%rbp)
cmpb $0x0, 0xcc(%r12)
je 0xf1933
cmpq $0x0, 0x18(%rdi)
je 0xf1969
leaq 0x179(%rip), %rcx       # 0xf1ab3
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %r8
callq 0xef5e4
movq 0x8(%r12), %rdi
callq 0xf6753
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x3, %r13b
je 0xf19ac
movb $0x0, 0x12(%rsp)
movw $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
addl $-0x2, %eax
cmpb $0x2, %al
ja 0xf199e
movzbl %al, %eax
leaq 0x57837(%rip), %rcx     # 0x1491c8
movq (%rcx,%rax,8), %rcx
cmpq %rbx, %rcx
jle 0xf1a32
xorl %eax, %eax
testb $0x1, %r13b
je 0xf19ae
testb %al, %al
jne 0xf1933
jmp 0xf1a23
xorl %eax, %eax
testb $0x2, %r13b
sete %cl
orb %al, %cl
jne 0xf1933
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 0xebbf7
movq %r12, %rdi
callq 0xf6410
movq %rax, -0x8(%rbp)
leaq 0x311(%rip), %rcx       # 0xf1d06
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %r8
callq 0xef5e4
movl %eax, %ebp
movq 0x8(%r12), %rdi
callq 0xf6753
movq (%rsp), %rdi
testl %ebp, %ebp
jne 0xf1933
movl $0xfeff, %esi           # imm = 0xFEFF
callq 0xec1c4
jmp 0xf1933
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xee8d4
movq 0x8(%rsp), %rax
movb (%r12), %dl
movb 0x1(%r12), %cl
cmpb $0x3, %al
je 0xf1a77
movzbl %al, %eax
cmpl $0x4, %eax
movq (%rsp), %rdi
jne 0xf1a8f
xorb $-0x2, %dl
notb %cl
movb $0x1, %al
orb %dl, %cl
jne 0xf199e
jmp 0xf19a0
notb %dl
xorb $-0x2, %cl
movb $0x1, %al
orb %dl, %cl
movq (%rsp), %rdi
jne 0xf199e
jmp 0xf19a0
cmpb $-0x11, %dl
jne 0xf199e
cmpb $-0x45, %cl
jne 0xf199e
movb $0x1, %al
cmpb $-0x41, 0x12(%rsp)
je 0xf19a0
jmp 0xf199e