// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorl %edi, %edi
movl $0x17, %esi
callq 0x1120
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0xc, -0x1c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xd6c(%rip), %rdx       # 0x2004
callq 0x1110
movl %eax, -0x5c(%rbp)
cmpl $-0x1, %eax
je 0x1334
movl -0x5c(%rbp), %eax
movl %eax, -0x134(%rbp)
subl $0x6d, %eax
je 0x12c6
jmp 0x12b9
movl -0x134(%rbp), %eax
subl $0x70, %eax
je 0x12f0
jmp 0x1304
movq 0x2d0b(%rip), %rax      # 0x3fd8
cmpq $0x0, (%rax)
je 0x12e7
movq 0x2cfe(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1130
movl %eax, -0x14(%rbp)
jmp 0x12ee
movl $0x3e8, -0x14(%rbp)     # imm = 0x3E8
jmp 0x132f
movq 0x2ce1(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1130
movl %eax, -0x18(%rbp)
jmp 0x132f
movq 0x2ced(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xcee(%rip), %rsi       # 0x200a
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x1642
jmp 0x128a
xorl %edi, %edi
movl $0x60, %esi
callq 0x10f0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x136d
movq 0x2ca6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcbe(%rip), %rsi       # 0x201a
movb $0x0, %al
callq 0x1080
movl $0x1, %edi
callq 0x1140
movq -0x58(%rbp), %rdi
callq 0x10d0
andl $0x8, %eax
cmpl $0x0, %eax
je 0x138e
leaq 0xcb7(%rip), %rdi       # 0x203c
movb $0x0, %al
callq 0x1060
jmp 0x13af
movq -0x58(%rbp), %rdi
callq 0x10d0
andl $0x4, %eax
cmpl $0x0, %eax
je 0x13ad
leaq 0xcad(%rip), %rdi       # 0x2053
movb $0x0, %al
callq 0x1060
jmp 0x13af
cmpl $0x0, -0x14(%rbp)
je 0x13df
movl -0x14(%rbp), %esi
leaq 0xca9(%rip), %rdi       # 0x2068
movb $0x0, %al
callq 0x1060
cmpl $0x0, -0x18(%rbp)
je 0x13dd
movl -0x18(%rbp), %esi
leaq 0xcab(%rip), %rdi       # 0x2081
movb $0x0, %al
callq 0x1060
jmp 0x13df
jmp 0x13e1
movq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1070
movl %eax, -0x60(%rbp)
cmpl $0x2, %eax
je 0x1622
cmpl $0x1, -0x60(%rbp)
jne 0x15ef
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl -0x1c(%rbp), %r8d
movl $0x32, %edx
leaq -0x80(%rbp), %rcx
callq 0x10c0
cmpl $0x3, -0x80(%rbp)
jne 0x1469
movq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x84(%rbp), %r8
leaq -0x88(%rbp), %r9
movq %rcx, %rdx
callq 0x1050
leaq -0x50(%rbp), %rsi
movl -0x84(%rbp), %edx
movl -0x88(%rbp), %ecx
leaq 0xc2b(%rip), %rdi       # 0x2088
movb $0x0, %al
callq 0x1060
jmp 0x159c
cmpl $0x4, -0x80(%rbp)
jne 0x14a9
movq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x8c(%rbp), %rdx
leaq -0x90(%rbp), %rcx
callq 0x10a0
movl -0x8c(%rbp), %esi
movl -0x90(%rbp), %edx
leaq 0xc02(%rip), %rdi       # 0x209f
movb $0x0, %al
callq 0x1060
jmp 0x159a
cmpl $0x5, -0x80(%rbp)
jne 0x150b
movq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x94(%rbp), %rdx
leaq -0x98(%rbp), %rcx
leaq -0x9c(%rbp), %r8
callq 0x1040
movl -0x94(%rbp), %ecx
leaq 0xc0d(%rip), %rsi       # 0x20eb
leaq 0xc02(%rip), %rax       # 0x20e7
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movl -0x98(%rbp), %edx
movl -0x9c(%rbp), %ecx
leaq 0xbcb(%rip), %rdi       # 0x20ca
movb $0x0, %al
callq 0x1060
jmp 0x1598
cmpl $-0x1, -0x80(%rbp)
jne 0x1584
movq $0x10, -0x128(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x120(%rbp), %rdx
leaq -0x80(%rbp), %rsi
leaq -0x128(%rbp), %rcx
leaq -0x130(%rbp), %r8
callq 0x1090
movq -0x130(%rbp), %rax
shrq $0x8, %rax
movsbl %al, %esi
movq -0x120(%rbp), %rdx
movq -0x118(%rbp), %rcx
movq -0x130(%rbp), %rax
shrq $0x10, %rax
movsbl %al, %r8d
movq -0x130(%rbp), %rax
movsbl %al, %r9d
leaq 0xb75(%rip), %rdi       # 0x20f0
movb $0x0, %al
callq 0x1060
jmp 0x1596
leaq -0x50(%rbp), %rsi
leaq 0xb83(%rip), %rdi       # 0x2112
movb $0x0, %al
callq 0x1060
jmp 0x1598
jmp 0x159a
jmp 0x159c
cmpl $0x0, -0x80(%rbp)
jne 0x15bd
movl -0x70(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x15bd
cmpq $0x43, -0x78(%rbp)
je 0x15bb
cmpq $0x63, -0x78(%rbp)
jne 0x15bd
jmp 0x1622
cmpl $0x0, -0x80(%rbp)
jne 0x15ed
cmpl $0x0, -0x70(%rbp)
jne 0x15ed
cmpq $0x3f, -0x78(%rbp)
jne 0x15ed
leaq 0xb43(%rip), %rdi       # 0x211a
movb $0x0, %al
callq 0x1060
movq 0x29eb(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x10b0
jmp 0x161d
cmpl $0x4, -0x60(%rbp)
jne 0x161b
callq 0x1030
cmpl $0x4, (%rax)
je 0x160d
leaq 0xb1b(%rip), %rdi       # 0x2121
callq 0x1100
jmp 0x1622
leaq 0xb1d(%rip), %rdi       # 0x2131
movb $0x0, %al
callq 0x1060
jmp 0x161d
jmp 0x13e1
cmpl $0x0, -0x14(%rbp)
je 0x1639
movl -0x14(%rbp), %esi
leaq 0xb16(%rip), %rdi       # 0x2148
movb $0x0, %al
callq 0x1060
movq -0x58(%rbp), %rdi
callq 0x10e0
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq