// File: codebrainz[P]libutfxx/utf/utf.cxx
// Function: ConvertUTF16toUTF32
ConversionResult ConvertUTF16toUTF32 (
	const UTF16** sourceStart, const UTF16* sourceEnd,
	UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
	ConversionResult result = conversionOK;
	const UTF16* source = *sourceStart;
	UTF32* target = *targetStart;
	UTF32 ch, ch2;
	while (source < sourceEnd) {
	const UTF16* oldSource = source; /*  In case we have to back up because of target overflow. */
	ch = *source++;
	/* If we have a surrogate pair, convert to UTF32 first. */
	if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
		/* If the 16 bits following the high surrogate are in the source buffer... */
		if (source < sourceEnd) {
		ch2 = *source;
		/* If it's a low surrogate, convert to UTF32. */
		if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
			ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
			+ (ch2 - UNI_SUR_LOW_START) + halfBase;
			++source;
		} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
			--source; /* return to the illegal value itself */
			result = sourceIllegal;
			break;
		}
		} else { /* We don't have the 16 bits following the high surrogate. */
		--source; /* return to the high surrogate */
		result = sourceExhausted;
		break;
		}
	} else if (flags == strictConversion) {
		/* UTF-16 surrogate values are illegal in UTF-32 */
		if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
		--source; /* return to the illegal value itself */
		result = sourceIllegal;
		break;
		}
	}
	if (target >= targetEnd) {
		source = oldSource; /* Back up source pointer! */
		result = targetExhausted; break;
	}
	*target++ = ch;
	}
	*sourceStart = source;
	*targetStart = target;
#ifdef CVTUTF_DEBUG
if (result == sourceIllegal) {
	fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
	fflush(stderr);
}
#endif
	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
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 0x6e0a
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x30(%rbp)
movzwl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jb 0x6da5
cmpl $0xdbff, -0x3c(%rbp)    # imm = 0xDBFF
ja 0x6da5
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x6d8e
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0xdc00, -0x40(%rbp)    # imm = 0xDC00
jb 0x6d6c
cmpl $0xdfff, -0x40(%rbp)    # imm = 0xDFFF
ja 0x6d6c
movl -0x3c(%rbp), %eax
subl $0xd800, %eax           # imm = 0xD800
shll $0xa, %eax
movl -0x40(%rbp), %ecx
subl $0xdc00, %ecx           # imm = 0xDC00
addl %ecx, %eax
addl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x6d8c
cmpl $0x0, -0x24(%rbp)
jne 0x6d8a
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x6e0a
jmp 0x6d8c
jmp 0x6da3
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x28(%rbp)
jmp 0x6e0a
jmp 0x6dd6
cmpl $0x0, -0x24(%rbp)
jne 0x6dd4
cmpl $0xdc00, -0x3c(%rbp)    # imm = 0xDC00
jb 0x6dd2
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x6dd2
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x6e0a
jmp 0x6dd4
jmp 0x6dd6
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x6df1
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x2, -0x28(%rbp)
jmp 0x6e0a
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, -0x38(%rbp)
movl %ecx, (%rax)
jmp 0x6cd5
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
popq %rbp
retq
nopw %cs:(%rax,%rax)