// File: fjamour[P]icentral/src/experiments.cc
// Function: insertion_test_qube(graph_t&, int)
void insertion_test_qube(graph_t& graph, int num_edges_to_insert)
{
    //1. prepare structures to store the results for each edge 
    //2. generate random edges
    //3. run Brandes and find time
    //4. run update after each edge insertion and record results
    //5. print the results progressively
    //int num_edges_to_insert = 2;
    
    printf("==========================================================\n");
    printf("Graph: \t\t\t%s\n", graph.graph_name.c_str());
    printf("Graph # nodes: \t\t%d\n", graph.size());
    printf("Graph # edges: \t\t%d\n", graph.edge_set.size());
    double brandes_time, muc_find_time;
    timer tm;
    
    printf("Brandes runtime: \t");
    tm.start();
    vector<double> bc_vec = brandes_betweenness_centrality(graph);
    tm.stop();
    brandes_time = tm.interval();
    printf("%f\n", brandes_time);
    
    printf("# MUCs: \t\t");
    tm.start();
    graph.find_mucs();
    tm.stop();
    muc_find_time = tm.interval();
    printf("%d\n", graph.get_num_mucs());
    printf("Time to find MUCs: \t%f\n", muc_find_time);
    
    vector<edge_t> rand_edge_vec;
    printf("Generating edges..n\n");
    for(int i = 0; i < num_edges_to_insert; ++i) {
        edge_t rand_edge;
        node_id_t src, dst;
        do {
            src = rand()%graph.size();
            dst = rand()%graph.size();
            rand_edge.first    = src;
            rand_edge.second   = dst;
        } while(graph.has_edge(rand_edge));
        rand_edge_vec.push_back(rand_edge);
    }

    vector<edge_stat_t> edge_stat_vec;
    
    //==========================================================
//    node_id_t nd = rand_edge_vec[0].first;
//    muc_id_t muc_id = graph.node_to_muc_vec[nd];
//    graph.muc_vec[muc_id].muc_subgraph.print_graph(false);
//    return;
    //==========================================================
    
    for(int i = 0; i < rand_edge_vec.size(); ++i) {
        edge_t rand_edge = rand_edge_vec[i];
        edge_stat_t edge_stat;
        printf("Adding edge (%d, %d)\n", rand_edge.first, rand_edge.second);
        tm.start();
        graph.insert_edge_update_muc(rand_edge.first, rand_edge.second);
        tm.stop();
        edge_stat.edge_ins_time = tm.interval();
        printf("\tTime to update MUCs: \t%f\n", edge_stat.edge_ins_time);

        muc_id_t muc_id = graph.node_to_muc_vec[rand_edge.first];
        edge_stat.muc_num_edges = graph.muc_vec[muc_id].muc_subgraph.edge_set.size();
        edge_stat.muc_num_nodes = graph.muc_vec[muc_id].muc_subgraph.nodes_map.size();
        tr1::unordered_map<node_id_t, double> bc_map;
        tm.start();
        graph.muc_vec[muc_id].compute_bc(bc_map);
        tm.stop();
        edge_stat.muc_bc_update_time = tm.interval();
        printf("\tTime to update BC: \t%f\n", edge_stat.muc_bc_update_time);
        printf("\tSpeedup: \t\t%f\n", brandes_time/edge_stat.muc_bc_update_time);
        printf("\tMUC # nodes: \t\t%d\n", edge_stat.muc_num_nodes);
        printf("\tMUC # edges: \t\t%d\n", edge_stat.muc_num_edges);
        edge_stat_vec.push_back(edge_stat);
    }
    
    
    double total_muc_update_time = 0;
    for(int i = 0; i < edge_stat_vec.size(); ++i) {
        total_muc_update_time += edge_stat_vec[i].muc_bc_update_time;
    }
    double avg_speedup = (num_edges_to_insert*brandes_time)/total_muc_update_time;
    
    printf("[[Average speedup: %f]]\n", avg_speedup);
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1b0(%rsp)
movl %esi, 0x1ac(%rsp)
leaq 0x335d3(%rip), %rdi     # 0x7b2e0
xorl %eax, %eax
movb %al, 0x7f(%rsp)
callq 0x9040
movq 0x1b0(%rsp), %rdi
addq $0x108, %rdi            # imm = 0x108
callq 0x9250
movq %rax, %rsi
movb 0x7f(%rsp), %al
leaq 0x335e2(%rip), %rdi     # 0x7b31c
callq 0x9040
movq 0x1b0(%rsp), %rdi
callq 0x56b40
movq %rax, %rsi
movb 0x7f(%rsp), %al
leaq 0x335d0(%rip), %rdi     # 0x7b32a
callq 0x9040
movq 0x1b0(%rsp), %rdi
addq $0x30, %rdi
callq 0x4dbe0
movq %rax, %rsi
movb 0x7f(%rsp), %al
leaq 0x335c1(%rip), %rdi     # 0x7b33f
callq 0x9040
movb 0x7f(%rsp), %al
leaq 0x335c6(%rip), %rdi     # 0x7b354
callq 0x9040
leaq 0x178(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x2d430
movq 0x1b0(%rsp), %rsi
leaq 0x160(%rsp), %rdi
callq 0x27d70
movq 0x80(%rsp), %rdi
callq 0x2d450
jmp 0x47dcc
leaq 0x178(%rsp), %rdi
callq 0x2d470
movsd %xmm0, 0x70(%rsp)
jmp 0x47de1
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x1a0(%rsp)
movsd 0x1a0(%rsp), %xmm0
leaq 0x33750(%rip), %rdi     # 0x7b550
movb $0x1, %al
callq 0x9040
jmp 0x47e09
leaq 0x33557(%rip), %rdi     # 0x7b367
xorl %eax, %eax
callq 0x9040
jmp 0x47e19
leaq 0x178(%rsp), %rdi
callq 0x2d430
jmp 0x47e28
movq 0x1b0(%rsp), %rdi
callq 0x68030
jmp 0x47e37
leaq 0x178(%rsp), %rdi
callq 0x2d450
jmp 0x47e46
leaq 0x178(%rsp), %rdi
callq 0x2d470
movsd %xmm0, 0x68(%rsp)
jmp 0x47e5b
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x198(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x68af0
movq %rax, 0x60(%rsp)
jmp 0x47e7e
movq 0x60(%rsp), %rsi
leaq 0x3357f(%rip), %rdi     # 0x7b409
xorl %eax, %eax
callq 0x9040
jmp 0x47e93
movsd 0x198(%rsp), %xmm0
leaq 0x334cf(%rip), %rdi     # 0x7b372
movb $0x1, %al
callq 0x9040
jmp 0x47eac
leaq 0x138(%rsp), %rdi
callq 0x4db80
leaq 0x334ca(%rip), %rdi     # 0x7b38a
xorl %eax, %eax
callq 0x9040
jmp 0x47ec9
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x1ac(%rsp), %eax
jge 0x4800f
leaq 0x12c(%rsp), %rdi
callq 0x41760
jmp 0x47ef7
jmp 0x47ef9
callq 0x91a0
cltq
movq %rax, 0x50(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x56b40
movq %rax, 0x58(%rsp)
jmp 0x47f19
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x128(%rsp)
callq 0x91a0
cltq
movq %rax, 0x40(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x56b40
movq %rax, 0x48(%rsp)
jmp 0x47f51
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x124(%rsp)
movl 0x128(%rsp), %eax
movl %eax, 0x12c(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x130(%rsp)
movq 0x1b0(%rsp), %rdi
leaq 0x12c(%rsp), %rsi
callq 0x57340
movb %al, 0x3f(%rsp)
jmp 0x47fa0
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x47ef9
jmp 0x47fae
leaq 0x138(%rsp), %rdi
leaq 0x12c(%rsp), %rsi
callq 0x42130
jmp 0x47fc5
jmp 0x47fc7
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x47ed4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x48466
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x48459
leaq 0x108(%rsp), %rdi
callq 0x4dca0
movl $0x0, 0x104(%rsp)
movslq 0x104(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x47680
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x4835b
movslq 0x104(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x4dbc0
movq (%rax), %rax
movq %rax, 0xf8(%rsp)
movl 0xf8(%rsp), %esi
movl 0xfc(%rsp), %edx
leaq 0x33318(%rip), %rdi     # 0x7b39f
xorl %eax, %eax
callq 0x9040
jmp 0x48090
leaq 0x178(%rsp), %rdi
callq 0x2d430
jmp 0x4809f
movq 0x1b0(%rsp), %rdi
movl 0xf8(%rsp), %esi
movl 0xfc(%rsp), %edx
callq 0x68820
jmp 0x480bc
leaq 0x178(%rsp), %rdi
callq 0x2d450
jmp 0x480cb
leaq 0x178(%rsp), %rdi
callq 0x2d470
movsd %xmm0, 0x28(%rsp)
jmp 0x480e0
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm0
leaq 0x332b6(%rip), %rdi     # 0x7b3b5
movb $0x1, %al
callq 0x9040
jmp 0x48108
movq 0x1b0(%rsp), %rdi
addq $0x18, %rdi
movl 0xf8(%rsp), %eax
movl %eax, %esi
callq 0xfbc0
movl (%rax), %eax
movl %eax, 0xdc(%rsp)
movq 0x1b0(%rsp), %rdi
addq $0x60, %rdi
movl 0xdc(%rsp), %eax
movl %eax, %esi
callq 0x4dcb0
movq %rax, %rdi
addq $0x90, %rdi
callq 0x4dbe0
movl %eax, 0xf4(%rsp)
movq 0x1b0(%rsp), %rdi
addq $0x60, %rdi
movl 0xdc(%rsp), %eax
movl %eax, %esi
callq 0x4dcb0
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4dcd0
movq %rax, 0x20(%rsp)
jmp 0x48188
movq 0x20(%rsp), %rax
movl %eax, 0xf0(%rsp)
leaq 0xa5(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x2d1e0
movq 0x18(%rsp), %r8
leaq 0xa8(%rsp), %rdi
movl $0xa, %esi
leaq 0xa7(%rsp), %rdx
leaq 0xa6(%rsp), %rcx
callq 0x2d1f0
jmp 0x481cf
leaq 0xa5(%rsp), %rdi
callq 0x2d230
leaq 0x178(%rsp), %rdi
callq 0x2d430
jmp 0x481eb
movq 0x1b0(%rsp), %rdi
addq $0x60, %rdi
movl 0xdc(%rsp), %eax
movl %eax, %esi
callq 0x4dcb0
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x68b80
jmp 0x4821c
leaq 0x178(%rsp), %rdi
callq 0x2d450
jmp 0x4822b
leaq 0x178(%rsp), %rdi
callq 0x2d470
movsd %xmm0, 0x10(%rsp)
jmp 0x48240
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
leaq 0x33171(%rip), %rdi     # 0x7b3d0
movb $0x1, %al
callq 0x9040
jmp 0x48268
movsd 0x1a0(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm1
divsd %xmm1, %xmm0
leaq 0x33164(%rip), %rdi     # 0x7b3e9
movb $0x1, %al
callq 0x9040
jmp 0x4828e
movl 0xf0(%rsp), %esi
leaq 0x3315d(%rip), %rdi     # 0x7b3f9
xorl %eax, %eax
callq 0x9040
jmp 0x482a5
movl 0xf4(%rsp), %esi
leaq 0x3315a(%rip), %rdi     # 0x7b40d
xorl %eax, %eax
callq 0x9040
jmp 0x482bc
leaq 0x108(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x4dce0
jmp 0x482d3
leaq 0xa8(%rsp), %rdi
callq 0x2d420
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x48027
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x4844c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0xa5(%rsp), %rdi
callq 0x2d230
jmp 0x4844c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x2d420
jmp 0x4844c
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movslq 0x94(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x4dd50
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x483d8
movslq 0x94(%rsp), %rsi
leaq 0x108(%rsp), %rdi
callq 0x4dd70
movsd 0x8(%rax), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x48372
cvtsi2sdl 0x1ac(%rsp), %xmm0
movsd 0x1a0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x98(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
leaq 0x3300d(%rip), %rdi     # 0x7b421
movb $0x1, %al
callq 0x9040
jmp 0x4841d
leaq 0x108(%rsp), %rdi
callq 0x4dd90
leaq 0x138(%rsp), %rdi
callq 0x4dc40
leaq 0x160(%rsp), %rdi
callq 0xfa50
addq $0x1b8, %rsp            # imm = 0x1B8
retq
leaq 0x108(%rsp), %rdi
callq 0x4dd90
leaq 0x138(%rsp), %rdi
callq 0x4dc40
leaq 0x160(%rsp), %rdi
callq 0xfa50
movq 0x158(%rsp), %rdi
callq 0x9cf0