// File: codebrainz[P]libutfxx/utf/utf.cxx
// Function: ConvertUTF16toUTF8
ConversionResult ConvertUTF16toUTF8 (
	const UTF16** sourceStart, const UTF16* sourceEnd,
	UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
	ConversionResult result = conversionOK;
	const UTF16* source = *sourceStart;
	UTF8* target = *targetStart;
	while (source < sourceEnd) {
	UTF32 ch;
	unsigned short bytesToWrite = 0;
	const UTF32 byteMask = 0xBF;
	const UTF32 byteMark = 0x80;
	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) {
		UTF32 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;
		}
	}
	/* Figure out how many bytes the result will require */
	if (ch < (UTF32)0x80) {	     bytesToWrite = 1;
	} else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
	} else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
	} else if (ch < (UTF32)0x110000) {  bytesToWrite = 4;
	} else {			    bytesToWrite = 3;
						ch = UNI_REPLACEMENT_CHAR;
	}

	target += bytesToWrite;
	if (target > targetEnd) {
		source = oldSource; /* Back up source pointer! */
		target -= bytesToWrite; result = targetExhausted; break;
	}
	switch (bytesToWrite) { /* note: everything falls through. */
		case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
		case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
		case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
		case 1: *--target =  (UTF8)(ch | firstByteMark[bytesToWrite]);
	}
	target += bytesToWrite;
	}
	*sourceStart = source;
	*targetStart = target;
	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 0x7106
movw $0x0, -0x3e(%rbp)
movl $0xbf, -0x44(%rbp)
movl $0x80, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%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 0x6f4c
cmpl $0xdbff, -0x3c(%rbp)    # imm = 0xDBFF
ja 0x6f4c
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x6f32
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x54(%rbp)
cmpl $0xdc00, -0x54(%rbp)    # imm = 0xDC00
jb 0x6f10
cmpl $0xdfff, -0x54(%rbp)    # imm = 0xDFFF
ja 0x6f10
movl -0x3c(%rbp), %eax
subl $0xd800, %eax           # imm = 0xD800
shll $0xa, %eax
movl -0x54(%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 0x6f30
cmpl $0x0, -0x24(%rbp)
jne 0x6f2e
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x7106
jmp 0x6f30
jmp 0x6f4a
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x28(%rbp)
jmp 0x7106
jmp 0x6f80
cmpl $0x0, -0x24(%rbp)
jne 0x6f7e
cmpl $0xdc00, -0x3c(%rbp)    # imm = 0xDC00
jb 0x6f7c
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x6f7c
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x7106
jmp 0x6f7e
jmp 0x6f80
cmpl $0x80, -0x3c(%rbp)
jae 0x6f91
movw $0x1, -0x3e(%rbp)
jmp 0x6fd7
cmpl $0x800, -0x3c(%rbp)     # imm = 0x800
jae 0x6fa2
movw $0x2, -0x3e(%rbp)
jmp 0x6fd5
cmpl $0x10000, -0x3c(%rbp)   # imm = 0x10000
jae 0x6fb3
movw $0x3, -0x3e(%rbp)
jmp 0x6fd3
cmpl $0x110000, -0x3c(%rbp)  # imm = 0x110000
jae 0x6fc4
movw $0x4, -0x3e(%rbp)
jmp 0x6fd1
movw $0x3, -0x3e(%rbp)
movl $0xfffd, -0x3c(%rbp)    # imm = 0xFFFD
jmp 0x6fd3
jmp 0x6fd5
jmp 0x6fd7
movzwl -0x3e(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x701e
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movzwl -0x3e(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x2, -0x28(%rbp)
jmp 0x7106
movzwl -0x3e(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x3, %eax
ja 0x70ef
movq -0x60(%rbp), %rax
leaq 0x4fc2(%rip), %rcx      # 0xc000
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x3c(%rbp), %eax
orl $0x80, %eax
andl $0xbf, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, -0x1(%rax)
movl -0x3c(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
orl $0x80, %eax
andl $0xbf, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, -0x1(%rax)
movl -0x3c(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
orl $0x80, %eax
andl $0xbf, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, -0x1(%rax)
movl -0x3c(%rbp), %eax
shrl $0x6, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movzwl -0x3e(%rbp), %ecx
movl %ecx, %edx
leaq 0x4fdf(%rip), %rcx      # 0xc0b4
movsbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, -0x1(%rax)
movzwl -0x3e(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x6e65
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)