// File: /workspace/github_c_makefile_star2_O1/neovim[P]libtermkey/termkey.c
// Function: parse_utf8
static TermKeyResult parse_utf8(const unsigned char *bytes, size_t len, long *cp, size_t *nbytep)
{
  unsigned int nbytes;

  unsigned char b0 = bytes[0];

  if(b0 < 0x80) {
    // Single byte ASCII
    *cp = b0;
    *nbytep = 1;
    return TERMKEY_RES_KEY;
  }
  else if(b0 < 0xc0) {
    // Starts with a continuation byte - that's not right
    *cp = UTF8_INVALID;
    *nbytep = 1;
    return TERMKEY_RES_KEY;
  }
  else if(b0 < 0xe0) {
    nbytes = 2;
    *cp = b0 & 0x1f;
  }
  else if(b0 < 0xf0) {
    nbytes = 3;
    *cp = b0 & 0x0f;
  }
  else if(b0 < 0xf8) {
    nbytes = 4;
    *cp = b0 & 0x07;
  }
  else if(b0 < 0xfc) {
    nbytes = 5;
    *cp = b0 & 0x03;
  }
  else if(b0 < 0xfe) {
    nbytes = 6;
    *cp = b0 & 0x01;
  }
  else {
    *cp = UTF8_INVALID;
    *nbytep = 1;
    return TERMKEY_RES_KEY;
  }

  for(unsigned int b = 1; b < nbytes; b++) {
    unsigned char cb;

    if(b >= len)
      return TERMKEY_RES_AGAIN;

    cb = bytes[b];
    if(cb < 0x80 || cb >= 0xc0) {
      *cp = UTF8_INVALID;
      *nbytep = b;
      return TERMKEY_RES_KEY;
    }

    *cp <<= 6;
    *cp |= cb & 0x3f;
  }

  // Check for overlong sequences
  if(nbytes > utf8_seqlen(*cp))
    *cp = UTF8_INVALID;

  // Check for UTF-16 surrogates or invalid *cps
  if((*cp >= 0xD800 && *cp <= 0xDFFF) ||
     *cp == 0xFFFE ||
     *cp == 0xFFFF)
    *cp = UTF8_INVALID;

  *nbytep = nbytes;
  return TERMKEY_RES_KEY;
}

// Assembly:
pushq %rbx
movzbl (%rdi), %eax
testb %al, %al
js 0x4be0
movq %rax, (%rdx)
jmp 0x4beb
cmpb $-0x41, %al
ja 0x4bfb
movq $0xfffd, (%rdx)         # imm = 0xFFFD
movl $0x1, %r8d
movq %r8, (%rcx)
movl $0x1, %eax
popq %rbx
retq
cmpb $-0x20, %al
jae 0x4c0d
movl $0x2, %r8d
movl $0x1f, %r9d
jmp 0x4c53
cmpb $-0x10, %al
jae 0x4c1f
movl $0x3, %r8d
movl $0xf, %r9d
jmp 0x4c53
cmpb $-0x8, %al
jae 0x4c31
movl $0x4, %r8d
movl $0x7, %r9d
jmp 0x4c53
cmpb $-0x4, %al
jae 0x4c43
movl $0x5, %r8d
movl $0x3, %r9d
jmp 0x4c53
cmpb $-0x2, %al
jae 0x4be4
movl $0x6, %r8d
movl $0x1, %r9d
andl %eax, %r9d
movq %r9, (%rdx)
movl %r8d, %r8d
movb $0x1, %r10b
movl $0x1, %r9d
cmpq %rsi, %r9
jae 0x4c86
movzbl (%rdi,%r9), %r11d
cmpb $-0x40, %r11b
jl 0x4c90
movq $0xfffd, (%rdx)         # imm = 0xFFFD
movq %r9, (%rcx)
movl $0x1, %eax
jmp 0x4c8b
movl $0x3, %eax
xorl %r11d, %r11d
jmp 0x4ca4
movq (%rdx), %rbx
shlq $0x6, %rbx
andl $0x3f, %r11d
orq %rbx, %r11
movq %r11, (%rdx)
movb $0x1, %r11b
testb %r11b, %r11b
je 0x4cb5
incq %r9
cmpq %r8, %r9
setb %r10b
jne 0x4c65
testb $0x1, %r10b
jne 0x4bf9
movq (%rdx), %rsi
movl $0x1, %eax
cmpq $0x80, %rsi
jl 0x4d09
movl $0x2, %eax
cmpq $0x800, %rsi            # imm = 0x800
jl 0x4d09
movl $0x3, %eax
cmpq $0x10000, %rsi          # imm = 0x10000
jl 0x4d09
movl $0x4, %eax
cmpq $0x200000, %rsi         # imm = 0x200000
jl 0x4d09
xorl %eax, %eax
cmpq $0x4000000, %rsi        # imm = 0x4000000
setge %al
addl $0x5, %eax
cmpl %eax, %r8d
jbe 0x4d15
movq $0xfffd, (%rdx)         # imm = 0xFFFD
movq (%rdx), %rax
movq %rax, %rsi
andq $-0x800, %rsi           # imm = 0xF800
cmpq $0xd800, %rsi           # imm = 0xD800
setne %sil
andq $-0x2, %rax
cmpq $0xfffe, %rax           # imm = 0xFFFE
setne %al
testb %al, %sil
jne 0x4bf1
movq $0xfffd, (%rdx)         # imm = 0xFFFD
jmp 0x4bf1