// File: 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
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 0x1c7b6
movw $0x0, -0x3e(%rbp)
movl $0xbf, -0x44(%rbp)
movl $0x80, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x30(%rbp)
movzwl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jb 0x1c5fc
cmpl $0xdbff, -0x3c(%rbp)    # imm = 0xDBFF
ja 0x1c5fc
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x1c5e2
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x54(%rbp)
cmpl $0xdc00, -0x54(%rbp)    # imm = 0xDC00
jb 0x1c5c0
cmpl $0xdfff, -0x54(%rbp)    # imm = 0xDFFF
ja 0x1c5c0
movl -0x3c(%rbp), %eax
subl $0xd800, %eax           # imm = 0xD800
shll $0xa, %eax
movl -0x54(%rbp), %ecx
subl $0xdc00, %ecx           # imm = 0xDC00
addl %ecx, %eax
addl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x1c5e0
cmpl $0x0, -0x24(%rbp)
jne 0x1c5de
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x1c7b6
jmp 0x1c5e0
jmp 0x1c5fa
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x28(%rbp)
jmp 0x1c7b6
jmp 0x1c630
cmpl $0x0, -0x24(%rbp)
jne 0x1c62e
cmpl $0xdc00, -0x3c(%rbp)    # imm = 0xDC00
jb 0x1c62c
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x1c62c
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x1c7b6
jmp 0x1c62e
jmp 0x1c630
cmpl $0x80, -0x3c(%rbp)
jae 0x1c641
movw $0x1, -0x3e(%rbp)
jmp 0x1c687
cmpl $0x800, -0x3c(%rbp)     # imm = 0x800
jae 0x1c652
movw $0x2, -0x3e(%rbp)
jmp 0x1c685
cmpl $0x10000, -0x3c(%rbp)   # imm = 0x10000
jae 0x1c663
movw $0x3, -0x3e(%rbp)
jmp 0x1c683
cmpl $0x110000, -0x3c(%rbp)  # imm = 0x110000
jae 0x1c674
movw $0x4, -0x3e(%rbp)
jmp 0x1c681
movw $0x3, -0x3e(%rbp)
movl $0xfffd, -0x3c(%rbp)    # imm = 0xFFFD
jmp 0x1c683
jmp 0x1c685
jmp 0x1c687
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 0x1c6ce
movq -0x50(%rbp), %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 0x1c7b6
movzwl -0x3e(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x3, %eax
ja 0x1c79f
movq -0x60(%rbp), %rax
leaq 0x200f2(%rip), %rcx     # 0x3c7e0
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 0x2010f(%rip), %rcx     # 0x3c894
movzbl (%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 0x1c515
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
nopw %cs:(%rax,%rax)