// File: /workspace/github_c_makefile_star2_O1/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 0x3303
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 0x2dc9
testl %ebx, %ebx
jne 0x2e25
cmpl $0x5, 0x20(%r15)
jb 0x2de9
movq %rbp, %rdx
subq %r13, %rdx
movq %r15, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x3340
incq %rbp
movq %rbp, %r13
cmpb $0x1, 0xe9(%r15)
jne 0x2faa
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2faa
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x2faa
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
jmp 0x2faa
movl %ebx, %eax
andb $-0x3, %al
cmpb $0x18, %al
jne 0x2e76
andb $-0x2, 0x24(%r15)
movl $0x0, 0x20(%r15)
cmpb $0x1, 0xe9(%r15)
jne 0x2fa7
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2fa7
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x2fa7
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
jmp 0x2fa7
movb 0x18(%r15), %al
cmpl $0x7, %ebx
je 0x2ea5
cmpl $0x1b, %ebx
jne 0x2f03
movl $0x0, 0x28(%r15)
cmpl $0x4, 0x20(%r15)
ja 0x2e9b
movl $0x0, 0x20(%r15)
orb $0x1, 0x24(%r15)
jmp 0x2faa
cmpl $0x5, 0x20(%r15)
jae 0x2f08
movl 0x20(%r15), %eax
cmpl $0xa, %eax
je 0x2faa
cmpl $0x5, %eax
jb 0x2ed1
movq %rbp, %rdx
subq %r13, %rdx
movq %r15, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x3340
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2ef2
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x2ef2
movq 0xe0(%r15), %rsi
movzbl %bl, %edi
callq *%rax
incq %rbp
cmpl $0x5, 0x20(%r15)
cmovaeq %rbp, %r13
jmp 0x2faa
cmpb $0x1f, %bl
jbe 0x2eac
xorb $0x1, %al
movq %rbp, %rdx
subq %r13, %rdx
movb 0x24(%r15), %cl
testb $0x1, %cl
je 0x2f5e
movl %ebx, %esi
andb $-0x20, %sil
cmpb $0x40, %sil
jne 0x2f38
cmpl $0x0, 0x28(%r15)
jne 0x2f38
cmpb $0x5c, %bl
je 0x2f45
cmpl $0x4, 0x20(%r15)
jbe 0x2f45
movl $0x0, 0x20(%r15)
xorl %r13d, %r13d
jmp 0x2f5e
subq $0x1, %rdx
movl $0x0, %eax
cmovbq %rax, %rdx
addl $0x40, %ebx
andb $-0x2, %cl
movb %cl, 0x24(%r15)
movb $0x1, %al
movl 0x20(%r15), %ecx
cmpq $0xa, %rcx
ja 0x2faa
leaq 0x83a1(%rip), %rsi      # 0xb310
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movq %r13, %rbp
cmpb $-0x64, %bl
sete %cl
cmpb $0x7, %bl
je 0x2f8f
andb %cl, %al
jne 0x2f8f
movq %rbp, %r13
jmp 0x2faa
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %ecx
callq 0x3340
movl $0x0, 0x20(%r15)
xorl %r13d, %r13d
incq %r12
cmpq 0x8(%rsp), %r12
jb 0x2db7
jmp 0x3306
movslq 0x40(%r15), %rax
cmpq $0xf, %rax
jg 0x2fd3
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %bl, 0x44(%r15,%rax)
addb $-0x40, %bl
cmpb $0x3e, %bl
ja 0x2faa
incq %rbp
movl $0x7, 0x20(%r15)
jmp 0x2f8a
movl %ebx, %eax
andb $-0x4, %al
cmpb $0x3c, %al
jne 0x306d
movslq 0x40(%r15), %rax
cmpq $0xe, %rax
jg 0x2faa
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %bl, 0x44(%r15,%rax)
jmp 0x2faa
testb $0x1, 0x24(%r15)
je 0x310a
movl %ebx, %eax
andb $-0x10, %al
cmpb $0x20, %al
je 0x3184
leal -0x30(%rbx), %eax
cmpb $0x4e, %al
ja 0x2faa
movsbl %bl, %esi
movq %r15, %rdi
callq 0x3433
andb $-0x2, 0x24(%r15)
jmp 0x2f9f
leal -0x30(%rbx), %ecx
cmpb $0x9, %cl
ja 0x314f
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 0x2faa
movslq 0x40(%r15), %rax
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 0x30d7
movslq 0x54(%r15), %rax
cmpq $0x7fffffff, 0x58(%r15,%rax,8) # imm = 0x7FFFFFFF
jne 0x30ae
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 0x2faa
cmpb $0x3a, %bl
jne 0x30ec
movslq 0x54(%r15), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
orq %rcx, 0x58(%r15,%rax,8)
movb $0x3b, %bl
cmpb $0x3b, %bl
jne 0x3168
movslq 0x54(%r15), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%r15)
movq $0x7fffffff, 0x60(%r15,%rax,8) # imm = 0x7FFFFFFF
jmp 0x2faa
testb $0x1, %al
je 0x3211
cmpb $-0x61, %bl
jg 0x3211
movzbl %bl, %edi
leal -0x98(%rdi), %eax
cmpl $0x7, %eax
ja 0x3259
leaq 0x8208(%rip), %rcx      # 0xb33c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0xe8(%r15)
movl $0xa, 0x20(%r15)
jmp 0x3160
movl $0x6, 0x20(%r15)
cmpb $0x3b, %bl
jne 0x323d
incq %rbp
jmp 0x2f8a
incl 0x54(%r15)
movl $0x0, 0x28(%r15)
movl $0x3, 0x20(%r15)
movl %ebx, %eax
andb $-0x10, %al
cmpb $0x20, %al
jne 0x31a3
movslq 0x28(%r15), %rax
cmpq $0xe, %rax
jg 0x2faa
leal 0x1(%rax), %ecx
movl %ecx, 0x28(%r15)
movb %bl, 0x2c(%r15,%rax)
jmp 0x2faa
leal -0x40(%rbx), %eax
cmpb $0x3e, %al
ja 0x2f9f
movslq 0x28(%r15), %rax
movb $0x0, 0x2c(%r15,%rax)
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2f9f
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x2f9f
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
jmp 0x2f9f
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x3244
movq (%rax), %rax
testq %rax, %rax
je 0x3244
movq 0x8(%rsp), %rsi
subq %r12, %rsi
movq 0xe0(%r15), %rdx
movq %rbp, %rdi
callq *%rax
cltq
jmp 0x3246
xorl %edx, %edx
jmp 0x2f7b
xorl %eax, %eax
cmpq $0x1, %rax
adcq $0x0, %rax
addq %rax, %r12
decq %r12
jmp 0x2faa
cmpl $0x90, %edi
jne 0x327e
movb $0x1, 0xe8(%r15)
movl $0x0, 0x40(%r15)
movl $0x4, 0x20(%r15)
jmp 0x2fa7
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2faa
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x2faa
movq 0xe0(%r15), %rsi
jmp 0x2e1e
movb $0x1, 0xe8(%r15)
movl $0x8, 0x20(%r15)
jmp 0x3160
movl $0xffffffff, 0x40(%r15) # imm = 0xFFFFFFFF
movb $0x1, 0xe8(%r15)
movl $0x5, 0x20(%r15)
jmp 0x2fa7
movl $0x0, 0x40(%r15)
movl $0x1, 0x20(%r15)
jmp 0x2fa7
movb $0x1, 0xe8(%r15)
movl $0x9, 0x20(%r15)
jmp 0x3160
xorl %r12d, %r12d
testq %r13, %r13
je 0x332c
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 0x3340
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq