// File: /workspace/github_c_makefile_star2_O1/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 0x1851
leal -0x5(%rsi), %r10d
leal -0x4(%rsi), %r11d
leal -0x3(%rsi), %ebx
leal -0x2(%rsi), %ebp
xorl %r15d, %r15d
xorl %eax, %eax
movslq %r15d, %r9
movzwl (%rdx,%r9,2), %r14d
testl %r14d, %r14d
je 0x1858
movl %r14d, %r9d
andl $0xfc00, %r9d           # imm = 0xFC00
cmpl $0xd800, %r9d           # imm = 0xD800
jne 0x174c
leal 0x1(%r15), %r9d
cmpl %ecx, %r9d
jge 0x1858
movslq %r9d, %r12
movzwl (%rdx,%r12,2), %r12d
movl %r12d, %r13d
andl $0xfc00, %r13d          # imm = 0xFC00
cmpl $0xdc00, %r13d          # imm = 0xDC00
jne 0x1858
shll $0xa, %r14d
addl %r12d, %r14d
addl $0xfca02400, %r14d      # imm = 0xFCA02400
jmp 0x174f
movl %r15d, %r9d
cmpl $0x7f, %r14d
ja 0x176b
cmpl %ebp, %eax
jg 0x185b
movslq %eax, %r15
incl %eax
movb %r14b, (%rdi,%r15)
jmp 0x1840
cmpl $0x7ff, %r14d           # imm = 0x7FF
ja 0x17a3
cmpl %ebx, %eax
jg 0x185b
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 0x1840
cmpl $0xffff, %r14d          # imm = 0xFFFF
ja 0x17ed
cmpl %r11d, %eax
jg 0x185b
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 0x1840
cmpl %r10d, %eax
jg 0x185b
movl %r14d, %r15d
shrl $0x12, %r15d
andb $0x7, %r15b
orb $-0x10, %r15b
movslq %eax, %r12
movb %r15b, (%rdi,%r12)
movl %r14d, %r15d
shrl $0xc, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, 0x1(%rdi,%r12)
movl %r14d, %r15d
shrl $0x6, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, 0x2(%rdi,%r12)
andb $0x3f, %r14b
orb $-0x80, %r14b
addl $0x4, %eax
movb %r14b, 0x3(%rdi,%r12)
incl %r9d
movl %r9d, %r15d
cmpl %ecx, %r9d
jl 0x16ec
jmp 0x185b
xorl %eax, %eax
xorl %r9d, %r9d
jmp 0x185b
movl %r15d, %r9d
cmpl %esi, %eax
jge 0x18fd
movslq %eax, %rsi
movb $0x0, (%rdi,%rsi)
testq %r8, %r8
je 0x18ee
movl %r9d, (%r8)
cmpl %ecx, %r9d
jge 0x18ee
movslq %r9d, %rsi
movzwl (%rdx,%rsi,2), %esi
testl %esi, %esi
je 0x18ee
movl %esi, %edi
andl $0xfc00, %edi           # imm = 0xFC00
cmpl $0xd800, %edi           # imm = 0xD800
jne 0x18bf
incl %r9d
cmpl %ecx, %r9d
jge 0x18ee
movslq %r9d, %rdi
movzwl (%rdx,%rdi,2), %edi
movl %edi, %r8d
andl $0xfc00, %r8d           # imm = 0xFC00
cmpl $0xdc00, %r8d           # imm = 0xDC00
jne 0x18ee
shll $0xa, %esi
addl %edi, %esi
addl $0xfca02400, %esi       # imm = 0xFCA02400
movl $0x1, %edi
cmpl $0x80, %esi
jb 0x18e7
movl $0x2, %edi
cmpl $0x800, %esi            # imm = 0x800
jb 0x18e7
cmpl $0x10000, %esi          # imm = 0x10000
movl $0x4, %edi
sbbl $0x0, %edi
addl %edi, %eax
incl %r9d
jmp 0x1872
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
nop