// File: /workspace/github_cpp_makefile_star2_O2/Ciaxur[P]yagb-emu/include/imgui/imgui.cpp
// Function: ImTextCharFromUtf8(unsigned int*, char const*, char const*)
int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)
{
    static const char lengths[32] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 };
    static const int masks[]  = { 0x00, 0x7f, 0x1f, 0x0f, 0x07 };
    static const uint32_t mins[] = { 0x400000, 0, 0x80, 0x800, 0x10000 };
    static const int shiftc[] = { 0, 18, 12, 6, 0 };
    static const int shifte[] = { 0, 6, 4, 2, 0 };
    int len = lengths[*(const unsigned char*)in_text >> 3];
    int wanted = len + !len;

    if (in_text_end == NULL)
        in_text_end = in_text + wanted; // Max length, nulls will be taken into account.

    // Copy at most 'len' bytes, stop copying at 0 or past in_text_end. Branch predictor does a good job here,
    // so it is fast even with excessive branching.
    unsigned char s[4];
    s[0] = in_text + 0 < in_text_end ? in_text[0] : 0;
    s[1] = in_text + 1 < in_text_end ? in_text[1] : 0;
    s[2] = in_text + 2 < in_text_end ? in_text[2] : 0;
    s[3] = in_text + 3 < in_text_end ? in_text[3] : 0;

    // Assume a four-byte character and load four bytes. Unused bits are shifted out.
    *out_char  = (uint32_t)(s[0] & masks[len]) << 18;
    *out_char |= (uint32_t)(s[1] & 0x3f) << 12;
    *out_char |= (uint32_t)(s[2] & 0x3f) <<  6;
    *out_char |= (uint32_t)(s[3] & 0x3f) <<  0;
    *out_char >>= shiftc[len];

    // Accumulate the various error conditions.
    int e = 0;
    e  = (*out_char < mins[len]) << 6; // non-canonical encoding
    e |= ((*out_char >> 11) == 0x1b) << 7;  // surrogate half?
    e |= (*out_char > IM_UNICODE_CODEPOINT_MAX) << 8;  // out of range?
    e |= (s[1] & 0xc0) >> 2;
    e |= (s[2] & 0xc0) >> 4;
    e |= (s[3]       ) >> 6;
    e ^= 0x2a; // top two bits of each tail byte correct?
    e >>= shifte[len];

    if (e)
    {
        // No bytes are consumed when *in_text == 0 || in_text == in_text_end.
        // One byte is consumed in case of invalid first byte of in_text.
        // All available bytes (at most `len` bytes) are consumed on incomplete/invalid second to last bytes.
        // Invalid or incomplete input may consume less bytes than wanted, therefore every byte has to be inspected in s.
        wanted = ImMin(wanted, !!s[0] + !!s[1] + !!s[2] + !!s[3]);
        *out_char = IM_UNICODE_CODEPOINT_INVALID;
    }

    return wanted;
}

// Assembly:
movzbl (%rsi), %r8d
movl %r8d, %eax
shrl $0x3, %eax
leaq 0x6e71e(%rip), %rcx     # 0xa21b0
movsbq (%rax,%rcx), %r9
movl $0x80ff0000, %ecx       # imm = 0x80FF0000
btl %eax, %ecx
movq %r9, %rax
adcq $0x0, %rax
leaq (%rsi,%rax), %rcx
testq %rdx, %rdx
cmovneq %rdx, %rcx
xorl %edx, %edx
cmpq %rsi, %rcx
cmovbel %edx, %r8d
leaq 0x1(%rsi), %r10
cmpq %rcx, %r10
jae 0x33ac6
movb (%r10), %dl
leaq 0x2(%rsi), %r10
cmpq %rcx, %r10
jae 0x33ad4
movb (%r10), %r10b
jmp 0x33ad7
xorl %r10d, %r10d
addq $0x3, %rsi
cmpq %rcx, %rsi
jae 0x33ae5
movb (%rsi), %sil
jmp 0x33ae7
xorl %esi, %esi
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movzbl %r8b, %ecx
leaq 0x69311(%rip), %r11     # 0x9ce10
andl (%r11,%r9,4), %ecx
shll $0x12, %ecx
movzbl %dl, %r11d
movl %r11d, %r14d
andl $0x3f, %r14d
shll $0xc, %r14d
orl %ecx, %r14d
movzbl %r10b, %ebx
movl %ebx, %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
orl %r14d, %ecx
movzbl %sil, %r14d
movl %r14d, %r12d
andl $0x3f, %r12d
orl %ecx, %r12d
leaq 0x69314(%rip), %rcx     # 0x9ce50
movb (%rcx,%r9,4), %cl
shrl %cl, %r12d
movl %r12d, (%rdi)
leaq 0x692e3(%rip), %r15     # 0x9ce30
xorl %ecx, %ecx
cmpl (%r15,%r9,4), %r12d
setb %cl
shll $0x6, %ecx
movl %r12d, %r13d
andl $0x7ffff800, %r13d      # imm = 0x7FFFF800
xorl %r15d, %r15d
cmpl $0xd800, %r13d          # imm = 0xD800
sete %r15b
shll $0x7, %r15d
xorl %r13d, %r13d
cmpl $0x10000, %r12d         # imm = 0x10000
setae %r13b
shll $0x8, %r13d
shrl $0x2, %r11d
andl $-0x10, %r11d
shrl $0x6, %ebx
shrl $0x6, %r14d
leal (%r11,%rbx,4), %r11d
orl %r14d, %r11d
orl %ecx, %r11d
orl %r13d, %r11d
orl %r15d, %r11d
xorl $0x2a, %r11d
leaq 0x692bf(%rip), %rcx     # 0x9ce70
movb (%rcx,%r9,4), %cl
shrl %cl, %r11d
testl %r11d, %r11d
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
je 0x33bf1
xorl %ecx, %ecx
testb %dl, %dl
setne %cl
cmpb $0x1, %r8b
sbbl $-0x1, %ecx
cmpb $0x1, %r10b
sbbl $-0x1, %ecx
cmpb $0x1, %sil
sbbl $-0x1, %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl $0xfffd, (%rdi)         # imm = 0xFFFD
movl %ecx, %eax
retq
retq