// File: Luthaf[P]zeopp/cycle.cc
// Function: compute_4cycle(DIJKSTRA_NETWORK*, std::vector<CYCLE, std::allocator<CYCLE>>*, bool, int)
bool compute_4cycle(DIJKSTRA_NETWORK* dnet, vector<CYCLE> *cycles,
                    bool weight_flag, int weight_range)
{
    /* Algorithm (Shortest cycle):
     * 1) For each Dijkstra node, identifiy all the edges from the node.
     * 2) For each edge from the node remove the edge and compute the
     * shortest path between the pair of nodes in the removed edge
     * using the Dijkstra's algorithm.
     */
    // First clear the active flag for each Dijkstra node
    // Set the max_radius of each connection to 1. Used in enabling or
    // disabling a connection without deleting the connection
    // Adds an additional check
    DnIt it = dnet->nodes.begin();
    int count = 0;
    for (; it != dnet->nodes.end(); ++it, ++count) {
        it->active = false;
        CnIt cit = it->connections.begin();
        for (; cit != it->connections.end(); ++cit) cit->max_radius = 1;
    }
    cout << "Length of vornet: " << count << endl;

    it = dnet->nodes.begin();
    for (; it != dnet->nodes.end(); ++it) {
        //cout << "x: " << it->x << " y: " << it->y << " z: " <<  it->z << endl;
        ;
    }
    
    for (int i = 0; i < dnet->nodes.size(); ++i){
        DIJKSTRA_NODE org_node = dnet->nodes.at(i);
        if (i == 0) cout << "org_node: "<< org_node.x << " " << org_node.y << " " << org_node.z << endl;
        CnIt it = org_node.connections.begin();
        int conn_ind = 0;
        for (; it != org_node.connections.end(); ++it, ++conn_ind){
            int path_length = 0;
            int org_node_id = it->from;
            int end_node_id = it->to;
            if (i == 0) cout << "end_node_id: " << end_node_id << endl;
            DIJKSTRA_NODE end_node = dnet->nodes.at(end_node_id);
            
            if (end_node.active) continue;

            CnIt oit = it + 1;       //Check the other connections of begin node
            int oconn_ind=conn_ind + 1;
            for (; oit != org_node.connections.end(); ++oit, ++oconn_ind) {
                int inter_node_id = oit->to;
                if (inter_node_id==org_node_id || inter_node_id==end_node_id) continue;
		if (i==0) cout << "    inter_node_id: " << inter_node_id << endl;
                DIJKSTRA_NODE inter_node = dnet->nodes.at(inter_node_id);
                if (inter_node.active) continue;
                for (CnIt iit = inter_node.connections.begin(); iit != inter_node.connections.end(); ++iit) 
                    if (i==0) cout << "        to: "<< iit->to << endl;
                CnIt eit = end_node.connections.begin();
                int econn_ind=0;
                for (; eit != end_node.connections.end(); ++eit, ++econn_ind) {
                    int final_node_id = eit->to;
                    if (final_node_id==end_node_id || final_node_id==org_node_id) continue;
                    if (i==0) cout << "    final_node_id: " << final_node_id << endl;
                    DIJKSTRA_NODE final_node = dnet->nodes.at(final_node_id);
                    if (final_node.active) continue;

                    CnIt iit = inter_node.connections.begin();
                    for (; iit != inter_node.connections.end(); ++iit) {
                        if (iit->to == final_node_id){  //Cycle found
                            CYCLE new_cycle;
                            new_cycle.length=4;
                            new_cycle.nodes.push_back(org_node);
                            new_cycle.nodes.push_back(end_node);
                            new_cycle.nodes.push_back(inter_node);
                            new_cycle.nodes.push_back(dnet->nodes.at(final_node_id));
                            cycles->push_back(new_cycle);
                        }
                    }
                }
            }
        }
        org_node.active = true;
    }
    cout << "No. of cycles: " << cycles->size() << endl;
    if (!cycles->size()) return false;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2a000
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29c80
movq %rax, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x67c30
testb $0x1, %al
jne 0x16fb44
jmp 0x16fbd0
leaq -0x28(%rbp), %rdi
callq 0x10c8c0
movb $0x0, 0x40(%rax)
leaq -0x28(%rbp), %rdi
callq 0x10c8c0
movq %rax, %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0x40(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x10c8c0
movq %rax, %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x16fb96
jmp 0x16fbb7
leaq -0x40(%rbp), %rdi
callq 0x653b0
movsd 0x824a9(%rip), %xmm0    # 0x1f2050
movsd %xmm0, 0x10(%rax)
leaq -0x40(%rbp), %rdi
callq 0x10cc90
jmp 0x16fb6a
jmp 0x16fbb9
leaq -0x28(%rbp), %rdi
callq 0x67c70
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x16fb21
movq 0x1033e9(%rip), %rdi    # 0x272fc0
leaq 0x8f539(%rip), %rsi     # 0x1ff117
callq 0x5540
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x58d0
movq %rax, %rdi
movq 0x1033a0(%rip), %rsi    # 0x272f98
callq 0x55b0
movq -0x10(%rbp), %rdi
callq 0x2a000
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29c80
movq %rax, -0x58(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x67c30
testb $0x1, %al
jne 0x16fc32
jmp 0x16fc3f
jmp 0x16fc34
leaq -0x28(%rbp), %rdi
callq 0x67c70
jmp 0x16fc12
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10100
movq %rax, %rcx
movq -0x248(%rbp), %rax
cmpq %rcx, %rax
jae 0x170451
movq -0x10(%rbp), %rdi
movslq -0x5c(%rbp), %rsi
callq 0x10130
movq %rax, %rsi
leaq -0xa8(%rbp), %rdi
callq 0x64400
cmpl $0x0, -0x5c(%rbp)
jne 0x16fd6c
movq 0x103326(%rip), %rdi    # 0x272fc0
leaq 0x8f489(%rip), %rsi     # 0x1ff12a
callq 0x5540
movq %rax, -0x250(%rbp)
jmp 0x16fcaf
movq -0x250(%rbp), %rdi
movsd -0xa0(%rbp), %xmm0
callq 0x5980
movq %rax, -0x258(%rbp)
jmp 0x16fccc
movq -0x258(%rbp), %rdi
leaq 0x8c5d4(%rip), %rsi     # 0x1fc2ae
callq 0x5540
movq %rax, -0x260(%rbp)
jmp 0x16fce8
movq -0x260(%rbp), %rdi
movsd -0x98(%rbp), %xmm0
callq 0x5980
movq %rax, -0x268(%rbp)
jmp 0x16fd05
movq -0x268(%rbp), %rdi
leaq 0x8c59b(%rip), %rsi     # 0x1fc2ae
callq 0x5540
movq %rax, -0x270(%rbp)
jmp 0x16fd21
movq -0x270(%rbp), %rdi
movsd -0x90(%rbp), %xmm0
callq 0x5980
movq %rax, -0x278(%rbp)
jmp 0x16fd3e
movq -0x278(%rbp), %rdi
movq 0x10324c(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x16fd53
jmp 0x16fd6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x170443
leaq -0xa8(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
leaq -0xa8(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0xd0(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x16fdc0
jmp 0x170425
movl $0x0, -0xd4(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x653b0
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x653b0
movl 0x4(%rax), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x16fe47
movq 0x1031c0(%rip), %rdi    # 0x272fc0
leaq 0x8f32e(%rip), %rsi     # 0x1ff135
callq 0x5540
movq %rax, -0x280(%rbp)
jmp 0x16fe15
movq -0x280(%rbp), %rdi
movl -0xdc(%rbp), %esi
callq 0x58d0
movq %rax, -0x288(%rbp)
jmp 0x16fe30
movq -0x288(%rbp), %rdi
movq 0x10315a(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x16fe45
jmp 0x16fe47
movq -0x10(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
callq 0x10130
movq %rax, -0x290(%rbp)
jmp 0x16fe60
movq -0x290(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x64400
jmp 0x16fe75
testb $0x1, -0xe8(%rbp)
je 0x16fe8d
movl $0x10, -0x12c(%rbp)
jmp 0x1703db
leaq -0xc0(%rbp), %rdi
movl $0x1, %esi
callq 0x66a90
movq %rax, -0x138(%rbp)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13c(%rbp)
leaq -0xa8(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x148(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x16fee7
jmp 0x1703d1
leaq -0x138(%rbp), %rdi
callq 0x653b0
movl 0x4(%rax), %eax
movl %eax, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpl -0xd8(%rbp), %eax
je 0x16ff18
movl -0x14c(%rbp), %eax
cmpl -0xdc(%rbp), %eax
jne 0x16ff1d
jmp 0x1703a3
cmpl $0x0, -0x5c(%rbp)
jne 0x16ff88
movq 0x103096(%rip), %rdi    # 0x272fc0
leaq 0x8f212(%rip), %rsi     # 0x1ff143
callq 0x5540
movq %rax, -0x298(%rbp)
jmp 0x16ff3f
movq -0x298(%rbp), %rdi
movl -0x14c(%rbp), %esi
callq 0x58d0
movq %rax, -0x2a0(%rbp)
jmp 0x16ff5a
movq -0x2a0(%rbp), %rdi
movq 0x103030(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x16ff6f
jmp 0x16ff88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x170417
movq -0x10(%rbp), %rdi
movslq -0x14c(%rbp), %rsi
callq 0x10130
movq %rax, -0x2a8(%rbp)
jmp 0x16ffa1
movq -0x2a8(%rbp), %rsi
leaq -0x198(%rbp), %rdi
callq 0x64400
jmp 0x16ffb6
testb $0x1, -0x158(%rbp)
je 0x16ffce
movl $0x13, -0x12c(%rbp)
jmp 0x170387
leaq -0x198(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x1a8(%rbp)
leaq -0x1a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x170018
jmp 0x17009f
cmpl $0x0, -0x5c(%rbp)
jne 0x17008c
movq 0x102f9b(%rip), %rdi    # 0x272fc0
leaq 0x8f12b(%rip), %rsi     # 0x1ff157
callq 0x5540
movq %rax, -0x2b0(%rbp)
jmp 0x17003a
leaq -0x1a0(%rbp), %rdi
callq 0x653b0
movq -0x2b0(%rbp), %rdi
movl 0x4(%rax), %esi
callq 0x58d0
movq %rax, -0x2b8(%rbp)
jmp 0x17005e
movq -0x2b8(%rbp), %rdi
movq 0x102f2c(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x170073
jmp 0x17008c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x1703c3
jmp 0x17008e
leaq -0x1a0(%rbp), %rdi
callq 0x10cc90
jmp 0x16ffe5
leaq -0x128(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0x1b0(%rbp)
movl $0x0, -0x1b4(%rbp)
leaq -0x128(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x1c0(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x1700f3
jmp 0x17037d
leaq -0x1b0(%rbp), %rdi
callq 0x653b0
movl 0x4(%rax), %eax
movl %eax, -0x1c4(%rbp)
movl -0x1c4(%rbp), %eax
cmpl -0xdc(%rbp), %eax
je 0x170124
movl -0x1c4(%rbp), %eax
cmpl -0xd8(%rbp), %eax
jne 0x170129
jmp 0x17034f
cmpl $0x0, -0x5c(%rbp)
jne 0x17017d
movq 0x102e8a(%rip), %rdi    # 0x272fc0
leaq 0x8f027(%rip), %rsi     # 0x1ff164
callq 0x5540
movq %rax, -0x2c0(%rbp)
jmp 0x17014b
movq -0x2c0(%rbp), %rdi
movl -0x1c4(%rbp), %esi
callq 0x58d0
movq %rax, -0x2c8(%rbp)
jmp 0x170166
movq -0x2c8(%rbp), %rdi
movq 0x102e24(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x17017b
jmp 0x17017d
movq -0x10(%rbp), %rdi
movslq -0x1c4(%rbp), %rsi
callq 0x10130
movq %rax, -0x2d0(%rbp)
jmp 0x170196
movq -0x2d0(%rbp), %rsi
leaq -0x210(%rbp), %rdi
callq 0x64400
jmp 0x1701ab
testb $0x1, -0x1d0(%rbp)
je 0x1701c3
movl $0x19, -0x12c(%rbp)
jmp 0x170333
leaq -0x198(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0x218(%rbp)
leaq -0x198(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x220(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x17020d
jmp 0x170329
leaq -0x218(%rbp), %rdi
callq 0x653b0
movl 0x4(%rax), %eax
cmpl -0x1c4(%rbp), %eax
jne 0x170316
leaq -0x240(%rbp), %rdi
callq 0x16fa00
jmp 0x170236
movabsq $0x4010000000000000, %rax # imm = 0x4010000000000000
movq %rax, -0x240(%rbp)
leaq -0x238(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x94e60
jmp 0x17025c
leaq -0x238(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x94e60
jmp 0x170271
leaq -0x238(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x94e60
jmp 0x170286
leaq -0x238(%rbp), %rax
movq %rax, -0x2e0(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x1c4(%rbp), %rsi
callq 0x10130
movq %rax, -0x2d8(%rbp)
jmp 0x1702ad
movq -0x2d8(%rbp), %rsi
movq -0x2e0(%rbp), %rdi
callq 0x94e60
jmp 0x1702c2
movq -0x18(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x170a30
jmp 0x1702d4
leaq -0x240(%rbp), %rdi
callq 0x170aa0
jmp 0x170316
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x17036f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x170aa0
jmp 0x17036f
jmp 0x170318
leaq -0x218(%rbp), %rdi
callq 0x10cc90
jmp 0x1701da
movl $0x0, -0x12c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x100e0
movl -0x12c(%rbp), %eax
testl %eax, %eax
je 0x17034d
jmp 0x17034b
jmp 0x17034f
jmp 0x17034f
leaq -0x1b0(%rbp), %rdi
callq 0x10cc90
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x1700c0
leaq -0x210(%rbp), %rdi
callq 0x100e0
jmp 0x1703c3
movl $0x0, -0x12c(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x100e0
movl -0x12c(%rbp), %eax
testl %eax, %eax
je 0x1703a1
jmp 0x17039f
jmp 0x1703a3
jmp 0x1703a3
leaq -0x138(%rbp), %rdi
callq 0x10cc90
movl -0x13c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13c(%rbp)
jmp 0x16feb4
leaq -0x198(%rbp), %rdi
callq 0x100e0
jmp 0x170417
movl $0x0, -0x12c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x100e0
movl -0x12c(%rbp), %eax
testl %eax, %eax
je 0x1703f5
jmp 0x1703f3
jmp 0x1703f7
jmp 0x1703f7
leaq -0xc0(%rbp), %rdi
callq 0x10cc90
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x16fd8d
leaq -0x128(%rbp), %rdi
callq 0x100e0
jmp 0x170443
movb $0x1, -0x68(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x100e0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x16fc46
leaq -0xa8(%rbp), %rdi
callq 0x100e0
jmp 0x1704b9
movq 0x102b68(%rip), %rdi    # 0x272fc0
leaq 0x8ed19(%rip), %rsi     # 0x1ff178
callq 0x5540
movq %rax, -0x2e8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x170ac0
movq -0x2e8(%rbp), %rdi
movq %rax, %rsi
callq 0x5160
movq %rax, %rdi
movq 0x102b0b(%rip), %rsi    # 0x272f98
callq 0x55b0
movq -0x18(%rbp), %rdi
callq 0x170ac0
cmpq $0x0, %rax
jne 0x1704a7
movb $0x0, -0x1(%rbp)
jmp 0x1704ab
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)