// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
movaps %xmm0, (%r15)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%r14)
movl $0x6, 0xc(%r14)
leaq 0x11a8f(%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 0x2072f
movq 0x8(%rsp), %r12
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x2070f
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x20703
movl %eax, %r15d
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %eax, %edi
callq 0x2200
testl %eax, %eax
je 0x2070c
callq 0x2030
movl (%rax), %ebp
movl %r15d, %edi
callq 0x2520
movq 0x28(%r12), %r12
jmp 0x206b7
callq 0x2030
movl (%rax), %ebp
jmp 0x2070f
movl %r15d, %r14d
movq 0x8(%rsp), %rdi
callq 0x2340
cmpl $-0x1, %r14d
je 0x2075c
movl %r14d, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x1889a(%rip), %rax     # 0x38fd0
movq (%rax), %r14
movl %ebp, %edi
callq 0x2370
leaq 0x119d0(%rip), %rsi     # 0x32117
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23b0
jmp 0x206ac
movq 0x1886d(%rip), %rax     # 0x38fd0
movq (%rax), %r15
movl %ebp, %edi
callq 0x20c0
leaq 0x119a3(%rip), %rsi     # 0x32117
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23b0
jmp 0x2071f