// File: 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
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 0x78ae
movw $0x0, -0x3e(%rbp)
movl $0xbf, -0x44(%rbp)
movl $0x80, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, -0x30(%rbp)
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x771d
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jb 0x771b
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x771b
movq -0x30(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x78ae
jmp 0x771d
cmpl $0x80, -0x3c(%rbp)
jae 0x772e
movw $0x1, -0x3e(%rbp)
jmp 0x777b
cmpl $0x800, -0x3c(%rbp)     # imm = 0x800
jae 0x773f
movw $0x2, -0x3e(%rbp)
jmp 0x7779
cmpl $0x10000, -0x3c(%rbp)   # imm = 0x10000
jae 0x7750
movw $0x3, -0x3e(%rbp)
jmp 0x7777
cmpl $0x10ffff, -0x3c(%rbp)  # imm = 0x10FFFF
ja 0x7761
movw $0x4, -0x3e(%rbp)
jmp 0x7775
movw $0x3, -0x3e(%rbp)
movl $0xfffd, -0x3c(%rbp)    # imm = 0xFFFD
movl $0x3, -0x28(%rbp)
jmp 0x7777
jmp 0x7779
jmp 0x777b
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 0x77c6
movq -0x30(%rbp), %rax
addq $-0x4, %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 0x78ae
movzwl -0x3e(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0x3, %eax
ja 0x7897
movq -0x50(%rbp), %rax
leaq 0x48a6(%rip), %rcx      # 0xc08c
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 0x4837(%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 0x76b5
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
nopl (%rax)