// File: /workspace/github_c_makefile_star2_O1/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
testl %ecx, %ecx
jle 0x15b6
xorl %r9d, %r9d
leaq 0x11bd4(%rip), %r10     # 0x13070
leaq 0x11ccd(%rip), %r11     # 0x13170
leaq 0x11b92(%rip), %rbx     # 0x1303c
xorl %r14d, %r14d
movslq %r9d, %rax
movzbl (%rdx,%rax), %r12d
testq %r12, %r12
je 0x15b1
movsbl (%r12,%r10), %r13d
leal (%r9,%r13), %eax
cmpl %ecx, %eax
jge 0x15b1
movslq %r13d, %rax
xorl %r15d, %r15d
cmpb $0x5, %al
ja 0x154d
movzbl %al, %ebp
movslq (%rbx,%rbp,4), %rbp
addq %rbx, %rbp
jmpq *%rbp
movzbl %r13b, %ebp
jmp 0x1542
incl %r9d
shll $0x6, %r12d
movl %r12d, %r15d
movslq %r9d, %r12
incl %r9d
movzbl (%rdx,%r12), %ebp
addl %r15d, %ebp
shll $0x6, %ebp
movl %ebp, %r15d
movslq %r9d, %r12
incl %r9d
movzbl (%rdx,%r12), %ebp
addl %r15d, %ebp
shll $0x6, %ebp
movl %ebp, %r15d
movslq %r9d, %r12
incl %r9d
movzbl (%rdx,%r12), %ebp
addl %r15d, %ebp
shll $0x6, %ebp
movl %ebp, %r15d
movslq %r9d, %r12
incl %r9d
movzbl (%rdx,%r12), %ebp
addl %r15d, %ebp
shll $0x6, %ebp
movslq %r9d, %r15
movzbl (%rdx,%r15), %r15d
addl %ebp, %r15d
subl (%r11,%rax,4), %r15d
cmpl $0xfffe, %r15d          # imm = 0xFFFE
ja 0x156c
leal 0x1(%r14), %eax
cmpl %esi, %eax
jge 0x15b1
movslq %r14d, %r14
movw %r15w, (%rdi,%r14,2)
jmp 0x15a0
leal 0x2(%r14), %eax
cmpl %esi, %eax
jge 0x15b1
leal 0x3ff0000(%r15), %ebp
shrl $0xa, %ebp
addl $0xffffd800, %ebp       # imm = 0xFFFFD800
movslq %r14d, %r14
movw %bp, (%rdi,%r14,2)
andl $0x3ff, %r15d           # imm = 0x3FF
orl $0xdc00, %r15d          # imm = 0xDC00
movw %r15w, 0x2(%rdi,%r14,2)
incl %r9d
movl %eax, %r14d
cmpl %ecx, %r9d
jl 0x14ad
jmp 0x15bb
movl %r14d, %eax
jmp 0x15bb
xorl %eax, %eax
xorl %r9d, %r9d
cmpl %esi, %eax
jge 0x16c1
movslq %eax, %rsi
movw $0x0, (%rdi,%rsi,2)
testq %r8, %r8
je 0x16b2
movl %r9d, (%r8)
cmpl %ecx, %r9d
jge 0x16b2
leaq 0x11a88(%rip), %rsi     # 0x13070
leaq 0x11b81(%rip), %rdi     # 0x13170
leaq 0x11a5e(%rip), %r8      # 0x13054
movslq %r9d, %r10
movzbl (%rdx,%r10), %ebx
testq %rbx, %rbx
je 0x16b2
movsbl (%rbx,%rsi), %r14d
leal (%r9,%r14), %r10d
cmpl %ecx, %r10d
jge 0x16b2
movslq %r14d, %r10
xorl %r11d, %r11d
cmpb $0x5, %r10b
ja 0x1696
movzbl %r10b, %r15d
movslq (%r8,%r15,4), %r15
addq %r8, %r15
jmpq *%r15
movzbl %r14b, %ebx
jmp 0x168b
incl %r9d
shll $0x6, %ebx
movl %ebx, %r11d
movslq %r9d, %rbx
incl %r9d
movzbl (%rdx,%rbx), %ebx
addl %r11d, %ebx
shll $0x6, %ebx
movl %ebx, %r11d
movslq %r9d, %rbx
incl %r9d
movzbl (%rdx,%rbx), %ebx
addl %r11d, %ebx
shll $0x6, %ebx
movl %ebx, %r11d
movslq %r9d, %rbx
incl %r9d
movzbl (%rdx,%rbx), %ebx
addl %r11d, %ebx
shll $0x6, %ebx
movl %ebx, %r11d
movslq %r9d, %rbx
incl %r9d
movzbl (%rdx,%rbx), %ebx
addl %r11d, %ebx
shll $0x6, %ebx
movslq %r9d, %r11
movzbl (%rdx,%r11), %r11d
addl %ebx, %r11d
subl (%rdi,%r10,4), %r11d
cmpl $0x10000, %r11d         # imm = 0x10000
sbbl $-0x1, %eax
incl %eax
incl %r9d
cmpl %ecx, %r9d
jl 0x15f6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060