// File: /workspace/github_c_makefile_star2_O2/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
leal -0x5(%rsi), %eax
movl %eax, 0x4(%rsp)
leal -0x4(%rsi), %r10d
leal -0x3(%rsi), %r11d
leal -0x2(%rsi), %ebx
xorl %eax, %eax
movd 0xfa8e(%rip), %xmm0     # 0x11190
movd 0xfa96(%rip), %xmm1     # 0x111a0
xorl %r15d, %r15d
movslq %eax, %r14
cmpl %ecx, %r15d
jge 0x186a
movslq %r15d, %r12
movzwl (%rdx,%r12,2), %r12d
testl %r12d, %r12d
je 0x186a
movl %r12d, %ebp
andl $0xfc00, %ebp           # imm = 0xFC00
cmpw $0xd800, %bp            # imm = 0xD800
jne 0x1774
leal 0x1(%r15), %ebp
cmpl %ecx, %ebp
jge 0x186a
movslq %ebp, %r13
movzwl (%rdx,%r13,2), %r13d
movl %r13d, %r9d
andl $0xfc00, %r9d           # imm = 0xFC00
cmpw $0xdc00, %r9w           # imm = 0xDC00
jne 0x186a
shll $0xa, %r12d
addl %r13d, %r12d
addl $0xfca02400, %r12d      # imm = 0xFCA02400
jmp 0x1777
movl %r15d, %ebp
cmpl $0x7f, %r12d
ja 0x1790
cmpl %ebx, %eax
jg 0x1867
incl %eax
movb %r12b, (%rdi,%r14)
jmp 0x185d
cmpl $0x7ff, %r12d           # imm = 0x7FF
ja 0x17c6
cmpl %r11d, %eax
jg 0x1867
movl %r12d, %r9d
shrl $0x6, %r9d
orb $-0x40, %r9b
movb %r9b, (%rdi,%r14)
andb $0x3f, %r12b
orb $-0x80, %r12b
addl $0x2, %eax
movb %r12b, 0x1(%rdi,%r14)
jmp 0x185d
cmpl $0xffff, %r12d          # imm = 0xFFFF
ja 0x180d
cmpl %r10d, %eax
jg 0x1867
movl %r12d, %r9d
shrl $0xc, %r9d
orb $-0x20, %r9b
movb %r9b, (%rdi,%r14)
movl %r12d, %r9d
shrl $0x6, %r9d
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x1(%rdi,%r14)
andb $0x3f, %r12b
orb $-0x80, %r12b
addl $0x3, %eax
movb %r12b, 0x2(%rdi,%r14)
jmp 0x185d
cmpl 0x4(%rsp), %eax
jg 0x1867
movl %r12d, %r9d
shrl $0x12, %r9d
addl $0x4, %eax
movl %r12d, %r15d
shrl $0x4, %r15d
andl $0xff00, %r15d          # imm = 0xFF00
movzbl %r9b, %r9d
orl %r15d, %r9d
movl %r12d, %r15d
shll $0xa, %r15d
andl $0xff0000, %r15d        # imm = 0xFF0000
orl %r9d, %r15d
shll $0x18, %r12d
orl %r15d, %r12d
movd %r12d, %xmm2
pand %xmm0, %xmm2
por %xmm1, %xmm2
movd %xmm2, (%rdi,%r14)
incl %ebp
movl %ebp, %r15d
jmp 0x170d
movl %ebp, %r15d
cmpl %esi, %eax
jge 0x1925
movb $0x0, (%rdi,%r14)
testq %r8, %r8
je 0x1916
movl %r15d, (%r8)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdi
pushq $0x4
popq %r8
cmpl %ecx, %r15d
jge 0x1916
movslq %r15d, %r9
movzwl (%rdx,%r9,2), %r9d
testl %r9d, %r9d
je 0x1916
movl %r9d, %r10d
andl $0xfc00, %r10d          # imm = 0xFC00
cmpw $0xd800, %r10w          # imm = 0xD800
jne 0x18e5
incl %r15d
cmpl %ecx, %r15d
jge 0x1916
movslq %r15d, %r10
movzwl (%rdx,%r10,2), %r10d
movl %r10d, %r11d
andl $0xfc00, %r11d          # imm = 0xFC00
cmpw $0xdc00, %r11w          # imm = 0xDC00
jne 0x1916
shll $0xa, %r9d
addl %r10d, %r9d
addl $0xfca02400, %r9d       # imm = 0xFCA02400
movl %esi, %r10d
cmpl $0x80, %r9d
jb 0x190b
movl %edi, %r10d
cmpl $0x800, %r9d            # imm = 0x800
jb 0x190b
cmpl $0x10000, %r9d          # imm = 0x10000
movl %r8d, %r10d
sbbl $0x0, %r10d
addl %r10d, %eax
incl %r15d
jmp 0x188d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
nop