// File: /workspace/github_cpp_makefile_star2_O2/cybozu[P]yrmcds/cybozu/tcp.cpp
// Function: cybozu::tcp_connect(char const*, unsigned short, unsigned int)
int tcp_connect(const char* node, std::uint16_t port, unsigned int timeout) {
    std::string s_port = std::to_string(port);

    struct addrinfo hint, *res;
    std::memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_INET;  // prefer IPv4
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG;
    int e = getaddrinfo(node, s_port.c_str(), &hint, &res);
    if( e == EAI_FAMILY || e == EAI_ADDRFAMILY ||
        e == EAI_NODATA || e == EAI_NONAME ) {
        hint.ai_family = AF_INET6;
        // intentionally drop AI_ADDRCONFIG to support IPv6 link-local address.
        // see https://github.com/cybozu/yrmcds/issues/40
        hint.ai_flags = AI_NUMERICSERV|AI_V4MAPPED;
        e = getaddrinfo(node, s_port.c_str(), &hint, &res);
    }
    if( e == EAI_SYSTEM ) {
        throw_unix_error(errno, "getaddrinfo");
    } else if( e != 0 ) {
        throw std::runtime_error(std::string("getaddrinfo: ") +
                                 gai_strerror(e));
    }

    int s = ::socket(res->ai_family,
                     res->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC,
                     res->ai_protocol);
    if( s == -1 ) {
        freeaddrinfo(res);
        throw_unix_error(errno, "socket");
    }
    e = ::connect(s, res->ai_addr, res->ai_addrlen);
    freeaddrinfo(res);
    if( e == 0 ) return s;
    if( errno != EINPROGRESS ) {
        ::close(s);
        throw_unix_error(errno, "connect");
    }

    struct pollfd fds;
    fds.fd = s;
    fds.events = POLLOUT;
    int poll_timeout = timeout ? timeout*1000 : -1;
    int n = ::poll(&fds, 1, poll_timeout);
    if( n == 0 ) { // timeout
        ::close(s);
        return -1;
    }
    if( n == -1 ) {
        ::close(s);
        throw_unix_error(errno, "poll");
    }

    if( fds.revents & (POLLERR|POLLHUP|POLLNVAL) ) {
        ::close(s);
        return -1;
    }
    socklen_t l = sizeof(e);
    if( getsockopt(s, SOL_SOCKET, SO_ERROR, &e, &l) == -1 ) {
        ::close(s);
        throw_unix_error(errno, "getsockopt(SO_ERROR)");
    }
    if( e != 0 ) {
        ::close(s);
        return -1;
    }
    return s;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xa0, %rsp
movl %edx, %ebp
movq %rdi, %rbx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0xd28d
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdx
movups %xmm0, 0x8(%rdx)
movups %xmm0, 0x18(%rdx)
andq $0x0, 0x28(%rdx)
movl $0x1, 0x8(%rdx)
movabsq $0x200000420, %rax      # imm = 0x200000420
movq %rax, (%rdx)
movq (%r14), %rsi
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
callq 0x7640
movl %eax, %r14d
addl $0x9, %eax
cmpl $0x7, %eax
ja 0x1bada
movl $0x99, %ecx
btl %eax, %ecx
jae 0x1bada
movabsq $0xa00000408, %rax      # imm = 0xA00000408
leaq 0x70(%rsp), %rdx
movq %rax, (%rdx)
movq 0x50(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
callq 0x7640
movl %eax, %r14d
testl %r14d, %r14d
jne 0x1bbd1
movq 0x8(%rsp), %rax
movl $0x80800, %esi          # imm = 0x80800
orl 0x8(%rax), %esi
movl 0x4(%rax), %edi
movl 0xc(%rax), %edx
callq 0x70c0
movq 0x8(%rsp), %rdi
cmpl $-0x1, %eax
je 0x1bc4d
movl %eax, %ebx
movq 0x18(%rdi), %rsi
movl 0x10(%rdi), %edx
movl %eax, %edi
callq 0x7430
movl %eax, %r14d
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rdi
callq 0x7680
testl %r14d, %r14d
je 0x1bb82
callq 0x7030
movq %rax, %r14
cmpl $0x73, (%rax)
jne 0x1bc63
leaq 0x10(%rsp), %rdi
movl %ebx, (%rdi)
movw $0x4, 0x4(%rdi)
imull $0x3e8, %ebp, %eax      # imm = 0x3E8
xorl %edx, %edx
cmpl $0x1, %ebp
sbbl %edx, %edx
orl %eax, %edx
pushq $0x1
popq %rsi
callq 0x7810
testl %eax, %eax
je 0x1bb78
cmpl $-0x1, %eax
je 0x1bc7c
testb $0x38, 0x16(%rsp)
je 0x1bb9a
movl %ebx, %edi
callq 0x7a90
pushq $-0x1
popq %rbx
leaq 0x50(%rsp), %rdi
callq 0x71d0
movl %ebx, %eax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x30(%rsp), %r8
movl $0x4, (%r8)
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
leaq 0x4(%rsp), %rcx
movl %ebx, %edi
callq 0x73d0
cmpl $-0x1, %eax
je 0x1bc92
cmpl $0x0, 0x4(%rsp)
je 0x1bb82
movl %ebx, %edi
callq 0x7a90
jmp 0x1bb7f
cmpl $-0xb, %r14d
jne 0x1bbea
callq 0x7030
movl (%rax), %edi
leaq 0xca2e(%rip), %rsi      # 0x28613
callq 0x8513
pushq $0x10
popq %rdi
callq 0x7240
movq %rax, %rbx
leaq 0xca23(%rip), %rsi      # 0x2861f
leaq 0x30(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x84d8
movl %r14d, %edi
callq 0x7750
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0x9636
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x78c0
xorl %ebp, %ebp
movq 0x1f3b2(%rip), %rsi     # 0x3aff0
movq 0x1f32b(%rip), %rdx     # 0x3af70
movq %rbx, %rdi
callq 0x7950
callq 0x7680
leaq 0xc9d4(%rip), %rbx      # 0x2862d
callq 0x7030
movq %rax, %r14
jmp 0x1bc71
movl %ebx, %edi
callq 0x7a90
leaq 0xc9c3(%rip), %rbx      # 0x28634
movl (%r14), %edi
movq %rbx, %rsi
callq 0x8513
movl %ebx, %edi
callq 0x7a90
movl (%r14), %edi
leaq 0xc9af(%rip), %rsi      # 0x2863c
callq 0x8513
movl %ebx, %edi
callq 0x7a90
movl (%r14), %edi
leaq 0xc99e(%rip), %rsi      # 0x28641
callq 0x8513
jmp 0x1bce1
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x71d0
jmp 0x1bcbf
movq %rax, %r14
movb $0x1, %bpl
leaq 0x30(%rsp), %rdi
callq 0x71d0
testb %bpl, %bpl
jne 0x1bcd3
jmp 0x1bce4
movq %rax, %r14
movq %rbx, %rdi
callq 0x7390
jmp 0x1bce4
jmp 0x1bce1
jmp 0x1bce1
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x71d0
movq %r14, %rdi
callq 0x7990