// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl 0x20(%rdi), %eax
addl $-0x6, %eax
cmpl $0x5, %eax
movl $0x0, %r9d
cmovbq %rsi, %r9
leaq 0x2c(%rdi), %rax
movq %rax, (%rsp)
leaq 0x44(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x58(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x8(%rsp), %rsi
subq %rbx, %rsi
jbe 0x31b9
leaq (%r14,%rbx), %rbp
movzbl (%r14,%rbx), %r13d
movzbl %r13b, %r12d
cmpl $0x7f, %r13d
je 0x2cb6
testl %r13d, %r13d
jne 0x2cfa
cmpl $0x5, 0x20(%r15)
jb 0x2cd6
movq %rbp, %rdx
subq %r9, %rdx
movq %r15, %rdi
movq %r9, %rsi
xorl %ecx, %ecx
callq 0x31f3
incq %rbp
movq %rbp, %r9
cmpb $0x1, 0xe9(%r15)
jne 0x314a
movq %r15, %rdi
movl %r12d, %esi
movq %r9, %r12
callq 0x32e6
movq %r12, %r9
jmp 0x314a
movl %r13d, %eax
andb $-0x3, %al
cmpb $0x18, %al
jne 0x2d2a
andb $-0x2, 0x24(%r15)
andl $0x0, 0x20(%r15)
cmpb $0x1, 0xe9(%r15)
jne 0x2d22
movq %r15, %rdi
movl %r12d, %esi
callq 0x32e6
xorl %r9d, %r9d
jmp 0x314a
movb 0x18(%r15), %al
cmpl $0x7, %r13d
je 0x2d55
cmpl $0x1b, %r13d
jne 0x2d7e
andl $0x0, 0x28(%r15)
cmpl $0x4, 0x20(%r15)
ja 0x2d4b
andl $0x0, 0x20(%r15)
orb $0x1, 0x24(%r15)
jmp 0x314a
cmpl $0x5, 0x20(%r15)
jae 0x2db1
movq %r15, %rdi
movl %r12d, %esi
movq %r9, %r12
callq 0x32e6
movq %r12, %r9
incq %rbp
cmpl $0x5, 0x20(%r15)
cmovaeq %rbp, %r9
jmp 0x314a
cmpb $0x1f, %r13b
ja 0x2db1
movl 0x20(%r15), %eax
cmpl $0xa, %eax
je 0x314a
cmpl $0x5, %eax
jb 0x2d5c
movq %rbp, %rdx
subq %r9, %rdx
movq %r15, %rdi
movq %r9, %rsi
xorl %ecx, %ecx
movq %r9, %r13
callq 0x31f3
movq %r13, %r9
jmp 0x2d5c
xorb $0x1, %al
movq %rbp, %rdx
subq %r9, %rdx
movb 0x24(%r15), %r12b
testb $0x1, %r12b
je 0x2e12
movl %r13d, %ecx
andb $-0x20, %cl
cmpb $0x40, %cl
jne 0x2e60
cmpl $0x0, 0x28(%r15)
jne 0x2e60
movl 0x20(%r15), %ecx
cmpl $0x5, %ecx
setae %dil
cmpb $0x5c, %r13b
setne %r8b
testb %dil, %r8b
jne 0x2e60
subq $0x1, %rdx
movl $0x0, %eax
cmovbq %rax, %rdx
addl $0x40, %r13d
andb $-0x2, %r12b
movb %r12b, 0x24(%r15)
movb $0x1, %al
jmp 0x2e16
movl 0x20(%r15), %ecx
cmpl $0xa, %ecx
ja 0x314a
movl %ecx, %ecx
leaq 0x74e8(%rip), %r8       # 0xa310
movslq (%r8,%rcx,4), %rdi
addq %r8, %rdi
movzbl %r13b, %ecx
jmpq *%rdi
movq %r9, %rbp
cmpb $-0x64, %r13b
sete %cl
cmpb $0x7, %r13b
je 0x2e4d
andb %cl, %al
je 0x3115
movq %r15, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rcx
callq 0x31f3
jmp 0x31af
andl $0x0, 0x20(%r15)
xorl %r9d, %r9d
testb $0x1, %r12b
je 0x2ee0
movl %r13d, %eax
andb $-0x10, %al
cmpb $0x20, %al
je 0x3134
leal -0x30(%r13), %eax
cmpb $0x4e, %al
ja 0x314a
movslq 0x28(%r15), %rbp
leaq 0x20(%rsp), %rdi
movq (%rsp), %rsi
movq %rbp, %rdx
callq 0x2070
movb %r13b, 0x20(%rsp,%rbp)
movb $0x0, 0x21(%rsp,%rbp)
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2ed3
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2ed3
incq %rbp
movq 0xe0(%r15), %rdx
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq *%rax
movb 0x24(%r15), %r12b
andb $-0x2, %r12b
movb %r12b, 0x24(%r15)
jmp 0x31af
testb $0x1, %al
je 0x2f22
cmpb $-0x61, %r13b
jg 0x2f22
movzbl %r13b, %esi
leal -0x98(%rsi), %eax
cmpl $0x7, %eax
ja 0x309a
leaq 0x7438(%rip), %rcx      # 0xa33c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0xe8(%r15)
movl $0xa, 0x20(%r15)
jmp 0x3112
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x2f4c
movq (%rax), %rax
testq %rax, %rax
je 0x2f4c
movq 0xe0(%r15), %rdx
movq %rbp, %rdi
movq %r9, %r12
callq *%rax
movq %r12, %r9
cltq
jmp 0x2f4e
xorl %eax, %eax
cmpq $0x1, %rax
adcq $0x0, %rax
addq %rax, %rbx
decq %rbx
jmp 0x314a
movslq 0x40(%r15), %rax
cmpq $0xf, %rax
jg 0x2f77
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %r13b, 0x44(%r15,%rax)
addb $-0x40, %r13b
cmpb $0x3e, %r13b
ja 0x314a
incq %rbp
movl $0x7, 0x20(%r15)
jmp 0x3115
movl %r13d, %edx
andb $-0x4, %dl
movslq 0x40(%r15), %rax
cmpb $0x3c, %dl
jne 0x2fec
cmpl $0xe, %eax
jg 0x314a
leal 0x1(%rax), %ecx
movl %ecx, 0x40(%r15)
movb %r13b, 0x44(%r15,%rax)
jmp 0x314a
leal -0x30(%r13), %edx
cmpb $0x9, %dl
ja 0x3085
movl 0x40(%r15), %eax
imull $0xa, %eax, %edx
cmpl $-0x1, %eax
movl $0x0, %eax
cmovel %eax, %edx
leal (%rcx,%rdx), %eax
addl $-0x30, %eax
movl %eax, 0x40(%r15)
jmp 0x314a
movb $0x0, 0x44(%r15,%rax)
andl $0x0, 0x54(%r15)
movq $0x7fffffff, 0x58(%r15) # imm = 0x7FFFFFFF
movl $0x2, 0x20(%r15)
leal -0x30(%r13), %eax
cmpb $0x9, %al
ja 0x304f
movslq 0x54(%r15), %rax
movq 0x58(%r15,%rax,8), %rcx
cmpq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
jne 0x3030
andq $0x0, 0x58(%r15,%rax,8)
movslq 0x54(%r15), %rax
movq 0x58(%r15,%rax,8), %rcx
imulq $0xa, %rcx, %rcx
movq %rcx, 0x58(%r15,%rax,8)
andb $0xf, %r13b
movzbl %r13b, %eax
movslq 0x54(%r15), %rcx
addq %rax, 0x58(%r15,%rcx,8)
jmp 0x314a
cmpb $0x3b, %r13b
je 0x306c
cmpl $0x3a, %ecx
jne 0x311a
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 0x314a
movl $0x6, 0x20(%r15)
cmpb $0x3b, %r13b
je 0x3112
xorl %edx, %edx
jmp 0x2e38
cmpl $0x90, %esi
jne 0x30bc
movb $0x1, 0xe8(%r15)
andl $0x0, 0x40(%r15)
movl $0x4, 0x20(%r15)
jmp 0x2d22
movq %r15, %rdi
jmp 0x2cea
movb $0x1, 0xe8(%r15)
movl $0x8, 0x20(%r15)
jmp 0x3112
orl $-0x1, 0x40(%r15)
movb $0x1, 0xe8(%r15)
movl $0x5, 0x20(%r15)
jmp 0x2d22
andl $0x0, 0x40(%r15)
movl $0x1, 0x20(%r15)
jmp 0x2d22
movb $0x1, 0xe8(%r15)
movl $0x9, 0x20(%r15)
incq %rbp
movq %rbp, %r9
jmp 0x314a
incl 0x54(%r15)
andl $0x0, 0x28(%r15)
movl $0x3, 0x20(%r15)
movl %r13d, %eax
andb $-0x10, %al
cmpb $0x20, %al
jne 0x3152
movslq 0x28(%r15), %rax
cmpq $0xe, %rax
jg 0x314a
leal 0x1(%rax), %ecx
movl %ecx, 0x28(%r15)
movb %r13b, 0x2c(%r15,%rax)
incq %rbx
jmp 0x2c90
leal -0x40(%r13), %eax
cmpb $0x3e, %al
ja 0x31af
movslq 0x28(%r15), %rax
movb $0x0, 0x2c(%r15,%rax)
movq 0xd8(%r15), %rax
testq %rax, %rax
je 0x31af
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x31af
cmpl $0x0, 0x40(%r15)
movq 0x18(%rsp), %rdi
movl $0x0, %esi
cmoveq %rsi, %rdi
movl 0x54(%r15), %edx
cmpl $0x0, 0x28(%r15)
movq (%rsp), %rcx
cmoveq %rsi, %rcx
movq 0xe0(%r15), %r9
movsbl %r13b, %r8d
movq 0x10(%rsp), %rsi
callq *%rax
andl $0x0, 0x20(%r15)
jmp 0x2d22
testq %r9, %r9
je 0x31df
addq %rbx, %r14
movzbl 0x24(%r15), %eax
andl $0x1, %eax
addq %r9, %rax
subq %rax, %r14
movq %r15, %rdi
movq %r9, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x31f3
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq