// File: /workspace/github_c_makefile_star2_O2/neovim[P]libtermkey/driver-csi.c
// Function: peekkey_csi
static TermKeyResult peekkey_csi(TermKey *tk, TermKeyCsi *csi, size_t introlen, TermKeyKey *key, int force, size_t *nbytep)
{
  size_t csi_len;
  size_t args = 16;
  long arg[16];
  unsigned long cmd;

  TermKeyResult ret = parse_csi(tk, introlen, &csi_len, arg, &args, &cmd);

  if(ret == TERMKEY_RES_AGAIN) {
    if(!force)
      return TERMKEY_RES_AGAIN;

    (*tk->method.emit_codepoint)(tk, '[', key);
    key->modifiers |= TERMKEY_KEYMOD_ALT;
    *nbytep = introlen;
    return TERMKEY_RES_KEY;
  }

  if(cmd == 'M' && args < 3) { // Mouse in X10 encoding consumes the next 3 bytes also
    tk->buffstart += csi_len;
    tk->buffcount -= csi_len;

    TermKeyResult mouse_result = (*tk->method.peekkey_mouse)(tk, key, nbytep);

    tk->buffstart -= csi_len;
    tk->buffcount += csi_len;

    if(mouse_result == TERMKEY_RES_KEY)
      *nbytep += csi_len;

    return mouse_result;
  }

  TermKeyResult result = TERMKEY_RES_NONE;

  // We know from the logic above that cmd must be >= 0x40 and < 0x80
  if(csi_handlers[(cmd & 0xff) - 0x40])
    result = (*csi_handlers[(cmd & 0xff) - 0x40])(tk, key, cmd, arg, args);

  if(result == TERMKEY_RES_NONE) {
#ifdef DEBUG
    switch(args) {
      case 0:
        fprintf(stderr, "CSI: Unknown cmd=%c\n", (char)cmd);
        break;
      case 1:
        fprintf(stderr, "CSI: Unknown arg1=%ld cmd=%c\n", arg[0], (char)cmd);
        break;
      case 2:
        fprintf(stderr, "CSI: Unknown arg1=%ld arg2=%ld cmd=%c\n", arg[0], arg[1], (char)cmd);
        break;
      case 3:
        fprintf(stderr, "CSI: Unknown arg1=%ld arg2=%ld arg3=%ld cmd=%c\n", arg[0], arg[1], arg[2], (char)cmd);
        break;
      default:
        fprintf(stderr, "CSI: Unknown arg1=%ld arg2=%ld arg3=%ld ... args=%d cmd=%c\n", arg[0], arg[1], arg[2], args, (char)cmd);
        break;
    }
#endif
    key->type = TERMKEY_TYPE_UNKNOWN_CSI;
    key->code.number = cmd;
    key->modifiers = 0;

    tk->hightide = csi_len - introlen;
    *nbytep = introlen; // Do not yet eat the data bytes
    return TERMKEY_RES_KEY;
  }

  *nbytep = csi_len;
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, %rbx
movl %ecx, %ebp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
leaq 0x18(%rsp), %r8
movq $0x10, (%r8)
leaq 0x8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x10(%rsp), %r9
callq 0x5083
cmpl $0x3, %eax
jne 0x5bcb
testl %ebp, %ebp
je 0x5c4c
pushq $0x5b
popq %rsi
movq %r15, %rdi
movq %r12, %rdx
callq *0x2a8(%r15)
orb $0x2, 0x10(%r12)
jmp 0x5c6d
movq 0x10(%rsp), %r13
cmpq $0x4d, %r13
sete %al
movq 0x18(%rsp), %r8
cmpq $0x3, %r8
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5c19
movq 0x8(%rsp), %r14
addq %r14, 0x18(%r15)
subq %r14, 0x20(%r15)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq *0x2b8(%r15)
subq %r14, 0x18(%r15)
addq %r14, 0x20(%r15)
cmpl $0x1, %eax
jne 0x5c73
addq (%rbx), %r14
jmp 0x5c6d
movzbl %r13b, %eax
leaq 0x605c(%rip), %rcx      # 0xbc80
movq -0x200(%rcx,%rax,8), %rax
testq %rax, %rax
je 0x5c51
leaq 0x20(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq *%rax
testl %eax, %eax
je 0x5c51
movq 0x8(%rsp), %r14
jmp 0x5c70
pushq $0x3
popq %rax
jmp 0x5c73
orl $-0x1, (%r12)
movl %r13d, 0x8(%r12)
andl $0x0, 0x10(%r12)
movq 0x8(%rsp), %rax
subq %r14, %rax
movq %rax, 0x30(%r15)
pushq $0x1
popq %rax
movq %r14, (%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)