// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
leaq 0x2c(%rsp), %rdi
movl %ecx, (%rdi)
leaq 0x28(%rsp), %rsi
movl %r8d, (%rsi)
callq 0x7a54
movq 0x20(%r15), %rax
subq 0x18(%r15), %rax
movq 0xb0(%r15), %r14
shrq $0x2, %rax
movq %r15, 0x18(%rsp)
subq 0xa8(%r15), %r14
movq %rax, 0xf0(%rsp)
movslq %eax, %r15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0xa8(%rsp), %r13
movl %ebp, (%r13)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x39ec
movl %ebp, (%r13)
leaq 0x50(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %r15, %rsi
callq 0x3c10
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq %rcx, %rax
je 0x7e44
movq (%rbx), %rdx
movq 0x50(%rsp), %rsi
movslq (%rax), %rdi
movl %edi, (%rdx,%rdi,4)
movl $0x0, (%rsi,%rdi,4)
addq $0x4, %rax
cmpq %rcx, %rax
jne 0x7e2e
leaq 0x80(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0xa8(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movq %r15, %rsi
callq 0x851c
sarq $0x3, %r14
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %r14, %rcx
movq %rcx, %rax
movq %rcx, 0xe8(%rsp)
decl %eax
movl %eax, 0x48(%rsp)
js 0x80ae
movl 0x2c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x48(%rsp), %ecx
leaq 0x80(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rcx, 0x68(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %r15
movq 0x8(%rax,%rcx,8), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, %r15
je 0x8061
movl (%r15), %r13d
movslq %r13d, %r14
movq (%rbx), %rax
cmpl $-0x1, (%rax,%r14,4)
je 0x7f3c
movq 0x50(%rsp), %rax
movl (%rax,%r14,4), %ecx
cmpl $-0x1, %ecx
je 0x7f2c
testl %ecx, %ecx
jne 0x8041
movl %r13d, 0x30(%rsp)
movl $0x0, 0x34(%rsp)
movq %rbp, %rdi
leaq 0x30(%rsp), %rsi
callq 0x8584
addq $0x4, %r15
cmpq 0x70(%rsp), %r15
jne 0x7eec
jmp 0x8061
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rax
leaq (%r14,%r14,2), %r12
movq (%rax,%r12,8), %rcx
cmpq 0x8(%rax,%r12,8), %rcx
je 0x7efc
leaq (%rax,%r12,8), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x2, %rsi
leaq 0x30(%rsp), %rdi
callq 0x6a60
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rax
movq (%rax,%r12,8), %rbp
movq 0x8(%rax,%r12,8), %r12
cmpq %r12, %rbp
je 0x7fc5
movslq (%rbp), %rdx
movq 0x50(%rsp), %rax
cmpl $-0x1, (%rax,%rdx,4)
je 0x7fbf
leaq (%rax,%rdx,4), %rsi
shlq $0x2, %rdx
addq (%rbx), %rdx
leaq 0x30(%rsp), %rdi
callq 0x682a
addq $0x4, %rbp
jmp 0x7f96
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rax
cmpq %rax, %rdi
leaq 0x80(%rsp), %rbp
je 0x802e
leaq 0x8(%rdi), %rcx
movq %rdi, %rdx
cmpq %rax, %rcx
je 0x8017
movq %rdi, %rsi
movl (%rcx), %r8d
movl (%rsi), %r9d
movq %rcx, %rdx
cmpl %r9d, %r8d
jl 0x800b
jg 0x8008
movl 0x4(%rcx), %r8d
movq %rcx, %rdx
cmpl 0x4(%rsi), %r8d
jl 0x800b
movq %rsi, %rdx
addq $0x8, %rcx
movq %rdx, %rsi
cmpq %rax, %rcx
jne 0x7feb
movl 0x4(%rdx), %eax
movq (%rbx), %rcx
movl %eax, (%rcx,%r14,4)
movq 0x50(%rsp), %rax
movl $0x0, (%rax,%r14,4)
testq %rdi, %rdi
je 0x7efc
callq 0x2150
jmp 0x7efc
movq 0x9f88(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
leaq 0x5631(%rip), %rsi      # 0xd683
movl %r13d, %edx
xorl %eax, %eax
callq 0x21b0
jmp 0x7f12
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
leaq 0xa8(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7adf
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
movq 0x68(%rsp), %r14
je 0x80a1
callq 0x2150
leaq -0x1(%r14), %rcx
testq %r14, %r14
jg 0x7ea9
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rcx
callq 0x85f2
cmpl $0x0, 0x48(%rsp)
jle 0x82bc
movl 0x2c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xe8(%rsp), %eax
movq %rax, 0x48(%rsp)
movl $0x1, %r14d
leaq 0xd0(%rsp), %r13
leaq 0x7c(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
movq %r14, 0x68(%rsp)
leaq (%r14,%r14,2), %rcx
movq (%rax,%rcx,8), %r14
movq 0x8(%rax,%rcx,8), %r15
cmpq %r15, %r14
movq %r15, 0x70(%rsp)
je 0x8269
movslq (%r14), %rax
movl %eax, 0x14(%rsp)
movq 0x50(%rsp), %rcx
cmpl $-0x1, (%rcx,%rax,4)
je 0x816a
addq $0x4, %r14
cmpq %r15, %r14
jne 0x814a
jmp 0x8269
movq 0x18(%rsp), %rcx
movq 0x78(%rcx), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rdx
cmpq 0x8(%rcx,%rax,8), %rdx
je 0x815c
leaq (%rcx,%rax,8), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq $0x0, 0xe0(%rsp)
movq (%rax), %r12
movq 0x8(%rax), %r15
cmpq %r15, %r12
je 0x81e2
movslq (%r12), %rcx
movq 0x50(%rsp), %rax
cmpl $-0x1, (%rax,%rcx,4)
je 0x81dc
leaq (%rax,%rcx,4), %rdx
movq 0x30(%rsp), %rax
xorl %esi, %esi
subl (%rax,%rcx,4), %esi
shlq $0x2, %rcx
movl %esi, 0x7c(%rsp)
addq (%rbx), %rcx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x862c
addq $0x4, %r12
jmp 0x81a4
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
cmpq %rsi, %rdi
movq 0x70(%rsp), %r15
je 0x824e
callq 0x8d0d
movslq (%rax), %rax
movq (%rbx), %rcx
movl (%rcx,%rax,4), %edx
movslq 0x14(%rsp), %rsi
movl %edx, (%rcx,%rsi,4)
movslq 0x14(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl $0x0, (%rdx,%rcx,4)
movq 0x30(%rsp), %rcx
movl (%rcx,%rax,4), %eax
movslq 0x14(%rsp), %rsi
movl %eax, (%rcx,%rsi,4)
movslq 0x14(%rsp), %rax
leaq (%rdx,%rax,4), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x14(%rsp), %rdx
callq 0x865c
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x815c
callq 0x2150
jmp 0x815c
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
movq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
leaq 0xa8(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x7adf
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
movq 0x68(%rsp), %r14
je 0x82ae
callq 0x2150
incq %r14
cmpq 0x48(%rsp), %r14
jne 0x8107
movq 0xf0(%rsp), %r13
testl %r13d, %r13d
jle 0x8331
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq 0x9cf9(%rip), %r14      # 0x11fd0
leaq 0x53ce(%rip), %r15      # 0xd6ac
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpl $0x0, (%rax,%r12,4)
jne 0x8309
movq (%rbx), %rax
movl (%rax,%r12,4), %ecx
cmpl $-0x1, %ecx
je 0x8313
movq 0x50(%rsp), %rax
cmpl $0x0, (%rax,%r12,4)
js 0x8313
incq %r12
cmpq %r12, %r13
jne 0x82e1
jmp 0x8331
movq (%r14), %rdi
movq 0x50(%rsp), %rax
movl (%rax,%r12,4), %r9d
movq %r15, %rsi
movl %r12d, %edx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x21b0
jmp 0x8309
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x8340
callq 0x2150
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x837a
callq 0x2150
xorl %eax, %eax
movq %rax, 0xa8(%rsp)
movl %eax, 0xb0(%rsp)
movq %rax, 0xb8(%rsp)
movl %eax, 0xc0(%rsp)
movq %rax, 0xc8(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x8389
callq 0x2150
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x840c
movq %rax, %rbx
jmp 0x8446
movq %rax, %rbx
jmp 0x83cf
jmp 0x83b5
jmp 0x83ba
jmp 0x83ba
jmp 0x83e8
movq %rax, %rbx
jmp 0x83fa
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x83cf
callq 0x2150
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x83e1
callq 0x2150
movq 0x30(%rsp), %rdi
jmp 0x8402
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x83fa
callq 0x2150
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x840c
callq 0x2150
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x8446
callq 0x2150
xorl %eax, %eax
movq %rax, 0xa8(%rsp)
movl %eax, 0xb0(%rsp)
movq %rax, 0xb8(%rsp)
movl %eax, 0xc0(%rsp)
movq %rax, 0xc8(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x8455
callq 0x2150
movq %rbx, %rdi
callq 0x2220
nop