// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/deark-ucstring.c
// Function: ucstring_to_sz
void ucstring_to_sz(de_ucstring *s, char *szbuf, size_t szbuf_len,
	unsigned int flags, de_ext_encoding ee)
{
	i64 i;
	i64 szpos = 0;
	i32 ch;
	i64 charcodelen;
	de_encoding encoding = DE_EXTENC_GET_BASE(ee);
	static const char *sc1 = "\x01<"; // DE_CODEPOINT_HL in UTF-8
	static const char *sc2 = ">\x02"; // DE_CODEPOINT_UNHL
	u8 charcodebuf[32];

	if(szbuf_len<1) return;

	for(i=0; i<s->len; i++) {
		ch = s->str[i];
		if(encoding==DE_ENCODING_UTF8) {
			de_uchar_to_utf8(ch, charcodebuf, &charcodelen);
		}
		else { // DE_ENCODING_LATIN1 or DE_ENCODING_ASCII
			// TODO: This may not work right if DE_CONVFLAG_MAKE_PRINTABLE is used,
			// but currently that never happens.
			if(ch>=0 && ch<=(encoding==DE_ENCODING_LATIN1?255:127))
				charcodebuf[0] = (u8)ch;
			else
				charcodebuf[0] = '_';
			charcodelen = 1;
		}

		if(flags & DE_CONVFLAG_MAKE_PRINTABLE) {
			// TODO: This is slightly inefficient, because we're overwriting the
			// conversion we already did.
			if(!de_is_printable_uchar(ch)) {
				if(ch==0x0a) {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf),
						"%s\\n%s", sc1, sc2);
				}
				else if(ch==0x0d) {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf),
						"%s\\r%s", sc1, sc2);
				}
				else if(ch==0x09) {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf),
						"%s\\t%s", sc1, sc2);
				}
				else if(ch==0x00) {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf),
						"%s\\0%s", sc1, sc2);
				}
				else if(ch>=DE_CODEPOINT_BYTE00 && ch<=DE_CODEPOINT_BYTEFF) {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf), "%s%02X%s",
						sc1, (int)(ch-DE_CODEPOINT_BYTE00), sc2);
				}
				else {
					de_snprintf((char*)charcodebuf, sizeof(charcodebuf),
						"%sU+%04X%s", sc1, (unsigned int)ch, sc2);
				}
				charcodelen = (i64)de_strlen((const char*)charcodebuf);
			}
		}

		if(szpos + charcodelen + 1 > (i64)szbuf_len) break;
		if(charcodelen==1) {
			szbuf[szpos] = charcodebuf[0];
		}
		else {
			de_memcpy(&szbuf[szpos], charcodebuf, (size_t)charcodelen);
		}
		szpos += charcodelen;
	}

	szbuf[szpos] = '\0';
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, 0x1c(%rsp)
movq %rsi, (%rsp)
testq %rdx, %rdx
je 0xf7488
movq %rdi, %r15
cmpq $0x0, 0x10(%rdi)
jle 0xf7477
movq %rdx, %r14
movzbl %r8b, %eax
xorl %ecx, %ecx
movl %eax, 0x18(%rsp)
cmpl $0x5, %eax
sete %cl
shll $0x7, %ecx
orl $0x7f, %ecx
movl %ecx, 0x14(%rsp)
leaq 0x52744(%rip), %rbp     # 0x149a10
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x8(%r15), %rax
movl (%rax,%rbx,4), %r12d
cmpl $0x2, 0x18(%rsp)
jne 0xf72f4
movl %r12d, %edi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xec77c
jmp 0xf7317
cmpl 0x14(%rsp), %r12d
movl %r12d, %eax
movl $0x5f, %ecx
cmovgl %ecx, %eax
testl %r12d, %r12d
cmovsl %ecx, %eax
movb %al, 0x20(%rsp)
movq $0x1, 0x8(%rsp)
testb $0x2, 0x1c(%rsp)
je 0xf7404
xorl %eax, %eax
cmpl %r12d, (%rbp,%rax,8)
jg 0xf7336
cmpl %r12d, 0x4(%rbp,%rax,8)
jge 0xf7404
incq %rax
cmpq $0xc, %rax
jne 0xf7324
cmpl $0x9, %r12d
jg 0xf7366
testl %r12d, %r12d
je 0xf73b8
cmpl $0x9, %r12d
jne 0xf7388
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x52670(%rip), %rdx     # 0x1499d4
jmp 0xf73e2
cmpl $0xd, %r12d
je 0xf73ce
cmpl $0xa, %r12d
jne 0xf7388
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x52640(%rip), %rdx     # 0x1499c6
jmp 0xf73e2
movl %r12d, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0x10000000, %eax       # imm = 0x10000000
jne 0xf7449
addl $0xf0000000, %r12d      # imm = 0xF0000000
movl $0x20, %esi
leaq 0x20(%rsp), %rdi
leaq 0x5262f(%rip), %rdx     # 0x1499e2
jmp 0xf745a
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x5260f(%rip), %rdx     # 0x1499db
jmp 0xf73e2
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x525eb(%rip), %rdx     # 0x1499cd
leaq 0x525d7(%rip), %rcx     # 0x1499c0
leaq 0x525d3(%rip), %r8      # 0x1499c3
xorl %eax, %eax
callq 0xe8d67
movq %r12, %rdi
callq 0x180e0
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdx
leaq (%rdx,%r13), %r12
cmpq %r14, %r12
jge 0xf747c
cmpq $0x1, %rdx
jne 0xf7426
movb 0x20(%rsp), %al
movq (%rsp), %rcx
movb %al, (%rcx,%r13)
jmp 0xf7437
addq (%rsp), %r13
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x181e0
incq %rbx
movq %r12, %r13
cmpq 0x10(%r15), %rbx
jl 0xf72d1
jmp 0xf747f
movl $0x20, %esi
leaq 0x20(%rsp), %rdi
leaq 0x52591(%rip), %rdx     # 0x1499eb
leaq 0x5255f(%rip), %rcx     # 0x1499c0
movl %r12d, %r8d
movq %rdi, %r12
leaq 0x52555(%rip), %r9      # 0x1499c3
xorl %eax, %eax
callq 0xe8d67
jmp 0xf73f7
xorl %r12d, %r12d
jmp 0xf747f
movq %r13, %r12
movq (%rsp), %rax
movb $0x0, (%rax,%r12)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq