// File: /workspace/github_cpp_makefile_star2_O3/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 0x3366
testl %eax, %eax
je 0x3014
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x5030
movq -0x20(%rbx), %rdi
cmpq %r14, %rdi
je 0x3005
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2230
xorl %eax, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r14
popq %r15
retq
leaq 0x300f(%rip), %rdi      # 0x602a
callq 0x2280
movq %rax, %rbx
movq 0x38(%rsp), %rax
testq %rax, %rax
setne %cl
testq %rbx, %rbx
sete %dl
orb %cl, %dl
jne 0x3108
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
movq %rbx, %rdi
callq 0x20d0
movq %rbx, %rdx
addq %rax, %rdx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x508e
leaq 0x2fd3(%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 0x3095
movups (%rcx), %xmm0
movups %xmm0, (%r15)
jmp 0x30a8
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 0x30ec
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x3103
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x38(%rsp), %rax
testq %rax, %rax
jne 0x313b
movq 0x5ebc(%rip), %rdi      # 0x8fd0
leaq 0x2f28(%rip), %rsi      # 0x6043
movl $0x20, %edx
callq 0x2260
movq 0x5ea4(%rip), %rdi      # 0x8fd0
callq 0x2110
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2270
xorps %xmm0, %xmm0
movups %xmm0, 0xee(%rsp)
movups %xmm0, 0xe2(%rsp)
movups %xmm0, 0xd2(%rsp)
movups %xmm0, 0xc2(%rsp)
movups %xmm0, 0xb2(%rsp)
movups %xmm0, 0xa2(%rsp)
movups %xmm0, 0x92(%rsp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0xc(%rsp)
testl %eax, %eax
jns 0x3194
callq 0x23b5
movl %eax, %ebx
movq 0x5e33(%rip), %rdi      # 0x8fd0
leaq 0x2ec7(%rip), %rsi      # 0x606b
movl $0xe, %edx
callq 0x2260
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x5e11(%rip), %rdi      # 0x8fd0
callq 0x2260
movq %rax, %r14
leaq 0x2eac(%rip), %rsi      # 0x607a
movl $0x6, %edx
movq %rax, %rdi
callq 0x2260
movq %r14, %rdi
callq 0x2110
leaq 0x92(%rsp), %r14
movq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x2200
movw $0x1, -0x2(%r14)
movq %r14, %rdi
callq 0x20d0
leal 0x2(%rax), %edx
leaq 0x90(%rsp), %rsi
movl %ebx, %edi
callq 0x21b0
testl %eax, %eax
jns 0x3222
callq 0x23a0
movl $0x8, %edi
callq 0x2220
movq %rax, %r14
leaq -0x7b4(%rip), %rsi      # 0x2a82
leaq 0xc(%rsp), %rdx
movq %rax, %rdi
callq 0x3c18
movq %r14, %rdi
callq 0x2340
cmpq $0x0, (%r14)
jne 0x32f4
movl $0x8, %esi
movq %r14, %rdi
callq 0x2230
movq 0x5d67(%rip), %rdi      # 0x8fd0
leaq 0x2e19(%rip), %rsi      # 0x6089
movl $0x2c, %edx
callq 0x2260
movq 0x5d4f(%rip), %rdi      # 0x8fd0
callq 0x2110
movl 0xc(%rsp), %ebx
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2e1c(%rip), %rsi      # 0x60b6
leaq 0x2e1c(%rip), %rdx      # 0x60bd
leaq 0x10(%rsp), %rdi
callq 0x508e
leaq 0x80(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2e00(%rip), %rsi      # 0x60be
leaq 0x2dfb(%rip), %rdx      # 0x60c0
leaq 0x70(%rsp), %rdi
callq 0x508e
leaq 0x2deb(%rip), %r8       # 0x60c1
leaq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movl %ebx, %edi
movl $0x1, %edx
movl $0x1, %r9d
xorl %eax, %eax
callq 0x2651
callq 0x2070
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r15, %rdi
je 0x3332
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x3332
jmp 0x332f
jmp 0x3351
jmp 0x3351
movq %rax, %rbx
movl $0x8, %esi
movq %r14, %rdi
jmp 0x3344
jmp 0x3351
jmp 0x3351
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x3354
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x3354
jmp 0x3351
jmp 0x3351
jmp 0x3351
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3c96
movq %rbx, %rdi
callq 0x2350