// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
testq %rdx, %rdx
je 0xeb7ae
movq %rdx, %r15
movq %rdi, %r12
movzbl %r8b, %ebx
xorl %eax, %eax
cmpl $0x5, %ebx
sete %al
shll $0x7, %eax
orl $0x7f, %eax
movl %eax, 0x18(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq 0x10(%r12), %r14
jge 0xeb7a5
movq 0x8(%r12), %rax
movl (%rax,%r14,4), %r13d
cmpl $0x2, %ebx
jne 0xeb64c
movl %r13d, %edi
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xe16f0
jmp 0xeb66d
cmpl 0x18(%rsp), %r13d
movl %r13d, %eax
pushq $0x5f
popq %rcx
cmovgl %ecx, %eax
testl %r13d, %r13d
cmovsl %ecx, %eax
movb %al, 0x20(%rsp)
movq $0x1, 0x10(%rsp)
testb $0x2, 0x1c(%rsp)
je 0xeb680
movl %r13d, %edi
callq 0xeb7bd
testl %eax, %eax
je 0xeb6c3
movq 0x10(%rsp), %rdx
leaq (%rdx,%rbp), %r13
cmpq %r15, %r13
jge 0xeb7a5
cmpq $0x1, %rdx
jne 0xeb6a6
movb 0x20(%rsp), %al
movq 0x8(%rsp), %rcx
movb %al, (%rcx,%rbp)
jmp 0xeb6b8
addq 0x8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x181e0
incq %r14
movq %r13, %rbp
jmp 0xeb61f
testl %r13d, %r13d
je 0xeb716
cmpl $0x9, %r13d
je 0xeb6ee
cmpl $0xd, %r13d
je 0xeb702
cmpl $0xa, %r13d
jne 0xeb752
leaq 0x20(%rsp), %r13
movq %r13, %rdi
pushq $0x20
popq %rsi
leaq 0x513aa(%rip), %rdx     # 0x13ca96
jmp 0xeb728
leaq 0x20(%rsp), %r13
movq %r13, %rdi
pushq $0x20
popq %rsi
leaq 0x513a4(%rip), %rdx     # 0x13caa4
jmp 0xeb728
leaq 0x20(%rsp), %r13
movq %r13, %rdi
pushq $0x20
popq %rsi
leaq 0x51389(%rip), %rdx     # 0x13ca9d
jmp 0xeb728
leaq 0x20(%rsp), %r13
movq %r13, %rdi
pushq $0x20
popq %rsi
leaq 0x51383(%rip), %rdx     # 0x13caab
leaq 0x51361(%rip), %rcx     # 0x13ca90
leaq 0x5135d(%rip), %r8      # 0x13ca93
xorl %eax, %eax
callq 0xde40c
movq %r13, %rdi
callq 0x180e0
movq %rax, %rdx
movq %rax, 0x10(%rsp)
jmp 0xeb685
movl %r13d, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0x10000000, %eax       # imm = 0x10000000
jne 0xeb779
addl $0xf0000000, %r13d      # imm = 0xF0000000
leaq 0x20(%rsp), %rdi
pushq $0x20
popq %rsi
leaq 0x5133b(%rip), %rdx     # 0x13cab2
jmp 0xeb788
leaq 0x20(%rsp), %rdi
pushq $0x20
popq %rsi
leaq 0x51333(%rip), %rdx     # 0x13cabb
leaq 0x51301(%rip), %rcx     # 0x13ca90
movl %r13d, %r8d
movq %rdi, %r13
leaq 0x512f7(%rip), %r9      # 0x13ca93
xorl %eax, %eax
callq 0xde40c
jmp 0xeb73d
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%rbp)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq