// File: /workspace/github_cpp_makefile_star2_O1/yuryu[P]tfoecho/client.cpp
// Function: getsock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<char, std::allocator<char>> const&)
int getsock(const std::string &server_addr, const std::vector<char> &msg_to_send)
{
    struct addrinfo hint, *res;
    
    std::memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_UNSPEC;
    hint.ai_socktype = SOCK_STREAM;
    
    int e = getaddrinfo(server_addr.c_str(), SERVER_PORT, &hint, &res);
    if( e == -1 )
    {
        cerror("getaddrinfo");
        std::exit(EXIT_FAILURE);
    }
    int sock;
    struct addrinfo *rp; 
    for( rp = res; rp != nullptr; rp = rp->ai_next )
    {
        // Need to create the socket again when EINTR is returned
redo:
        sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if( sock == -1 )
            continue;
        ssize_t r = sendto(sock, &msg_to_send[0], msg_to_send.size(),
                MSG_FASTOPEN, rp->ai_addr, rp->ai_addrlen);
        if( r == -1 && errno == EINTR )
        {
            close(sock);
            goto redo;
        }
        // Exit the loop when sendto succeeded
        if( r != -1 )
            break;
        // Try without TCP FastOpen
        r = connect(sock, rp->ai_addr, rp->ai_addrlen);
        if( r == -1 && errno == EINTR )
        {
            close(sock);
            goto redo;
        }
        if( r != -1 )
        {
            ssize_t sent;
            do
            {
                sent = send(sock, &msg_to_send[0], msg_to_send.size(), 0);
            } while( sent == -1 && sent == EINTR );
            // Success
            if( r != -1 )
                break;
        }
        close(sock);
    }
    freeaddrinfo(res);
    if( rp == nullptr )
    {
        std::cerr << "Couldn't get a socket." << std::endl;
        std::exit(EXIT_FAILURE);
    }
    return sock;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, 0x8(%rdx)
movq (%rdi), %rdi
leaq 0x1bf3(%rip), %rsi      # 0x4004
leaq 0x8(%rsp), %rcx
callq 0x2200
cmpl $-0x1, %eax
je 0x2524
movq 0x8(%rsp), %r12
testq %r12, %r12
sete %r15b
je 0x2503
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x2060
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x24ef
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
movq 0x18(%r12), %r8
movl 0x10(%r12), %r9d
movl %ebp, %edi
movl $0x20000000, %ecx       # imm = 0x20000000
callq 0x2050
movl $0x2, %r13d
cmpq $-0x1, %rax
jne 0x24df
callq 0x2040
cmpl $0x4, (%rax)
jne 0x249a
movl %ebp, %edi
callq 0x2290
movl $0x5, %r13d
jmp 0x24df
movq %rax, %r14
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %ebp, %edi
callq 0x2160
cmpl $-0x1, %eax
je 0x24c8
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x20f0
jmp 0x24df
xorl %r13d, %r13d
cmpl $0x4, (%r14)
sete %r13b
movl %ebp, %edi
callq 0x2290
leal (%r13,%r13,4), %r13d
cmpl $0x5, %r13d
je 0x2436
cmpl $0x2, %r13d
je 0x2503
movq 0x28(%r12), %r12
testq %r12, %r12
sete %r15b
jne 0x2436
jmp 0x2503
movq 0x8(%rsp), %rdi
callq 0x2210
testb $0x1, %r15b
jne 0x2532
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1adf(%rip), %rdi      # 0x400a
callq 0x2557
jmp 0x254d
movq 0x3ab7(%rip), %rdi      # 0x5ff0
leaq 0x1ad6(%rip), %rsi      # 0x4016
callq 0x21a0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x21f0