// File: 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
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 0x1cf5e
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 0x1cdcd
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jb 0x1cdcb
cmpl $0xdfff, -0x3c(%rbp)    # imm = 0xDFFF
ja 0x1cdcb
movq -0x30(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x30(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x1cf5e
jmp 0x1cdcd
cmpl $0x80, -0x3c(%rbp)
jae 0x1cdde
movw $0x1, -0x3e(%rbp)
jmp 0x1ce2b
cmpl $0x800, -0x3c(%rbp)     # imm = 0x800
jae 0x1cdef
movw $0x2, -0x3e(%rbp)
jmp 0x1ce29
cmpl $0x10000, -0x3c(%rbp)   # imm = 0x10000
jae 0x1ce00
movw $0x3, -0x3e(%rbp)
jmp 0x1ce27
cmpl $0x10ffff, -0x3c(%rbp)  # imm = 0x10FFFF
ja 0x1ce11
movw $0x4, -0x3e(%rbp)
jmp 0x1ce25
movw $0x3, -0x3e(%rbp)
movl $0xfffd, -0x3c(%rbp)    # imm = 0xFFFD
movl $0x3, -0x28(%rbp)
jmp 0x1ce27
jmp 0x1ce29
jmp 0x1ce2b
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 0x1ce76
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 0x1cf5e
movzwl -0x3e(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0x3, %eax
ja 0x1cf47
movq -0x50(%rbp), %rax
leaq 0x1f9d6(%rip), %rcx     # 0x3c86c
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 0x1f967(%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 0x1cd65
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)