// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdi, %rbx
leaq 0xe(%rsp), %rax
movw %si, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
leaq 0x8081(%rip), %rcx      # 0x15c00
movq %rcx, (%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x30(%rdi)
xorl %ecx, %ecx
movq %rcx, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movq %rcx, (%rsp)
movl $0x1, 0x50(%rsp)
movq $0x1, 0x48(%rsp)
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0xe904
movq 0x10(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %rsp, %rcx
xorl %edi, %edi
callq 0x5660
movl %eax, %ebp
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xdbee
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5070
testl %ebp, %ebp
js 0xdc9f
movq (%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0xb8d(%rip), %rcx       # 0xe796
movq %rcx, 0x28(%rsp)
leaq 0xb89(%rip), %rcx       # 0xe79e
movq %rcx, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x5430
movl %eax, 0x18(%rbx)
testl %eax, %eax
js 0xdcd3
leaq 0x44(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x53a0
movl 0x18(%rbx), %edi
movq (%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x5600
movl 0x18(%rbx), %edi
testl %eax, %eax
js 0xdd05
movl $0xa, %esi
callq 0x5670
testl %eax, %eax
js 0xdd28
leaq 0x10(%rsp), %rdi
callq 0xe98e
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1c8b(%rip), %rsi      # 0xf93e
movq %rax, %rdi
callq 0x50d0
movq 0x82d6(%rip), %rsi      # 0x15f98
movq 0x8317(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
jmp 0xdd05
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1c6e(%rip), %rsi      # 0xf955
movq %rax, %rdi
callq 0x50d0
movq 0x82a2(%rip), %rsi      # 0x15f98
movq 0x82e3(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
callq 0x53d0
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1c51(%rip), %rsi      # 0xf96f
movq %rax, %rdi
callq 0x50d0
jmp 0xdd4c
movl 0x18(%rbx), %edi
callq 0x53d0
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1c66(%rip), %rsi      # 0xf9aa
movq %rax, %rdi
callq 0x50d0
movq 0x8245(%rip), %rsi      # 0x15f98
movq 0x8286(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
jmp 0xdd68
jmp 0xdd68
jmp 0xdda9
movq %rax, %r14
movq %r15, %rdi
callq 0x55f0
jmp 0xddac
jmp 0xdda4
movq %rax, %r14
movq %r15, %rdi
callq 0x55f0
jmp 0xddb6
movq %rax, %r14
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xddb6
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5070
jmp 0xddb6
movq %rax, %r14
jmp 0xddb6
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0xe98e
leaq 0x28(%rbx), %rdi
callq 0xc5e2
movq %rbx, %rdi
callq 0x8832
movq %r14, %rdi
callq 0x5200
nop