// File: /workspace/github_c_makefile_star2_O3/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)
testl %ecx, %ecx
jle 0x15df
xorl %r9d, %r9d
leaq 0x11bbe(%rip), %r11     # 0x13070
leaq 0x11cb7(%rip), %r8      # 0x13170
leaq 0x11b7c(%rip), %r14     # 0x1303c
xorl %r10d, %r10d
movslq %r9d, %r13
movzbl (%rdx,%r13), %r12d
testq %r12, %r12
je 0x15e5
movsbl (%r12,%r11), %ebp
leal (%r9,%rbp), %eax
cmpl %ecx, %eax
jge 0x15e5
movslq %ebp, %rax
xorl %r15d, %r15d
cmpb $0x5, %al
ja 0x157b
movzbl %al, %ebx
movslq (%r14,%rbx,4), %rbx
addq %r14, %rbx
jmpq *%rbx
movzbl %bpl, %ebp
jmp 0x1574
movl %r12d, %r15d
shll $0x6, %r15d
movb 0x1(%rdx,%r13), %r12b
incq %r13
movl %r13d, %r9d
movslq %r9d, %r9
movzbl %r12b, %ebx
addl %r15d, %ebx
shll $0x6, %ebx
movb 0x1(%rdx,%r9), %r12b
incq %r9
movl %ebx, %r15d
movslq %r9d, %r9
movzbl %r12b, %ebx
addl %r15d, %ebx
shll $0x6, %ebx
movb 0x1(%rdx,%r9), %r12b
incq %r9
movl %ebx, %r15d
movslq %r9d, %r9
movzbl %r12b, %ebx
addl %r15d, %ebx
shll $0x6, %ebx
movb 0x1(%rdx,%r9), %r12b
incq %r9
movl %ebx, %r15d
movslq %r9d, %r9
movzbl %r12b, %ebp
addl %r15d, %ebp
shll $0x6, %ebp
movb 0x1(%rdx,%r9), %r12b
incq %r9
movzbl %r12b, %r15d
addl %ebp, %r15d
subl (%r8,%rax,4), %r15d
cmpl $0xfffe, %r15d          # imm = 0xFFFE
ja 0x159a
leal 0x1(%r10), %eax
cmpl %esi, %eax
jge 0x15e5
movslq %r10d, %r10
movw %r15w, (%rdi,%r10,2)
jmp 0x15ce
leal 0x2(%r10), %eax
cmpl %esi, %eax
jge 0x15e5
leal 0x3ff0000(%r15), %ebx
shrl $0xa, %ebx
addl $0xffffd800, %ebx       # imm = 0xFFFFD800
movslq %r10d, %r10
movw %bx, (%rdi,%r10,2)
andl $0x3ff, %r15d           # imm = 0x3FF
orl $0xdc00, %r15d          # imm = 0xDC00
movw %r15w, 0x2(%rdi,%r10,2)
incl %r9d
movl %eax, %r10d
cmpl %ecx, %r9d
jl 0x14c3
jmp 0x15f1
xorl %r10d, %r10d
xorl %r9d, %r9d
movl %r10d, %eax
cmpl %esi, %r10d
jge 0x1705
movslq %eax, %rsi
movw $0x0, (%rdi,%rsi,2)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x16f6
movl %r9d, (%rsi)
cmpl %ecx, %r9d
jge 0x16f6
leaq 0x11a56(%rip), %rsi     # 0x13070
leaq 0x11b4f(%rip), %rdi     # 0x13170
leaq 0x11a2c(%rip), %r8      # 0x13054
movslq %r9d, %r14
movzbl (%rdx,%r14), %r11d
testq %r11, %r11
je 0x16f6
movsbl (%r11,%rsi), %r15d
leal (%r9,%r15), %r10d
cmpl %ecx, %r10d
jge 0x16f6
movslq %r15d, %r10
xorl %ebx, %ebx
cmpb $0x5, %r10b
ja 0x16db
movzbl %r10b, %r12d
movslq (%r8,%r12,4), %r12
addq %r8, %r12
jmpq *%r12
movzbl %r15b, %ebp
jmp 0x16d5
movl %r11d, %ebx
shll $0x6, %ebx
movb 0x1(%rdx,%r14), %r11b
incq %r14
movl %r14d, %r9d
movslq %r9d, %r9
movzbl %r11b, %ebp
addl %ebx, %ebp
shll $0x6, %ebp
movb 0x1(%rdx,%r9), %r11b
incq %r9
movl %ebp, %ebx
movslq %r9d, %r9
movzbl %r11b, %ebp
addl %ebx, %ebp
shll $0x6, %ebp
movb 0x1(%rdx,%r9), %r11b
incq %r9
movl %ebp, %ebx
movslq %r9d, %r9
movzbl %r11b, %ebp
addl %ebx, %ebp
shll $0x6, %ebp
movb 0x1(%rdx,%r9), %r11b
incq %r9
movl %ebp, %ebx
movslq %r9d, %r9
movzbl %r11b, %ebp
addl %ebx, %ebp
shll $0x6, %ebp
movb 0x1(%rdx,%r9), %r11b
incq %r9
movzbl %r11b, %ebx
addl %ebp, %ebx
subl (%rdi,%r10,4), %ebx
cmpl $0x10000, %ebx          # imm = 0x10000
sbbl $-0x1, %eax
incl %eax
incl %r9d
cmpl %ecx, %r9d
jl 0x1628
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060