// File: /workspace/github_cpp_makefile_star2_O3/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 0x1f8d3
movl $0x2, %r15d
cmpw $0x64, %bp
jb 0x1f8d3
movzwl %bp, %eax
movl $0x3, %r15d
cmpl $0x3e8, %eax            # imm = 0x3E8
jb 0x1f8d3
cmpl $0x2710, %eax           # imm = 0x2710
movl $0x5, %r15d
sbbl $0x0, %r15d
movl %r15d, %esi
leaq 0x68(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x7580
movq (%r12), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xea9e
xorps %xmm0, %xmm0
leaq 0x78(%rsp), %rdx
movups %xmm0, 0x8(%rdx)
movups %xmm0, 0x18(%rdx)
movq $0x0, 0x28(%rdx)
movl $0x1, 0x8(%rdx)
movabsq $0x200000420, %rax      # imm = 0x200000420
movq %rax, (%rdx)
movq (%r12), %rsi
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
callq 0x7630
movl %eax, %ebp
addl $0x9, %eax
cmpl $0x7, %eax
ja 0x1f976
movl $0x99, %ecx
btl %eax, %ecx
jae 0x1f976
movabsq $0xa00000408, %rax      # imm = 0xA00000408
leaq 0x78(%rsp), %rdx
movq %rax, (%rdx)
movq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
callq 0x7630
movl %eax, %ebp
testl %ebp, %ebp
jne 0x1fa90
movq 0x10(%rsp), %rax
movl $0x80800, %esi          # imm = 0x80800
orl 0x8(%rax), %esi
movl 0x4(%rax), %edi
movl 0xc(%rax), %edx
callq 0x70b0
movq 0x10(%rsp), %rdi
cmpl $-0x1, %eax
je 0x1fb0d
movl %eax, %ebp
movq 0x18(%rdi), %rsi
movl 0x10(%rdi), %edx
movl %eax, %edi
callq 0x7410
movl %eax, %r14d
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rdi
callq 0x7660
testl %r14d, %r14d
je 0x1fa23
callq 0x7040
movq %rax, %r14
cmpl $0x73, (%rax)
jne 0x1fb23
leaq 0x38(%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 0x7800
testl %eax, %eax
je 0x1fa15
cmpl $-0x1, %eax
je 0x1fb3c
testb $0x38, 0x3e(%rsp)
je 0x1fa4e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebp, %edi
callq 0x7a80
movl %ebx, %ebp
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x1fa3a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x7500
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rsp), %r8
movl $0x4, (%r8)
leaq 0xc(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x73b0
cmpl $-0x1, %eax
je 0x1fb9e
cmpl $0x0, 0xc(%rsp)
je 0x1fa8c
movl %ebp, %edi
callq 0x7a80
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %ebx
jmp 0x1fa21
cmpl $-0xb, %ebp
jne 0x1faa8
callq 0x7040
movl (%rax), %edi
leaq 0x16be0(%rip), %rsi     # 0x36683
callq 0x8d8d
movl $0x10, %edi
callq 0x7220
movq %rax, %rbx
leaq 0x28(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x16bca(%rip), %rsi     # 0x3668f
leaq 0x16bd0(%rip), %rdx     # 0x3669c
leaq 0x18(%rsp), %rdi
callq 0x9760
movl %ebp, %edi
callq 0x7730
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x7a70
leaq 0x48(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1fb52
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x1fb5f
callq 0x7660
leaq 0x16b84(%rip), %rbx     # 0x3669d
callq 0x7040
movq %rax, %r14
jmp 0x1fb31
movl %ebp, %edi
callq 0x7a80
leaq 0x16b73(%rip), %rbx     # 0x366a4
movl (%r14), %edi
movq %rbx, %rsi
callq 0x8d8d
movl %ebp, %edi
callq 0x7a80
movl (%r14), %edi
leaq 0x16b5f(%rip), %rsi     # 0x366ac
callq 0x8d8d
movq %rdx, 0x38(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x48(%rsp)
movq 0x8(%rax), %rdx
leaq 0x38(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x78b0
xorl %ebp, %ebp
movq 0x26461(%rip), %rsi     # 0x45ff0
movq 0x263da(%rip), %rdx     # 0x45f70
movq %rbx, %rdi
callq 0x7960
movl %ebp, %edi
callq 0x7a80
movl (%r14), %edi
leaq 0x16b02(%rip), %rsi     # 0x366b1
callq 0x8d8d
jmp 0x1fc1b
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %r12, %rdi
je 0x1fbd0
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x7500
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x1fbe7
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7500
testb %bpl, %bpl
jne 0x1fc0d
jmp 0x1fc1e
movq %rax, %r14
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x1fc0d
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7500
jmp 0x1fc0d
movq %rax, %r14
movq %rbx, %rdi
callq 0x7370
jmp 0x1fc1e
jmp 0x1fc1b
jmp 0x1fc1b
movq %rax, %r14
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x1fc35
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x7500
movq %r14, %rdi
callq 0x79a0
nop