// File: /workspace/github_c_makefile_star2_O2/MikeGray-APH[P]LibLouisAPH/source/utf-convert.c
// Function: utf8_convert_to_utf16
int utf8_convert_to_utf16(unichar *uchars, const int uchars_max, const char *cchars, const int cchars_len, int *cchars_crs)
{
	static const char byte_count[256] =
	{
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5,
	};
	static const uint32_t offsets[6] = { 0x00000000, 0x00003080, 0x000e2080, 0x03c82080, 0xfa082080, 0x82082080, };
	uint32_t uchar32;
	int cnt, crs, i;

	crs = 0;
	for(i = 0; i < cchars_len; i++)
	{
		if(cchars[i] == 0)
			break;

		cnt = byte_count[(unsigned char)cchars[i]];
		if(i + cnt >= cchars_len)
			break;
		uchar32 = 0;
		switch(cnt)
		{
		case 5:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
		case 4:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
		case 3:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
		case 2:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
		case 1:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
		case 0:  uchar32 += (unsigned char)cchars[i];
		}
		uchar32 -= offsets[cnt];

		if(uchar32 < 0xffff)
		{
			if(crs + 1 >= uchars_max)
				break;
			uchars[crs++] = uchar32;
		}
		else
		{
			if(crs + 2 >= uchars_max)
				break;
			uchar32 -= 0x10000;
			uchars[crs++] = (unichar)((uchar32 >> 10)    + 0xd800);
			uchars[crs++] = (unichar)((uchar32 & 0x03ff) + 0xdc00);
		}
	}

	ASSERT(crs < uchars_max)
	uchars[crs] = 0;

	if(cchars_crs)
	{
		*cchars_crs = i;

		for( ; i < cchars_len; i++)
		{
			if(cchars[i] == 0)
				break;

			cnt = byte_count[(unsigned char)cchars[i]];
			if(i + cnt >= cchars_len)
				break;
			uchar32 = 0;
			switch(cnt)
			{
			case 5:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
			case 4:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
			case 3:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
			case 2:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
			case 1:  uchar32 += (unsigned char)cchars[i++];  uchar32 <<= 6;
			case 0:  uchar32 += (unsigned char)cchars[i];
			}
			uchar32 -= offsets[cnt];

			if(uchar32 > 0xffff)
				crs++;
			crs++;
		}
	}

	return crs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, (%rsp)
xorl %eax, %eax
leaq 0xfbee(%rip), %r12      # 0x11070
leaq 0xfbb3(%rip), %r8       # 0x1103c
xorl %r11d, %r11d
movslq %eax, %rbx
cmpl %ecx, %r11d
jge 0x15be
movslq %r11d, %rbp
movzbl (%rdx,%rbp), %r13d
testq %r13, %r13
je 0x15be
movsbl (%r13,%r12), %r10d
leal (%r11,%r10), %r15d
cmpl %ecx, %r15d
jge 0x15be
movq %r12, %r14
movslq %r10d, %r15
xorl %r12d, %r12d
cmpb $0x5, %r15b
ja 0x155d
movzbl %r15b, %r9d
movslq (%r8,%r9,4), %r9
addq %r8, %r9
jmpq *%r9
movzbl %r10b, %r10d
jmp 0x1556
movl %r13d, %r12d
shll $0x6, %r12d
movb 0x1(%rdx,%rbp), %r13b
incq %rbp
movl %ebp, %r11d
movslq %r11d, %r11
movzbl %r13b, %r9d
addl %r12d, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r13b
incq %r11
movl %r9d, %r12d
movslq %r11d, %r11
movzbl %r13b, %r9d
addl %r12d, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r13b
incq %r11
movl %r9d, %r12d
movslq %r11d, %r11
movzbl %r13b, %r9d
addl %r12d, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r13b
incq %r11
movl %r9d, %r12d
movslq %r11d, %r11
movzbl %r13b, %r10d
addl %r12d, %r10d
shll $0x6, %r10d
movb 0x1(%rdx,%r11), %r13b
incq %r11
movzbl %r13b, %r12d
addl %r10d, %r12d
leaq 0xfc0c(%rip), %r9       # 0x11170
subl (%r9,%r15,4), %r12d
cmpl $0xfffe, %r12d          # imm = 0xFFFE
ja 0x157f
leal 0x1(%rax), %ebp
cmpl %esi, %ebp
jge 0x15bb
movw %r12w, (%rdi,%rbx,2)
jmp 0x15ae
leal 0x2(%rax), %ebp
cmpl %esi, %ebp
jge 0x15bb
leal 0x3ff0000(%r12), %eax
shrl $0xa, %eax
addl $0xffffd800, %eax       # imm = 0xFFFFD800
movw %ax, (%rdi,%rbx,2)
andl $0x3ff, %r12d           # imm = 0x3FF
orl $0xdc00, %r12d          # imm = 0xDC00
movw %r12w, 0x2(%rdi,%rbx,2)
incl %r11d
movl %ebp, %eax
movq %r14, %r12
jmp 0x148c
movq %r14, %r12
cmpl %esi, %eax
jge 0x16d6
andw $0x0, (%rdi,%rbx,2)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x16c7
movl %r11d, (%rsi)
leaq 0xfa72(%rip), %rsi      # 0x11054
cmpl %ecx, %r11d
jge 0x16c7
movslq %r11d, %r14
movzbl (%rdx,%r14), %r8d
testq %r8, %r8
je 0x16c7
movsbl (%r8,%r12), %r10d
leal (%r11,%r10), %edi
cmpl %ecx, %edi
jge 0x16c7
movslq %r10d, %rdi
xorl %ebx, %ebx
cmpb $0x5, %dil
ja 0x16a9
movzbl %dil, %r9d
movslq (%rsi,%r9,4), %r9
addq %rsi, %r9
jmpq *%r9
movzbl %r10b, %r10d
jmp 0x16a2
movl %r8d, %ebx
shll $0x6, %ebx
movb 0x1(%rdx,%r14), %r8b
incq %r14
movl %r14d, %r11d
movslq %r11d, %r11
movzbl %r8b, %r9d
addl %ebx, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r8b
incq %r11
movl %r9d, %ebx
movslq %r11d, %r11
movzbl %r8b, %r9d
addl %ebx, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r8b
incq %r11
movl %r9d, %ebx
movslq %r11d, %r11
movzbl %r8b, %r9d
addl %ebx, %r9d
shll $0x6, %r9d
movb 0x1(%rdx,%r11), %r8b
incq %r11
movl %r9d, %ebx
movslq %r11d, %r11
movzbl %r8b, %r10d
addl %ebx, %r10d
shll $0x6, %r10d
movb 0x1(%rdx,%r11), %r8b
incq %r11
movzbl %r8b, %ebx
addl %r10d, %ebx
leaq 0xfac0(%rip), %r8       # 0x11170
subl (%r8,%rdi,4), %ebx
cmpl $0x10000, %ebx          # imm = 0x10000
sbbl $-0x1, %eax
incl %eax
incl %r11d
jmp 0x15e2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060