// File: /workspace/github_cpp_makefile_star2_O2/Eba-M[P]E3DC-Control/SocketConnection.cpp
// Function: OpenConnection(char const*, int)
int OpenConnection(const char *hostname, int port)
{
    int sd, err;
    struct addrinfo hints = {}, *addrs;
    char port_str[16] = {};

    hints.ai_family = AF_INET; // Since your original code was using sockaddr_in and
                               // PF_INET, I'm using AF_INET here to match.  Use
                               // AF_UNSPEC instead if you want to allow getaddrinfo()
                               // to find both IPv4 and IPv6 addresses for the hostname.
                               // Just make sure the rest of your code is equally family-
                               // agnostic when dealing with the IP addresses associated
                               // with this connection. For instance, make sure any uses
                               // of sockaddr_in are changed to sockaddr_storage,
                               // and pay attention to its ss_family field, etc...
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    sprintf(port_str, "%d", port);

    err = getaddrinfo(hostname, port_str, &hints, &addrs);
    if (err != 0)
    {
        fprintf(stderr, "%s: %s\n", hostname, gai_strerror(err));
    }

    for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next)
    {
        sd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (sd == -1)
        {
            err = errno;
            break; // if using AF_UNSPEC above instead of AF_INET/6 specifically,
                   // replace this 'break' with 'continue' instead, as the 'ai_family'
                   // may be different on the next iteration...
        }

        if (connect(sd, addr->ai_addr, addr->ai_addrlen) == 0)
            break;

        err = errno;

        close(sd);
        sd = -1;
    }

    freeaddrinfo(addrs);

    if (sd == -1)
    {
        fprintf(stderr, "%s: %s\n", hostname, strerror(err));
    }

    return sd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movl %esi, %edx
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
leaq 0x10(%rsp), %r12
movaps %xmm0, (%r12)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%r14)
movl $0x6, 0xc(%r14)
leaq 0xfbfe(%rip), %rsi      # 0x2cf44
movq %r12, %rdi
xorl %eax, %eax
callq 0x2050
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2340
movl %eax, %r14d
testl %eax, %eax
jne 0x1d3e5
pushq $-0x1
popq %rbp
movq (%r15), %r15
testq %r15, %r15
je 0x1d3c7
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x1d3ba
movl %eax, %r12d
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x2200
testl %eax, %eax
je 0x1d3c4
callq 0x2030
movl (%rax), %r14d
movl %r12d, %edi
callq 0x2550
addq $0x28, %r15
jmp 0x1d370
callq 0x2030
movl (%rax), %r14d
jmp 0x1d3c7
movl %r12d, %ebp
movq 0x8(%rsp), %rdi
callq 0x2360
cmpl $-0x1, %ebp
je 0x1d413
movl %ebp, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18be4(%rip), %rax     # 0x35fd0
movq (%rax), %r12
movl %r14d, %edi
callq 0x23b0
leaq 0xfb49(%rip), %rsi      # 0x2cf47
movq %r12, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23f0
jmp 0x1d36d
movq 0x18bb6(%rip), %rax     # 0x35fd0
movq (%rax), %r15
movl %r14d, %edi
callq 0x20c0
leaq 0xfb1b(%rip), %rsi      # 0x2cf47
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23f0
jmp 0x1d3d6