// File: /workspace/github_cpp_makefile_star2_O2/codebrainz[P]libutfxx/utf/utf.cxx
// Function: ConvertUTF32toUTF8
ConversionResult ConvertUTF32toUTF8 (
	const UTF32** sourceStart, const UTF32* sourceEnd,
	UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
	ConversionResult result = conversionOK;
	const UTF32* source = *sourceStart;
	UTF8* target = *targetStart;
	while (source < sourceEnd) {
	UTF32 ch;
	unsigned short bytesToWrite = 0;
	const UTF32 byteMask = 0xBF;
	const UTF32 byteMark = 0x80;
	ch = *source++;
	if (flags == strictConversion ) {
		/* UTF-16 surrogate values are illegal in UTF-32 */
		if (ch >= UNI_SUR_HIGH_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. Turn any
	 * illegally large UTF32 things (> Plane 17) into replacement chars.
	 */
	if (ch < (UTF32)0x80) {	     bytesToWrite = 1;
	} else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
	} else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
	} else if (ch <= UNI_MAX_LEGAL_UTF32) {  bytesToWrite = 4;
	} else {			    bytesToWrite = 3;
						ch = UNI_REPLACEMENT_CHAR;
						result = sourceIllegal;
	}

	target += bytesToWrite;
	if (target > targetEnd) {
		--source; /* 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), %r9
movq (%rdx), %r12
xorl %eax, %eax
movl $0x3, %r10d
leaq 0x25fd(%rip), %rbx      # 0x7028
leaq 0x261e(%rip), %r14      # 0x7050
movl $0xfffd, %ebp           # imm = 0xFFFD
cmpq %rsi, %r9
jae 0x4b28
movl (%r9), %r15d
movl %r15d, %r11d
andl $0xfffff800, %r11d      # imm = 0xFFFFF800
xorl $0xd800, %r11d          # imm = 0xD800
orl %r8d, %r11d
je 0x4b1e
movw $0x1, %r11w
cmpl $0x80, %r15d
jb 0x4aa8
movw $0x2, %r11w
cmpl $0x800, %r15d           # imm = 0x800
jb 0x4aa8
movw $0x3, %r11w
cmpl $0x10000, %r15d         # imm = 0x10000
jb 0x4aa8
cmpl $0x110000, %r15d        # imm = 0x110000
movl $0x0, %r11d
adcw $0x3, %r11w
cmpl $0x110000, %r15d        # imm = 0x110000
cmovael %ebp, %r15d
cmovael %r10d, %eax
movzwl %r11w, %r13d
leaq (%r12,%r13), %r11
cmpq %rcx, %r11
ja 0x4b23
leal -0x1(%r13), %r12d
movslq (%rbx,%r12,4), %r12
addq %rbx, %r12
jmpq *%r12
movl %r15d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r11)
decq %r11
shrl $0x6, %r15d
movl %r15d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r11)
decq %r11
shrl $0x6, %r15d
movl %r15d, %r12d
andb $0x3f, %r12b
orb $-0x80, %r12b
movb %r12b, -0x1(%r11)
decq %r11
shrl $0x6, %r15d
addq $0x4, %r9
orb (%r13,%r14), %r15b
movb %r15b, -0x1(%r11)
leaq (%r11,%r13), %r12
decq %r12
jmp 0x4a37
movl %r10d, %eax
jmp 0x4b28
movl $0x2, %eax
movq %r9, (%rdi)
movq %r12, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq