// File: hzeller[P]gcode-cli/machine-connection.cc
// Function: OpenTCPSocket(char const*)
int OpenTCPSocket(const char *host) {
    char *host_copy = NULL;
    const char *port = "8888";
    const char *colon_pos;
    if ((colon_pos = strchr(host, ':')) != NULL) {
        port = colon_pos + 1;
        host_copy = strdup(host);
        host_copy[colon_pos - host] = '\0';
        host = host_copy;
    }
    struct addrinfo addr_hints = {};
    addr_hints.ai_family = AF_INET;
    addr_hints.ai_socktype = SOCK_STREAM;

    struct addrinfo *addr_result = NULL;
    int rc;
    if ((rc = getaddrinfo(host, port, &addr_hints, &addr_result)) != 0) {
        // We're in OpenTCPSocket(), because opening as a tty failed before,
        // so make reference of that in this error message.
        fprintf(stderr,
                "Not a tty and "
                "can't resolve as TCP endpoint '%s' (port %s): %s\n",
                host, port, gai_strerror(rc));
        free(host_copy);
        return -1;
    }
    free(host_copy);
    if (addr_result == NULL) return -1;
    int fd = socket(addr_result->ai_family, addr_result->ai_socktype,
                    addr_result->ai_protocol);
    if (fd >= 0 &&
        connect(fd, addr_result->ai_addr, addr_result->ai_addrlen) < 0) {
        perror("TCP connect()");
        close(fd);
        fd = -1;
    }

    freeaddrinfo(addr_result);
    return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
leaq 0x3aea(%rip), %rax      # 0x7b78
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl $0x3a, %esi
callq 0x20f0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x40de
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2430
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rdx
subq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2170
movl $0x2, -0x54(%rbp)
movl $0x1, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x22e0
movl %eax, -0x64(%rbp)
cmpl $0x0, %eax
je 0x4179
movq 0x6eb0(%rip), %rax      # 0xafd8
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x64(%rbp), %edi
callq 0x2320
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq %rax, %r8
leaq 0x3a20(%rip), %rsi      # 0x7b7d
movb $0x0, %al
callq 0x2360
movq -0x18(%rbp), %rdi
callq 0x22b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41fb
movq -0x18(%rbp), %rdi
callq 0x22b0
cmpq $0x0, -0x60(%rbp)
jne 0x4192
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41fb
movq -0x60(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x60(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x60(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x2090
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jl 0x41ec
movl -0x68(%rbp), %edi
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x60(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x21f0
cmpl $0x0, %eax
jge 0x41ec
leaq 0x39e5(%rip), %rdi      # 0x7bbd
callq 0x2200
movl -0x68(%rbp), %edi
callq 0x2450
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movq -0x60(%rbp), %rdi
callq 0x2310
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)