// File: sangwoojun[P]rv32emulator/./linenoise.hpp
// Function: linenoise::unicodeUTF8CharToCodePoint(char const*, int, int*)
inline int unicodeUTF8CharToCodePoint(
   const char* buf,
   int         len,
   int*        cp)
{
    if (len) {
        unsigned char byte = buf[0];
        if ((byte & 0x80) == 0) {
            *cp = byte;
            return 1;
        } else if ((byte & 0xE0) == 0xC0) {
            if (len >= 2) {
                *cp = (((unsigned long)(buf[0] & 0x1F)) << 6) |
                       ((unsigned long)(buf[1] & 0x3F));
                return 2;
            }
        } else if ((byte & 0xF0) == 0xE0) {
            if (len >= 3) {
                *cp = (((unsigned long)(buf[0] & 0x0F)) << 12) |
                      (((unsigned long)(buf[1] & 0x3F)) << 6) |
                       ((unsigned long)(buf[2] & 0x3F));
                return 3;
            }
        } else if ((byte & 0xF8) == 0xF0) {
            if (len >= 4) {
                *cp = (((unsigned long)(buf[0] & 0x07)) << 18) |
                      (((unsigned long)(buf[1] & 0x3F)) << 12) |
                      (((unsigned long)(buf[2] & 0x3F)) << 6) |
                       ((unsigned long)(buf[3] & 0x3F));
                return 4;
            }
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x8a84
movq -0x10(%rbp), %rax
movb (%rax), %al
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x8956
movzbl -0x21(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x8a8b
movzbl -0x21(%rbp), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x89a6
cmpl $0x2, -0x14(%rbp)
jl 0x89a1
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
andl $0x1f, %eax
cltq
shlq $0x6, %rax
movq -0x10(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x8a8b
jmp 0x8a80
movzbl -0x21(%rbp), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x8a08
cmpl $0x3, -0x14(%rbp)
jl 0x8a06
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
andl $0xf, %eax
cltq
shlq $0xc, %rax
movq -0x10(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
orq %rcx, %rax
movq -0x10(%rbp), %rcx
movsbl 0x2(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x3, -0x4(%rbp)
jmp 0x8a8b
jmp 0x8a7e
movzbl -0x21(%rbp), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x8a7c
cmpl $0x4, -0x14(%rbp)
jl 0x8a7a
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
andl $0x7, %eax
cltq
shlq $0x12, %rax
movq -0x10(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
shlq $0xc, %rcx
orq %rcx, %rax
movq -0x10(%rbp), %rcx
movsbl 0x2(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
orq %rcx, %rax
movq -0x10(%rbp), %rcx
movsbl 0x3(%rcx), %ecx
andl $0x3f, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x8a8b
jmp 0x8a7c
jmp 0x8a7e
jmp 0x8a80
jmp 0x8a82
jmp 0x8a84
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq