// File: Linkerist[P]xs/src/xs.cc
// Function: main
int
main(int argc, char ** argv)
{
 int c;
 int opt_idx = 0;

 char * arg = NULL;

 string optname;
 string argument;

 const static string xs_short_opts = "a:f:u:brco:hV";

 static struct option xs_long_opts[] = {
  {"help",      no_argument, NULL, 'h'},
  {"version",   no_argument, NULL, 'V'},
 
  {"browse",    no_argument, 0, 0},
  {"nowrap",    no_argument, 0, 0},
  {"noresolve", no_argument, 0, 0},
  {"output",    required_argument, 0, 0},
 
  {NULL, 0, NULL, 0}
 };

 while ((c = getopt_long(argc, argv, xs_short_opts.c_str(), xs_long_opts, &opt_idx)) != -1) {
  switch (c) {
   case 0:
    optname = string(xs_long_opts[opt_idx].name);

    if (optname == "help") {
     version();
     usage(EXIT_SUCCESS);
     exit(0);
    } else if (optname == "version") {
     version();
     exit(0);
    } else if(optname == "browse")
     mode = BROWSE;
    else if (optname == "nowrap")
     opt_no_wrap = true;
    else if (optname == "noresolve")
     opt_no_resolve = true;

    break;

   case 'b':
    mode = BROWSE;
    break;
   case 'r':
    opt_no_resolve = true;
    break;
   case 'h':
    version();
    usage(EXIT_SUCCESS);
    break;
   case 'V':
    version();
    exit(0);
   default:
    usage(EXIT_FAILURE);
  }
 }

 (void)arg;

 if (optind < argc) {
  Needle = argv[optind];
  if (strlen(Needle) > 0)
   NeedleGiven = true;
  
  if (strlen(Needle) == 1 && isdigit(Needle[0])) {
   CurrPosition = atoi(Needle);
   Needle = NULL;
  }

  //usage(EXIT_FAILURE);
 }

 // leave terminal tidy
 // FIXME: what other signals do I need to catch? 
 signal(SIGINT, terminate);
 signal(SIGTERM, terminate);
 signal(SIGSEGV, terminate);

 init_curses();

 // answer to terminal reizing
 signal(SIGWINCH, resizeevent);

 /* get list from file or start in browse mode */
 if (!list_from_file())
  /* doesn't exist. browse current dir */
  mode = BROWSE;

 // if we're browsing read the CWD
 if(mode == BROWSE)
  list_from_dir();

 /* main event loop */
 /* determines the entry to use */
 while (1) {
  display_list();
  c = getch();
  if (!user_interaction(c))
   break;
 }

 finish(current_entry(), true);
 exit(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4590
leaq -0x60(%rbp), %rdi
callq 0x4590
cmpb $0x0, 0x11d7f(%rip)     # 0x17600
jne 0x58ef
leaq 0x11d76(%rip), %rdi     # 0x17600
callq 0x4760
cmpl $0x0, %eax
je 0x58ef
leaq -0x61(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x4740
movq -0xc8(%rbp), %rdx
leaq 0x11d2e(%rip), %rdi     # 0x175e0
leaq 0xa87f(%rip), %rsi      # 0x10138
callq 0x5e40
jmp 0x58c0
leaq -0x61(%rbp), %rdi
callq 0x4450
movq 0x116e0(%rip), %rdi     # 0x16fb0
leaq 0x11d09(%rip), %rsi     # 0x175e0
leaq 0x11b3a(%rip), %rdx     # 0x17418
callq 0x4370
leaq 0x11d16(%rip), %rdi     # 0x17600
callq 0x4290
jmp 0x58f1
movl -0x8(%rbp), %eax
movl %eax, -0xd4(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq 0x11cd4(%rip), %rdi     # 0x175e0
callq 0x41a0
movl -0xd4(%rbp), %edi
movq -0xd0(%rbp), %rsi
movq %rax, %rdx
leaq 0x11b08(%rip), %rcx     # 0x17430
leaq -0x18(%rbp), %r8
callq 0x46e0
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x5bb0
movl -0x14(%rbp), %eax
movl %eax, -0xd8(%rbp)
testl %eax, %eax
je 0x59b9
jmp 0x594c
movl -0xd8(%rbp), %eax
subl $0x56, %eax
je 0x5b8f
jmp 0x595d
movl -0xd8(%rbp), %eax
subl $0x62, %eax
je 0x5b68
jmp 0x596e
movl -0xd8(%rbp), %eax
subl $0x68, %eax
je 0x5b7d
jmp 0x597f
movl -0xd8(%rbp), %eax
subl $0x72, %eax
je 0x5b74
jmp 0x5b9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x4450
leaq 0x11c51(%rip), %rdi     # 0x17600
callq 0x4280
jmp 0x5d5d
movslq -0x18(%rbp), %rax
shlq $0x5, %rax
leaq 0x11a68(%rip), %rcx     # 0x17430
movq (%rax,%rcx), %rax
movq %rax, -0xe8(%rbp)
leaq -0x99(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x4740
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x5e40
jmp 0x5a02
leaq -0x40(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
leaq -0x98(%rbp), %rsi
movq %rsi, -0x100(%rbp)
callq 0x44f0
movq -0x100(%rbp), %rdi
callq 0x4828
leaq -0x99(%rbp), %rdi
callq 0x4450
movq -0xf8(%rbp), %rdi
leaq 0xb5a9(%rip), %rsi      # 0x10fef
callq 0x6210
movb %al, -0xe9(%rbp)
jmp 0x5a53
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x5a5f
jmp 0x5aa4
callq 0xc540
jmp 0x5a66
xorl %edi, %edi
callq 0xc560
jmp 0x5a6f
xorl %edi, %edi
callq 0x44d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x4450
jmp 0x5d5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x5d5d
leaq 0xa69b(%rip), %rsi      # 0x10146
leaq -0x40(%rbp), %rdi
callq 0x6210
movb %al, -0x101(%rbp)
jmp 0x5abc
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x5ac8
jmp 0x5ad6
callq 0xc540
jmp 0x5acf
xorl %edi, %edi
callq 0x44d0
leaq 0xa671(%rip), %rsi      # 0x1014e
leaq -0x40(%rbp), %rdi
callq 0x6210
movb %al, -0x102(%rbp)
jmp 0x5aee
movb -0x102(%rbp), %al
testb $0x1, %al
jne 0x5afa
jmp 0x5b06
movl $0x1, 0x11a94(%rip)     # 0x17598
jmp 0x5b62
leaq 0xa648(%rip), %rsi      # 0x10155
leaq -0x40(%rbp), %rdi
callq 0x6210
movb %al, -0x103(%rbp)
jmp 0x5b1e
movb -0x103(%rbp), %al
testb $0x1, %al
jne 0x5b2a
jmp 0x5b33
movb $0x1, 0x11a6c(%rip)     # 0x1759d
jmp 0x5b60
leaq 0xa622(%rip), %rsi      # 0x1015c
leaq -0x40(%rbp), %rdi
callq 0x6210
movb %al, -0x104(%rbp)
jmp 0x5b4b
movb -0x104(%rbp), %al
testb $0x1, %al
jne 0x5b57
jmp 0x5b5e
movb $0x1, 0x11a40(%rip)     # 0x1759e
jmp 0x5b60
jmp 0x5b62
jmp 0x5b64
jmp 0x5b66
jmp 0x5bab
movl $0x1, 0x11a26(%rip)     # 0x17598
jmp 0x5bab
movb $0x1, 0x11a23(%rip)     # 0x1759e
jmp 0x5bab
callq 0xc540
jmp 0x5b84
xorl %edi, %edi
callq 0xc560
jmp 0x5b8d
jmp 0x5bab
callq 0xc540
jmp 0x5b96
xorl %edi, %edi
callq 0x44d0
movl $0x1, %edi
callq 0xc560
jmp 0x5ba9
jmp 0x5bab
jmp 0x58f1
movq 0x11419(%rip), %rax     # 0x16fd0
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jge 0x5c35
movq -0x10(%rbp), %rax
movq 0x11407(%rip), %rcx     # 0x16fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x11981(%rip)     # 0x17558
movq 0x1197a(%rip), %rdi     # 0x17558
callq 0x41c0
cmpq $0x0, %rax
jbe 0x5bf0
movb $0x1, 0x11970(%rip)     # 0x17560
movq 0x11961(%rip), %rdi     # 0x17558
callq 0x41c0
cmpq $0x1, %rax
jne 0x5c33
movq 0x1194f(%rip), %rax     # 0x17558
movsbl (%rax), %edi
callq 0x4390
cmpl $0x0, %eax
je 0x5c33
movq 0x1193b(%rip), %rdi     # 0x17558
callq 0x4690
movl %eax, 0x1193c(%rip)     # 0x17564
movq $0x0, 0x11925(%rip) # 0x17558
jmp 0x5c35
leaq -0x5dc(%rip), %rsi      # 0x5660
movq %rsi, -0x110(%rbp)
movl $0x2, %edi
callq 0x44b0
movq -0x110(%rbp), %rsi
movl $0xf, %edi
callq 0x44b0
movq -0x110(%rbp), %rsi
movl $0xb, %edi
callq 0x44b0
callq 0xc950
jmp 0x5c76
leaq -0x57d(%rip), %rsi      # 0x5700
movl $0x1c, %edi
callq 0x44b0
callq 0x6c60
movb %al, -0x111(%rbp)
jmp 0x5c94
movb -0x111(%rbp), %al
testb $0x1, %al
jne 0x5caa
jmp 0x5ca0
movl $0x1, 0x118ee(%rip)     # 0x17598
cmpl $0x1, 0x118e7(%rip)     # 0x17598
jne 0x5cc3
leaq 0xa4de(%rip), %rdi      # 0x10198
callq 0x7250
jmp 0x5cc1
jmp 0x5cc3
jmp 0x5cc5
jmp 0x5cc7
callq 0xe830
jmp 0x5cce
movq 0x112eb(%rip), %rax     # 0x16fc0
movq (%rax), %rdi
callq 0x4160
movl %eax, -0x118(%rbp)
jmp 0x5ce5
movl -0x118(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edi
callq 0xc9b0
movb %al, -0x119(%rbp)
jmp 0x5cfe
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x5d0c
jmp 0x5d0a
jmp 0x5d0e
jmp 0x5cc5
leaq -0xc0(%rbp), %rdi
callq 0x8200
jmp 0x5d1c
leaq -0xc0(%rbp), %rdi
movl $0x1, %esi
callq 0x53d0
jmp 0x5d2f
leaq -0xc0(%rbp), %rdi
callq 0x4828
movl $0x1, %edi
callq 0x44d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4828
leaq -0x60(%rbp), %rdi
callq 0x4828
leaq -0x40(%rbp), %rdi
callq 0x4828
movq -0x70(%rbp), %rdi
callq 0x4730
nopl (%rax,%rax)