// File: /workspace/github_c_makefile_star2_O3/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 0xf7027
movq %rdi, %r15
cmpq $0x0, 0x10(%rdi)
jle 0xf7016
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 0x53145(%rip), %rbp     # 0x149fb0
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x8(%r15), %rax
movl (%rax,%rbx,4), %r12d
cmpl $0x2, 0x18(%rsp)
jne 0xf6e93
movl %r12d, %edi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xec124
jmp 0xf6eb6
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)
jne 0xf6ec7
movq 0x8(%rsp), %rdx
jmp 0xf6fa8
xorl %eax, %eax
cmpl %r12d, (%rbp,%rax,8)
jg 0xf6ed7
cmpl %r12d, 0x4(%rbp,%rax,8)
jge 0xf6ebd
incq %rax
cmpq $0xc, %rax
jne 0xf6ec9
cmpl $0x9, %r12d
jg 0xf6f07
testl %r12d, %r12d
je 0xf6f59
cmpl $0x9, %r12d
jne 0xf6f29
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x5306f(%rip), %rdx     # 0x149f74
jmp 0xf6f83
cmpl $0xd, %r12d
je 0xf6f6f
cmpl $0xa, %r12d
jne 0xf6f29
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x5303f(%rip), %rdx     # 0x149f66
jmp 0xf6f83
movl %r12d, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0x10000000, %eax       # imm = 0x10000000
jne 0xf6fe8
addl $0xf0000000, %r12d      # imm = 0xF0000000
movl $0x20, %esi
leaq 0x20(%rsp), %rdi
leaq 0x5302e(%rip), %rdx     # 0x149f82
jmp 0xf6ff9
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x5300e(%rip), %rdx     # 0x149f7b
jmp 0xf6f83
movl $0x20, %esi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x52fea(%rip), %rdx     # 0x149f6d
leaq 0x52fd6(%rip), %rcx     # 0x149f60
leaq 0x52fd2(%rip), %r8      # 0x149f63
xorl %eax, %eax
callq 0xe8193
movq %r12, %rdi
callq 0x180e0
movq %rax, %rdx
movq %rax, 0x8(%rsp)
leaq (%rdx,%r13), %r12
cmpq %r14, %r12
jge 0xf701b
cmpq $0x1, %rdx
jne 0xf6fc5
movb 0x20(%rsp), %al
movq (%rsp), %rcx
movb %al, (%rcx,%r13)
jmp 0xf6fd6
addq (%rsp), %r13
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x181e0
incq %rbx
movq %r12, %r13
cmpq 0x10(%r15), %rbx
jl 0xf6e70
jmp 0xf701e
movl $0x20, %esi
leaq 0x20(%rsp), %rdi
leaq 0x52f92(%rip), %rdx     # 0x149f8b
leaq 0x52f60(%rip), %rcx     # 0x149f60
movl %r12d, %r8d
movq %rdi, %r12
leaq 0x52f56(%rip), %r9      # 0x149f63
xorl %eax, %eax
callq 0xe8193
jmp 0xf6f98
xorl %r12d, %r12d
jmp 0xf701e
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