// File: /workspace/github_cpp_makefile_star2_O1/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), %rbx
xorl %eax, %eax
leaq 0x11e15(%rip), %r9      # 0x21800
leaq 0x11e6e(%rip), %r10     # 0x21860
cmpq %rsi, %r15
jae 0xfb4f
leaq 0x2(%r15), %r11
movzwl (%r15), %r14d
movl %r14d, %r12d
andl $0xfc00, %r12d          # imm = 0xFC00
cmpl $0xd800, %r12d          # imm = 0xD800
jne 0xfa46
cmpq %rsi, %r11
jae 0xfa6a
movzwl (%r11), %r12d
movl %r12d, %ebp
andl $0xfc00, %ebp           # imm = 0xFC00
cmpl $0xdc00, %ebp           # imm = 0xDC00
jne 0xfa76
shll $0xa, %r14d
addl %r12d, %r14d
addl $0xfca02400, %r14d      # imm = 0xFCA02400
leaq 0x4(%r15), %r11
xorl %ebp, %ebp
jmp 0xfa81
testl %r8d, %r8d
jne 0xfa8d
movl $0x3, %ebp
movzwl %r12w, %r12d
cmpl $0xdc00, %r12d          # imm = 0xDC00
jne 0xfa8d
movq %r15, %r11
movl $0x3, %eax
jmp 0xfb2d
movl $0x3, %ebp
movl $0x1, %eax
jmp 0xfacd
xorl %ebp, %ebp
testl %r8d, %r8d
je 0xfb3a
movb $0x1, %r12b
testb %r12b, %r12b
je 0xfb2d
movw $0x1, %bp
cmpl $0x80, %r14d
jb 0xfab6
movw $0x2, %bp
cmpl $0x800, %r14d           # imm = 0x800
jb 0xfab6
cmpl $0x10000, %r14d         # imm = 0x10000
movw $0x4, %bp
sbbw $0x0, %bp
movzwl %bp, %r12d
leaq (%rbx,%r12), %r13
cmpq %rcx, %r13
jbe 0xfad2
movl $0x3, %ebp
movl $0x2, %eax
movq %r15, %r11
jmp 0xfb2d
leal -0x1(%r12), %ebx
movslq (%r9,%rbx,4), %rbx
addq %r9, %rbx
jmpq *%rbx
movl %r14d, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
movl %r14d, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
movl %r14d, %ebx
andb $0x3f, %bl
orb $-0x80, %bl
movb %bl, -0x1(%r13)
decq %r13
shrl $0x6, %r14d
orb (%r12,%r10), %r14b
movb %r14b, -0x1(%r13)
leaq (%r12,%r13), %rbx
decq %rbx
xorl %ebp, %ebp
movq %r11, %r15
testl %ebp, %ebp
je 0xf9f2
jmp 0xfb52
movl $0x3, %eax
movq %r15, %r11
xorl %r12d, %r12d
movl $0x3, %ebp
jmp 0xfa84
movq %r15, %r11
movq %r11, (%rdi)
movq %rbx, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq