// File: /workspace/github_cpp_makefile_star2_O3/andrakis[P]alisp/contrib/linenoise-ng/src/ConvertUTF.cpp
// Function: linenoise_ng::ConvertUTF16toUTF8(unsigned short const**, unsigned short const*, unsigned char**, unsigned char*, linenoise_ng::ConversionFlags)
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), %r15
movq (%rdx), %r14
xorl %eax, %eax
cmpq %rsi, %r15
jae 0xf7ce
leaq 0x11140(%rip), %r9      # 0x207f0
leaq 0x11199(%rip), %r10     # 0x20850
leaq 0x2(%r15), %r11
movzwl (%r15), %ebx
movl %ebx, %ebp
andl $0xfc00, %ebp           # imm = 0xFC00
cmpl $0xd800, %ebp           # imm = 0xD800
jne 0xf6ff
cmpq %rsi, %r11
jae 0xf7c9
movzwl (%r11), %r12d
movl %r12d, %ebp
andl $0xfc00, %ebp           # imm = 0xFC00
cmpl $0xdc00, %ebp           # imm = 0xDC00
jne 0xf714
shll $0xa, %ebx
addl %r12d, %ebx
addl $0xfca02400, %ebx       # imm = 0xFCA02400
leaq 0x4(%r15), %r11
jmp 0xf71d
testl %r8d, %r8d
jne 0xf71d
movzwl %bp, %ebp
cmpl $0xdc00, %ebp           # imm = 0xDC00
jne 0xf71d
jmp 0xf7c2
testl %r8d, %r8d
je 0xf7c2
movw $0x1, %bp
cmpl $0x80, %ebx
jb 0xf743
movw $0x2, %bp
cmpl $0x800, %ebx            # imm = 0x800
jb 0xf743
cmpl $0x10000, %ebx          # imm = 0x10000
movw $0x4, %bp
sbbw $0x0, %bp
movzwl %bp, %r12d
leaq (%r14,%r12), %r13
cmpq %rcx, %r13
ja 0xf7bb
leal -0x1(%r12), %r14d
movslq (%r9,%r14,4), %r14
addq %r9, %r14
jmpq *%r14
movl %ebx, %ebp
andb $0x3f, %bpl
orb $-0x80, %bpl
movb %bpl, -0x1(%r13)
decq %r13
shrl $0x6, %ebx
movl %ebx, %ebp
andb $0x3f, %bpl
orb $-0x80, %bpl
movb %bpl, -0x1(%r13)
decq %r13
shrl $0x6, %ebx
movl %ebx, %ebp
andb $0x3f, %bpl
orb $-0x80, %bpl
movb %bpl, -0x1(%r13)
decq %r13
shrl $0x6, %ebx
orb (%r12,%r10), %bl
movb %bl, -0x1(%r13)
leaq (%r12,%r13), %r14
decq %r14
movq %r11, %r15
cmpq %rsi, %r11
jb 0xf6b7
movq %r11, %r15
jmp 0xf7ce
movl $0x2, %eax
jmp 0xf7ce
movl $0x3, %eax
jmp 0xf7ce
movl $0x1, %eax
movq %r15, (%rdi)
movq %r14, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq