// File: /workspace/github_cpp_makefile_star2_O2/brianbbsu[P]TCP-Ping/src/client.cpp
// Function: pinger(target&, int, int, int, int)
void pinger(target &tg, int target_id, int num, int tout, int twait){
    std::string domain = tg.host;
    std::string ip = domain_to_ip(domain);
    if(ip == "")return logger::log_error("Cannot resolve host: " + domain, target_id);
    else tg.ip = ip;
    logger::log_info("Start pinging " + tg.to_string(0) + " (" + ip + ")", target_id);

    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(tg.port);
    addr.sin_addr.s_addr = inet_addr(tg.ip.c_str());

    while(1)
    {
        int fd;
        if((fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)return logger::log_error("Failed to creat socket!", target_id);

        fd_set write_fd;
        FD_ZERO(&write_fd);
        FD_SET(fd, &write_fd);
        timeval timeout;
        timeout.tv_sec = (tout / 1000);
        timeout.tv_usec = (tout % 1000) * 1000;

        auto start = std::chrono::steady_clock::now();
        int con_ret = connect(fd, (struct sockaddr*) &addr, sizeof addr);
        tg.ping_send += 1;
        if(con_ret != 0 && errno != EINPROGRESS)
            return close_socket(fd), logger::log_error("Failed to connect to target '" + tg.to_string() + "'", target_id);
        int sel_ret = select(fd + 1, NULL, &write_fd, NULL, &timeout);
        auto end = std::chrono::steady_clock::now();
        
        if(sel_ret == -1)
            return close_socket(fd), logger::log_error("Error occurs when connecting to target '" + tg.to_string() + "'", target_id);
        
        double delta = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.0;
        if(sel_ret == 0 || delta > double(tout))logger::log_info("timeout when connecting to " + tg.to_string(), target_id);
        else{
            char delta_str[20];
            memset(delta_str, 0, sizeof(delta_str));
            sprintf(delta_str, "%0.2f", delta);
            logger::log_info("recv from " + tg.to_string() + ", RTT = " + std::string(delta_str) + " msec", target_id);
            tg.pong_recv += 1;
            tg.rtt_tt += delta;
        }

        close_socket(fd);

        if(num == 1)break;
        else if(num > 1)num --;
        std::this_thread::sleep_for(std::chrono::milliseconds(twait));
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movl %r8d, %ebx
movl %ecx, %r15d
movl %edx, 0x38(%rsp)
movl %esi, 0x8(%rsp)
movq %rdi, %r14
leaq 0x1a8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x64a0
leaq 0x188(%rsp), %rdi
movq %r12, %rsi
callq 0x64a0
leaq 0xc8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x6a7c
leaq 0x188(%rsp), %rdi
callq 0x66b8
leaq 0x7252(%rip), %rsi      # 0xe224
leaq 0xc8(%rsp), %rdi
callq 0x81ce
testb %al, %al
je 0x701d
leaq 0x70f1(%rip), %rsi      # 0xe0db
leaq 0x168(%rsp), %rdi
leaq 0x1a8(%rsp), %rdx
callq 0x80a0
leaq 0x168(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0xd0b2
leaq 0x168(%rsp), %rdi
jmp 0x7516
leaq 0x28(%r14), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x61a0
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x81dc
leaq 0x70aa(%rip), %rsi      # 0xe0f1
leaq 0x40(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x8151
leaq 0x70a0(%rip), %rdx      # 0xe100
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x8081
leaq 0x258(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x8132
leaq 0x7697(%rip), %rdx      # 0xe727
leaq 0x148(%rsp), %rdi
leaq 0x258(%rsp), %rsi
callq 0x8081
leaq 0x148(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0xcfd0
leaq 0x148(%rsp), %rdi
callq 0x66b8
leaq 0x258(%rsp), %rdi
callq 0x66b8
leaq 0x18(%rsp), %rdi
callq 0x66b8
leaq 0x40(%rsp), %rdi
callq 0x66b8
leaq 0xa8(%rsp), %r13
movq %r13, %rdi
callq 0x66b8
leaq 0x1c8(%rsp), %r12
movw $0x2, (%r12)
movzwl 0x20(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r12)
movq %r14, 0x10(%rsp)
movq 0x28(%r14), %rdi
callq 0x6340
movl %eax, 0x4(%r12)
movl $0x3e8, %ecx            # imm = 0x3E8
movl %r15d, %eax
cltd
idivl %ecx
cltq
movq %rax, 0x78(%rsp)
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltq
movq %rax, 0x70(%rsp)
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x60(%rsp)
movslq %ebx, %rax
movq %rax, 0x68(%rsp)
leaq 0x1f8(%rsp), %r12
movl 0x38(%rsp), %r14d
pushq $0x2
popq %rdi
movl $0x801, %esi            # imm = 0x801
xorl %edx, %edx
callq 0x60c0
testl %eax, %eax
js 0x7402
movl %eax, %r15d
xorl %eax, %eax
cmpq $0x10, %rax
je 0x7194
andq $0x0, 0x258(%rsp,%rax,8)
incq %rax
jmp 0x7180
pushq $0x1
popq %rax
movl %r15d, %ecx
shlq %cl, %rax
movl %r15d, %ecx
shrl $0x6, %ecx
orq %rax, 0x258(%rsp,%rcx,8)
movq 0x78(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0xa0(%rsp)
callq 0x6470
movq %rax, %rbx
movl %r15d, %edi
leaq 0x1c8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x62b0
movq 0x10(%rsp), %rcx
incl 0x48(%rcx)
testl %eax, %eax
je 0x71fa
callq 0x6040
cmpl $0x73, (%rax)
jne 0x74a0
movq %r12, %rbp
movq %r13, %r12
movl %r15d, %edi
incl %edi
xorl %esi, %esi
leaq 0x258(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x98(%rsp), %r8
callq 0x6310
movl %eax, %r13d
callq 0x6470
cmpl $-0x1, %r13d
je 0x743c
subq %rbx, %rax
cqto
movl $0x3e8, %ecx            # imm = 0x3E8
idivq %rcx
cvtsi2sd %rax, %xmm1
divsd 0x6dd6(%rip), %xmm1     # 0xe020
testl %r13d, %r13d
je 0x737c
ucomisd 0x60(%rsp), %xmm1
ja 0x737c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
andl $0x0, 0x90(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x6efb(%rip), %rsi      # 0xe17e
movsd %xmm1, 0x38(%rsp)
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x6080
movq %rbp, %r13
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x81dc
leaq 0x18(%rsp), %rbx
movq %r12, %rdi
leaq 0x6ece(%rip), %rsi      # 0xe184
movq %r13, %rdx
callq 0x8151
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
leaq 0x6ec2(%rip), %rdx      # 0xe18f
callq 0x8081
leaq 0x1d8(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
leaq 0xf(%rsp), %rdx
callq 0x8046
movq %rbp, %r12
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
callq 0x82a1
leaq 0x218(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x6e81(%rip), %rdx      # 0xe198
callq 0x8081
movq %rbp, %rdi
movl 0x8(%rsp), %esi
callq 0xcfd0
movq %rbp, %rdi
callq 0x66b8
movq %rbx, %rdi
callq 0x66b8
movq %r13, %rdi
callq 0x66b8
leaq 0x40(%rsp), %rdi
callq 0x66b8
leaq 0xa8(%rsp), %r13
movq %r13, %rdi
callq 0x66b8
movq %r12, %rdi
callq 0x66b8
movq 0x10(%rsp), %rax
incl 0x4c(%rax)
movsd 0x38(%rsp), %xmm0
addsd 0x50(%rax), %xmm0
movsd %xmm0, 0x50(%rax)
jmp 0x73cd
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x81dc
movq %r12, %r13
leaq 0x238(%rsp), %r12
movq %r12, %rdi
leaq 0x6dbc(%rip), %rsi      # 0xe162
movq %rbx, %rdx
callq 0x8151
movq %r12, %rdi
movl 0x8(%rsp), %esi
callq 0xcfd0
movq %r12, %rdi
callq 0x66b8
movq %rbx, %rdi
callq 0x66b8
movq %rbp, %r12
movl %r15d, %edi
callq 0x6f51
cmpl $0x1, %r14d
je 0x751b
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x8308
xorl %eax, %eax
cmpl $0x2, %r14d
setge %al
subl %eax, %r14d
jmp 0x7164
leaq 0x6cfa(%rip), %rsi      # 0xe103
leaq 0x128(%rsp), %rdi
leaq 0x258(%rsp), %rdx
callq 0x8046
leaq 0x128(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0xd0b2
leaq 0x128(%rsp), %rdi
jmp 0x7516
movl %r15d, %edi
callq 0x6f51
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rsi
callq 0x81dc
leaq 0x6cdc(%rip), %rsi      # 0xe139
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x8151
leaq 0x6bf5(%rip), %rdx      # 0xe068
leaq 0xe8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8081
leaq 0xe8(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0xd0b2
leaq 0xe8(%rsp), %rdi
jmp 0x7502
movl %r15d, %edi
callq 0x6f51
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rsi
callq 0x81dc
leaq 0x6c5a(%rip), %rsi      # 0xe11b
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x8151
leaq 0x6b91(%rip), %rdx      # 0xe068
leaq 0x108(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8081
leaq 0x108(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0xd0b2
leaq 0x108(%rsp), %rdi
callq 0x66b8
leaq 0x18(%rsp), %rdi
callq 0x66b8
leaq 0x40(%rsp), %rdi
callq 0x66b8
leaq 0xc8(%rsp), %rdi
callq 0x66b8
leaq 0x1a8(%rsp), %rdi
callq 0x66b8
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x756d
jmp 0x7574
jmp 0x7583
jmp 0x76bf
jmp 0x76bf
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x66b8
jmp 0x7577
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66b8
jmp 0x7586
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x76b2
jmp 0x76bf
jmp 0x76bf
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x76b2
jmp 0x76bf
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x66b8
jmp 0x75c4
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x66b8
jmp 0x75d6
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66b8
jmp 0x75e5
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x66b8
jmp 0x75f4
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x76b2
jmp 0x76bf
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x76b2
jmp 0x76bf
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0x76ca
movq %rax, %rbx
jmp 0x76cf
movq %rax, %rbx
jmp 0x7693
movq %rax, %rbx
jmp 0x769d
movq %rax, %rbx
jmp 0x76aa
jmp 0x76bf
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x66b8
jmp 0x7659
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x76b2
jmp 0x76bf
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x66b8
jmp 0x7677
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66b8
jmp 0x7686
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x66b8
leaq 0x40(%rsp), %rdi
callq 0x66b8
leaq 0xa8(%rsp), %rdi
callq 0x66b8
leaq 0x1f8(%rsp), %rdi
callq 0x66b8
jmp 0x76c2
jmp 0x76bf
jmp 0x76bf
jmp 0x76bf
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x66b8
leaq 0x1a8(%rsp), %rdi
callq 0x66b8
movq %rbx, %rdi
callq 0x65e0