// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
movl $0x1, %r15d
cmpw $0xa, %bp
jb 0x20103
movl $0x2, %r15d
cmpw $0x64, %bp
jb 0x20103
movzwl %bp, %eax
movl $0x3, %r15d
cmpl $0x3e8, %eax            # imm = 0x3E8
jb 0x20103
cmpl $0x2710, %eax           # imm = 0x2710
movl $0x5, %r15d
sbbl $0x0, %r15d
movl %r15d, %esi
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x7590
movq (%r12), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xeac2
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%rdx)
movl $0x420, (%rdx)          # imm = 0x420
movq (%r12), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
callq 0x7640
movl %eax, 0x4(%rsp)
addl $0x9, %eax
cmpl $0x7, %eax
ja 0x201a5
movl $0x99, %ecx
btl %eax, %ecx
jae 0x201a5
movabsq $0xa00000408, %rax      # imm = 0xA00000408
leaq 0x70(%rsp), %rdx
movq %rax, (%rdx)
movq 0x30(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
callq 0x7640
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
testl %eax, %eax
jne 0x202c2
movq 0x8(%rsp), %rax
movl $0x80800, %esi          # imm = 0x80800
orl 0x8(%rax), %esi
movl 0x4(%rax), %edi
movl 0xc(%rax), %edx
callq 0x70b0
movq 0x8(%rsp), %rdi
cmpl $-0x1, %eax
je 0x20341
movl %eax, %ebp
movq 0x18(%rdi), %rsi
movl 0x10(%rdi), %edx
movl %eax, %edi
callq 0x7420
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rdi
callq 0x7670
cmpl $0x0, 0x4(%rsp)
je 0x20255
callq 0x7040
movq %rax, %r14
cmpl $0x73, (%rax)
jne 0x20357
leaq 0x10(%rsp), %rdi
movl %ebp, (%rdi)
movw $0x4, 0x4(%rdi)
imull $0x3e8, %ebx, %eax      # imm = 0x3E8
xorl %edx, %edx
cmpl $0x1, %ebx
sbbl %edx, %edx
orl %eax, %edx
movl $0x1, %esi
callq 0x7810
testl %eax, %eax
je 0x20247
cmpl $-0x1, %eax
je 0x20370
testb $0x38, 0x16(%rsp)
je 0x20280
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebp, %edi
callq 0x7a90
movl %ebx, %ebp
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x2026c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x7510
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50(%rsp), %r8
movl $0x4, (%r8)
leaq 0x4(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x73b0
cmpl $-0x1, %eax
je 0x203d2
cmpl $0x0, 0x4(%rsp)
je 0x202be
movl %ebp, %edi
callq 0x7a90
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %ebx
jmp 0x20253
cmpl $-0xb, %eax
jne 0x202da
callq 0x7040
movl (%rax), %edi
leaq 0x163e8(%rip), %rsi     # 0x366bd
callq 0x8dd3
movl $0x10, %edi
callq 0x7220
movq %rax, %rbx
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x163d2(%rip), %rsi     # 0x366c9
leaq 0x163d8(%rip), %rdx     # 0x366d6
leaq 0x50(%rsp), %rdi
callq 0x97a0
movl 0x4(%rsp), %edi
callq 0x7740
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x7a80
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x20386
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x20393
callq 0x7670
leaq 0x1638a(%rip), %rbx     # 0x366d7
callq 0x7040
movq %rax, %r14
jmp 0x20365
movl %ebp, %edi
callq 0x7a90
leaq 0x16379(%rip), %rbx     # 0x366de
movl (%r14), %edi
movq %rbx, %rsi
callq 0x8dd3
movl %ebp, %edi
callq 0x7a90
movl (%r14), %edi
leaq 0x16365(%rip), %rsi     # 0x366e6
callq 0x8dd3
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x8(%rax), %rdx
leaq 0x10(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x78c0
xorl %ebp, %ebp
movq 0x25c2d(%rip), %rsi     # 0x45ff0
movq 0x25ba6(%rip), %rdx     # 0x45f70
movq %rbx, %rdi
callq 0x7970
movl %ebp, %edi
callq 0x7a90
movl (%r14), %edi
leaq 0x16308(%rip), %rsi     # 0x366eb
callq 0x8dd3
jmp 0x2043e
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x2040c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x2040c
movq %rax, %r14
movb $0x1, %bpl
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x2042b
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x2042b
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x20441
movq %rbx, %rdi
callq 0x7370
jmp 0x20441
jmp 0x2043e
jmp 0x2043e
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x20458
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x7510
movq %r14, %rdi
callq 0x79b0