// File: /workspace/github_c_makefile_star2_O1/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 0x2623
negq %rax
incq %r10
leaq 0x8c41(%rip), %r11      # 0xb000
movabsq $0x200000001, %rbx      # imm = 0x200000001
movabsq $0x300000002, %r14      # imm = 0x300000002
movslq (%rcx), %r13
cmpl %r8d, %r13d
jge 0x2623
movzbl -0x1(%r9,%r10), %ebp
movl $0x1, %r12d
cmpl $0x20, %ebp
jb 0x2428
cmpb $0x7e, %bpl
ja 0x2423
cmpl $0x0, (%rsi)
je 0x240a
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
movslq (%rcx), %r15
leal 0x1(%r15), %r12d
movl %r12d, (%rcx)
movl %ebp, (%rdx,%r15,4)
movl $0x0, (%rsi)
xorl %r12d, %r12d
jmp 0x2428
cmpl $0x7f, %ebp
jne 0x244a
testb $0x3, %r12b
jne 0x2623
movq %r10, (%rdi)
leaq (%rax,%r10), %r15
incq %r15
incq %r10
cmpq $0x1, %r15
jne 0x23d3
jmp 0x2623
cmpb $-0x41, %bpl
ja 0x24a5
movl (%rsi), %r15d
testl %r15d, %r15d
je 0x24f5
movl 0x8(%rsi), %r12d
shll $0x6, %r12d
andl $0x3f, %ebp
orl %r12d, %ebp
movl %ebp, 0x8(%rsi)
xorl %r12d, %r12d
decl %r15d
movl %r15d, (%rsi)
jne 0x2428
movl 0x4(%rsi), %r15d
addl $-0x2, %r15d
cmpl $0x4, %r15d
ja 0x25ab
movslq (%r11,%r15,4), %r15
addq %r11, %r15
jmpq *%r15
cmpl $0x80, %ebp
jl 0x25a4
jmp 0x25ab
cmpb $-0x21, %bpl
ja 0x24cd
cmpl $0x0, (%rsi)
je 0x24bf
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
andl $0x1f, %ebp
movl %ebp, 0x8(%rsi)
movq %rbx, (%rsi)
jmp 0x241e
cmpb $-0x11, %bpl
ja 0x250e
cmpl $0x0, (%rsi)
je 0x24e7
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
andl $0xf, %ebp
movl %ebp, 0x8(%rsi)
movq %r14, (%rsi)
jmp 0x241e
leal 0x1(%r13), %ebp
movl %ebp, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
movl $0x4, %r12d
jmp 0x2428
cmpb $-0x9, %bpl
ja 0x253a
cmpl $0x0, (%rsi)
je 0x2528
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
andl $0x7, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x400000003, %r15      # imm = 0x400000003
jmp 0x2594
cmpb $-0x5, %bpl
ja 0x2566
cmpl $0x0, (%rsi)
je 0x2554
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
andl $0x3, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x500000004, %r15      # imm = 0x500000004
jmp 0x2594
cmpb $-0x3, %bpl
ja 0x2610
cmpl $0x0, (%rsi)
je 0x2584
leal 0x1(%r13), %r15d
movl %r15d, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
andl $0x1, %ebp
movl %ebp, 0x8(%rsi)
movabsq $0x600000005, %r15      # imm = 0x600000005
movq %r15, (%rsi)
jmp 0x241e
cmpl $0x3ffffff, %ebp        # imm = 0x3FFFFFF
jg 0x25ab
movl $0xfffd, 0x8(%rsi)      # imm = 0xFFFD
movl 0x8(%rsi), %ebp
movl %ebp, %r15d
andl $0xfffff800, %r15d      # imm = 0xFFFFF800
cmpl $0xd800, %r15d          # imm = 0xD800
setne %r15b
andl $-0x2, %ebp
cmpl $0xfffe, %ebp           # imm = 0xFFFE
setne %bpl
testb %bpl, %r15b
jne 0x25dc
movl $0xfffd, 0x8(%rsi)      # imm = 0xFFFD
movl 0x8(%rsi), %ebp
movslq (%rcx), %r15
leal 0x1(%r15), %r13d
movl %r13d, (%rcx)
movl %ebp, (%rdx,%r15,4)
jmp 0x2428
cmpl $0x10000, %ebp          # imm = 0x10000
jl 0x25a4
jmp 0x25ab
cmpl $0x200000, %ebp         # imm = 0x200000
jl 0x25a4
jmp 0x25ab
cmpl $0x800, %ebp            # imm = 0x800
jl 0x25a4
jmp 0x25ab
leal 0x1(%r13), %ebp
movl %ebp, (%rcx)
movl $0xfffd, (%rdx,%r13,4)  # imm = 0xFFFD
jmp 0x241e
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq