// File: jgeumlek[P]moltengamepadctl/source/main.cpp
// Function: main
int main(int argc, char* argv[]) {
  
  int retcode = 0;
  options options;
  options.interactive = false;

  int ret = parse_opts(options, argc, argv);

  if (ret > 0) return 0; //This was something like "--help" where we don't do anything.
  if (ret < 0) return ret;

  const char* run_dir = getenv("XDG_RUNTIME_DIR");
  if (options.socket_path.empty() && run_dir) {
    options.socket_path = std::string(run_dir) + "/mg.sock";
  }

  if (options.socket_path.empty()) {
    std::cout << "Could not determine socket path." << std::endl;
    exit(-1);
  }

  sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    exit(-2);
  }
  std::cout << "connecting to " << options.socket_path << "  ... " << std::endl;
  strcpy(addr.sun_path, options.socket_path.c_str());//, options.socket_path.size()+1);
  addr.sun_family = AF_UNIX;
  auto len = strlen(addr.sun_path) + sizeof(addr.sun_family);
  ret = connect(sock, (struct sockaddr *)&addr, len);
  if (ret < 0) {
    perror("connect");
    exit(-3);
  }

  std::thread* reader = new std::thread(socket_read_loop, sock);
  reader->detach();
  delete reader;

  int request_id = 1;
  std::cout << "Listening for plug events and slot events..." << std::endl;
  osc_msg(sock, "/listen", request_id++, "sb", "plug", true);
  osc_msg(sock, "/listen", request_id++, "sb", "slot", true);
  for (auto command : options.eval_commands) {
    std::cout << "Running: " << command << std::endl;
    osc_msg(sock, "/eval", request_id++, "s", command.c_str());
  }

  if (options.interactive)
    interactive_loop(sock, request_id, std::cin);

  while (true) {
    std::lock_guard<std::mutex> guard(pending_lock);
    if (pending_requests.size() == 0)
      break;
  }
  close(sock);
  exit(retcode);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
leaq -0x58(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x6070
movq -0x288(%rbp), %rdi
movb $0x0, -0x20(%rbp)
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x4dd0
movl %eax, -0x27c(%rbp)
jmp 0x44df
movl -0x27c(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x4512
movl $0x0, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x4da8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4dbd
cmpl $0x0, -0x5c(%rbp)
jge 0x452a
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x4da8
leaq 0x9b91(%rip), %rdi      # 0xe0c2
callq 0x3320
movq %rax, -0x78(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x454c
jmp 0x461a
cmpq $0x0, -0x78(%rbp)
je 0x461a
movq -0x78(%rbp), %rax
movq %rax, -0x298(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x3480
movq -0x298(%rbp), %rsi
movq -0x290(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
callq 0x3370
jmp 0x4591
leaq 0x9b3a(%rip), %rdx      # 0xe0d2
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x60a0
jmp 0x45ad
leaq -0x58(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3330
leaq -0x98(%rbp), %rdi
callq 0x31b0
leaq -0xb8(%rbp), %rdi
callq 0x31b0
leaq -0xb9(%rbp), %rdi
callq 0x32e0
jmp 0x461a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4609
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x31b0
leaq -0xb9(%rbp), %rdi
callq 0x32e0
jmp 0x4dbd
leaq -0x58(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4629
jmp 0x4664
movq 0x10998(%rip), %rdi     # 0x14fc8
leaq 0x9aa4(%rip), %rsi      # 0xe0db
callq 0x32a0
movq %rax, -0x2a0(%rbp)
jmp 0x4645
movq -0x2a0(%rbp), %rdi
movq 0x10955(%rip), %rsi     # 0x14fa8
callq 0x32d0
jmp 0x465a
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3310
leaq -0x128(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x3160
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x3080
movl %eax, -0x12c(%rbp)
cmpl $0x0, -0x12c(%rbp)
jge 0x46ac
leaq 0x9a61(%rip), %rdi      # 0xe0fc
callq 0x3240
jmp 0x46a2
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x3310
movq 0x10915(%rip), %rdi     # 0x14fc8
leaq 0x9a49(%rip), %rsi      # 0xe103
callq 0x32a0
movq %rax, -0x2a8(%rbp)
jmp 0x46c8
movq -0x2a8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3270
movq %rax, -0x2b0(%rbp)
jmp 0x46e1
movq -0x2b0(%rbp), %rdi
leaq 0x9a23(%rip), %rsi      # 0xe112
callq 0x32a0
movq %rax, -0x2b8(%rbp)
jmp 0x46fd
movq -0x2b8(%rbp), %rdi
movq 0x1089d(%rip), %rsi     # 0x14fa8
callq 0x32d0
jmp 0x4712
leaq -0x126(%rbp), %rax
movq %rax, -0x2c8(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3100
movq -0x2c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3290
movq -0x2c8(%rbp), %rdi
movw $0x1, -0x128(%rbp)
callq 0x3120
addq $0x2, %rax
movq %rax, -0x138(%rbp)
movl -0x12c(%rbp), %edi
movl -0x138(%rbp), %edx
leaq -0x128(%rbp), %rsi
callq 0x3230
movl %eax, -0x2bc(%rbp)
jmp 0x4778
movl -0x2bc(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jge 0x479f
leaq 0x998b(%rip), %rdi      # 0xe119
callq 0x3240
jmp 0x4795
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
callq 0x3310
movl $0x8, %edi
callq 0x32b0
movq %rax, -0x2d0(%rbp)
jmp 0x47b2
movq -0x2d0(%rbp), %rdi
leaq -0xa10(%rip), %rsi      # 0x3db0
leaq -0x12c(%rbp), %rdx
callq 0x60f0
jmp 0x47ce
movq -0x2d0(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3460
jmp 0x47ea
movq -0x140(%rbp), %rax
movq %rax, -0x2d8(%rbp)
cmpq $0x0, %rax
je 0x481b
movq -0x2d8(%rbp), %rdi
callq 0x61b0
movq -0x2d8(%rbp), %rdi
movl $0x8, %esi
callq 0x32c0
movl $0x1, -0x144(%rbp)
movq 0x1079c(%rip), %rdi     # 0x14fc8
leaq 0x98ee(%rip), %rsi      # 0xe121
callq 0x32a0
movq %rax, -0x2e0(%rbp)
jmp 0x4841
movq -0x2e0(%rbp), %rdi
movq 0x10759(%rip), %rsi     # 0x14fa8
callq 0x32d0
jmp 0x4856
movl -0x12c(%rbp), %eax
movl %eax, -0x2ec(%rbp)
leaq -0x169(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x3480
movq -0x2e8(%rbp), %rdx
leaq 0x98cb(%rip), %rsi      # 0xe14e
leaq -0x168(%rbp), %rdi
callq 0x3370
jmp 0x4891
movl -0x144(%rbp), %eax
movl %eax, -0x2fc(%rbp)
incl %eax
movl %eax, -0x144(%rbp)
leaq -0x191(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x3480
movq -0x2f8(%rbp), %rdx
leaq 0x9890(%rip), %rsi      # 0xe156
leaq -0x190(%rbp), %rdi
callq 0x3370
jmp 0x48d4
movl -0x2fc(%rbp), %edx
movl -0x2ec(%rbp), %edi
leaq 0x9872(%rip), %r8       # 0xe159
xorl %eax, %eax
leaq -0x168(%rbp), %rsi
leaq -0x190(%rbp), %rcx
movl $0x1, %r9d
callq 0x37a0
jmp 0x4904
leaq -0x190(%rbp), %rdi
callq 0x31b0
leaq -0x191(%rbp), %rdi
callq 0x32e0
leaq -0x168(%rbp), %rdi
callq 0x31b0
leaq -0x169(%rbp), %rdi
callq 0x32e0
movl -0x12c(%rbp), %eax
movl %eax, -0x30c(%rbp)
leaq -0x1b9(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x3480
movq -0x308(%rbp), %rdx
leaq 0x97ed(%rip), %rsi      # 0xe14e
leaq -0x1b8(%rbp), %rdi
callq 0x3370
jmp 0x496f
movl -0x144(%rbp), %eax
movl %eax, -0x31c(%rbp)
incl %eax
movl %eax, -0x144(%rbp)
leaq -0x1e1(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x3480
movq -0x318(%rbp), %rdx
leaq 0x97b2(%rip), %rsi      # 0xe156
leaq -0x1e0(%rbp), %rdi
callq 0x3370
jmp 0x49b2
movl -0x31c(%rbp), %edx
movl -0x30c(%rbp), %edi
leaq 0x9799(%rip), %r8       # 0xe15e
xorl %eax, %eax
leaq -0x1b8(%rbp), %rsi
leaq -0x1e0(%rbp), %rcx
movl $0x1, %r9d
callq 0x37a0
jmp 0x49e2
leaq -0x1e0(%rbp), %rdi
callq 0x31b0
leaq -0x1e1(%rbp), %rdi
callq 0x32e0
leaq -0x1b8(%rbp), %rdi
callq 0x31b0
leaq -0x1b9(%rbp), %rdi
callq 0x32e0
leaq -0x58(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rdi
callq 0x61e0
movq %rax, -0x1f8(%rbp)
movq -0x1f0(%rbp), %rdi
callq 0x6210
movq %rax, -0x200(%rbp)
leaq -0x1f8(%rbp), %rdi
leaq -0x200(%rbp), %rsi
callq 0x6240
testb $0x1, %al
jne 0x4a63
jmp 0x4d1d
leaq -0x1f8(%rbp), %rdi
callq 0x6280
movq %rax, %rsi
leaq -0x220(%rbp), %rdi
callq 0x3110
jmp 0x4a80
movq 0x10541(%rip), %rdi     # 0x14fc8
leaq 0x96d5(%rip), %rsi      # 0xe163
callq 0x32a0
movq %rax, -0x328(%rbp)
jmp 0x4a9c
movq -0x328(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x3270
movq %rax, -0x330(%rbp)
jmp 0x4ab8
movq -0x330(%rbp), %rdi
movq 0x104e2(%rip), %rsi     # 0x14fa8
callq 0x32d0
jmp 0x4acd
movl -0x12c(%rbp), %eax
movl %eax, -0x33c(%rbp)
leaq -0x241(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0x3480
movq -0x338(%rbp), %rdx
leaq 0x9673(%rip), %rsi      # 0xe16d
leaq -0x240(%rbp), %rdi
callq 0x3370
jmp 0x4b08
movl -0x144(%rbp), %eax
movl %eax, -0x34c(%rbp)
incl %eax
movl %eax, -0x144(%rbp)
leaq -0x269(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x3480
movq -0x348(%rbp), %rdx
leaq 0x9636(%rip), %rsi      # 0xe173
leaq -0x268(%rbp), %rdi
callq 0x3370
jmp 0x4b4b
leaq -0x220(%rbp), %rdi
callq 0x3100
movl -0x33c(%rbp), %edi
movl -0x34c(%rbp), %edx
movq %rax, %r8
xorl %eax, %eax
leaq -0x240(%rbp), %rsi
leaq -0x268(%rbp), %rcx
callq 0x37a0
jmp 0x4b7d
leaq -0x268(%rbp), %rdi
callq 0x31b0
leaq -0x269(%rbp), %rdi
callq 0x32e0
leaq -0x240(%rbp), %rdi
callq 0x31b0
leaq -0x241(%rbp), %rdi
callq 0x32e0
leaq -0x220(%rbp), %rdi
callq 0x31b0
leaq -0x1f8(%rbp), %rdi
callq 0x62a0
jmp 0x4a47
movq -0x2d0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x8, %esi
callq 0x32c0
jmp 0x4dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4c38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4c20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x31b0
leaq -0x191(%rbp), %rdi
callq 0x32e0
leaq -0x168(%rbp), %rdi
callq 0x31b0
leaq -0x169(%rbp), %rdi
callq 0x32e0
jmp 0x4dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4c7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x31b0
leaq -0x1e1(%rbp), %rdi
callq 0x32e0
leaq -0x1b8(%rbp), %rdi
callq 0x31b0
leaq -0x1b9(%rbp), %rdi
callq 0x32e0
jmp 0x4dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4d0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4d00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4ce8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x31b0
leaq -0x269(%rbp), %rdi
callq 0x32e0
leaq -0x240(%rbp), %rdi
callq 0x31b0
leaq -0x241(%rbp), %rdi
callq 0x32e0
leaq -0x220(%rbp), %rdi
callq 0x31b0
jmp 0x4dbd
testb $0x1, -0x20(%rbp)
je 0x4d3f
movl -0x12c(%rbp), %edi
movl -0x144(%rbp), %esi
movq 0x102a2(%rip), %rdx     # 0x14fd8
callq 0x4ff0
jmp 0x4d3d
jmp 0x4d3f
jmp 0x4d41
jmp 0x4d43
leaq 0x10626(%rip), %rsi     # 0x15370
leaq -0x278(%rbp), %rdi
callq 0x5440
jmp 0x4d58
leaq 0x105f9(%rip), %rdi     # 0x15358
callq 0x62c0
cmpq $0x0, %rax
jne 0x4d73
movl $0x5, -0x70(%rbp)
jmp 0x4d7a
movl $0x0, -0x70(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x54e0
movl -0x70(%rbp), %eax
testl %eax, %eax
je 0x4d91
jmp 0x4d8f
jmp 0x4d93
jmp 0x4d41
movl -0x12c(%rbp), %edi
callq 0x34c0
jmp 0x4da0
movl -0x14(%rbp), %edi
callq 0x3310
leaq -0x58(%rbp), %rdi
callq 0x62e0
movl -0x4(%rbp), %eax
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x62e0
movq -0x68(%rbp), %rdi
callq 0x3470
nop