// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x18ec
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x170a
jmp 0x18ec
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x1c9b1(%rip), %rax     # 0x1e0d0
movsbl (%rax,%rcx), %eax
movl %eax, -0x30(%rbp)
movl -0x38(%rbp), %eax
addl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jl 0x1736
jmp 0x18ec
movl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x40(%rbp)
subq $0x5, %rax
ja 0x182d
movq -0x40(%rbp), %rax
leaq 0x1c947(%rip), %rcx     # 0x1e0a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movslq -0x30(%rbp), %rcx
leaq 0x1c998(%rip), %rax     # 0x1e1d0
movl (%rax,%rcx,4), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0xffff, -0x2c(%rbp)    # imm = 0xFFFF
jae 0x187a
movl -0x34(%rbp), %eax
addl $0x1, %eax
cmpl -0xc(%rbp), %eax
jl 0x185c
jmp 0x18ec
movl -0x2c(%rbp), %eax
movw %ax, %dx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x18dc
movl -0x34(%rbp), %eax
addl $0x2, %eax
cmpl -0xc(%rbp), %eax
jl 0x1887
jmp 0x18ec
movl -0x2c(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shrl $0xa, %eax
addl $0xd800, %eax           # imm = 0xD800
movw %ax, %dx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movl -0x2c(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
addl $0xdc00, %eax           # imm = 0xDC00
movw %ax, %dx
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x18de
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x16e8
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jl 0x18f9
callq 0x1050
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movw $0x0, (%rax,%rcx,2)
cmpq $0x0, -0x28(%rbp)
je 0x1aa1
movl -0x38(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1a9f
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x193d
jmp 0x1a9f
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x1c77e(%rip), %rax     # 0x1e0d0
movsbl (%rax,%rcx), %eax
movl %eax, -0x30(%rbp)
movl -0x38(%rbp), %eax
addl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jl 0x1969
jmp 0x1a9f
movl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x48(%rbp)
subq $0x5, %rax
ja 0x1a60
movq -0x48(%rbp), %rax
leaq 0x1c72c(%rip), %rcx     # 0x1e0b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x38(%rbp)
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movslq -0x30(%rbp), %rcx
leaq 0x1c765(%rip), %rax     # 0x1e1d0
movl (%rax,%rcx,4), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0xffff, -0x2c(%rbp)    # imm = 0xFFFF
jbe 0x1a88
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x191b
jmp 0x1aa1
movl -0x34(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)