// File: /workspace/github_cpp_makefile_star2_O3/andrakis[P]alisp/contrib/linenoise-ng/src/ConvertUTF.cpp
// Function: linenoise_ng::ConvertUTF8toUTF32(unsigned char const**, unsigned char const*, unsigned int**, unsigned int*, linenoise_ng::ConversionFlags)
ConversionResult ConvertUTF8toUTF32 (
        const UTF8** sourceStart, const UTF8* sourceEnd, 
        UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
    ConversionResult result = conversionOK;
    const UTF8* source = *sourceStart;
    UTF32* target = *targetStart;
    while (source < sourceEnd) {
        UTF32 ch = 0;
        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
        if (source + extraBytesToRead >= sourceEnd) {
            result = sourceExhausted; break;
        }
        /* Do this check whether lenient or strict */
        if (! isLegalUTF8(source, extraBytesToRead+1)) {
            result = sourceIllegal;
            break;
        }
        /*
         * The cases all fall through. See "Note A" below.
         */
        switch (extraBytesToRead) {
            case 5: ch += *source++; ch <<= 6;
            case 4: ch += *source++; ch <<= 6;
            case 3: ch += *source++; ch <<= 6;
            case 2: ch += *source++; ch <<= 6;
            case 1: ch += *source++; ch <<= 6;
            case 0: ch += *source++;
        }
        ch -= offsetsFromUTF8[extraBytesToRead];

        if (target >= targetEnd) {
            source -= (extraBytesToRead+1); /* Back up the source pointer! */
            result = targetExhausted; break;
        }
        if (ch <= UNI_MAX_LEGAL_UTF32) {
            /*
             * UTF-16 surrogate values are illegal in UTF-32, and anything
             * over Plane 17 (> 0x10FFFF) is illegal.
             */
            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
                if (flags == strictConversion) {
                    source -= (extraBytesToRead+1); /* return to the illegal value itself */
                    result = sourceIllegal;
                    break;
                } else {
                    *target++ = UNI_REPLACEMENT_CHAR;
                }
            } else {
                *target++ = ch;
            }
        } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
            result = sourceIllegal;
            *target++ = UNI_REPLACEMENT_CHAR;
        }
    }
    *sourceStart = source;
    *targetStart = target;
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdi), %r15
movq %rdx, 0x18(%rsp)
movq (%rdx), %r12
cmpq %rsi, %r15
jae 0xfcdb
movq %rsi, %rbx
movl $0x0, 0x8(%rsp)
movzbl (%r15), %r13d
leaq 0x10c8f(%rip), %rax     # 0x20860
movsbl (%r13,%rax), %eax
movzwl %ax, %ebp
leaq (%r15,%rbp), %rax
cmpq %rbx, %rax
jae 0xfce5
leal 0x1(%rbp), %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0xf805
testb %al, %al
je 0xfd05
xorl %eax, %eax
cmpb $0x5, %bpl
ja 0xfc7c
movzbl %bpl, %ecx
leaq 0x10c27(%rip), %rdx     # 0x20838
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %bpl, %ecx
jmp 0xfc73
movl %r13d, %eax
shll $0x6, %eax
movb 0x1(%r15), %r13b
incq %r15
movzbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movzbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movzbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
movl %ecx, %eax
movzbl %r13b, %ecx
addl %eax, %ecx
shll $0x6, %ecx
movb 0x1(%r15), %r13b
incq %r15
incq %r15
movzbl %r13b, %eax
addl %ecx, %eax
cmpq 0x20(%rsp), %r12
jae 0xfcef
leaq 0x10cd6(%rip), %rcx     # 0x20960
subl (%rcx,%rbp,4), %eax
cmpl $0x10ffff, %eax         # imm = 0x10FFFF
ja 0xfcbb
cmpl $0x0, 0xc(%rsp)
setne %cl
movl %eax, %edx
andl $0x1ff800, %edx         # imm = 0x1FF800
cmpl $0xd800, %edx           # imm = 0xD800
setne %dl
movl $0xfffd, %esi           # imm = 0xFFFD
cmovel %esi, %eax
orb %cl, %dl
jne 0xfcc8
jmp 0xfcff
movl $0x3, 0x8(%rsp)
movl $0xfffd, %eax           # imm = 0xFFFD
movl %eax, (%r12)
addq $0x4, %r12
cmpq %rbx, %r15
jb 0xfbc6
jmp 0xfd0d
movl $0x0, 0x8(%rsp)
jmp 0xfd0d
movl $0x1, 0x8(%rsp)
jmp 0xfd0d
movl %r14d, %eax
subq %rax, %r15
movl $0x2, 0x8(%rsp)
jmp 0xfd0d
movl %r14d, %eax
subq %rax, %r15
movl $0x3, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq %r15, (%rax)
movq 0x18(%rsp), %rax
movq %r12, (%rax)
movl 0x8(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq