// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, 0x30(%rsp)
movq (%rdi), %rax
movq %rdi, (%rsp)
movq 0x8(%rdi), %rcx
xorl %ebp, %ebp
cmpq %rcx, %rax
je 0x109aea
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movb $0x0, 0x40(%rax)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdi
cmpq %rdi, %rsi
je 0x109adf
movq %rdx, 0x10(%rsi)
addq $0x28, %rsi
jmp 0x109ad0
addq $0x48, %rax
incl %ebp
cmpq %rcx, %rax
jne 0x109ac4
movq 0x8d4cf(%rip), %r15     # 0x196fc0
leaq 0x4f5e7(%rip), %rsi     # 0x1590df
movl $0x12, %edx
movq %r15, %rdi
callq 0x44e0
movq %r15, %rdi
movl %ebp, %esi
callq 0x4750
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movq (%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x10a19f
leaq 0x100(%rsp), %r13
leaq 0x138(%rsp), %r15
xorl %r14d, %r14d
leaq (%r14,%r14,8), %rcx
leaq (%rax,%rcx,8), %r12
addq $0x20, %r12
movups -0x20(%r12), %xmm0
movups -0x10(%r12), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x38318
movb 0x20(%r12), %al
movb %al, 0x20(%rbx)
movq 0x18(%r12), %rax
movq %rax, 0x18(%rbx)
testq %r14, %r14
jne 0x109c47
movl $0xa, %edx
movq 0x8d40b(%rip), %rdi     # 0x196fc0
leaq 0x4f536(%rip), %rsi     # 0x1590f2
callq 0x44e0
movsd 0x48(%rsp), %xmm0
movq 0x8d3f2(%rip), %rdi     # 0x196fc0
callq 0x46b0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4d8a1(%rip), %rsi     # 0x157486
callq 0x44e0
movsd 0x50(%rsp), %xmm0
movq %r12, %rdi
callq 0x46b0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4d87c(%rip), %rsi     # 0x157486
callq 0x44e0
movsd 0x58(%rsp), %xmm0
movq %r12, %rdi
callq 0x46b0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movq 0x60(%rsp), %rbp
cmpq 0x68(%rsp), %rbp
je 0x10a154
movq %r14, 0x18(%rsp)
movl (%rbp), %eax
movl %eax, 0xc(%rsp)
movslq 0x4(%rbp), %rbx
testq %r14, %r14
jne 0x109cbc
movl $0xd, %edx
movq 0x8d348(%rip), %rdi     # 0x196fc0
leaq 0x4f47e(%rip), %rsi     # 0x1590fd
callq 0x44e0
movq 0x8d335(%rip), %rdi     # 0x196fc0
movl %ebx, %esi
callq 0x4750
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movq (%rsp), %rax
movq %rbx, %rsi
movq (%rax), %rbx
movq 0x8(%rax), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rax # imm = 0x8E38E38E38E38E39
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x10a245
movq %rsi, 0x10(%rsp)
leaq (%rsi,%rsi,8), %r14
leaq (%rbx,%r14,8), %rsi
addq $0x20, %rsi
movups -0x20(%rsi), %xmm0
movups -0x10(%rsi), %xmm1
movaps %xmm1, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq %r13, %rdi
callq 0x38318
movq %rbp, 0x28(%rsp)
leaq (%rbx,%r14,8), %rax
movb 0x40(%rax), %cl
movb %cl, 0x20(%r13)
movq 0x38(%rax), %rax
movq %rax, 0x18(%r13)
cmpb $0x0, 0x120(%rsp)
leaq 0xb0(%rsp), %r13
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rbx
jne 0x10a11b
movq 0x28(%rsp), %rcx
leaq 0x28(%rcx), %rax
cmpq 0x68(%rsp), %rax
je 0x10a11b
movl 0x2c(%rcx), %ebp
movq %rax, 0x38(%rsp)
cmpl 0xc(%rsp), %ebp
je 0x10a111
cmpl %ebx, %ebp
je 0x10a111
testq %r14, %r14
jne 0x109dd4
movl $0x13, %edx
movq 0x8d230(%rip), %rdi     # 0x196fc0
leaq 0x4f374(%rip), %rsi     # 0x15910b
callq 0x44e0
movq 0x8d21d(%rip), %rdi     # 0x196fc0
movl %ebp, %esi
callq 0x4750
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movslq %ebp, %rsi
movq (%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rax # imm = 0x8E38E38E38E38E39
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x10a237
leaq (%rsi,%rsi,8), %r14
leaq (%rbx,%r14,8), %rsi
addq $0x20, %rsi
movups -0x20(%rsi), %xmm0
movups -0x10(%rsi), %xmm1
movaps %xmm1, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %r13, %rdi
callq 0x38318
leaq (%rbx,%r14,8), %rax
movb 0x40(%rax), %cl
movb %cl, 0x20(%r13)
movq 0x38(%rax), %rax
movq %rax, 0x18(%r13)
cmpb $0x0, 0xd0(%rsp)
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rbx
jne 0x10a0ec
movq 0xb0(%rsp), %rbx
cmpq 0xb8(%rsp), %rbx
movq 0x8d152(%rip), %r13     # 0x196fc0
je 0x109ecc
testq %r14, %r14
jne 0x109ebe
movl $0xc, %edx
movq %r13, %rdi
leaq 0x4f29b(%rip), %rsi     # 0x15911f
callq 0x44e0
movl 0x4(%rbx), %esi
movq %r13, %rdi
callq 0x4750
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
addq $0x28, %rbx
cmpq 0xb8(%rsp), %rbx
jne 0x109e70
movq 0x100(%rsp), %r13
cmpq 0x108(%rsp), %r13
movq 0x10(%rsp), %rbx
je 0x10a0ec
movl 0x4(%r13), %r12d
cmpl %ebx, %r12d
je 0x10a00f
cmpl 0xc(%rsp), %r12d
je 0x10a00f
testq %r14, %r14
jne 0x109f55
movl $0x13, %edx
movq 0x8d0b0(%rip), %rdi     # 0x196fc0
leaq 0x4f215(%rip), %rsi     # 0x15912c
callq 0x44e0
movq 0x8d09d(%rip), %rdi     # 0x196fc0
movl %r12d, %esi
callq 0x4750
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %rbp, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movslq %r12d, %rsi
movq (%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rdx
subq %r14, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rax # imm = 0x8E38E38E38E38E39
imulq %rax, %rdx
cmpq %rsi, %rdx
jbe 0x10a229
movl %r12d, 0x24(%rsp)
movq %r13, %r12
movq %r15, %r13
movq %rsi, %rbp
leaq (%rsi,%rsi,8), %rbx
leaq 0x20(%r14,%rbx,8), %rsi
movups -0x20(%rsi), %xmm0
movups -0x10(%rsi), %xmm1
movaps %xmm1, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
leaq 0x170(%rsp), %r15
movq %r15, %rdi
callq 0x38318
leaq (%r14,%rbx,8), %rax
movb 0x40(%rax), %cl
movb %cl, 0x20(%r15)
movq 0x38(%rax), %rax
movq %rax, 0x18(%r15)
cmpb $0x0, 0x190(%rsp)
movq %r13, %r15
movq %r12, %r13
movl 0x24(%rsp), %r12d
je 0x10a026
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x10a005
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rbx
addq $0x28, %r13
cmpq 0x108(%rsp), %r13
jne 0x109ee7
jmp 0x10a0ec
movq 0xb0(%rsp), %r14
cmpq 0xb8(%rsp), %r14
je 0x109fe8
cmpl %r12d, 0x4(%r14)
jne 0x10a0e3
xorps %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x140(%rsp)
movabsq $0x4010000000000000, %rax # imm = 0x4010000000000000
movq %rax, 0x130(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x51244
movq %r15, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x51244
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
callq 0x51244
movq (%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rcx, %rdx
cmpq %rbp, %rdx
jbe 0x10a218
leaq (%rax,%rbx,8), %rsi
movq %r15, %rdi
callq 0x51244
movq 0x30(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x10a60c
movq %r15, %rdi
callq 0xf07c
addq $0x28, %r14
jmp 0x10a02e
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x10a109
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0xb0(%rsp), %r13
movq 0x38(%rsp), %rcx
jmp 0x109d56
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x10a138
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x28(%rsp), %rbp
addq $0x28, %rbp
cmpq 0x68(%rsp), %rbp
leaq 0x100(%rsp), %r13
jne 0x109c5c
movb $0x1, 0x80(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x10a173
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
incq %r14
movq (%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x71c71c71c71c71c7, %rdx # imm = 0x8E38E38E38E38E39
imulq %rdx, %rcx
cmpq %r14, %rcx
ja 0x109b5d
movq 0x8ce1a(%rip), %r14     # 0x196fc0
leaq 0x4ef93(%rip), %rsi     # 0x159140
movl $0xf, %edx
movq %r14, %rdi
callq 0x44e0
movq 0x30(%rsp), %rbx
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x5, %rsi
movq %r14, %rdi
callq 0x42b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42c0
movq 0x8(%rbx), %rax
cmpq (%rbx), %rax
setne %al
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45486(%rip), %rdi     # 0x14f6a5
movq %rbp, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0x45475(%rip), %rdi     # 0x14f6a5
xorl %eax, %eax
callq 0x4630
leaq 0x45467(%rip), %rdi     # 0x14f6a5
xorl %eax, %eax
callq 0x4630
leaq 0x45459(%rip), %rdi     # 0x14f6a5
xorl %eax, %eax
callq 0x4630
jmp 0x10a259
jmp 0x10a259
jmp 0x10a259
movq %rax, %rbx
jmp 0x10a2d6
jmp 0x10a262
jmp 0x10a262
movq %rax, %rbx
jmp 0x10a2b9
jmp 0x10a26d
jmp 0x10a26d
jmp 0x10a26d
movq %rax, %rbx
jmp 0x10a29c
jmp 0x10a274
movq %rax, %rbx
movq %r15, %rdi
callq 0xf07c
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x10a29c
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x10a2b9
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x10a2d6
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x10a2ed
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780