// File: /workspace/github_c_makefile_star2_O2/neovim[P]libtermkey/demo.c
// Function: main
int main(int argc, char *argv[])
{
  TERMKEY_CHECK_VERSION;

  int mouse = 0;
  int mouse_proto = 0;
  TermKeyFormat format = TERMKEY_FORMAT_VIM;

  char buffer[50];
  TermKey *tk;

  int opt;
  while((opt = getopt(argc, argv, "m::p:")) != -1) {
    switch(opt) {
    case 'm':
      if(optarg)
        mouse = atoi(optarg);
      else
        mouse = 1000;

      break;

    case 'p':
      mouse_proto = atoi(optarg);
      break;

    default:
      fprintf(stderr, "Usage: %s [-m]\n", argv[0]);
      return 1;
    }
  }

  tk = termkey_new(0, TERMKEY_FLAG_SPACESYMBOL|TERMKEY_FLAG_CTRLC);

  if(!tk) {
    fprintf(stderr, "Cannot allocate termkey instance\n");
    exit(1);
  }

  if(termkey_get_flags(tk) & TERMKEY_FLAG_UTF8)
    printf("Termkey in UTF-8 mode\n");
  else if(termkey_get_flags(tk) & TERMKEY_FLAG_RAW)
    printf("Termkey in RAW mode\n");

  TermKeyResult ret;
  TermKeyKey key;

  if(mouse) {
    printf("\033[?%dhMouse mode active\n", mouse);
    if(mouse_proto)
      printf("\033[?%dh", mouse_proto);
  }

  while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
    if(ret == TERMKEY_RES_KEY) {
      termkey_strfkey(tk, buffer, sizeof buffer, &key, format);
      if(key.type == TERMKEY_TYPE_MOUSE) {
        int line, col;
        termkey_interpret_mouse(tk, &key, NULL, NULL, &line, &col);
        printf("%s at line=%d, col=%d\n", buffer, line, col);
      }
      else if(key.type == TERMKEY_TYPE_POSITION) {
        int line, col;
        termkey_interpret_position(tk, &key, &line, &col);
        printf("Cursor position report at line=%d, col=%d\n", line, col);
      }
      else if(key.type == TERMKEY_TYPE_MODEREPORT) {
        int initial, mode, value;
        termkey_interpret_modereport(tk, &key, &initial, &mode, &value);
        printf("Mode report %s mode %d = %d\n", initial ? "DEC" : "ANSI", mode, value);
      }
      else if(key.type == TERMKEY_TYPE_UNKNOWN_CSI) {
        long args[16];
        size_t nargs = 16;
        unsigned long command;
        termkey_interpret_csi(tk, &key, args, &nargs, &command);
        printf("Unrecognised CSI %c %ld;%ld %c%c\n", (char)(command >> 8), args[0], args[1], (char)(command >> 16), (char)command);
      }
      else {
        printf("Key %s\n", buffer);
      }

      if(key.type == TERMKEY_TYPE_UNICODE &&
         key.modifiers & TERMKEY_KEYMOD_CTRL &&
         (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
        break;

      if(key.type == TERMKEY_TYPE_UNICODE &&
         key.modifiers == 0 &&
         key.code.codepoint == '?') {
        // printf("\033[?6n"); // DECDSR 6 == request cursor position
        printf("\033[?1$p"); // DECRQM == request mode, DEC origin mode
        fflush(stdout);
      }
    }
    else if(ret == TERMKEY_RES_ERROR) {
      if(errno != EINTR) {
        perror("termkey_waitkey");
        break;
      }
      printf("Interrupted by signal\n");
    }
  }

  if(mouse)
    printf("\033[?%dlMouse mode deactivated\n", mouse);

  termkey_destroy(tk);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
movl %edi, %r15d
xorl %ebp, %ebp
pushq $0x17
popq %rsi
xorl %edi, %edi
callq 0x1130
leaq 0xd8a(%rip), %r12       # 0x2020
movq 0x2d3b(%rip), %r13      # 0x3fd8
xorl %ebx, %ebx
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1120
cmpl $0x6d, %eax
jne 0x12c9
movq (%r13), %rdi
movl $0x3e8, %ebx            # imm = 0x3E8
testq %rdi, %rdi
je 0x129f
callq 0x1140
movl %eax, %ebx
jmp 0x129f
cmpl $-0x1, %eax
je 0x12e4
cmpl $0x70, %eax
jne 0x153f
movq (%r13), %rdi
callq 0x1140
movl %eax, %ebp
jmp 0x129f
pushq $0x60
popq %rsi
xorl %edi, %edi
callq 0x1100
testq %rax, %rax
je 0x159b
movq %rax, %r14
movq %rax, %rdi
callq 0x10e0
testb $0x8, %al
jne 0x131b
movq %r14, %rdi
callq 0x10e0
testb $0x4, %al
je 0x1327
leaq 0xe26(%rip), %rdi       # 0x213f
jmp 0x1322
leaq 0xe31(%rip), %rdi       # 0x2153
callq 0x1050
testl %ebx, %ebx
je 0x134f
leaq 0xd26(%rip), %rdi       # 0x2058
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
testl %ebp, %ebp
je 0x134f
leaq 0xd2b(%rip), %rdi       # 0x2071
movl %ebp, %esi
xorl %eax, %eax
callq 0x1070
leaq 0x10(%rsp), %r15
leaq 0xb0(%rsp), %r12
leaq 0xca1(%rip), %rbp       # 0x2004
leaq 0xdff(%rip), %r13       # 0x2169
movq %r14, %rdi
movq %r15, %rsi
callq 0x1080
cmpl $0x1, %eax
je 0x13a1
cmpl $0x4, %eax
je 0x1389
cmpl $0x2, %eax
jne 0x136a
jmp 0x156b
callq 0x1030
cmpl $0x4, (%rax)
jne 0x155f
movq %r13, %rdi
callq 0x1050
jmp 0x136a
movq %r14, %rdi
movq %r12, %rsi
pushq $0x32
popq %rdx
movq %r15, %rcx
pushq $0xc
popq %r8
callq 0x10d0
movl 0x10(%rsp), %eax
incl %eax
cmpl $0x6, %eax
ja 0x14c0
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movq $0x10, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
leaq 0x8(%rsp), %r8
callq 0x10a0
movl 0x8(%rsp), %eax
movl %eax, %edi
movsbl %al, %r9d
shrl $0x8, %eax
movsbl %al, %esi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
shrl $0x10, %edi
movsbl %dil, %r8d
leaq 0xcc9(%rip), %rdi       # 0x20e0
xorl %eax, %eax
callq 0x1070
jmp 0x14d1
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
callq 0x10b0
movl 0x30(%rsp), %esi
movl (%rsp), %edx
leaq 0xc4b(%rip), %rdi       # 0x208f
xorl %eax, %eax
callq 0x1070
jmp 0x14d1
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
movq %rsp, %r9
callq 0x1060
movl 0x30(%rsp), %edx
movl (%rsp), %ecx
leaq 0xc03(%rip), %rdi       # 0x2078
movq %r12, %rsi
jmp 0x14b7
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
leaq 0x8(%rsp), %r8
callq 0x1040
cmpl $0x0, 0x30(%rsp)
leaq 0xc39(%rip), %rsi       # 0x20d7
leaq 0xc36(%rip), %rax       # 0x20db
cmoveq %rax, %rsi
movl (%rsp), %edx
movl 0x8(%rsp), %ecx
leaq 0xc03(%rip), %rdi       # 0x20ba
xorl %eax, %eax
callq 0x1070
jmp 0x14d1
leaq 0xc3b(%rip), %rdi       # 0x2102
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
movl 0x10(%rsp), %ecx
movl 0x20(%rsp), %eax
testl %ecx, %ecx
je 0x14e4
movq 0x18(%rsp), %rdx
jmp 0x1503
movl %eax, %esi
shrb $0x2, %sil
movq 0x18(%rsp), %rdx
movq %rdx, %rdi
andq $-0x21, %rdi
cmpq $0x43, %rdi
sete %dil
testb %dil, %sil
jne 0x156b
testl %ecx, %ecx
jne 0x136a
testl %eax, %eax
jne 0x136a
cmpq $0x3f, %rdx
jne 0x136a
leaq 0xbe6(%rip), %rdi       # 0x210a
xorl %eax, %eax
callq 0x1070
movq 0x2a9e(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x10c0
jmp 0x136a
movq 0x2ab2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0xad3(%rip), %rsi       # 0x2026
xorl %eax, %eax
callq 0x1090
pushq $0x1
popq %rax
jmp 0x1589
leaq 0xbab(%rip), %rdi       # 0x2111
callq 0x1110
testl %ebx, %ebx
je 0x157f
leaq 0xbab(%rip), %rdi       # 0x2121
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
movq %r14, %rdi
callq 0x10f0
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2a56(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa8a(%rip), %rdi       # 0x2036
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
pushq $0x1
popq %rdi
callq 0x1150