// File: /workspace/github_cpp_makefile_star2_O1/andrakis[P]alisp/contrib/linenoise-ng/src/ConvertUTF.cpp
// Function: linenoise_ng::ConvertUTF32toUTF8(unsigned int const**, unsigned int const*, unsigned char**, unsigned char*, linenoise_ng::ConversionFlags)
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), %r15
xorl %eax, %eax
leaq 0x11a1c(%rip), %r10     # 0x21838
leaq 0x11a3d(%rip), %r11     # 0x21860
movl $0xfffd, %ebx           # imm = 0xFFFD
movl $0x3, %ebp
cmpq %rsi, %r9
jae 0xff2f
movl (%r9), %r14d
testl %r8d, %r8d
jne 0xfe5e
movl %r14d, %r12d
andl $0xfffff800, %r12d      # imm = 0xFFFFF800
cmpl $0xd800, %r12d          # imm = 0xD800
jne 0xfe5e
xorl %r14d, %r14d
movl $0x3, %eax
jmp 0xff26
movw $0x1, %r12w
cmpl $0x80, %r14d
jb 0xfea8
movw $0x2, %r12w
cmpl $0x800, %r14d           # imm = 0x800
jb 0xfea8
movw $0x3, %r12w
cmpl $0x10000, %r14d         # imm = 0x10000
jb 0xfea8
cmpl $0x110000, %r14d        # imm = 0x110000
movl $0x0, %r12d
adcw $0x3, %r12w
cmpl $0x110000, %r14d        # imm = 0x110000
cmovael %ebx, %r14d
cmovael %ebp, %eax
movzwl %r12w, %r12d
leaq (%r15,%r12), %r13
cmpq %rcx, %r13
jbe 0xfebf
movl $0x2, %eax
xorl %r14d, %r14d
jmp 0xff26
leal -0x1(%r12), %r15d
movslq (%r10,%r15,4), %r15
addq %r10, %r15
jmpq *%r15
movl %r14d, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
movl %r14d, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
movl %r14d, %r15d
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
orb (%r12,%r11), %r14b
addq $0x4, %r9
movb %r14b, -0x1(%r13)
leaq (%r12,%r13), %r15
decq %r15
movb $0x1, %r14b
testb %r14b, %r14b
jne 0xfe2d
movq %r9, (%rdi)
movq %r15, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq