// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
movq (%rdi), %r15
movq %rdx, 0x18(%rsp)
movq (%rdx), %rax
movq %rax, (%rsp)
xorl %edx, %edx
leaq 0x118f8(%rip), %rbp     # 0x21870
cmpq %r13, %r15
jae 0x100b2
movzbl (%r15), %r14d
movsbl (%r14,%rbp), %eax
movzwl %ax, %r12d
leaq (%r15,%r12), %rax
cmpq %r13, %rax
jae 0xffd0
movl %edx, 0xc(%rsp)
leal 0x1(%r12), %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0xfb89
xorl %ecx, %ecx
testb %al, %al
je 0xffda
cmpb $0x5, %r12b
ja 0x1002d
movzbl %r12b, %eax
leaq 0x11887(%rip), %rdx     # 0x21848
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movzbl %r12b, %eax
jmp 0x10024
movl $0x1, %edx
jmp 0x1008a
movl $0x3, %edx
jmp 0x1008c
incq %r15
shll $0x6, %r14d
movl %r14d, %ecx
movzbl (%r15), %eax
incq %r15
addl %ecx, %eax
shll $0x6, %eax
movl %eax, %ecx
movzbl (%r15), %eax
incq %r15
addl %ecx, %eax
shll $0x6, %eax
movl %eax, %ecx
movzbl (%r15), %eax
incq %r15
addl %ecx, %eax
shll $0x6, %eax
movl %eax, %ecx
movzbl (%r15), %eax
incq %r15
addl %ecx, %eax
shll $0x6, %eax
movzbl (%r15), %ecx
incq %r15
addl %eax, %ecx
movq 0x20(%rsp), %rax
cmpq %rax, (%rsp)
jae 0x10080
leaq 0x11931(%rip), %rax     # 0x21970
subl (%rax,%r12,4), %ecx
cmpl $0x10ffff, %ecx         # imm = 0x10FFFF
ja 0x10096
cmpl $0x0, 0x8(%rsp)
setne %al
movl %ecx, %edx
andl $0x1ff800, %edx         # imm = 0x1FF800
cmpl $0xd800, %edx           # imm = 0xD800
setne %dl
movl $0xfffd, %edi           # imm = 0xFFFD
cmovel %edi, %ecx
orb %al, %dl
movl 0xc(%rsp), %edx
jne 0x100a0
movl %ebx, %eax
subq %rax, %r15
movl $0x3, %edx
jmp 0x1008a
movl %ebx, %eax
subq %rax, %r15
movl $0x2, %edx
xorl %ecx, %ecx
testb %cl, %cl
jne 0xff78
jmp 0x100b2
movl $0x3, %edx
movl $0xfffd, %ecx           # imm = 0xFFFD
movq (%rsp), %rax
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, (%rsp)
movb $0x1, %cl
jmp 0x1008c
movq 0x10(%rsp), %rax
movq %r15, (%rax)
movq 0x18(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop