// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rdx
movl %edi, %esi
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x19(%rax)
leaq 0x48(%rsp), %rdi
callq 0x2fdc
testl %eax, %eax
je 0x2d5b
leaq 0x48(%rsp), %rdi
callq 0x3794
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r14
retq
leaq 0x22c8(%rip), %rdi      # 0x502a
callq 0x2280
movq 0x50(%rsp), %rcx
testq %rcx, %rcx
setne %dl
testq %rax, %rax
sete %sil
orb %dl, %sil
jne 0x2dd7
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rax, %rsi
callq 0x20c0
leaq 0x22a3(%rip), %rdx      # 0x503a
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x36ee
leaq 0x48(%rsp), %rbx
leaq 0x88(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2290
movq %r14, %rdi
callq 0x20f0
leaq 0x8(%rsp), %rdi
callq 0x20f0
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
jne 0x2dfb
movq 0x51ed(%rip), %rdi      # 0x7fd0
leaq 0x2259(%rip), %rsi      # 0x5043
callq 0x2230
movq %rax, %rdi
callq 0x2120
pushq $-0x1
jmp 0x2e58
xorps %xmm0, %xmm0
movups %xmm0, 0xe6(%rsp)
movups %xmm0, 0xda(%rsp)
movups %xmm0, 0xca(%rsp)
movups %xmm0, 0xba(%rsp)
movups %xmm0, 0xaa(%rsp)
movups %xmm0, 0x9a(%rsp)
movups %xmm0, 0x8a(%rsp)
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x4(%rsp)
testl %eax, %eax
jns 0x2e5e
leaq 0x2213(%rip), %rdi      # 0x5064
callq 0x21d0
pushq $-0x2
popq %rdi
callq 0x2270
movl %eax, %ebx
movq 0x5169(%rip), %rdi      # 0x7fd0
leaq 0x21fd(%rip), %rsi      # 0x506b
callq 0x2230
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x2200
leaq 0x21f3(%rip), %rsi      # 0x507a
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2120
leaq 0x8a(%rsp), %r14
movq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x2220
movw $0x1, -0x2(%r14)
movq %r14, %rdi
callq 0x20e0
leal 0x2(%rax), %edx
leaq 0x88(%rsp), %rsi
movl %ebx, %edi
callq 0x21c0
testl %eax, %eax
jns 0x2ee4
leaq 0x21a9(%rip), %rdi      # 0x5081
callq 0x21d0
pushq $-0x3
jmp 0x2e58
pushq $0x8
popq %rdi
callq 0x2240
movq %rax, %r14
leaq -0x618(%rip), %rsi      # 0x28de
leaq 0x4(%rsp), %rdx
movq %rax, %rdi
callq 0x370e
movq %r14, %rdi
callq 0x2330
movq %r14, %rdi
callq 0x3786
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x2250
movq 0x50ab(%rip), %rdi      # 0x7fd0
leaq 0x215d(%rip), %rsi      # 0x5089
callq 0x2230
movq %rax, %rdi
callq 0x2120
movl 0x4(%rsp), %ebx
leaq 0x2172(%rip), %rsi      # 0x50b6
leaq 0x8(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x20c0
leaq 0x2164(%rip), %rsi      # 0x50be
leaq 0x28(%rsp), %rdi
leaq 0x2(%rsp), %rdx
callq 0x20c0
leaq 0x2151(%rip), %r8       # 0x50c1
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rcx
pushq $0x1
popq %rdx
movl %ebx, %edi
movl %edx, %r9d
xorl %eax, %eax
callq 0x25a1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20f0
jmp 0x2fb5
jmp 0x2fb2
jmp 0x2fc7
jmp 0x2fc7
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x2250
jmp 0x2fca
jmp 0x2fc7
jmp 0x2fc7
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20f0
jmp 0x2fca
jmp 0x2fc7
jmp 0x2fc7
jmp 0x2fc7
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3794
movq %rbx, %rdi
callq 0x2340