// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x58, %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), %r15
movaps %xmm0, (%r15)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%r14)
movl $0x6, 0xc(%r14)
leaq 0x11bf5(%rip), %rsi     # 0x32114
movq %r15, %rdi
xorl %eax, %eax
callq 0x2050
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2330
movl %eax, %ebp
testl %eax, %eax
jne 0x205a9
movq 0x8(%rsp), %r15
testq %r15, %r15
je 0x205dd
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x205d6
movl %eax, %r14d
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x2200
testl %eax, %eax
je 0x20591
callq 0x2030
movl (%rax), %ebp
movl %r14d, %edi
callq 0x2520
movq 0x28(%r15), %r15
jmp 0x20547
movq 0x8(%rsp), %rdi
callq 0x2340
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x18a20(%rip), %rax     # 0x38fd0
movq (%rax), %r14
movl %ebp, %edi
callq 0x2370
leaq 0x11b56(%rip), %rsi     # 0x32117
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23b0
jmp 0x20542
callq 0x2030
movl (%rax), %ebp
movq 0x8(%rsp), %rdi
callq 0x2340
movq 0x189e2(%rip), %rax     # 0x38fd0
movq (%rax), %r14
movl %ebp, %edi
callq 0x20c0
leaq 0x11b18(%rip), %rsi     # 0x32117
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23b0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x2059b