// File: /workspace/github_cpp_makefile_star2_O1/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)
movq $0x0, 0x60(%rsp)
movq 0xe3b7(%rip), %rax      # 0x15f98
movl (%rax), %ebx
movl %ebx, %r15d
shrl %r15d
leaq 0x238(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x5690
leaq 0x2b8(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x5690
leaq 0x238(%rsp), %rdi
callq 0x53f0
leaq 0x198(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x76b8(%rip), %rsi      # 0xf2e4
leaq 0x76b8(%rip), %rdx      # 0xf2eb
callq 0x5630
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x897b
leaq 0x88(%rsp), %rsi
movl $0x0, (%rsi)
movq %rax, %rdi
callq 0x89f2
leaq 0x769d(%rip), %rsi      # 0xf2ff
leaq 0x769c(%rip), %rcx      # 0xf305
movq %rbx, %rdi
movq %rax, %rdx
callq 0x5670
movq %rax, %rbx
leaq 0x14(%rsp), %rdi
callq 0x897b
leaq 0x160(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x89f2
leaq 0x76c3(%rip), %rsi      # 0xf361
leaq 0x76c6(%rip), %rcx      # 0xf36b
movq %rbx, %rdi
movq %rax, %rdx
callq 0x5670
movq %rax, %r15
leaq 0x1c(%rsp), %rbx
movq %rbx, %rdi
callq 0x897b
leaq 0x20(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x89f2
leaq 0x76d3(%rip), %rsi      # 0xf3ad
leaq 0x76d3(%rip), %rcx      # 0xf3b4
movq %r15, %rdi
movq %rax, %rdx
callq 0x5670
leaq 0x2b8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x5370
leaq 0x2b8(%rsp), %rdi
callq 0x53f0
movq %rax, 0x198(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8b01
leaq 0x7783(%rip), %rsi      # 0xf4aa
leaq 0x76bf(%rip), %rcx      # 0xf3ed
leaq 0x198(%rsp), %rdi
movq %rax, %rdx
callq 0x5670
leaq 0x160(%rsp), %rdi
callq 0x5470
leaq 0x7758(%rip), %rsi      # 0xf4aa
leaq 0x160(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x53a0
leaq 0x198(%rsp), %rdi
callq 0x5140
leaq 0x88(%rsp), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x8b78
leaq 0x88(%rsp), %rdi
leaq 0x2b8(%rsp), %r14
movq %r14, %rsi
callq 0x52d0
leaq 0x88(%rsp), %rdi
movq %r14, 0x70(%rdi)
leaq 0x160(%rsp), %rsi
callq 0x5590
leaq 0x20(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x8c0c
leaq 0x20(%rsp), %rdi
leaq 0x198(%rsp), %rsi
xorl %edx, %edx
callq 0x55e0
leaq 0x20(%rsp), %rdi
callq 0x8fe2
leaq 0xd8(%rsp), %rdi
callq 0x905c
leaq 0xb8(%rsp), %rdi
callq 0x9094
leaq 0x88(%rsp), %rdi
callq 0x8f1a
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x760c(%rip), %rsi      # 0xf42a
leaq 0x7609(%rip), %rdx      # 0xf42e
leaq 0x88(%rsp), %rdi
callq 0x96ea
leaq 0x1a8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xe10c
movq %rax, %r14
leaq 0x1b0(%rsp), %r15
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x7e77
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
cmpq %r15, %r14
jne 0x7eb1
leaq 0x198(%rsp), %rdi
callq 0x51c0
cmpl $0x0, 0x14(%rsp)
jns 0x7ed3
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x758b(%rip), %rsi      # 0xf42f
movq %rax, %rdi
callq 0x5650
jmp 0x7f45
leaq 0x3b8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x8d02
leaq 0x3b8(%rsp), %rdi
callq 0x76a8
cmpl $0x0, 0x18(%rsp)
jns 0x7ef8
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x7560(%rip), %rsi      # 0xf44e
movq %rax, %rdi
callq 0x5650
jmp 0x7f45
cmpl $0x0, 0x1c(%rsp)
jns 0x7f1d
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x7556(%rip), %rsi      # 0xf469
movq %rax, %rdi
callq 0x5650
jmp 0x7f45
movq 0x58(%rsp), %rax
cmpq 0x50(%rsp), %rax
jne 0x7f5b
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x7550(%rip), %rsi      # 0xf48d
movq %rax, %rdi
callq 0x5650
movq 0xe094(%rip), %rsi      # 0x15fe0
movq 0xe075(%rip), %rdx      # 0x15fc8
movq %r15, %rdi
callq 0x55c0
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %r12
cmpq %r12, %r13
jne 0x80b3
leaq 0x130(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x754c(%rip), %rsi      # 0xf4cd
leaq 0x757e(%rip), %rdx      # 0xf506
leaq 0x120(%rsp), %rdi
callq 0x96ea
leaq 0x120(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe6e4
movq 0x120(%rsp), %rdi
cmpq %r14, %rdi
je 0x7fc4
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x5360
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0xe494(%rip), %rax      # 0x16470
cmpq %rax, 0xe495(%rip)      # 0x16478
je 0x805c
xorl %r15d, %r15d
leaq 0x18(%rsp), %rbp
leaq 0x14(%rsp), %r14
movabsq $0x2e8ba2e8ba2e8ba3, %r12 # imm = 0x2E8BA2E8BA2E8BA3
xorl %r13d, %r13d
addq %r15, %rax
movq %rax, 0x88(%rsp)
incq %r13
movl %r13d, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0x20(%rsp), %rdi
leaq -0x1a0c(%rip), %rsi     # 0x6616
leaq 0x88(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x8e22
movq 0xe437(%rip), %rcx      # 0x16478
movq 0xe428(%rip), %rax      # 0x16470
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq $0x58, %r15
cmpq %r13, %rcx
ja 0x7fff
leaq -0x957(%rip), %rax      # 0x770c
leaq 0x90(%rsp), %rdi
movq %rax, -0x8(%rdi)
movl $0x0, 0x80(%rdi)
callq 0x5430
leaq 0x88(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0x5440
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r14
cmpq %r14, %rbx
je 0x8179
movq %rbx, %rdi
callq 0x55b0
addq $0x8, %rbx
jmp 0x809c
leaq 0x150(%rsp), %rbp
leaq 0x140(%rsp), %r14
leaq 0x88(%rsp), %r15
movq %rbp, 0x140(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x965a
movq %r15, %rdi
movq %r14, %rsi
callq 0x5c9f
leaq 0xe378(%rip), %rdi      # 0x16470
movq %r15, %rsi
callq 0xe196
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x8125
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x814a
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x140(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8167
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x5360
addq $0x20, %r13
cmpq %r12, %r13
jne 0x80cb
jmp 0x7f6e
callq 0x770c
jmp 0x8182
jmp 0x8182
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8e86
jmp 0x8421
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8dec
jmp 0x81a9
movq %rax, %rbx
movq 0x140(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8421
movq 0x150(%rsp), %rsi
jmp 0x81e2
jmp 0x81ef
movq %rax, %rbx
movq 0x120(%rsp), %rdi
cmpq %r14, %rdi
je 0x8421
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x8421
movq %rax, %rbx
jmp 0x8421
jmp 0x81fd
jmp 0x81fd
jmp 0x81fd
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x5270
jmp 0x8280
movq %rdx, %r14
movq %rax, %rbx
leaq 0x3b8(%rsp), %rdi
callq 0x8daa
jmp 0x8280
jmp 0x827a
movq %rdx, %r14
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x8280
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x8280
jmp 0x827a
movq %rdx, %r14
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8fe2
jmp 0x826b
movq %rdx, %r14
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8ce2
jmp 0x8280
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x8421
movq %rbx, %rdi
callq 0x5130
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rax, %rsi
callq 0x8492
leaq 0x71fd(%rip), %rsi      # 0xf4b1
leaq 0x88(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x85b1
leaq 0x6f51(%rip), %rsi      # 0xf21e
leaq 0x88(%rsp), %rdi
callq 0x5660
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x82fb
movups (%rcx), %xmm0
movups %xmm0, (%r14)
jmp 0x8308
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
movq 0x8(%rax), %rdx
leaq 0x68(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe839
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x8345
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8366
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8381
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x338(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x8d02
leaq 0x338(%rsp), %rdi
callq 0x76a8
movq %rax, %rbx
leaq 0x338(%rsp), %rdi
callq 0x8daa
jmp 0x841c
jmp 0x8419
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x83d6
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83d6
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x83fc
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83fc
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x841c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x841c
movq %rax, %rbx
callq 0x5570
leaq 0x198(%rsp), %rdi
callq 0x8ec2
jmp 0x843d
movq %rax, %rdi
callq 0x8621
jmp 0x843a
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x8ef4
jmp 0x845b
jmp 0x8458
jmp 0x8458
jmp 0x8458
jmp 0x8458
jmp 0x8458
jmp 0x8458
movq %rax, %rbx
leaq 0x2b8(%rsp), %rdi
callq 0x8daa
jmp 0x846d
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x8daa
jmp 0x847f
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x8f1a
movq %rbx, %rdi
callq 0x55f0
nop