// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movl %esi, 0x8c(%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2170
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2170
movl $0x2, 0x54(%rsp)
movl $0x1, 0x58(%rsp)
movl $0x6, 0x5c(%rsp)
leaq 0x30(%rsp), %rdi
movl 0x8c(%rsp), %edx
leaq 0x2bea1(%rip), %rsi     # 0x61de4
movb $0x0, %al
callq 0x2060
movq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
callq 0x2340
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
je 0x35fba
movq 0x38052(%rip), %rax     # 0x6dfd0
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x84(%rsp), %edi
callq 0x23b0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
leaq 0x2be34(%rip), %rsi     # 0x61de7
movb $0x0, %al
callq 0x23e0
movq 0x48(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
je 0x3606a
movq 0x28(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x28(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x28(%rsp), %rax
movl 0xc(%rax), %edx
callq 0x2080
movl %eax, 0x88(%rsp)
cmpl $-0x1, 0x88(%rsp)
jne 0x3600e
callq 0x2030
movl (%rax), %eax
movl %eax, 0x84(%rsp)
jmp 0x3606a
movl 0x88(%rsp), %edi
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x28(%rsp), %rax
movl 0x10(%rax), %edx
callq 0x2230
cmpl $0x0, %eax
jne 0x36032
jmp 0x3606a
callq 0x2030
movl (%rax), %eax
movl %eax, 0x84(%rsp)
movl 0x88(%rsp), %edi
callq 0x2540
movl $0xffffffff, 0x88(%rsp) # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x28(%rsp)
jmp 0x35fc4
movq 0x48(%rsp), %rdi
callq 0x2350
cmpl $-0x1, 0x88(%rsp)
jne 0x360c1
movq 0x37f4b(%rip), %rax     # 0x6dfd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x84(%rsp), %edi
callq 0x20d0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %rax, %rcx
leaq 0x2bd2d(%rip), %rsi     # 0x61de7
movb $0x0, %al
callq 0x23e0
movl 0x88(%rsp), %eax
addq $0x98, %rsp
retq