// File: /workspace/github_cpp_makefile_star2_O2/sharmaeklavya2[P]ddos-on-anycast/src/lib/attack.cpp
// Function: attack(Network const&, std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>>&, int, int)
void attack(const Network& network, const ivec& victims, ivec& target, int in_weight, int side_weight) {
    // TODO: Remove side edge transitivity

    normalize_edge_weights(in_weight, side_weight);
    int n = network.size();
    int h = network.height();

    // init targets
    target.resize(n, -1);
    ivec target_dist(n, -1);
    for(int v: victims) {
        target[v] = v;
        target_dist[v] = 0;
    }

    /*
    vector<ivec> depthwise_victims(h+1);
    for(int victim: victims) {
        depthwise_victims[network.depth[victim]].push_back(victim);
    }
    */

    // Apply Dijkstra's algorithm
    vector<bool> visited(n, false);
    for(int d=h; d>=0; --d) {
        // initialize targets, target_dists and heap
        PQ pq;
        for(int u: network.depthwise[d]) {
            if(target[u] == -1 && !network.down_nbrs[u].empty()) {
                ipvec dvlist;
                dvlist.reserve(network.down_nbrs[u].size());
                for(int v: network.down_nbrs[u]) {
                    if(target_dist[v] != -1) {
                        dvlist.emplace_back(target_dist[v], target[v]);
                    }
                }
                if(!dvlist.empty()) {
                    iipair min_elem = *(std::min_element(dvlist.begin(), dvlist.end()));
                    target[u] = min_elem.second;
                    target_dist[u] = 0;
                }
            }
            if(target_dist[u] != -1) {
                if(target_dist[u] != 0) {
                    fprintf(stderr, "target_dist[%d] = %d instead of 0 or -1\n", u, target_dist[u]);
                }
                pq.push(PQElem(0, u));
            }
        }

        dijkstra(network, pq, target, target_dist, visited, in_weight, side_weight);
    }

    typedef tuple<int, int, int> itrip;
    ivec target_depth(network.depth.begin(), network.depth.end());

    for(int d=1; d<=h; ++d) {
        PQ pq;
        for(int u: network.depthwise[d]) {
            if(target_dist[u] == -1 && !network.up_nbrs[u].empty()) {
                vector<itrip> dvlist;
                for(int v: network.up_nbrs[u]) {
                    if(target_dist[v] != -1) {
                        dvlist.emplace_back(-target_depth[v], target_dist[v], target[v]);
                    }
                }
                if(!dvlist.empty()) {
                    int v = std::get<2>(*(std::min_element(dvlist.begin(), dvlist.end())));
                    target[u] = target[v];
                    target_dist[u] = 0;
                    target_depth[u] = target_depth[v];
                    pq.emplace(target_dist[u], u);
                }
            }
        }

        dijkstra(network, pq, target, target_dist, visited, in_weight, side_weight);
    }
    // cerr << "target_dist: " << target_dist << endl;

    for(int i=0; i<n; ++i) {
        if(network.netsize[i] == 0 && (target[i] == -1 || target_dist[i] < 0)) {
            fprintf(stderr, "target[%d] = %d and target_dist[%d] = %d\n", i, target[i], i, target_dist[i]);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x7c(%rsp), %rdi
movl %ecx, (%rdi)
leaq 0x78(%rsp), %rsi
movl %r8d, (%rsi)
callq 0x693c
movq 0x20(%r14), %rsi
movq 0xb0(%r14), %rax
subq 0x18(%r14), %rsi
shrq $0x2, %rsi
movq %r14, 0x10(%rsp)
subq 0xa8(%r14), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %r14
movq %rsi, 0xb8(%rsp)
movslq %esi, %r12
leaq 0xc0(%rsp), %r13
orl $-0x1, (%r13)
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x339a
orl $-0x1, (%r13)
leaq 0x58(%rsp), %r13
leaq 0xc0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %r13, %rdi
movq %r12, %rsi
callq 0x3578
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq (%rbx), %rdx
movq (%r13), %rsi
cmpq %rcx, %rax
je 0x6cba
movslq (%rax), %rdi
movl %edi, (%rdx,%rdi,4)
andl $0x0, (%rsi,%rdi,4)
addq $0x4, %rax
jmp 0x6ca5
leaq 0x80(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0xc0(%rsp), %rdi
leaq 0x20(%rsp), %rcx
movq %r12, %rsi
callq 0x7206
decl %r14d
movl 0x7c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x80(%rsp), %r12
movq %r14, 0x40(%rsp)
movl %r14d, %eax
testl %eax, %eax
js 0x6ea8
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movl %eax, 0x48(%rsp)
movl %eax, %eax
movq 0x10(%rsp), %rcx
movq 0xa8(%rcx), %rcx
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %r15
movq 0x8(%rcx,%rax), %rax
movq %rax, 0x50(%rsp)
cmpq 0x50(%rsp), %r15
je 0x6e6c
movl (%r15), %ebp
movslq %ebp, %r14
movq (%rbx), %rax
cmpl $-0x1, (%rax,%r14,4)
jne 0x6e1f
movq 0x10(%rsp), %rax
movq 0x90(%rax), %rax
imulq $0x18, %r14, %r13
movq (%rax,%r13), %rcx
cmpq 0x8(%rax,%r13), %rcx
je 0x6e1f
addq %r13, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x2, %rsi
leaq 0x20(%rsp), %rdi
callq 0x589a
movq 0x10(%rsp), %rax
movq 0x90(%rax), %rax
movq (%rax,%r13), %r12
movq 0x8(%rax,%r13), %r13
cmpq %r13, %r12
je 0x6de5
movslq (%r12), %rdx
movq 0x58(%rsp), %rax
cmpl $-0x1, (%rax,%rdx,4)
je 0x6ddf
leaq (%rax,%rdx,4), %rsi
shlq $0x2, %rdx
addq (%rbx), %rdx
leaq 0x20(%rsp), %rdi
callq 0x572e
addq $0x4, %r12
jmp 0x6db6
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
cmpq %rsi, %rdi
leaq 0x80(%rsp), %r12
je 0x6e15
callq 0x777b
movl 0x4(%rax), %eax
movq (%rbx), %rcx
movl %eax, (%rcx,%r14,4)
movq 0x58(%rsp), %rax
andl $0x0, (%rax,%r14,4)
leaq 0x20(%rsp), %rdi
callq 0x366e
movq 0x58(%rsp), %rax
movl (%rax,%r14,4), %ecx
cmpl $-0x1, %ecx
je 0x6e47
testl %ecx, %ecx
jne 0x6e50
movl %ebp, 0x20(%rsp)
andl $0x0, 0x24(%rsp)
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x726a
addq $0x4, %r15
jmp 0x6d3d
movq 0xa179(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
leaq 0x47f2(%rip), %rsi      # 0xb653
movl %ebp, %edx
xorl %eax, %eax
callq 0x2180
jmp 0x6e31
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
leaq 0x58(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl 0x1c(%rsp), %r9d
callq 0x69bf
movq %r12, %rdi
callq 0x7308
movl 0x48(%rsp), %eax
decl %eax
jmp 0x6cfd
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
leaq 0x20(%rsp), %rdi
leaq 0x80(%rsp), %rcx
callq 0x7280
xorl %eax, %eax
movq 0x40(%rsp), %rcx
testl %ecx, %ecx
cmovlel %eax, %ecx
incl %ecx
movq %rcx, 0x40(%rsp)
pushq $0x1
popq %rcx
leaq 0xa0(%rsp), %r13
leaq 0x74(%rsp), %rbp
cmpq 0x40(%rsp), %rcx
je 0x7083
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movq 0x10(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rcx, 0x48(%rsp)
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %r12
movq 0x8(%rax,%rcx), %r14
movq %r14, 0x50(%rsp)
cmpq %r14, %r12
je 0x703d
movslq (%r12), %rcx
movl %ecx, 0xc(%rsp)
movq 0x58(%rsp), %rax
cmpl $-0x1, (%rax,%rcx,4)
jne 0x7034
movq 0x10(%rsp), %rax
movq 0x78(%rax), %rax
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rdx
cmpq 0x8(%rax,%rcx), %rdx
je 0x7034
andq $0x0, 0xb0(%rsp)
addq %rcx, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r15, %r14
je 0x6fc3
movslq (%r14), %rcx
movq 0x58(%rsp), %rax
cmpl $-0x1, (%rax,%rcx,4)
je 0x6fbd
leaq (%rax,%rcx,4), %rdx
movq 0x20(%rsp), %rax
xorl %esi, %esi
subl (%rax,%rcx,4), %esi
shlq $0x2, %rcx
movl %esi, 0x74(%rsp)
addq (%rbx), %rcx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x72b2
addq $0x4, %r14
jmp 0x6f86
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
cmpq %rsi, %rdi
movq 0x50(%rsp), %r14
je 0x702c
callq 0x7a86
movslq (%rax), %rax
movq (%rbx), %rcx
movl (%rcx,%rax,4), %edx
movslq 0xc(%rsp), %rsi
movl %edx, (%rcx,%rsi,4)
movslq 0xc(%rsp), %rcx
movq 0x58(%rsp), %rdx
andl $0x0, (%rdx,%rcx,4)
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %eax
movslq 0xc(%rsp), %rsi
movl %eax, (%rcx,%rsi,4)
movslq 0xc(%rsp), %rax
leaq (%rdx,%rax,4), %rsi
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x72e2
movq %r13, %rdi
callq 0x78f6
addq $0x4, %r12
jmp 0x6f2c
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
movq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x58(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl 0x1c(%rsp), %r9d
callq 0x69bf
movq %r14, %rdi
callq 0x7308
movq 0x48(%rsp), %rcx
incq %rcx
jmp 0x6eea
xorl %eax, %eax
movq 0xb8(%rsp), %r14
testl %r14d, %r14d
cmovlel %eax, %r14d
leaq 0x45e1(%rip), %r15      # 0xb67c
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x70ee
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
cmpl $0x0, (%rax,%r12,4)
jne 0x70cd
movq (%rbx), %rax
movl (%rax,%r12,4), %ecx
movq 0x58(%rsp), %rax
movl (%rax,%r12,4), %r9d
cmpl $-0x1, %ecx
je 0x70d2
testl %r9d, %r9d
js 0x70d2
incq %r12
jmp 0x709e
movq 0x9ef7(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
movq %r15, %rsi
movl %r12d, %edx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x2180
jmp 0x70cd
leaq 0x20(%rsp), %rdi
callq 0x3660
leaq 0xc0(%rsp), %rdi
callq 0x72f8
leaq 0x58(%rsp), %rdi
callq 0x3660
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x7182
movq %rax, %rbx
jmp 0x718f
movq %rax, %rbx
jmp 0x714f
jmp 0x713a
jmp 0x713f
jmp 0x713f
jmp 0x7168
jmp 0x7168
movq %rax, %rbx
jmp 0x7175
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x78f6
leaq 0x80(%rsp), %rdi
callq 0x7308
leaq 0x20(%rsp), %rdi
callq 0x3660
jmp 0x7182
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x366e
leaq 0x80(%rsp), %rdi
callq 0x7308
leaq 0xc0(%rsp), %rdi
callq 0x72f8
leaq 0x58(%rsp), %rdi
callq 0x3660
movq %rbx, %rdi
callq 0x21f0
nop