// File: /workspace/github_c_makefile_star2_O3/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
cmpq %rax, %r10
jae 0x25df
negq %rax
incq %r10
leaq 0x9c61(%rip), %r11      # 0xc000
movabsq $0x200000001, %rbx      # imm = 0x200000001
movabsq $0x300000002, %r14      # imm = 0x300000002
movl (%rcx), %r13d
cmpl %r8d, %r13d
jge 0x25df
movzbl -0x1(%r9,%r10), %ebp
cmpl $0x20, %ebp
jb 0x25df
movslq %r13d, %r12
cmpb $0x7e, %bpl
ja 0x2419
cmpl $0x0, (%rsi)
je 0x23ed
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)
movl $0x0, (%rsi)
movq %r10, (%rdi)
leaq (%rax,%r10), %r15
incq %r15
incq %r10
cmpq $0x1, %r15
jne 0x23b3
jmp 0x25df
cmpl $0x7f, %ebp
je 0x25df
cmpb $-0x41, %bpl
ja 0x247a
movl (%rsi), %r15d
testl %r15d, %r15d
je 0x254f
movl 0x8(%rsi), %r12d
shll $0x6, %r12d
andl $0x3f, %ebp
orl %r12d, %ebp
movl %ebp, 0x8(%rsi)
decl %r15d
movl %r15d, (%rsi)
jne 0x2401
movl 0x4(%rsi), %r15d
addl $-0x2, %r15d
cmpl $0x4, %r15d
ja 0x2576
movslq (%r11,%r15,4), %r15
addq %r11, %r15
jmpq *%r15
cmpl $0x80, %ebp
jl 0x256a
jmp 0x2576
cmpb $-0x21, %bpl
ja 0x24a1
cmpl $0x0, (%rsi)
je 0x2493
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0x1f, %ebp
movl %ebp, 0x8(%rsi)
movq %rbx, (%rsi)
jmp 0x2401
cmpb $-0x11, %bpl
ja 0x24c8
cmpl $0x0, (%rsi)
je 0x24ba
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
andl $0xf, %ebp
movl %ebp, 0x8(%rsi)
movq %r14, (%rsi)
jmp 0x2401
cmpb $-0x9, %bpl
ja 0x24f3
cmpl $0x0, (%rsi)
je 0x24e1
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 0x2547
cmpb $-0x5, %bpl
ja 0x251e
cmpl $0x0, (%rsi)
je 0x250c
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 0x2547
cmpb $-0x3, %bpl
ja 0x254f
cmpl $0x0, (%rsi)
je 0x2537
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 0x2401
incl %r13d
movl %r13d, (%rcx)
movl $0xfffd, (%rdx,%r12,4)  # imm = 0xFFFD
jmp 0x2401
cmpl $0x3ffffff, %ebp        # imm = 0x3FFFFFF
jg 0x2576
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 0x25ae
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 0x2401
cmpl $0x10000, %ebp          # imm = 0x10000
jl 0x256a
jmp 0x2576
cmpl $0x200000, %ebp         # imm = 0x200000
jl 0x256a
jmp 0x2576
cmpl $0x800, %ebp            # imm = 0x800
jl 0x256a
jmp 0x2576
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq