// File: /workspace/github_c_makefile_star2_O2/neovim[P]libvterm/src/encoding.c
// Function: decode_utf8
static void decode_utf8(VTermEncoding *enc, void *data_,
                        uint32_t cp[], int *cpi, int cplen,
                        const char bytes[], size_t *pos, size_t bytelen)
{
  struct UTF8DecoderData *data = data_;

#ifdef DEBUG_PRINT_UTF8
  printf("BEGIN UTF-8\n");
#endif

  for(; *pos < bytelen && *cpi < cplen; (*pos)++) {
    unsigned char c = bytes[*pos];

#ifdef DEBUG_PRINT_UTF8
    printf(" pos=%zd c=%02x rem=%d\n", *pos, c, data->bytes_remaining);
#endif

    if(c < 0x20) // C0
      return;

    else if(c >= 0x20 && c < 0x7f) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      cp[(*cpi)++] = c;
#ifdef DEBUG_PRINT_UTF8
      printf(" UTF-8 char: U+%04x\n", c);
#endif
      data->bytes_remaining = 0;
    }

    else if(c == 0x7f) // DEL
      return;

    else if(c >= 0x80 && c < 0xc0) {
      if(!data->bytes_remaining) {
        cp[(*cpi)++] = UNICODE_INVALID;
        continue;
      }

      data->this_cp <<= 6;
      data->this_cp |= c & 0x3f;
      data->bytes_remaining--;

      if(!data->bytes_remaining) {
#ifdef DEBUG_PRINT_UTF8
        printf(" UTF-8 raw char U+%04x bytelen=%d ", data->this_cp, data->bytes_total);
#endif
        // Check for overlong sequences
        switch(data->bytes_total) {
        case 2:
          if(data->this_cp <  0x0080) data->this_cp = UNICODE_INVALID;
          break;
        case 3:
          if(data->this_cp <  0x0800) data->this_cp = UNICODE_INVALID;
          break;
        case 4:
          if(data->this_cp < 0x10000) data->this_cp = UNICODE_INVALID;
          break;
        case 5:
          if(data->this_cp < 0x200000) data->this_cp = UNICODE_INVALID;
          break;
        case 6:
          if(data->this_cp < 0x4000000) data->this_cp = UNICODE_INVALID;
          break;
        }
        // Now look for plain invalid ones
        if((data->this_cp >= 0xD800 && data->this_cp <= 0xDFFF) ||
           data->this_cp == 0xFFFE ||
           data->this_cp == 0xFFFF)
          data->this_cp = UNICODE_INVALID;
#ifdef DEBUG_PRINT_UTF8
        printf(" char: U+%04x\n", data->this_cp);
#endif
        cp[(*cpi)++] = data->this_cp;
      }
    }

    else if(c >= 0xc0 && c < 0xe0) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      data->this_cp = c & 0x1f;
      data->bytes_total = 2;
      data->bytes_remaining = 1;
    }

    else if(c >= 0xe0 && c < 0xf0) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      data->this_cp = c & 0x0f;
      data->bytes_total = 3;
      data->bytes_remaining = 2;
    }

    else if(c >= 0xf0 && c < 0xf8) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      data->this_cp = c & 0x07;
      data->bytes_total = 4;
      data->bytes_remaining = 3;
    }

    else if(c >= 0xf8 && c < 0xfc) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      data->this_cp = c & 0x03;
      data->bytes_total = 5;
      data->bytes_remaining = 4;
    }

    else if(c >= 0xfc && c < 0xfe) {
      if(data->bytes_remaining)
        cp[(*cpi)++] = UNICODE_INVALID;

      data->this_cp = c & 0x01;
      data->bytes_total = 6;
      data->bytes_remaining = 5;
    }

    else {
      cp[(*cpi)++] = UNICODE_INVALID;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdi
movq (%rdi), %r10
leaq 0x7ce5(%rip), %r11      # 0xa000
movabsq $0x200000001, %rbx      # imm = 0x200000001
movabsq $0x300000002, %r14      # imm = 0x300000002
cmpq %rax, %r10
jae 0x2550
movl (%rcx), %r13d
cmpl %r8d, %r13d
jge 0x2550
movzbl (%r9,%r10), %ebp
cmpl $0x20, %ebp
jb 0x2550
movslq %r13d, %r12
cmpb $0x7e, %bpl
ja 0x238a
cmpl $0x0, (%rsi)
je 0x2371
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
movl (%rcx), %r13d
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movslq %r13d, %r15
movl %ebp, (%rdx,%r15,4)
andl $0x0, (%rsi)
incq %r10
movq %r10, (%rdi)
jmp 0x232f
cmpl $0x7f, %ebp
je 0x2550
cmpb $-0x41, %bpl
ja 0x23eb
movl (%rsi), %r15d
testl %r15d, %r15d
je 0x24c0
movl 0x8(%rsi), %r12d
shll $0x6, %r12d
andl $0x3f, %ebp
orl %r12d, %ebp
movl %ebp, 0x8(%rsi)
decl %r15d
movl %r15d, (%rsi)
jne 0x2382
movl 0x4(%rsi), %r15d
addl $-0x2, %r15d
cmpl $0x4, %r15d
ja 0x24e7
movslq (%r11,%r15,4), %r15
addq %r11, %r15
jmpq *%r15
cmpl $0x80, %ebp
jl 0x24db
jmp 0x24e7
cmpb $-0x21, %bpl
ja 0x2412
cmpl $0x0, (%rsi)
je 0x2404
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0x1f, %ebp
movl %ebp, 0x8(%rsi)
movq %rbx, (%rsi)
jmp 0x2382
cmpb $-0x11, %bpl
ja 0x2439
cmpl $0x0, (%rsi)
je 0x242b
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0xf, %ebp
movl %ebp, 0x8(%rsi)
movq %r14, (%rsi)
jmp 0x2382
cmpb $-0x9, %bpl
ja 0x2464
cmpl $0x0, (%rsi)
je 0x2452
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0x7, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x400000003, %r15      # imm = 0x400000003
jmp 0x24b8
cmpb $-0x5, %bpl
ja 0x248f
cmpl $0x0, (%rsi)
je 0x247d
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0x3, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x500000004, %r15      # imm = 0x500000004
jmp 0x24b8
cmpb $-0x3, %bpl
ja 0x24c0
cmpl $0x0, (%rsi)
je 0x24a8
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0x1, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x600000005, %r15      # imm = 0x600000005
movq %r15, (%rsi)
jmp 0x2382
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
jmp 0x2382
cmpl $0x3ffffff, %ebp        # imm = 0x3FFFFFF
jg 0x24e7
movl $0xfffd, 0x8(%rsi)      # imm = 0xFFFD
movl $0xfffd, %ebp           # imm = 0xFFFD
movl %ebp, %r15d
andl $0xfffff800, %r15d      # imm = 0xFFFFF800
cmpl $0xd800, %r15d          # imm = 0xD800
setne %r15b
movl %ebp, %r12d
andl $-0x2, %r12d
cmpl $0xfffe, %r12d          # imm = 0xFFFE
setne %r12b
testb %r12b, %r15b
jne 0x251f
movl $0xfffd, 0x8(%rsi)      # imm = 0xFFFD
movl $0xfffd, %ebp           # imm = 0xFFFD
movslq (%rcx), %r15
leal 0x1(%r15), %r12d
movl %r12d, (%rcx)
movl %ebp, (%rdx,%r15,4)
jmp 0x2382
cmpl $0x10000, %ebp          # imm = 0x10000
jl 0x24db
jmp 0x24e7
cmpl $0x200000, %ebp         # imm = 0x200000
jl 0x24db
jmp 0x24e7
cmpl $0x800, %ebp            # imm = 0x800
jl 0x24db
jmp 0x24e7
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq