// File: 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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x458(%rbp)
callq 0x71b0
movq -0x458(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x71b0
jmp 0x88a2
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x8060
jmp 0x88b1
leaq -0x78(%rbp), %rdi
callq 0x78b8
leaq 0x1594b(%rip), %rsi     # 0x1e20c
leaq -0x58(%rbp), %rdi
callq 0xa7b0
movb %al, -0x459(%rbp)
jmp 0x88d2
movb -0x459(%rbp), %al
testb $0x1, %al
jne 0x88e1
jmp 0x898b
leaq 0x157db(%rip), %rsi     # 0x1e0c3
leaq -0xa8(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0xa4f0
jmp 0x88fa
movl -0xc(%rbp), %esi
leaq -0xa8(%rbp), %rdi
callq 0x1d310
jmp 0x890b
leaq -0xa8(%rbp), %rdi
callq 0x78b8
movl $0x1, -0xac(%rbp)
jmp 0x9342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x9366
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x78b8
jmp 0x9366
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x935d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x78b8
jmp 0x935d
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
leaq -0x58(%rbp), %rsi
callq 0x7050
jmp 0x899e
jmp 0x89a0
movq -0x8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
xorl %edx, %edx
callq 0xa7e0
jmp 0x89b4
leaq 0x1571e(%rip), %rsi     # 0x1e0d9
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rdx
callq 0xa690
jmp 0x89d0
leaq 0x15711(%rip), %rdx     # 0x1e0e8
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0xa4a0
jmp 0x89ec
leaq -0xf0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0xa640
jmp 0x8a05
leaq 0x15d03(%rip), %rdx     # 0x1e70f
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xa4a0
jmp 0x8a21
movl -0xc(%rbp), %esi
leaq -0xd0(%rbp), %rdi
callq 0x1d1c0
jmp 0x8a32
leaq -0xd0(%rbp), %rdi
callq 0x78b8
leaq -0xf0(%rbp), %rdi
callq 0x78b8
leaq -0x110(%rbp), %rdi
callq 0x78b8
leaq -0x130(%rbp), %rdi
callq 0x78b8
leaq -0x150(%rbp), %rdi
callq 0x78b8
movw $0x2, -0x160(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x20(%rax), %edi
callq 0x7070
movw %ax, -0x15e(%rbp)
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
callq 0x7190
movq %rax, %rdi
callq 0x7440
movl %eax, -0x15c(%rbp)
jmp 0x8aa8
movl $0x2, %edi
movl $0x801, %esi            # imm = 0x801
xorl %edx, %edx
callq 0x7100
movl %eax, -0x164(%rbp)
cmpl $0x0, %eax
jge 0x8c00
leaq -0x189(%rbp), %rdi
movq %rdi, -0x468(%rbp)
callq 0x77c0
movq -0x468(%rbp), %rdx
leaq 0x15602(%rip), %rsi     # 0x1e0eb
leaq -0x188(%rbp), %rdi
callq 0xa3f0
jmp 0x8af7
movl -0xc(%rbp), %esi
leaq -0x188(%rbp), %rdi
callq 0x1d310
jmp 0x8b08
leaq -0x188(%rbp), %rdi
callq 0x78b8
leaq -0x189(%rbp), %rdi
callq 0x74b0
movl $0x1, -0xac(%rbp)
jmp 0x9342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8bb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8ba6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8b9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8b8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x78b8
leaq -0xf0(%rbp), %rdi
callq 0x78b8
leaq -0x110(%rbp), %rdi
callq 0x78b8
leaq -0x130(%rbp), %rdi
callq 0x78b8
leaq -0x150(%rbp), %rdi
callq 0x78b8
jmp 0x935d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8bef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x78b8
leaq -0x189(%rbp), %rdi
callq 0x74b0
jmp 0x935d
jmp 0x8c02
leaq -0x210(%rbp), %rax
movq %rax, -0x220(%rbp)
movl $0x0, -0x214(%rbp)
movl -0x214(%rbp), %eax
cmpq $0x10, %rax
jae 0x8c4c
movq -0x220(%rbp), %rax
movl -0x214(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x214(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x214(%rbp)
jmp 0x8c1a
jmp 0x8c4e
jmp 0x8c50
movl -0x164(%rbp), %ecx
movl %ecx, %edx
addl $0x3f, %edx
testl %ecx, %ecx
movl %ecx, %eax
cmovsl %edx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movb %cl, %sil
sarl $0x6, %eax
cltq
movq -0x210(%rbp,%rax,8), %rcx
movb %sil, %dl
btsq %rdx, %rcx
movq %rcx, -0x210(%rbp,%rax,8)
movslq -0x14(%rbp), %rax
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x26, %rax
addl %ecx, %eax
cltq
movq %rax, -0x230(%rbp)
movslq -0x14(%rbp), %rcx
movl %ecx, %eax
imulq $0x10624dd3, %rcx, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltq
movq %rax, -0x228(%rbp)
callq 0x7600
movq %rax, -0x238(%rbp)
movl -0x164(%rbp), %edi
leaq -0x160(%rbp), %rsi
movl $0x10, %edx
callq 0x7390
movl %eax, -0x46c(%rbp)
jmp 0x8d05
movl -0x46c(%rbp), %eax
movl %eax, -0x23c(%rbp)
movq -0x8(%rbp), %rax
movl 0x48(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x48(%rax)
cmpl $0x0, -0x23c(%rbp)
je 0x8e33
callq 0x7060
cmpl $0x73, (%rax)
je 0x8e33
movl -0x164(%rbp), %edi
callq 0x8830
jmp 0x8d46
movq -0x8(%rbp), %rsi
leaq -0x2a0(%rbp), %rdi
movl $0x1, %edx
callq 0xa7e0
jmp 0x8d5d
leaq 0x1539f(%rip), %rsi     # 0x1e103
leaq -0x280(%rbp), %rdi
leaq -0x2a0(%rbp), %rdx
callq 0xa690
jmp 0x8d79
leaq 0x152d0(%rip), %rdx     # 0x1e050
leaq -0x260(%rbp), %rdi
leaq -0x280(%rbp), %rsi
callq 0xa4a0
jmp 0x8d95
movl -0xc(%rbp), %esi
leaq -0x260(%rbp), %rdi
callq 0x1d310
jmp 0x8da6
leaq -0x260(%rbp), %rdi
callq 0x78b8
leaq -0x280(%rbp), %rdi
callq 0x78b8
leaq -0x2a0(%rbp), %rdi
callq 0x78b8
movl $0x1, -0xac(%rbp)
jmp 0x9342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8e22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8e16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x78b8
leaq -0x280(%rbp), %rdi
callq 0x78b8
leaq -0x2a0(%rbp), %rdi
callq 0x78b8
jmp 0x935d
movl -0x164(%rbp), %edi
incl %edi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x210(%rbp), %rdx
leaq -0x230(%rbp), %r8
movq %rcx, %rsi
callq 0x7410
movl %eax, -0x470(%rbp)
jmp 0x8e5d
movl -0x470(%rbp), %eax
movl %eax, -0x2a4(%rbp)
callq 0x7600
movq %rax, -0x2b0(%rbp)
cmpl $-0x1, -0x2a4(%rbp)
jne 0x8f7c
movl -0x164(%rbp), %edi
callq 0x8830
jmp 0x8e8f
movq -0x8(%rbp), %rsi
leaq -0x310(%rbp), %rdi
movl $0x1, %edx
callq 0xa7e0
jmp 0x8ea6
leaq 0x15274(%rip), %rsi     # 0x1e121
leaq -0x2f0(%rbp), %rdi
leaq -0x310(%rbp), %rdx
callq 0xa690
jmp 0x8ec2
leaq 0x15187(%rip), %rdx     # 0x1e050
leaq -0x2d0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0xa4a0
jmp 0x8ede
movl -0xc(%rbp), %esi
leaq -0x2d0(%rbp), %rdi
callq 0x1d310
jmp 0x8eef
leaq -0x2d0(%rbp), %rdi
callq 0x78b8
leaq -0x2f0(%rbp), %rdi
callq 0x78b8
leaq -0x310(%rbp), %rdi
callq 0x78b8
movl $0x1, -0xac(%rbp)
jmp 0x9342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8f6b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x8f5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x78b8
leaq -0x2f0(%rbp), %rdi
callq 0x78b8
leaq -0x310(%rbp), %rdi
callq 0x78b8
jmp 0x935d
leaq -0x2b0(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0xa9a0
movq %rax, -0x478(%rbp)
jmp 0x8f98
movq -0x478(%rbp), %rax
movq %rax, -0x328(%rbp)
leaq -0x328(%rbp), %rdi
callq 0xa970
movq %rax, -0x480(%rbp)
jmp 0x8fbb
movq -0x480(%rbp), %rax
movq %rax, -0x320(%rbp)
leaq -0x320(%rbp), %rdi
callq 0xa9f0
movq %rax, -0x488(%rbp)
jmp 0x8fde
movq -0x488(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x15016(%rip), %xmm1    # 0x1e008
divsd %xmm1, %xmm0
movsd %xmm0, -0x318(%rbp)
cmpl $0x0, -0x2a4(%rbp)
je 0x901e
movsd -0x318(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x90bc
movq -0x8(%rbp), %rsi
leaq -0x368(%rbp), %rdi
movl $0x1, %edx
callq 0xa7e0
jmp 0x9035
leaq 0x1510e(%rip), %rsi     # 0x1e14a
leaq -0x348(%rbp), %rdi
leaq -0x368(%rbp), %rdx
callq 0xa690
jmp 0x9051
movl -0xc(%rbp), %esi
leaq -0x348(%rbp), %rdi
callq 0x1d1c0
jmp 0x9062
leaq -0x348(%rbp), %rdi
callq 0x78b8
leaq -0x368(%rbp), %rdi
callq 0x78b8
jmp 0x92ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x90ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x78b8
leaq -0x368(%rbp), %rdi
callq 0x78b8
jmp 0x935d
xorps %xmm0, %xmm0
movaps %xmm0, -0x380(%rbp)
movl $0x0, -0x370(%rbp)
movsd -0x318(%rbp), %xmm0
leaq 0x15087(%rip), %rsi     # 0x1e166
leaq -0x380(%rbp), %rdi
movb $0x1, %al
callq 0x70c0
movq -0x8(%rbp), %rsi
leaq -0x420(%rbp), %rdi
movl $0x1, %edx
callq 0xa7e0
jmp 0x9104
leaq 0x15061(%rip), %rsi     # 0x1e16c
leaq -0x400(%rbp), %rdi
leaq -0x420(%rbp), %rdx
callq 0xa690
jmp 0x9120
leaq 0x15050(%rip), %rdx     # 0x1e177
leaq -0x3e0(%rbp), %rdi
leaq -0x400(%rbp), %rsi
callq 0xa4a0
jmp 0x913c
leaq -0x441(%rbp), %rdi
movq %rdi, -0x490(%rbp)
callq 0x77c0
movq -0x490(%rbp), %rdx
leaq -0x440(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0xa3f0
jmp 0x916b
leaq -0x3c0(%rbp), %rdi
leaq -0x3e0(%rbp), %rsi
leaq -0x440(%rbp), %rdx
callq 0xaa10
jmp 0x9187
leaq 0x14ff2(%rip), %rdx     # 0x1e180
leaq -0x3a0(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0xa4a0
jmp 0x91a3
movl -0xc(%rbp), %esi
leaq -0x3a0(%rbp), %rdi
callq 0x1d1c0
jmp 0x91b4
leaq -0x3a0(%rbp), %rdi
callq 0x78b8
leaq -0x3c0(%rbp), %rdi
callq 0x78b8
leaq -0x440(%rbp), %rdi
callq 0x78b8
leaq -0x441(%rbp), %rdi
callq 0x74b0
leaq -0x3e0(%rbp), %rdi
callq 0x78b8
leaq -0x400(%rbp), %rdi
callq 0x78b8
leaq -0x420(%rbp), %rdi
callq 0x78b8
movq -0x8(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movsd -0x318(%rbp), %xmm0
movq -0x8(%rbp), %rax
addsd 0x50(%rax), %xmm0
movsd %xmm0, 0x50(%rax)
jmp 0x92ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x92df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x92d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x92bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x92af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x92a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x3a0(%rbp), %rdi
callq 0x78b8
leaq -0x3c0(%rbp), %rdi
callq 0x78b8
leaq -0x440(%rbp), %rdi
callq 0x78b8
leaq -0x441(%rbp), %rdi
callq 0x74b0
leaq -0x3e0(%rbp), %rdi
callq 0x78b8
leaq -0x400(%rbp), %rdi
callq 0x78b8
leaq -0x420(%rbp), %rdi
callq 0x78b8
jmp 0x935d
movl -0x164(%rbp), %edi
callq 0x8830
jmp 0x92fa
cmpl $0x1, -0x10(%rbp)
jne 0x9302
jmp 0x9338
cmpl $0x1, -0x10(%rbp)
jle 0x9311
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x9313
leaq -0x450(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0xab90
jmp 0x9325
leaq -0x450(%rbp), %rdi
callq 0xaae0
jmp 0x9333
jmp 0x8aa6
movl $0x0, -0xac(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x78b8
leaq -0x38(%rbp), %rdi
callq 0x78b8
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x78b8
leaq -0x38(%rbp), %rdi
callq 0x78b8
movq -0x80(%rbp), %rdi
callq 0x77a0
nopl (%rax,%rax)