// File: /workspace/github_cpp_makefile_star2_O2/Themaister[P]umusd/tcpcommand.cpp
// Function: TCPCommand::TCPCommand(unsigned short)
TCPCommand::TCPCommand(std::uint16_t port) : remote(nullptr)
{
   struct addrinfo hints{}, *servinfo{nullptr};

   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_PASSIVE;

   if (getaddrinfo(nullptr, stringify(port).c_str(), &hints, &servinfo) < 0)
      throw std::runtime_error("getaddrinfo() failed.\n");

   std::unique_ptr<struct addrinfo, std::function<void (struct addrinfo*)>> holder(servinfo,
         [](struct addrinfo *info) {
            freeaddrinfo(info);
         });

   fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
   if (fd < 0)
      throw std::runtime_error("Failed to create socket.\n");

   int yes = 1;
   setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
   if (bind(fd, servinfo->ai_addr, servinfo->ai_addrlen) < 0)
   {
      close(fd);
      throw std::runtime_error("Failed to bind socket. umusd is probably already running.\n");
   }

   if (listen(fd, 10) < 0)
   {
      close(fd);
      throw std::runtime_error("Failed to listen to socket.\n");
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
leaq 0x16(%rsp), %rax
movw %si, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
leaq 0x93e6(%rip), %rcx      # 0x14c08
movq %rcx, (%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x30(%rdi)
andq $0x0, 0x68(%rsp)
andq $0x0, 0x8(%rsp)
movups %xmm0, 0x58(%rsp)
movups %xmm0, 0x48(%rsp)
movl $0x1, 0x48(%rsp)
movq $0x1, 0x40(%rsp)
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0xc2a6
movq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %edi, %edi
callq 0x4650
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
callq 0x4540
testl %ebp, %ebp
js 0xb948
movq 0x8(%rsp), %rsi
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r14
movaps %xmm0, (%r14)
leaq 0x8ff(%rip), %rax       # 0xc1a2
movq %rax, 0x18(%r14)
leaq 0x8fc(%rip), %rax       # 0xc1aa
movq %rax, 0x10(%r14)
leaq 0x18(%rsp), %rdi
movq %r14, %rdx
callq 0xc3dc
movq %r14, %rdi
callq 0x7162
movq 0x8(%rsp), %rax
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x4440
movl %eax, 0x18(%rbx)
testl %eax, %eax
js 0xb97a
leaq 0x70(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x43b0
movl 0x18(%rbx), %edi
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x45d0
movl 0x18(%rbx), %edi
testl %eax, %eax
js 0xb9aa
pushq $0xa
popq %rsi
callq 0x4660
testl %eax, %eax
js 0xb9cb
leaq 0x18(%rsp), %rdi
callq 0xc30e
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x4430
movq %rax, %r15
leaq 0x1f64(%rip), %rsi      # 0xd8be
movq %rax, %rdi
callq 0x40d0
movq 0x9637(%rip), %rsi      # 0x14fa0
movq 0x9670(%rip), %rdx      # 0x14fe0
movq %r15, %rdi
callq 0x41c0
jmp 0xb9aa
pushq $0x10
popq %rdi
callq 0x4430
movq %rax, %r15
leaq 0x1f49(%rip), %rsi      # 0xd8d5
movq %rax, %rdi
callq 0x40d0
movq 0x9605(%rip), %rsi      # 0x14fa0
movq 0x963e(%rip), %rdx      # 0x14fe0
movq %r15, %rdi
callq 0x41c0
callq 0x43e0
pushq $0x10
popq %rdi
callq 0x4430
movq %rax, %r15
leaq 0x1f2e(%rip), %rsi      # 0xd8ef
movq %rax, %rdi
callq 0x40d0
jmp 0xb9ed
movl 0x18(%rbx), %edi
callq 0x43e0
pushq $0x10
popq %rdi
callq 0x4430
movq %rax, %r15
leaq 0x1f45(%rip), %rsi      # 0xd92a
movq %rax, %rdi
callq 0x40d0
movq 0x95ac(%rip), %rsi      # 0x14fa0
movq 0x95e5(%rip), %rdx      # 0x14fe0
movq %r15, %rdi
callq 0x41c0
jmp 0xba09
jmp 0xba09
jmp 0xba39
movq %rax, %r14
movq %r15, %rdi
callq 0x45c0
jmp 0xba3c
jmp 0xba34
movq %rax, %r14
movq %r15, %rdi
callq 0x45c0
jmp 0xba46
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x4540
jmp 0xba46
movq %rax, %r14
jmp 0xba46
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0xc30e
leaq 0x28(%rbx), %rdi
callq 0xa2ba
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x4f44
movq %r14, %rdi
callq 0x4200
nop