// File: /workspace/github_c_makefile_star2_O3/neovim[P]libvterm/src/parser.c
// Function: vterm_input_write
size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len)
{
  size_t pos = 0;
  const char *string_start;

  switch(vt->parser.state) {
  case NORMAL:
  case CSI_LEADER:
  case CSI_ARGS:
  case CSI_INTERMED:
  case OSC_COMMAND:
  case DCS_COMMAND:
    string_start = NULL;
    break;
  case OSC:
  case DCS:
  case APC:
  case PM:
  case SOS:
    string_start = bytes;
    break;
  }

#define ENTER_STATE(st)        do { vt->parser.state = st; string_start = NULL; } while(0)
#define ENTER_NORMAL_STATE()   ENTER_STATE(NORMAL)

#define IS_STRING_STATE()      (vt->parser.state >= OSC_COMMAND)

  for( ; pos < len; pos++) {
    unsigned char c = bytes[pos];
    bool c1_allowed = !vt->mode.utf8;

    if(c == 0x00 || c == 0x7f) { // NUL, DEL
      if(IS_STRING_STATE()) {
        string_fragment(vt, string_start, bytes + pos - string_start, false);
        string_start = bytes + pos + 1;
      }
      if(vt->parser.emit_nul)
        do_control(vt, c);
      continue;
    }
    if(c == 0x18 || c == 0x1a) { // CAN, SUB
      vt->parser.in_esc = false;
      ENTER_NORMAL_STATE();
      if(vt->parser.emit_nul)
        do_control(vt, c);
      continue;
    }
    else if(c == 0x1b) { // ESC
      vt->parser.intermedlen = 0;
      if(!IS_STRING_STATE())
        vt->parser.state = NORMAL;
      vt->parser.in_esc = true;
      continue;
    }
    else if(c == 0x07 &&  // BEL, can stand for ST in OSC or DCS state
            IS_STRING_STATE()) {
      // fallthrough
    }
    else if(c < 0x20) { // other C0
      if(vt->parser.state == SOS)
        continue; // All other C0s permitted in SOS

      if(IS_STRING_STATE())
        string_fragment(vt, string_start, bytes + pos - string_start, false);
      do_control(vt, c);
      if(IS_STRING_STATE())
        string_start = bytes + pos + 1;
      continue;
    }
    // else fallthrough

    size_t string_len = bytes + pos - string_start;

    if(vt->parser.in_esc) {
      // Hoist an ESC letter into a C1 if we're not in a string mode
      // Always accept ESC \ == ST even in string mode
      if(!vt->parser.intermedlen &&
          c >= 0x40 && c < 0x60 &&
          ((!IS_STRING_STATE() || c == 0x5c))) {
        c += 0x40;
        c1_allowed = true;
        if(string_len)
          string_len -= 1;
        vt->parser.in_esc = false;
      }
      else {
        string_start = NULL;
        vt->parser.state = NORMAL;
      }
    }

    switch(vt->parser.state) {
    case CSI_LEADER:
      /* Extract leader bytes 0x3c to 0x3f */
      if(c >= 0x3c && c <= 0x3f) {
        if(vt->parser.v.csi.leaderlen < CSI_LEADER_MAX-1)
          vt->parser.v.csi.leader[vt->parser.v.csi.leaderlen++] = c;
        break;
      }

      /* else fallthrough */
      vt->parser.v.csi.leader[vt->parser.v.csi.leaderlen] = 0;

      vt->parser.v.csi.argi = 0;
      vt->parser.v.csi.args[0] = CSI_ARG_MISSING;
      vt->parser.state = CSI_ARGS;

      /* fallthrough */
    case CSI_ARGS:
      /* Numerical value of argument */
      if(c >= '0' && c <= '9') {
        if(vt->parser.v.csi.args[vt->parser.v.csi.argi] == CSI_ARG_MISSING)
          vt->parser.v.csi.args[vt->parser.v.csi.argi] = 0;
        vt->parser.v.csi.args[vt->parser.v.csi.argi] *= 10;
        vt->parser.v.csi.args[vt->parser.v.csi.argi] += c - '0';
        break;
      }
      if(c == ':') {
        vt->parser.v.csi.args[vt->parser.v.csi.argi] |= CSI_ARG_FLAG_MORE;
        c = ';';
      }
      if(c == ';') {
        vt->parser.v.csi.argi++;
        vt->parser.v.csi.args[vt->parser.v.csi.argi] = CSI_ARG_MISSING;
        break;
      }

      /* else fallthrough */
      vt->parser.v.csi.argi++;
      vt->parser.intermedlen = 0;
      vt->parser.state = CSI_INTERMED;
    case CSI_INTERMED:
      if(is_intermed(c)) {
        if(vt->parser.intermedlen < INTERMED_MAX-1)
          vt->parser.intermed[vt->parser.intermedlen++] = c;
        break;
      }
      else if(c == 0x1b) {
        /* ESC in CSI cancels */
      }
      else if(c >= 0x40 && c <= 0x7e) {
        vt->parser.intermed[vt->parser.intermedlen] = 0;
        do_csi(vt, c);
      }
      /* else was invalid CSI */

      ENTER_NORMAL_STATE();
      break;

    case OSC_COMMAND:
      /* Numerical value of command */
      if(c >= '0' && c <= '9') {
        if(vt->parser.v.osc.command == -1)
          vt->parser.v.osc.command = 0;
        else
          vt->parser.v.osc.command *= 10;
        vt->parser.v.osc.command += c - '0';
        break;
      }
      if(c == ';') {
        vt->parser.state = OSC;
        string_start = bytes + pos + 1;
        break;
      }

      /* else fallthrough */
      string_start = bytes + pos;
      string_len   = 0;
      vt->parser.state = OSC;
      goto string_state;

    case DCS_COMMAND:
      if(vt->parser.v.dcs.commandlen < CSI_LEADER_MAX)
        vt->parser.v.dcs.command[vt->parser.v.dcs.commandlen++] = c;

      if(c >= 0x40 && c<= 0x7e) {
        string_start = bytes + pos + 1;
        vt->parser.state = DCS;
      }
      break;

string_state:
    case OSC:
    case DCS:
    case APC:
    case PM:
    case SOS:
      if(c == 0x07 || (c1_allowed && c == 0x9c)) {
        string_fragment(vt, string_start, string_len, true);
        ENTER_NORMAL_STATE();
      }
      break;

    case NORMAL:
      if(vt->parser.in_esc) {
        if(is_intermed(c)) {
          if(vt->parser.intermedlen < INTERMED_MAX-1)
            vt->parser.intermed[vt->parser.intermedlen++] = c;
        }
        else if(c >= 0x30 && c < 0x7f) {
          do_escape(vt, c);
          vt->parser.in_esc = 0;
          ENTER_NORMAL_STATE();
        }
        else {
          DEBUG_LOG("TODO: Unhandled byte %02x in Escape\n", c);
        }
        break;
      }
      if(c1_allowed && c >= 0x80 && c < 0xa0) {
        switch(c) {
        case 0x90: // DCS
          vt->parser.string_initial = true;
          vt->parser.v.dcs.commandlen = 0;
          ENTER_STATE(DCS_COMMAND);
          break;
        case 0x98: // SOS
          vt->parser.string_initial = true;
          ENTER_STATE(SOS);
          string_start = bytes + pos + 1;
          string_len = 0;
          break;
        case 0x9b: // CSI
          vt->parser.v.csi.leaderlen = 0;
          ENTER_STATE(CSI_LEADER);
          break;
        case 0x9d: // OSC
          vt->parser.v.osc.command = -1;
          vt->parser.string_initial = true;
          string_start = bytes + pos + 1;
          ENTER_STATE(OSC_COMMAND);
          break;
        case 0x9e: // PM
          vt->parser.string_initial = true;
          ENTER_STATE(PM);
          string_start = bytes + pos + 1;
          string_len = 0;
          break;
        case 0x9f: // APC
          vt->parser.string_initial = true;
          ENTER_STATE(APC);
          string_start = bytes + pos + 1;
          string_len = 0;
          break;
        default:
          do_control(vt, c);
          break;
        }
      }
      else {
        size_t eaten = 0;
        if(vt->parser.callbacks && vt->parser.callbacks->text)
          eaten = (*vt->parser.callbacks->text)(bytes + pos, len - pos, vt->parser.cbdata);

        if(!eaten) {
          DEBUG_LOG("libvterm: Text callback did not consume any input\n");
          /* force it to make progress */
          eaten = 1;
        }

        pos += (eaten - 1); // we'll ++ it again in a moment
      }
      break;
    }
  }

  if(string_start) {
    size_t string_len = bytes + pos - string_start;
    if(vt->parser.in_esc)
      string_len -= 1;
    string_fragment(vt, string_start, string_len, false);
  }

  return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl 0x20(%rdi), %eax
addl $-0x6, %eax
cmpl $0x5, %eax
movl $0x0, %r13d
cmovbq %rsi, %r13
movq %rdx, 0x8(%rsp)
testq %rdx, %rdx
je 0x32f9
leaq 0x2c(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0x44(%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0x58(%r15), %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
leaq (%r14,%r12), %rbp
movzbl (%r14,%r12), %ebx
cmpl $0x7f, %ebx
je 0x2db9
testl %ebx, %ebx
jne 0x2e15
cmpl $0x5, 0x20(%r15)
jb 0x2dd9
movq %rbp, %rdx
subq %r13, %rdx
movq %r15, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x3336
incq %rbp
movq %rbp, %r13
cmpb $0x1, 0xe9(%r15)
jne 0x32e9
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x32e9
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x32e9
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
jmp 0x32e9
movl %ebx, %eax
andb $-0x3, %al
cmpb $0x18, %al
jne 0x2e66
andb $-0x2, 0x24(%r15)
movl $0x0, 0x20(%r15)
cmpb $0x1, 0xe9(%r15)
jne 0x32e6
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x32e6
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x32e6
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
jmp 0x32e6
movb 0x18(%r15), %al
cmpl $0x7, %ebx
je 0x2e95
cmpl $0x1b, %ebx
jne 0x2ece
movl $0x0, 0x28(%r15)
cmpl $0x4, 0x20(%r15)
ja 0x2e8b
movl $0x0, 0x20(%r15)
orb $0x1, 0x24(%r15)
jmp 0x32e9
cmpl $0x5, 0x20(%r15)
jae 0x2efa
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2ebd
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x2ebd
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
incq %rbp
cmpl $0x5, 0x20(%r15)
cmovaeq %rbp, %r13
jmp 0x32e9
cmpb $0x1f, %bl
ja 0x2efa
movl 0x20(%r15), %eax
cmpl $0xa, %eax
je 0x32e9
cmpl $0x5, %eax
jb 0x2e9c
movq %rbp, %rdx
subq %r13, %rdx
movq %r15, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x3336
jmp 0x2e9c
xorb $0x1, %al
movq %rbp, %rdx
subq %r13, %rdx
movb 0x24(%r15), %cl
testb $0x1, %cl
je 0x2f54
movl %ebx, %esi
andb $-0x20, %sil
cmpb $0x40, %sil
jne 0x2f9e
cmpl $0x0, 0x28(%r15)
jne 0x2f9e
movl 0x20(%r15), %esi
cmpl $0x5, %esi
setae %dil
cmpb $0x5c, %bl
setne %r8b
testb %dil, %r8b
jne 0x2f9e
subq $0x1, %rdx
movl $0x0, %eax
cmovbq %rax, %rdx
addl $0x40, %ebx
andb $-0x2, %cl
movb %cl, 0x24(%r15)
movb $0x1, %al
jmp 0x2f58
movl 0x20(%r15), %esi
cmpl $0xa, %esi
ja 0x32e9
movl %esi, %esi
leaq 0x93a6(%rip), %rdi      # 0xc310
movslq (%rdi,%rsi,4), %rsi
addq %rdi, %rsi
jmpq *%rsi
movq %r13, %rbp
cmpb $-0x64, %bl
sete %cl
cmpb $0x7, %bl
je 0x2f89
andb %cl, %al
je 0x3245
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %ecx
callq 0x3336
jmp 0x32de
movl $0x0, 0x20(%r15)
xorl %r13d, %r13d
testb $0x1, %cl
je 0x2fda
movl %ebx, %eax
andb $-0x10, %al
cmpb $0x20, %al
je 0x3269
leal -0x30(%rbx), %eax
cmpb $0x4e, %al
ja 0x32e9
movsbl %bl, %esi
movq %r15, %rdi
callq 0x3429
andb $-0x2, 0x24(%r15)
jmp 0x32de
testb $0x1, %al
je 0x301a
cmpb $-0x61, %bl
jg 0x301a
movzbl %bl, %edi
leal -0x98(%rdi), %eax
cmpl $0x7, %eax
ja 0x31a0
leaq 0x9340(%rip), %rcx      # 0xc33c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0xe8(%r15)
movl $0xa, 0x20(%r15)
jmp 0x3242
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x3046
movq (%rax), %rax
testq %rax, %rax
je 0x3046
movq 0x8(%rsp), %rsi
subq %r12, %rsi
movq 0xe0(%r15), %rdx
movq %rbp, %rdi
callq *%rax
cltq
jmp 0x3048
xorl %eax, %eax
cmpq $0x1, %rax
adcq $0x0, %rax
addq %rax, %r12
decq %r12
jmp 0x32e9
movslq 0x40(%r15), %rax
cmpq $0xf, %rax
jg 0x3071
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %bl, 0x44(%r15,%rax)
addb $-0x40, %bl
cmpb $0x3e, %bl
ja 0x32e9
incq %rbp
movl $0x7, 0x20(%r15)
jmp 0x3245
movl %ebx, %ecx
andb $-0x4, %cl
movslq 0x40(%r15), %rax
cmpb $0x3c, %cl
jne 0x30e7
cmpl $0xe, %eax
jg 0x32e9
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %bl, 0x44(%r15,%rax)
jmp 0x32e9
leal -0x30(%rbx), %ecx
cmpb $0x9, %cl
ja 0x3188
movzbl %bl, %eax
movl 0x40(%r15), %ecx
leal (%rcx,%rcx), %edx
cmpl $-0x1, %ecx
leal (%rdx,%rdx,4), %ecx
movl $0x0, %edx
cmovel %edx, %ecx
addl %ecx, %eax
addl $-0x30, %eax
movl %eax, 0x40(%r15)
jmp 0x32e9
movb $0x0, 0x44(%r15,%rax)
movl $0x0, 0x54(%r15)
movq $0x7fffffff, 0x58(%r15) # imm = 0x7FFFFFFF
movl $0x2, 0x20(%r15)
leal -0x30(%rbx), %eax
cmpb $0x9, %al
ja 0x3150
movslq 0x54(%r15), %rax
movq 0x58(%r15,%rax,8), %rcx
cmpq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
jne 0x3130
movq $0x0, 0x58(%r15,%rax,8)
movslq 0x54(%r15), %rax
movq 0x58(%r15,%rax,8), %rcx
addq %rcx, %rcx
leaq (%rcx,%rcx,4), %rcx
movq %rcx, 0x58(%r15,%rax,8)
andb $0xf, %bl
movzbl %bl, %eax
movslq 0x54(%r15), %rcx
addq %rax, 0x58(%r15,%rcx,8)
jmp 0x32e9
cmpb $0x3b, %bl
je 0x316f
movzbl %bl, %eax
cmpl $0x3a, %eax
jne 0x324d
movslq 0x54(%r15), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
orq %rcx, 0x58(%r15,%rax,8)
movslq 0x54(%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%r15)
movq $0x7fffffff, 0x60(%r15,%rax,8) # imm = 0x7FFFFFFF
jmp 0x32e9
movl $0x6, 0x20(%r15)
cmpb $0x3b, %bl
je 0x3242
xorl %edx, %edx
jmp 0x2f76
cmpl $0x90, %edi
jne 0x31c5
movb $0x1, 0xe8(%r15)
movl $0x0, 0x40(%r15)
movl $0x4, 0x20(%r15)
jmp 0x32e6
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x32e9
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x32e9
movq 0xe0(%r15), %rsi
jmp 0x2e0e
movb $0x1, 0xe8(%r15)
movl $0x8, 0x20(%r15)
jmp 0x3242
movl $0xffffffff, 0x40(%r15) # imm = 0xFFFFFFFF
movb $0x1, 0xe8(%r15)
movl $0x5, 0x20(%r15)
jmp 0x32e6
movl $0x0, 0x40(%r15)
movl $0x1, 0x20(%r15)
jmp 0x32e6
movb $0x1, 0xe8(%r15)
movl $0x9, 0x20(%r15)
incq %rbp
movq %rbp, %r13
jmp 0x32e9
incl 0x54(%r15)
movl $0x0, 0x28(%r15)
movl $0x3, 0x20(%r15)
movl %ebx, %eax
andb $-0x10, %al
cmpb $0x20, %al
jne 0x3281
movslq 0x28(%r15), %rax
cmpq $0xe, %rax
jg 0x32e9
leal 0x1(%rax), %ecx
movl %ecx, 0x28(%r15)
movb %bl, 0x2c(%r15,%rax)
jmp 0x32e9
leal -0x40(%rbx), %eax
cmpb $0x3e, %al
ja 0x32de
movslq 0x28(%r15), %rax
movb $0x0, 0x2c(%r15,%rax)
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x32de
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x32de
cmpl $0x0, 0x40(%r15)
movq 0x18(%rsp), %rdi
movl $0x0, %esi
cmoveq %rsi, %rdi
movl 0x54(%r15), %edx
cmpl $0x0, 0x28(%r15)
movq 0x20(%rsp), %rcx
cmoveq %rsi, %rcx
movq 0xe0(%r15), %r9
movsbl %bl, %r8d
movq 0x10(%rsp), %rsi
callq *%rax
movl $0x0, 0x20(%r15)
xorl %r13d, %r13d
incq %r12
cmpq 0x8(%rsp), %r12
jb 0x2da7
jmp 0x32fc
xorl %r12d, %r12d
testq %r13, %r13
je 0x3322
addq %r12, %r14
movzbl 0x24(%r15), %eax
andl $0x1, %eax
addq %r13, %rax
subq %rax, %r14
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x3336
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq