// File: /workspace/github_cpp_makefile_star2_O1/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 0x81c1(%rip), %rcx      # 0x15c00
movq %rcx, (%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x30(%rdi)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, (%rsp)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x44(%rsp)
movl $0x1, 0x40(%rsp)
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0xe762
movq 0x10(%rsp), %rsi
leaq 0x40(%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 0xdab6
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5070
testl %ebp, %ebp
js 0xdb67
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0xb27(%rip), %rax       # 0xe5f4
movq %rax, 0x28(%rsp)
leaq 0xb23(%rip), %rax       # 0xe5fc
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
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 0xdb9b
leaq 0x3c(%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 0xdbcd
movl $0xa, %esi
callq 0x5670
testl %eax, %eax
js 0xdbf0
leaq 0x10(%rsp), %rdi
callq 0xe7ec
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1d63(%rip), %rsi      # 0xf8de
movq %rax, %rdi
callq 0x50d0
movq 0x840e(%rip), %rsi      # 0x15f98
movq 0x844f(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
jmp 0xdbcd
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1d46(%rip), %rsi      # 0xf8f5
movq %rax, %rdi
callq 0x50d0
movq 0x83da(%rip), %rsi      # 0x15f98
movq 0x841b(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
callq 0x53d0
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1d29(%rip), %rsi      # 0xf90f
movq %rax, %rdi
callq 0x50d0
jmp 0xdc14
movl 0x18(%rbx), %edi
callq 0x53d0
movl $0x10, %edi
callq 0x5420
movq %rax, %r15
leaq 0x1d3e(%rip), %rsi      # 0xf94a
movq %rax, %rdi
callq 0x50d0
movq 0x837d(%rip), %rsi      # 0x15f98
movq 0x83be(%rip), %rdx      # 0x15fe0
movq %r15, %rdi
callq 0x51c0
jmp 0xdc30
jmp 0xdc30
jmp 0xdc71
movq %rax, %r14
movq %r15, %rdi
callq 0x55f0
jmp 0xdc74
jmp 0xdc6c
movq %rax, %r14
movq %r15, %rdi
callq 0x55f0
jmp 0xdc7e
movq %rax, %r14
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xdc7e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5070
jmp 0xdc7e
movq %rax, %r14
jmp 0xdc7e
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0xe7ec
leaq 0x28(%rbx), %rdi
callq 0xc3a8
movq %rbx, %rdi
callq 0x8592
movq %r14, %rdi
callq 0x5200
nop