// File: /workspace/github_cpp_makefile_star2_O2/brianbbsu[P]TCP-Ping/src/client.cpp
// Function: main
int main(int argc, char *argv[])
{
    int num, tout, twait;
    std::vector<std::string> target_strs;
    po::options_description desc, desc_all;
    desc.add_options()
    ("help,h", "Print help messages")
    ("num,n", po::value<int>(&num)->default_value(0), "Amount of packets to send to each target, 0 means keep sending messages until program close")
    ("timeout,t", po::value<int>(&tout)->default_value(1000), "Maximum millisecond(s) the client needs to wait for each response")
    ("wait,w", po::value<int>(&twait)->default_value(1000), "Millisecond(s) the client need to wait between each ping");
    desc_all.add(desc);
    desc_all.add_options()
    ("target", po::value<std::vector<std::string>>(&target_strs), "Target(s) in `host:port` form, host can be in hostname or ip");

    po::positional_options_description pos_desc;
    pos_desc.add("target", -1);

    po::variables_map vmap;
    try{
        po::store(po::command_line_parser(argc, argv).options(desc_all).positional(pos_desc).run(), vmap);
        if(vmap.count("help"))print_help(desc);
        po::notify(vmap);
        if(tout < 0)throw std::invalid_argument("timeout should not be negative");
        if(num < 0)throw std::invalid_argument("num should not be negative");
        if(twait < 0)throw std::invalid_argument("waiting time should not be negative");
        if(target_strs.size() == 0)throw std::invalid_argument("there should be at least one target");
    }catch(const std::exception &ex){
        logger::log_error("Failed to parse arguments: " + std::string(ex.what()) + "\n");
        print_help(desc);
    }

    for(auto &s:target_strs)targets.push_back(cmd_to_target(s));

    logger::log_info("== TCP Ping - Client - Made by Po-Hsuan Su (b08902107) ==");

    std::vector<std::thread> thread_v;

    // Start all thread
    for(int i = 0;i < targets.size();i ++)thread_v.emplace_back(pinger, std::ref(targets[i]), i + 1, num, tout, twait);
    
    struct sigaction sa;
    sa.sa_handler = finalize;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);

    // Wail all thread to finish
    for(auto &t:thread_v)t.join();

    finalize(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rsi, %r14
movl %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movq 0xe5c9(%rip), %rax      # 0x15f90
movl (%rax), %ebx
movl %ebx, %r15d
shrl %r15d
leaq 0x238(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x6690
leaq 0x2b8(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x6690
leaq 0x238(%rsp), %rdi
callq 0x63c0
leaq 0x198(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x68d7(%rip), %rsi      # 0xe2e9
leaq 0x68d7(%rip), %rdx      # 0xe2f0
callq 0x6620
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x837f
leaq 0x68(%rsp), %rsi
andl $0x0, (%rsi)
movq %rax, %rdi
callq 0x83c2
leaq 0x68c2(%rip), %rsi      # 0xe304
leaq 0x68c1(%rip), %rcx      # 0xe30a
movq %rbx, %rdi
movq %rax, %rdx
callq 0x6660
movq %rax, %rbx
leaq 0x14(%rsp), %rdi
callq 0x837f
leaq 0x140(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x83c2
leaq 0x68e8(%rip), %rsi      # 0xe366
leaq 0x68eb(%rip), %rcx      # 0xe370
movq %rbx, %rdi
movq %rax, %rdx
callq 0x6660
movq %rax, %r15
leaq 0x1c(%rsp), %rbx
movq %rbx, %rdi
callq 0x837f
leaq 0x20(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x83c2
leaq 0x68f8(%rip), %rsi      # 0xe3b2
leaq 0x68f8(%rip), %rcx      # 0xe3b9
movq %r15, %rdi
movq %rax, %rdx
callq 0x6660
leaq 0x2b8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x6360
leaq 0x2b8(%rsp), %rdi
callq 0x63c0
movq %rax, 0x198(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8434
leaq 0x69a8(%rip), %rsi      # 0xe4af
leaq 0x68e4(%rip), %rcx      # 0xe3f2
leaq 0x198(%rsp), %rdi
movq %rax, %rdx
callq 0x6660
leaq 0x140(%rsp), %rdi
callq 0x6450
leaq 0x697d(%rip), %rsi      # 0xe4af
leaq 0x140(%rsp), %rdi
pushq $-0x1
popq %rdx
callq 0x6390
leaq 0x198(%rsp), %rdi
callq 0x6130
leaq 0x68(%rsp), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x8476
leaq 0x68(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
callq 0x8506
movq %rax, %r14
leaq 0x140(%rsp), %rsi
movq %rax, %rdi
callq 0x6580
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x8524
leaq 0x20(%rsp), %rdi
leaq 0x198(%rsp), %rsi
xorl %edx, %edx
callq 0x65d0
leaq 0x20(%rsp), %rdi
callq 0x898c
leaq 0x68(%rsp), %rdi
callq 0x85b2
leaq 0x6870(%rip), %rsi      # 0xe42f
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8046
leaq 0x1a8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x85d2
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x66b8
testq %r14, %r14
jne 0x7c25
leaq 0x198(%rsp), %rdi
callq 0x61b0
cmpl $0x0, 0x14(%rsp)
jns 0x7c47
pushq $0x10
popq %rdi
callq 0x6180
movq %rax, %r15
leaq 0x681c(%rip), %rsi      # 0xe434
movq %rax, %rdi
callq 0x6640
jmp 0x7cb6
leaq 0x3b8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x85ec
leaq 0x3b8(%rsp), %rdi
callq 0x76e4
cmpl $0x0, 0x18(%rsp)
jns 0x7c6a
pushq $0x10
popq %rdi
callq 0x6180
movq %rax, %r15
leaq 0x67f3(%rip), %rsi      # 0xe453
movq %rax, %rdi
callq 0x6640
jmp 0x7cb6
cmpl $0x0, 0x1c(%rsp)
jns 0x7c8d
pushq $0x10
popq %rdi
callq 0x6180
movq %rax, %r15
leaq 0x67eb(%rip), %rsi      # 0xe46e
movq %rax, %rdi
callq 0x6640
jmp 0x7cb6
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %rbp
cmpq %r14, %rbp
jne 0x7ccc
pushq $0x10
popq %rdi
callq 0x6180
movq %rax, %r15
leaq 0x67e4(%rip), %rsi      # 0xe492
movq %rax, %rdi
callq 0x6640
movq 0xe323(%rip), %rsi      # 0x15fe0
movq 0xe304(%rip), %rdx      # 0x15fc8
movq %r15, %rdi
callq 0x65b0
leaq 0x178(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0xe790(%rip), %r13      # 0x16470
cmpq %rbp, %r14
je 0x7d1c
movq %r15, %rdi
movq %r14, %rsi
callq 0x64a0
movq %r12, %rdi
movq %r15, %rsi
callq 0x6b15
movq %r13, %rdi
movq %r12, %rsi
callq 0xca64
movq %r12, %rdi
callq 0x8698
movq %r15, %rdi
callq 0x66b8
addq $0x20, %r14
jmp 0x7ce0
leaq 0x67af(%rip), %rsi      # 0xe4d2
leaq 0x100(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x8046
leaq 0x100(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0xcfd0
leaq 0x100(%rsp), %rdi
callq 0x66b8
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %rbp
leaq 0x14(%rsp), %r14
xorl %r12d, %r12d
movq 0xe6fa(%rip), %rax      # 0x16478
movq 0xe6eb(%rip), %rcx      # 0x16470
subq %rcx, %rax
cqto
pushq $0x58
popq %rsi
idivq %rsi
cmpq %r12, %rax
jbe 0x7dd2
addq %r15, %rcx
movq %rcx, 0x68(%rsp)
leal 0x1(%r12), %eax
movl %eax, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0x20(%rsp), %rdi
leaq -0xe52(%rip), %rsi      # 0x6f64
leaq 0x68(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x86ae
addq $0x58, %r15
incq %r12
jmp 0x7d77
leaq -0x691(%rip), %rax      # 0x7748
leaq 0x70(%rsp), %rdi
movq %rax, -0x8(%rdi)
andl $0x0, 0x80(%rdi)
callq 0x6400
pushq $0x2
popq %rdi
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x6410
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r14
cmpq %r14, %rbx
je 0x7e1a
movq %rbx, %rdi
callq 0x65a0
addq $0x8, %rbx
jmp 0x7e07
callq 0x7748
jmp 0x7e23
jmp 0x7e23
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8714
jmp 0x7fd6
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x7e5e
jmp 0x7e68
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x8698
jmp 0x7e56
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x66b8
jmp 0x7fd6
movq %rax, %rbx
jmp 0x7fd6
jmp 0x7e76
jmp 0x7e76
jmp 0x7e76
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x6260
jmp 0x7edb
movq %rdx, %r14
movq %rax, %rbx
leaq 0x3b8(%rsp), %rdi
callq 0x8670
jmp 0x7edb
jmp 0x7ed5
movq %rdx, %r14
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x66b8
jmp 0x7edb
jmp 0x7ed5
movq %rdx, %r14
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x898c
jmp 0x7ec9
movq %rdx, %r14
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x85b2
jmp 0x7edb
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x7fd6
movq %rbx, %rdi
callq 0x6120
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rax, %rsi
callq 0x8046
leaq 0x65a7(%rip), %rsi      # 0xe4b6
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8151
leaq 0x62fe(%rip), %rdx      # 0xe223
leaq 0x120(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x8081
leaq 0x120(%rsp), %rdi
pushq $-0x1
popq %rsi
callq 0xd0b2
leaq 0x120(%rsp), %rdi
callq 0x66b8
leaq 0x68(%rsp), %rdi
callq 0x66b8
leaq 0x20(%rsp), %rdi
callq 0x66b8
leaq 0x338(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x85ec
leaq 0x338(%rsp), %rdi
callq 0x76e4
movq %rax, %rbx
leaq 0x338(%rsp), %rdi
callq 0x8670
jmp 0x7fd1
jmp 0x7fce
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x66b8
jmp 0x7fb3
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x66b8
jmp 0x7fc2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x66b8
jmp 0x7fd1
movq %rax, %rbx
callq 0x6560
leaq 0x198(%rsp), %rdi
callq 0x8736
jmp 0x7ff2
movq %rax, %rdi
callq 0x8175
jmp 0x7fef
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x8768
jmp 0x8010
jmp 0x800d
jmp 0x800d
jmp 0x800d
jmp 0x800d
jmp 0x800d
jmp 0x800d
movq %rax, %rbx
leaq 0x2b8(%rsp), %rdi
callq 0x8670
jmp 0x8022
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x8670
jmp 0x8034
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x877e
movq %rbx, %rdi
callq 0x65e0