// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movq %rsi, %rdx
movl %edi, %esi
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, 0x19(%r14)
leaq 0x30(%rsp), %rdi
callq 0x33aa
testl %eax, %eax
je 0x305f
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x518e
movq -0x20(%rbx), %rdi
cmpq %r14, %rdi
je 0x3050
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2230
xorl %eax, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r14
popq %r15
retq
leaq 0x2fc4(%rip), %rdi      # 0x602a
callq 0x2280
testq %rax, %rax
je 0x314a
cmpq $0x0, 0x38(%rsp)
jne 0x314a
movq %rax, %rbx
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
movq %rax, %rdi
callq 0x20d0
movq %rbx, %rdx
addq %rax, %rdx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x51ea
leaq 0x2f8c(%rip), %rsi      # 0x603a
leaq 0x10(%rsp), %rdi
callq 0x2370
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x30dc
movups (%rcx), %xmm0
movups %xmm0, (%r15)
jmp 0x30ef
movq %rdx, 0x90(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa0(%rsp)
movq 0x8(%rax), %rdx
leaq 0x90(%rsp), %rbx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2290
movq (%rbx), %rdi
cmpq %r15, %rdi
je 0x3133
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x314a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
cmpq $0x0, 0x38(%rsp)
jne 0x3180
movq 0x5e77(%rip), %rdi      # 0x8fd0
leaq 0x2ee3(%rip), %rsi      # 0x6043
movl $0x20, %edx
callq 0x2260
movq 0x5e5f(%rip), %rdi      # 0x8fd0
callq 0x2110
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2270
xorps %xmm0, %xmm0
movups %xmm0, 0xee(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0xc(%rsp)
testl %eax, %eax
jns 0x31d9
callq 0x23b5
movq 0x5df0(%rip), %rdi      # 0x8fd0
leaq 0x2e84(%rip), %rsi      # 0x606b
movl $0xe, %edx
callq 0x2260
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x5dce(%rip), %rdi      # 0x8fd0
callq 0x2260
movq %rax, %rbx
leaq 0x2e69(%rip), %rsi      # 0x607a
movl $0x6, %edx
movq %rax, %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x2110
leaq 0x92(%rsp), %rbx
movq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x2200
movw $0x1, -0x2(%rbx)
movq %rbx, %rdi
callq 0x20d0
movl 0xc(%rsp), %edi
leal 0x2(%rax), %edx
leaq 0x90(%rsp), %rsi
callq 0x21b0
testl %eax, %eax
jns 0x3266
callq 0x23a0
movl $0x8, %edi
callq 0x2220
movq %rax, %r14
leaq -0x7f7(%rip), %rsi      # 0x2a83
leaq 0xc(%rsp), %rdx
movq %rax, %rdi
callq 0x3c5a
movq %r14, %rdi
callq 0x2340
cmpq $0x0, (%r14)
jne 0x3338
movl $0x8, %esi
movq %r14, %rdi
callq 0x2230
movq 0x5d23(%rip), %rdi      # 0x8fd0
leaq 0x2dd5(%rip), %rsi      # 0x6089
movl $0x2c, %edx
callq 0x2260
movq 0x5d0b(%rip), %rdi      # 0x8fd0
callq 0x2110
movl 0xc(%rsp), %ebx
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2dd8(%rip), %rsi      # 0x60b6
leaq 0x2dd8(%rip), %rdx      # 0x60bd
leaq 0x10(%rsp), %rdi
callq 0x51ea
leaq 0x80(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2dbc(%rip), %rsi      # 0x60be
leaq 0x2db7(%rip), %rdx      # 0x60c0
leaq 0x70(%rsp), %rdi
callq 0x51ea
leaq 0x2da7(%rip), %r8       # 0x60c1
leaq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movl %ebx, %edi
movl $0x1, %edx
movl $0x1, %r9d
xorl %eax, %eax
callq 0x2655
callq 0x2070
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r15, %rdi
je 0x3376
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x3376
jmp 0x3373
jmp 0x3395
jmp 0x3395
movq %rax, %rbx
movl $0x8, %esi
movq %r14, %rdi
jmp 0x3388
jmp 0x3395
jmp 0x3395
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x3398
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x3398
jmp 0x3395
jmp 0x3395
jmp 0x3395
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3cea
movq %rbx, %rdi
callq 0x2350