// File: /workspace/github_cpp_makefile_star2_O3/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 0xe3c8(%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 0x76c9(%rip), %rsi      # 0xf2e4
leaq 0x76c9(%rip), %rdx      # 0xf2eb
callq 0x5630
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x8953
leaq 0x88(%rsp), %rsi
movl $0x0, (%rsi)
movq %rax, %rdi
callq 0x89ca
leaq 0x76ae(%rip), %rsi      # 0xf2ff
leaq 0x76ad(%rip), %rcx      # 0xf305
movq %rbx, %rdi
movq %rax, %rdx
callq 0x5670
movq %rax, %rbx
leaq 0x14(%rsp), %rdi
callq 0x8953
leaq 0x160(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x89ca
leaq 0x76d4(%rip), %rsi      # 0xf361
leaq 0x76d7(%rip), %rcx      # 0xf36b
movq %rbx, %rdi
movq %rax, %rdx
callq 0x5670
movq %rax, %r15
leaq 0x1c(%rsp), %rbx
movq %rbx, %rdi
callq 0x8953
leaq 0x20(%rsp), %rsi
movl $0x3e8, (%rsi)          # imm = 0x3E8
movq %rax, %rdi
callq 0x89ca
leaq 0x76e4(%rip), %rsi      # 0xf3ad
leaq 0x76e4(%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 0x8ad7
leaq 0x7794(%rip), %rsi      # 0xf4aa
leaq 0x76d0(%rip), %rcx      # 0xf3ed
leaq 0x198(%rsp), %rdi
movq %rax, %rdx
callq 0x5670
leaq 0x160(%rsp), %rdi
callq 0x5470
leaq 0x7769(%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 0x8b4e
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 0x8be2
leaq 0x20(%rsp), %rdi
leaq 0x198(%rsp), %rsi
xorl %edx, %edx
callq 0x55e0
leaq 0x20(%rsp), %rdi
callq 0x8fb2
leaq 0xd8(%rsp), %rdi
callq 0x902c
leaq 0xb8(%rsp), %rdi
callq 0x9064
leaq 0x88(%rsp), %rdi
callq 0x8ee6
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x761d(%rip), %rsi      # 0xf42a
leaq 0x761a(%rip), %rdx      # 0xf42e
leaq 0x88(%rsp), %rdi
callq 0x96cc
leaq 0x1a8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xe006
movq %rax, %r14
leaq 0x1b0(%rsp), %r15
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x7e66
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
cmpq %r15, %r14
jne 0x7ea0
leaq 0x198(%rsp), %rdi
callq 0x51c0
cmpl $0x0, 0x14(%rsp)
jns 0x7ec2
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x759c(%rip), %rsi      # 0xf42f
movq %rax, %rdi
callq 0x5650
jmp 0x7f37
leaq 0x3b8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x8cc8
leaq 0x3b8(%rsp), %rdi
callq 0x7697
cmpl $0x0, 0x18(%rsp)
jns 0x7ee7
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x7571(%rip), %rsi      # 0xf44e
movq %rax, %rdi
callq 0x5650
jmp 0x7f37
cmpl $0x0, 0x1c(%rsp)
jns 0x7f0c
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x7567(%rip), %rsi      # 0xf469
movq %rax, %rdi
callq 0x5650
jmp 0x7f37
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %r12
cmpq %r13, %r12
jne 0x7f4d
movl $0x10, %edi
callq 0x5190
movq %rax, %r15
leaq 0x755e(%rip), %rsi      # 0xf48d
movq %rax, %rdi
callq 0x5650
movq 0xe0a2(%rip), %rsi      # 0x15fe0
movq 0xe083(%rip), %rdx      # 0x15fc8
movq %r15, %rdi
callq 0x55c0
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 0x9636
movq %r15, %rdi
movq %r14, %rsi
callq 0x5c9f
leaq 0xe4de(%rip), %rdi      # 0x16470
movq %r15, %rsi
callq 0xe090
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7fbf
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x7fe4
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x140(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8001
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x5360
addq $0x20, %r13
cmpq %r12, %r13
jne 0x7f65
leaq 0x130(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x74ac(%rip), %rsi      # 0xf4cd
leaq 0x74de(%rip), %rdx      # 0xf506
leaq 0x120(%rsp), %rdi
callq 0x96cc
leaq 0x120(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe5cc
movq 0x120(%rsp), %rdi
cmpq %r14, %rdi
je 0x8064
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x5360
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0xe3f4(%rip), %rax      # 0x16470
cmpq %rax, 0xe3f5(%rip)      # 0x16478
je 0x80fc
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 -0x1aa1(%rip), %rsi     # 0x6621
leaq 0x88(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x8de8
movq 0xe397(%rip), %rcx      # 0x16478
movq 0xe388(%rip), %rax      # 0x16470
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq $0x58, %r15
cmpq %r13, %rcx
ja 0x809f
leaq -0xa08(%rip), %rax      # 0x76fb
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 0x814f
movq %rbx, %rdi
callq 0x55b0
addq $0x8, %rbx
jmp 0x813c
callq 0x76fb
jmp 0x8158
jmp 0x8158
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8e52
jmp 0x83f7
movq %rax, %rbx
movq 0x120(%rsp), %rdi
cmpq %r14, %rdi
je 0x83f7
movq 0x130(%rsp), %rsi
jmp 0x81b8
jmp 0x81c5
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8db2
jmp 0x819f
movq %rax, %rbx
movq 0x140(%rsp), %rdi
cmpq %rbp, %rdi
je 0x83f7
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83f7
movq %rax, %rbx
jmp 0x83f7
jmp 0x81d3
jmp 0x81d3
jmp 0x81d3
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x5270
jmp 0x8256
movq %rdx, %r14
movq %rax, %rbx
leaq 0x3b8(%rsp), %rdi
callq 0x8d70
jmp 0x8256
jmp 0x8250
movq %rdx, %r14
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x8256
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x8256
jmp 0x8250
movq %rdx, %r14
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8fb2
jmp 0x8241
movq %rdx, %r14
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8ca8
jmp 0x8256
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x83f7
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 0x8468
leaq 0x7227(%rip), %rsi      # 0xf4b1
leaq 0x88(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8587
leaq 0x6f7b(%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 0x82d1
movups (%rcx), %xmm0
movups %xmm0, (%r14)
jmp 0x82de
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 0xe721
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x831b
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x833c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8357
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x338(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x8cc8
leaq 0x338(%rsp), %rdi
callq 0x7697
movq %rax, %rbx
leaq 0x338(%rsp), %rdi
callq 0x8d70
jmp 0x83f2
jmp 0x83ef
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x83ac
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83ac
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x83d2
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83d2
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x83f2
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x83f2
movq %rax, %rbx
callq 0x5570
leaq 0x198(%rsp), %rdi
callq 0x8e8e
jmp 0x8413
movq %rax, %rdi
callq 0x85f7
jmp 0x8410
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x8ec0
jmp 0x8431
jmp 0x842e
jmp 0x842e
jmp 0x842e
jmp 0x842e
jmp 0x842e
jmp 0x842e
movq %rax, %rbx
leaq 0x2b8(%rsp), %rdi
callq 0x8d70
jmp 0x8443
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x8d70
jmp 0x8455
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x8ee6
movq %rbx, %rdi
callq 0x55f0
nop