// File: /workspace/github_cpp_makefile_star2_O2/codebrainz[P]libutfxx/utf/utf.cxx
// Function: ConvertUTF8toUTF16
ConversionResult ConvertUTF8toUTF16 (
	const UTF8** sourceStart, const UTF8* sourceEnd,
	UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
	ConversionResult result = conversionOK;
	const UTF8* source = *sourceStart;
	UTF16* target = *targetStart;
	while (source < sourceEnd) {
	UTF32 ch = 0;
	unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
	if (source + extraBytesToRead >= sourceEnd) {
		result = sourceExhausted; break;
	}
	/* Do this check whether lenient or strict */
	if (! isLegalUTF8(source, extraBytesToRead+1)) {
		result = sourceIllegal;
		break;
	}
	/*
	 * The cases all fall through. See "Note A" below.
	 */
	switch (extraBytesToRead) {
		case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
		case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
		case 3: ch += *source++; ch <<= 6;
		case 2: ch += *source++; ch <<= 6;
		case 1: ch += *source++; ch <<= 6;
		case 0: ch += *source++;
	}
	ch -= offsetsFromUTF8[extraBytesToRead];

	if (target >= targetEnd) {
		source -= (extraBytesToRead+1); /* Back up source pointer! */
		result = targetExhausted; break;
	}
	if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
		/* UTF-16 surrogate values are illegal in UTF-32 */
		if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
		if (flags == strictConversion) {
			source -= (extraBytesToRead+1); /* return to the illegal value itself */
			result = sourceIllegal;
			break;
		} else {
			*target++ = UNI_REPLACEMENT_CHAR;
		}
		} else {
		*target++ = (UTF16)ch; /* normal case */
		}
	} else if (ch > UNI_MAX_UTF16) {
		if (flags == strictConversion) {
		result = sourceIllegal;
		source -= (extraBytesToRead+1); /* return to the start */
		break; /* Bail out; shouldn't continue */
		} else {
		*target++ = UNI_REPLACEMENT_CHAR;
		}
	} else {
		/* target is a character in range 0xFFFF - 0x10FFFF. */
		if (target + 1 >= targetEnd) {
		source -= (extraBytesToRead+1); /* Back up source pointer! */
		result = targetExhausted; break;
		}
		ch -= halfBase;
		*target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
		*target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
	}
	}
	*sourceStart = source;
	*targetStart = target;
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, 0x10(%rsp)
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
movq (%rdi), %r15
movq %rdx, 0x20(%rsp)
movq (%rdx), %r14
cmpq %rbp, %r15
jae 0x49db
movsbq (%r15), %r13
leaq 0x27c7(%rip), %rax      # 0x7060
movsbl (%r13,%rax), %eax
movzwl %ax, %r12d
leaq (%r15,%r12), %rax
cmpq %rbp, %rax
jae 0x49df
leal 0x1(%r12), %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0x483b
testb %al, %al
je 0x49e8
xorl %eax, %eax
cmpb $0x5, %r12b
ja 0x4945
movzbl %r12b, %ecx
leaq 0x2736(%rip), %rdx      # 0x7010
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %r12b, %ecx
jmp 0x493c
movl %r13d, %eax
shll $0x6, %eax
movb 0x1(%r15), %r13b
incq %r15
movsbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movsbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movsbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movsbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
incq %r15
movsbl %r13b, %eax
addl %ecx, %eax
cmpq 0x10(%rsp), %r14
jae 0x49d2
leaq 0x2809(%rip), %rcx      # 0x7160
subl (%rcx,%r12,4), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
ja 0x497f
movl %eax, %ecx
andl $0xf800, %ecx           # imm = 0xF800
cmpl $0xd800, %ecx           # imm = 0xD800
je 0x4986
movw %ax, (%r14)
addq $0x2, %r14
jmp 0x4885
cmpl $0x110000, %eax         # imm = 0x110000
jb 0x499c
cmpl $0x0, 0xc(%rsp)
je 0x49e3
movw $0xfffd, (%r14)         # imm = 0xFFFD
addq $0x2, %r14
jmp 0x4885
leaq 0x2(%r14), %rcx
cmpq 0x10(%rsp), %rcx
jae 0x49d2
leal 0x3ff0000(%rax), %ecx
shrl $0xa, %ecx
addl $0xffffd800, %ecx       # imm = 0xFFFFD800
movw %cx, (%r14)
andl $0x3ff, %eax            # imm = 0x3FF
orl $0xdc00, %eax           # imm = 0xDC00
movw %ax, 0x2(%r14)
addq $0x4, %r14
jmp 0x4885
movl %ebx, %eax
subq %rax, %r15
pushq $0x2
jmp 0x49ea
xorl %ecx, %ecx
jmp 0x49eb
pushq $0x1
jmp 0x49ea
movl %ebx, %eax
subq %rax, %r15
pushq $0x3
popq %rcx
movq 0x18(%rsp), %rax
movq %r15, (%rax)
movq 0x20(%rsp), %rax
movq %r14, (%rax)
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq