// File: /workspace/github_c_makefile_star2_O1/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
xorl %edi, %edi
movl $0x17, %esi
callq 0x1130
leaq 0xd38(%rip), %r12       # 0x2020
movq 0x2ce9(%rip), %r13      # 0x3fd8
xorl %ebx, %ebx
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1120
cmpl $0x6d, %eax
jne 0x131b
movq (%r13), %rdi
movl $0x3e8, %ebx            # imm = 0x3E8
testq %rdi, %rdi
je 0x12f1
callq 0x1140
movl %eax, %ebx
jmp 0x12f1
cmpl $-0x1, %eax
je 0x1336
cmpl $0x70, %eax
jne 0x1588
movq (%r13), %rdi
callq 0x1140
movl %eax, %ebp
jmp 0x12f1
xorl %edi, %edi
movl $0x60, %esi
callq 0x1100
testq %rax, %rax
je 0x15d3
movq %rax, %r14
movq %rax, %rdi
callq 0x10e0
testb $0x8, %al
jne 0x136f
movq %r14, %rdi
callq 0x10e0
testb $0x4, %al
je 0x137b
leaq 0xdd2(%rip), %rdi       # 0x213f
jmp 0x1376
leaq 0xddd(%rip), %rdi       # 0x2153
callq 0x1050
testl %ebx, %ebx
je 0x13a3
leaq 0xcd2(%rip), %rdi       # 0x2058
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
testl %ebp, %ebp
je 0x13a3
leaq 0xcd7(%rip), %rdi       # 0x2071
movl %ebp, %esi
xorl %eax, %eax
callq 0x1070
leaq 0x10(%rsp), %r15
leaq 0xb0(%rsp), %r12
leaq 0xc4d(%rip), %rbp       # 0x2004
leaq 0xdab(%rip), %r13       # 0x2169
movq %r14, %rdi
movq %r15, %rsi
callq 0x1080
cmpl $0x1, %eax
je 0x13f5
cmpl $0x4, %eax
je 0x13dd
cmpl $0x2, %eax
jne 0x13be
jmp 0x15a3
callq 0x1030
cmpl $0x4, (%rax)
jne 0x1597
movq %r13, %rdi
callq 0x1050
jmp 0x13be
movl $0x32, %edx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
movl $0xc, %r8d
callq 0x10d0
movl 0x10(%rsp), %eax
incl %eax
cmpl $0x6, %eax
ja 0x1518
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 0xc71(%rip), %rdi       # 0x20e0
xorl %eax, %eax
callq 0x1070
jmp 0x1529
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
callq 0x10b0
movl 0x30(%rsp), %esi
movl (%rsp), %edx
leaq 0xbf3(%rip), %rdi       # 0x208f
xorl %eax, %eax
callq 0x1070
jmp 0x1529
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 0xbab(%rip), %rdi       # 0x2078
movq %r12, %rsi
jmp 0x150f
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
leaq 0x8(%rsp), %r8
callq 0x1040
cmpl $0x0, 0x30(%rsp)
leaq 0xbe1(%rip), %rsi       # 0x20d7
leaq 0xbde(%rip), %rax       # 0x20db
cmoveq %rax, %rsi
movl (%rsp), %edx
movl 0x8(%rsp), %ecx
leaq 0xbab(%rip), %rdi       # 0x20ba
xorl %eax, %eax
callq 0x1070
jmp 0x1529
leaq 0xbe3(%rip), %rdi       # 0x2102
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
movl 0x10(%rsp), %eax
testl %eax, %eax
jne 0x1547
testb $0x4, 0x20(%rsp)
je 0x1547
movq 0x18(%rsp), %rcx
andq $-0x21, %rcx
cmpq $0x43, %rcx
je 0x15a3
testl %eax, %eax
jne 0x13be
cmpl $0x0, 0x20(%rsp)
jne 0x13be
cmpq $0x3f, 0x18(%rsp)
jne 0x13be
leaq 0xb9d(%rip), %rdi       # 0x210a
xorl %eax, %eax
callq 0x1070
movq 0x2a55(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x10c0
jmp 0x13be
movq %r14, %rdi
callq 0x11a5
movl $0x1, %eax
jmp 0x15c1
leaq 0xb73(%rip), %rdi       # 0x2111
callq 0x1110
testl %ebx, %ebx
je 0x15b7
leaq 0xb73(%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
callq 0x1180