// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2130
movl $0x0, -0x3c(%rbp)
movl $0x1, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x20c0
movq %rax, %rdi
leaq 0x4b34(%rip), %rsi      # 0x7018
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2290
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x2510
leaq 0x4b1d(%rip), %rdi      # 0x701e
callq 0x26e0
movl $0x1, %edi
callq 0x2270
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x2689
jmp 0x2525
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x2060
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
jne 0x254d
jmp 0x2678
movl -0x50(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2d80
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2da0
movl -0x74(%rbp), %edi
movq -0x70(%rbp), %rsi
movq %rax, %rdx
movq -0x58(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x58(%rbp), %rax
movl 0x10(%rax), %r9d
movl $0x20000000, %ecx       # imm = 0x20000000
callq 0x2040
movq %rax, -0x60(%rbp)
cmpq $-0x1, -0x60(%rbp)
jne 0x25b3
callq 0x2030
cmpl $0x4, (%rax)
jne 0x25b3
movl -0x50(%rbp), %edi
callq 0x2340
jmp 0x2525
cmpq $-0x1, -0x60(%rbp)
je 0x25bf
jmp 0x2689
movl -0x50(%rbp), %edi
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x58(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x21b0
cltq
movq %rax, -0x60(%rbp)
cmpq $-0x1, -0x60(%rbp)
jne 0x25fa
callq 0x2030
cmpl $0x4, (%rax)
jne 0x25fa
movl -0x50(%rbp), %edi
callq 0x2340
jmp 0x2525
cmpq $-0x1, -0x60(%rbp)
je 0x2670
jmp 0x2603
movl -0x50(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2d80
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2da0
movl -0x84(%rbp), %edi
movq -0x80(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2110
movq %rax, -0x68(%rbp)
xorl %eax, %eax
cmpq $-0x1, -0x68(%rbp)
movb %al, -0x85(%rbp)
jne 0x265b
cmpq $0x4, -0x68(%rbp)
sete %al
movb %al, -0x85(%rbp)
movb -0x85(%rbp), %al
testb $0x1, %al
jne 0x2603
cmpq $-0x1, -0x60(%rbp)
je 0x266e
jmp 0x2689
jmp 0x2670
movl -0x50(%rbp), %edi
callq 0x2340
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x2518
movq -0x48(%rbp), %rdi
callq 0x22a0
cmpq $0x0, -0x58(%rbp)
jne 0x26c5
movq 0x8950(%rip), %rdi      # 0xaff0
leaq 0x4983(%rip), %rsi      # 0x702a
callq 0x21f0
movq %rax, %rdi
movq 0x8902(%rip), %rsi      # 0xafb8
callq 0x2220
movl $0x1, %edi
callq 0x2270
movl -0x50(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)