// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x765a
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movsbq (%rax), %rcx
leaq 0x4d23(%rip), %rax      # 0xc0c0
movsbl (%rax,%rcx), %eax
movw %ax, -0x3e(%rbp)
movq -0x30(%rbp), %rax
movzwl -0x3e(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
cmpq -0x10(%rbp), %rax
jb 0x73c5
movl $0x1, -0x28(%rbp)
jmp 0x765a
movq -0x30(%rbp), %rdi
movzwl -0x3e(%rbp), %esi
addl $0x1, %esi
callq 0x7190
cmpb $0x0, %al
jne 0x73e5
movl $0x3, -0x28(%rbp)
jmp 0x765a
movzwl -0x3e(%rbp), %eax
movq %rax, -0x48(%rbp)
subq $0x5, %rax
ja 0x74c8
movq -0x48(%rbp), %rax
leaq 0x4c72(%rip), %rcx      # 0xc074
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movzwl -0x3e(%rbp), %eax
movl %eax, %ecx
leaq 0x4ceb(%rip), %rax      # 0xc1c0
movl (%rax,%rcx,4), %ecx
movl -0x3c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x7510
movzwl -0x3e(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x2, -0x28(%rbp)
jmp 0x765a
cmpl $0xffff, -0x3c(%rbp)    # imm = 0xFFFF
ja 0x758a
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jb 0x756d
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x756d
cmpl $0x0, -0x24(%rbp)
jne 0x7557
movzwl -0x3e(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x765a
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x38(%rbp)
movw $0xfffd, (%rax)         # imm = 0xFFFD
jmp 0x7585
movl -0x3c(%rbp), %eax
movw %ax, %cx
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x38(%rbp)
movw %cx, (%rax)
jmp 0x7655
cmpl $0x10ffff, -0x3c(%rbp)  # imm = 0x10FFFF
jbe 0x75d5
cmpl $0x0, -0x24(%rbp)
jne 0x75bf
movl $0x3, -0x28(%rbp)
movzwl -0x3e(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x765a
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x38(%rbp)
movw $0xfffd, (%rax)         # imm = 0xFFFD
jmp 0x7653
movq -0x38(%rbp), %rax
addq $0x2, %rax
cmpq -0x20(%rbp), %rax
jb 0x7606
movzwl -0x3e(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x2, -0x28(%rbp)
jmp 0x765a
movl -0x3c(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0xa, %eax
addl $0xd800, %eax           # imm = 0xD800
movw %ax, %cx
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x38(%rbp)
movw %cx, (%rax)
movl -0x3c(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
addl $0xdc00, %eax           # imm = 0xDC00
movw %ax, %cx
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x38(%rbp)
movw %cx, (%rax)
jmp 0x7655
jmp 0x7379
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl -0x28(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)