// File: /workspace/github_c_makefile_star2_O3/MikeGray-APH[P]LibLouisAPH/source/utf-convert.c
// Function: utf16_convert_to_utf8
int utf16_convert_to_utf8(char *cchars, const int cchars_max, const unichar *uchars, const int uchars_len, int *uchars_crs)
{
	uint32_t uchar32;
	int crs, i;

	crs = 0;
	for(i = 0; i < uchars_len; i++)
	{
		if(uchars[i] == 0)
			break;

		uchar32 = uchars[i];
		if(uchar32 >= 0xd800 && uchar32 <= 0xdbff)
		{
			if(i + 1 >= uchars_len)
				break;
			i++;
			if(uchars[i] >= 0xdc00 && uchars[i] <= 0xdfff)
				uchar32  = ((uchar32 - 0xd800) << 10) + (uchars[i] - 0xdc00) + 0x10000;
			else
			{
				i--;
				break;
			}
		}

		if(uchar32 < 0x80)
		{
			if(crs > cchars_max - 2)
				break;
			cchars[crs++] = (char)uchar32 & 0x7f;
		}
		else if(uchar32 < 0x800)
		{
			if(crs > cchars_max - 3)
				break;
			cchars[crs++] = ((uchar32 >> 6) & 0x1f) | 0xc0;
			cchars[crs++] = ((uchar32 >> 0) & 0x3f) | 0x80;
		}
		else if(uchar32 < 0x10000)
		{
			if(crs > cchars_max - 4)
				break;
			cchars[crs++] = ((uchar32 >> 12) & 0x0f) | 0xe0;
			cchars[crs++] = ((uchar32 >>  6) & 0x3f) | 0x80;
			cchars[crs++] = ((uchar32 >>  0) & 0x3f) | 0x80;
		}
		else if(uchar32 < 0x110000)
		{
			if(crs > cchars_max - 5)
				break;
			cchars[crs++] = ((uchar32 >> 18) & 0x07) | 0xf0;
			cchars[crs++] = ((uchar32 >> 12) & 0x3f) | 0x80;
			cchars[crs++] = ((uchar32 >>  6) & 0x3f) | 0x80;
			cchars[crs++] = ((uchar32 >>  0) & 0x3f) | 0x80;
		}
		else
			break;
	}

	ASSERT(crs < cchars_max)
	cchars[crs] = 0;

	if(uchars_crs)
	{
		*uchars_crs = i;

		for( ; i < uchars_len; i++)
		{
			if(uchars[i] == 0)
				break;

			uchar32 = uchars[i];
			if(uchar32 >= 0xd800 && uchar32 <= 0xdbff)
			{
				if(i + 1 >= uchars_len)
					break;
				i++;
				if(uchars[i] >= 0xdc00 && uchars[i] <= 0xdfff)
					uchar32  = ((uchar32 - 0xd800) * 0x400) + (uchars[i] - 0xdc00) + 0x10000;
				else
					break;
			}
			     if(uchar32 < 0x80)
				crs += 1;
			else if(uchar32 < 0x800)
				crs += 2;
			else if(uchar32 < 0x10000)
				crs += 3;
			else if(uchar32 < 0x110000)
				crs += 4;
			else
				break;
		}
	}

	return crs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testl %ecx, %ecx
jle 0x18a4
leal -0x5(%rsi), %r10d
leal -0x4(%rsi), %r11d
leal -0x3(%rsi), %ebx
leal -0x2(%rsi), %ebp
xorl %r15d, %r15d
movd 0x11a5a(%rip), %xmm0    # 0x13190
movd 0x11a62(%rip), %xmm1    # 0x131a0
xorl %eax, %eax
movslq %r15d, %r9
movzwl (%rdx,%r9,2), %r14d
testl %r14d, %r14d
je 0x18ab
movl %r14d, %r9d
andl $0xfc00, %r9d           # imm = 0xFC00
cmpl $0xd800, %r9d           # imm = 0xD800
jne 0x17a0
leal 0x1(%r15), %r9d
cmpl %ecx, %r9d
jge 0x18ab
movslq %r9d, %r12
movzwl (%rdx,%r12,2), %r12d
movl %r12d, %r13d
andl $0xfc00, %r13d          # imm = 0xFC00
cmpl $0xdc00, %r13d          # imm = 0xDC00
jne 0x18ab
shll $0xa, %r14d
addl %r12d, %r14d
addl $0xfca02400, %r14d      # imm = 0xFCA02400
jmp 0x17a3
movl %r15d, %r9d
cmpl $0x7f, %r14d
ja 0x17bf
cmpl %ebp, %eax
jg 0x18ae
movslq %eax, %r15
incl %eax
movb %r14b, (%rdi,%r15)
jmp 0x1893
cmpl $0x7ff, %r14d           # imm = 0x7FF
ja 0x17f7
cmpl %ebx, %eax
jg 0x18ae
movl %r14d, %r15d
shrl $0x6, %r15d
orb $-0x40, %r15b
movslq %eax, %r12
movb %r15b, (%rdi,%r12)
andb $0x3f, %r14b
orb $-0x80, %r14b
addl $0x2, %eax
movb %r14b, 0x1(%rdi,%r12)
jmp 0x1893
cmpl $0xffff, %r14d          # imm = 0xFFFF
ja 0x1841
cmpl %r11d, %eax
jg 0x18ae
movl %r14d, %r15d
shrl $0xc, %r15d
orb $-0x20, %r15b
movslq %eax, %r12
movb %r15b, (%rdi,%r12)
movl %r14d, %r15d
shrl $0x6, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, 0x1(%rdi,%r12)
andb $0x3f, %r14b
orb $-0x80, %r14b
addl $0x3, %eax
movb %r14b, 0x2(%rdi,%r12)
jmp 0x1893
cmpl %r10d, %eax
jg 0x18ae
movl %r14d, %r15d
shrl $0x12, %r15d
movslq %eax, %r12
addl $0x4, %eax
movl %r14d, %r13d
shrl $0x4, %r13d
andl $0xff00, %r13d          # imm = 0xFF00
movzbl %r15b, %r15d
orl %r13d, %r15d
movl %r14d, %r13d
shll $0xa, %r13d
andl $0xff0000, %r13d        # imm = 0xFF0000
orl %r15d, %r13d
shll $0x18, %r14d
orl %r13d, %r14d
movd %r14d, %xmm2
pand %xmm0, %xmm2
por %xmm1, %xmm2
movd %xmm2, (%rdi,%r12)
incl %r9d
movl %r9d, %r15d
cmpl %ecx, %r9d
jl 0x1740
jmp 0x18ae
xorl %eax, %eax
xorl %r9d, %r9d
jmp 0x18ae
movl %r15d, %r9d
cmpl %esi, %eax
jge 0x1950
movslq %eax, %rsi
movb $0x0, (%rdi,%rsi)
testq %r8, %r8
je 0x1941
movl %r9d, (%r8)
cmpl %ecx, %r9d
jge 0x1941
movslq %r9d, %rsi
movzwl (%rdx,%rsi,2), %esi
testl %esi, %esi
je 0x1941
movl %esi, %edi
andl $0xfc00, %edi           # imm = 0xFC00
cmpl $0xd800, %edi           # imm = 0xD800
jne 0x1912
incl %r9d
cmpl %ecx, %r9d
jge 0x1941
movslq %r9d, %rdi
movzwl (%rdx,%rdi,2), %edi
movl %edi, %r8d
andl $0xfc00, %r8d           # imm = 0xFC00
cmpl $0xdc00, %r8d           # imm = 0xDC00
jne 0x1941
shll $0xa, %esi
addl %edi, %esi
addl $0xfca02400, %esi       # imm = 0xFCA02400
movl $0x1, %edi
cmpl $0x80, %esi
jb 0x193a
movl $0x2, %edi
cmpl $0x800, %esi            # imm = 0x800
jb 0x193a
cmpl $0x10000, %esi          # imm = 0x10000
movl $0x4, %edi
sbbl $0x0, %edi
addl %edi, %eax
incl %r9d
jmp 0x18c5
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
nopl (%rax)