// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%rdi), %r13
movq (%rdx), %r12
movl $0x3, %eax
movl $0x2, %r10d
leaq 0x2924(%rip), %r11      # 0x7000
leaq 0x296d(%rip), %rbx      # 0x7050
cmpq %rsi, %r13
jae 0x47f5
leaq 0x2(%r13), %r15
movzwl (%r13), %r14d
movl %r14d, %r9d
andl $0xfc00, %r9d           # imm = 0xFC00
cmpw $0xd800, %r9w           # imm = 0xD800
jne 0x4738
cmpq %rsi, %r15
jae 0x47fe
movzwl (%r15), %r9d
movl %r9d, %ebp
andl $0xfc00, %ebp           # imm = 0xFC00
cmpw $0xdc00, %bp            # imm = 0xDC00
jne 0x474a
shll $0xa, %r14d
addl %r9d, %r14d
addl $0xfca02400, %r14d      # imm = 0xFCA02400
leaq 0x4(%r13), %r15
jmp 0x4753
testl %r8d, %r8d
jne 0x4753
cmpw $0xdc00, %r9w           # imm = 0xDC00
jne 0x4753
jmp 0x4803
testl %r8d, %r8d
je 0x4803
movw $0x1, %r9w
cmpl $0x80, %r14d
jb 0x4780
movw $0x2, %r9w
cmpl $0x800, %r14d           # imm = 0x800
jb 0x4780
cmpl $0x10000, %r14d         # imm = 0x10000
movw $0x4, %r9w
sbbw $0x0, %r9w
movzwl %r9w, %ebp
leaq (%r12,%rbp), %r9
cmpq %rcx, %r9
ja 0x47f9
leal -0x1(%rbp), %r12d
movslq (%r11,%r12,4), %r12
addq %r11, %r12
jmpq *%r12
movl %r14d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r9)
decq %r9
shrl $0x6, %r14d
movl %r14d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r9)
decq %r9
shrl $0x6, %r14d
movl %r14d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r9)
decq %r9
shrl $0x6, %r14d
orb (%rbp,%rbx), %r14b
movb %r14b, -0x1(%r9)
leaq (%r9,%rbp), %r12
decq %r12
movq %r15, %r13
jmp 0x46e3
xorl %eax, %eax
jmp 0x4803
movl %r10d, %eax
jmp 0x4803
movl $0x1, %eax
movq %r13, (%rdi)
movq %r12, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq