// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movl -0x20(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x44(%rbp)
cmpq $0x1, -0x18(%rbp)
jae 0x17223a
jmp 0x17247c
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x172470
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x44(%rbp)
jne 0x17227e
movl -0x34(%rbp), %edi
leaq -0x70(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x160b50
jmp 0x1722b2
cmpl $0x0, -0x34(%rbp)
jl 0x1722a6
movl -0x34(%rbp), %eax
movl -0x44(%rbp), %esi
movl $0x7f, %ecx
movl $0xff, %edx
cmpl $0x5, %esi
cmovel %edx, %ecx
cmpl %ecx, %eax
jg 0x1722a6
movl -0x34(%rbp), %eax
movb %al, -0x70(%rbp)
jmp 0x1722aa
movb $0x5f, -0x70(%rbp)
movq $0x1, -0x40(%rbp)
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x172413
movl -0x34(%rbp), %edi
callq 0x172490
cmpl $0x0, %eax
jne 0x172411
cmpl $0xa, -0x34(%rbp)
jne 0x172302
leaq -0x70(%rbp), %rdi
movq 0xb3815(%rip), %rcx     # 0x225af8
movq 0xb3816(%rip), %r8      # 0x225b00
movl $0x20, %esi
leaq 0x63780(%rip), %rdx     # 0x1d5a76
movb $0x0, %al
callq 0x15a980
jmp 0x172404
cmpl $0xd, -0x34(%rbp)
jne 0x172332
leaq -0x70(%rbp), %rdi
movq 0xb37e5(%rip), %rcx     # 0x225af8
movq 0xb37e6(%rip), %r8      # 0x225b00
movl $0x20, %esi
leaq 0x63757(%rip), %rdx     # 0x1d5a7d
movb $0x0, %al
callq 0x15a980
jmp 0x172402
cmpl $0x9, -0x34(%rbp)
jne 0x172362
leaq -0x70(%rbp), %rdi
movq 0xb37b5(%rip), %rcx     # 0x225af8
movq 0xb37b6(%rip), %r8      # 0x225b00
movl $0x20, %esi
leaq 0x6372e(%rip), %rdx     # 0x1d5a84
movb $0x0, %al
callq 0x15a980
jmp 0x172400
cmpl $0x0, -0x34(%rbp)
jne 0x17238f
leaq -0x70(%rbp), %rdi
movq 0xb3785(%rip), %rcx     # 0x225af8
movq 0xb3786(%rip), %r8      # 0x225b00
movl $0x20, %esi
leaq 0x63705(%rip), %rdx     # 0x1d5a8b
movb $0x0, %al
callq 0x15a980
jmp 0x1723fe
cmpl $0x10000000, -0x34(%rbp) # imm = 0x10000000
jl 0x1723d3
cmpl $0x100000ff, -0x34(%rbp) # imm = 0x100000FF
jg 0x1723d3
leaq -0x70(%rbp), %rdi
movq 0xb374c(%rip), %rcx     # 0x225af8
movl -0x34(%rbp), %r8d
subl $0x10000000, %r8d       # imm = 0x10000000
movq 0xb3742(%rip), %r9      # 0x225b00
movl $0x20, %esi
leaq 0x636c8(%rip), %rdx     # 0x1d5a92
movb $0x0, %al
callq 0x15a980
jmp 0x1723fc
leaq -0x70(%rbp), %rdi
movq 0xb371a(%rip), %rcx     # 0x225af8
movl -0x34(%rbp), %r8d
movq 0xb3717(%rip), %r9      # 0x225b00
movl $0x20, %esi
leaq 0x636a6(%rip), %rdx     # 0x1d5a9b
movb $0x0, %al
callq 0x15a980
jmp 0x1723fe
jmp 0x172400
jmp 0x172402
jmp 0x172404
leaq -0x70(%rbp), %rdi
callq 0x190e0
movq %rax, -0x40(%rbp)
jmp 0x172413
movq -0x30(%rbp), %rax
addq -0x40(%rbp), %rax
addq $0x1, %rax
cmpq -0x18(%rbp), %rax
jle 0x172427
jmp 0x172470
cmpq $0x1, -0x40(%rbp)
jne 0x17243e
movb -0x70(%rbp), %dl
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x172453
movq -0x10(%rbp), %rdi
addq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x191e0
movq -0x40(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x172242
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movb $0x0, (%rax,%rcx)
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)