// File: /workspace/github_cpp_makefile_star2_O2/fjamour[P]icentral/src/experiments.cc
// Function: insertion_test_qube_hash(graph_t&, int, std::vector<std::pair<unsigned int, unsigned int>, std::allocator<std::pair<unsigned int, unsigned int>>>, bool, int)
void insertion_test_qube_hash(graph_t& graph,
        int num_edges_to_insert,
        vector<edge_t> rand_edge_vec,
        bool use_incremental_algo,
        int max_iter)
{
    //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());
    graph_hash_t g;
    g.fill_graph(graph);
    
    double brandes_time, muc_find_time;
    timer tm;
    
    printf("Brandes runtime: \t");
    tr1_map_t(double) bc_vec;
    tm.start();
    bc_vec = brandes_bc_hash_out(graph, max_iter);
    tm.stop();
    if(max_iter == -1)
        brandes_time = tm.interval();
    else
        brandes_time = (tm.interval()/max_iter)*graph.size();
    printf("%f\n", brandes_time);
    printf("Brandes avg iter time: \t%f\n", brandes_time/graph.size());
    
    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;
    if(rand_edge_vec.size() == 0) {
        printf("Generating edges...\n");
        gen_rand_edges(num_edges_to_insert, graph, rand_edge_vec);
    }
    vector<edge_stat_t> edge_stat_vec;
    
    double tot_speedup = 0;
    double tot_ideal_speedup = 0;
    
    vector<double> speedup_vec;
    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();

        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();
        if(!use_incremental_algo) {
            graph.muc_vec[muc_id].compute_bc(bc_map, max_iter);
        } else {
            graph.muc_vec[muc_id].compute_bc_inc(bc_map, rand_edge.first, rand_edge.second);
        }
        tm.stop();
        if(max_iter == -1)
            edge_stat.muc_bc_update_time = tm.interval();
        else
            edge_stat.muc_bc_update_time = (tm.interval()/max_iter)*edge_stat.muc_num_nodes;
        //TMP CODE XXX FIX THIS SHIT
        edge_stat.muc_bc_update_time += edge_stat.edge_ins_time;
        ////
        
        double speedup = brandes_time/edge_stat.muc_bc_update_time;
        tot_speedup += speedup;
        speedup_vec.push_back(speedup);
        //estimating ideal speedup using the node and edge proportions
        double ideal_speedup;
        double V  = graph.size(); double E = graph.edge_set.size();
        double Vm = edge_stat.muc_num_nodes; double Em = edge_stat.muc_num_edges;
        ideal_speedup = ((V+E)/(Vm+Em))*(V/Vm);
        tot_ideal_speedup += ideal_speedup;
        ////
        
        printf("\tMUC # nodes: \t\t%d (%f G_n)\n",
                edge_stat.muc_num_nodes,
                edge_stat.muc_num_nodes/(double)graph.size());
        printf("\tMUC # edges: \t\t%d (%f G_M)\n",
                edge_stat.muc_num_edges,
                edge_stat.muc_num_edges/(double)graph.edge_set.size());
        printf("\n");
        printf("\tTime to update BC: \t%f\n", edge_stat.muc_bc_update_time);
        printf("\tSpeedup: \t\t%f (Ideal: %f)\n", speedup, ideal_speedup);
        printf("\n");
        printf("\tQUBE avg iter time: \t%f\n",
                edge_stat.muc_bc_update_time/edge_stat.muc_num_nodes);
        printf("\tTime to update MUCs: \t%f\n", edge_stat.edge_ins_time);

        edge_stat_vec.push_back(edge_stat);
    }
    
    double avg_speedup = tot_speedup/num_edges_to_insert;
    double avg_ideal_speedup = tot_ideal_speedup/num_edges_to_insert;
    printf("[[Average speedup: %f (Ideal: %f)]]\n",
        avg_speedup,
        avg_ideal_speedup);
    
    //calculating 95% confidence interval
    double s_mean, s_median, s_stddev, s_min, s_max;
    simple_stats(speedup_vec, s_mean, s_stddev, s_median, s_min, s_max);
    double diff = 1.96*s_stddev/sqrt(num_edges_to_insert);
    double v1 = s_mean - diff;
    double v2 = s_mean + diff;
    printf("[[Speedup 95 CI: %f - %f]]\n", v1, v2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl %r8d, %ebp
movl %ecx, 0x5c(%rsp)
movq %rdx, 0x40(%rsp)
movl %esi, %r14d
movq %rdi, %r12
leaq 0x15813(%rip), %rdi     # 0x28b00
callq 0x2360
movq 0x108(%r12), %rsi
leaq 0x14f0f(%rip), %rdi     # 0x28210
xorl %eax, %eax
callq 0x2030
movq %r12, %rdi
callq 0x18d3c
leaq 0x14f07(%rip), %rdi     # 0x2821e
movq %rax, %rsi
xorl %eax, %eax
callq 0x2030
movq 0x58(%r12), %rsi
leaq 0x14f06(%rip), %rdi     # 0x28233
xorl %eax, %eax
callq 0x2030
leaq 0x170(%rsp), %rbx
movq %rbx, %rdi
callq 0x11468
movq %rbx, %rdi
movq %r12, %rsi
callq 0x17db0
leaq 0x14ef2(%rip), %rdi     # 0x28248
xorl %eax, %eax
callq 0x2030
leaq 0x140(%rsp), %rdi
pushq $0xa
popq %rsi
leaq 0xb0(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
leaq 0x80(%rsp), %r8
callq 0xb770
leaq 0x110(%rsp), %rdi
callq 0xb094
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x946d
leaq 0x140(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x164f6
leaq 0xb0(%rsp), %rdi
callq 0xb070
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0xb09c
movq %rbx, %rdi
callq 0xb0a8
movaps %xmm0, 0x60(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x78(%rsp)
cmpl $-0x1, %ebp
je 0x1342d
movq %r12, %rdi
callq 0x18d3c
movapd 0x60(%rsp), %xmm0
divsd 0x78(%rsp), %xmm0
movq %rax, %xmm2
punpckldq 0x14ddd(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x14de5(%rip), %xmm2    # 0x28200
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
jmp 0x13433
movapd 0x60(%rsp), %xmm0
leaq 0x14fe0(%rip), %rdi     # 0x2841a
movapd %xmm0, 0x60(%rsp)
movb $0x1, %al
callq 0x2030
movq %r12, %rdi
callq 0x18d3c
movq %rax, %xmm0
punpckldq 0x14d94(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x14d9c(%rip), %xmm0    # 0x28200
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x60(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x14e98(%rip), %rdi     # 0x28319
movb $0x1, %al
callq 0x2030
leaq 0x14dcc(%rip), %rdi     # 0x2825b
xorl %eax, %eax
callq 0x2030
leaq 0x110(%rsp), %rdi
callq 0xb094
movq %r12, %rdi
callq 0x2058a
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0xb09c
movq %rbx, %rdi
callq 0xb0a8
movsd %xmm0, 0x10(%rsp)
movq %r12, %rdi
callq 0x20b20
leaq 0x14e10(%rip), %rdi     # 0x282e8
movq %rax, %rsi
xorl %eax, %eax
callq 0x2030
leaq 0x14d7d(%rip), %rdi     # 0x28266
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
movq 0x40(%rsp), %rcx
movq 0x8(%rcx), %rax
cmpq (%rcx), %rax
jne 0x13520
leaq 0x155e1(%rip), %rdi     # 0x28aec
callq 0x2360
movl %r14d, %edi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
callq 0x12cba
movl %r14d, 0x58(%rsp)
movl %ebp, 0xc(%rsp)
leaq 0xe0(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
leaq 0x80(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x110(%rsp), %r13
movsd %xmm0, 0x48(%rsp)
xorl %r14d, %r14d
movq 0x40(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x1387e
movl (%rax,%r14,8), %ebp
movl 0x4(%rax,%r14,8), %ebx
leaq 0x14ce8(%rip), %rdi     # 0x2827e
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2030
movq %r13, %rdi
callq 0xb094
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x20972
movq %r13, %rdi
callq 0xb09c
movq %r13, %rdi
callq 0xb0a8
movsd %xmm0, 0x28(%rsp)
movq 0x18(%r12), %rax
movq 0x60(%r12), %rcx
movl (%rax,%rbp,4), %eax
imulq $0x568, %rax, %r15      # imm = 0x568
movl 0xb8(%rcx,%r15), %eax
movl %eax, 0x3c(%rsp)
movl 0x78(%rcx,%r15), %eax
movl %eax, 0x38(%rsp)
leaq 0xb0(%rsp), %rdi
pushq $0xa
popq %rsi
movq %rsp, %rdx
leaq 0xa8(%rsp), %rcx
leaq 0xa0(%rsp), %r8
callq 0xb770
movq %r13, %rdi
callq 0xb094
addq 0x60(%r12), %r15
cmpb $0x0, 0x5c(%rsp)
je 0x13649
movq %r15, %rdi
leaq 0xb0(%rsp), %rsi
movl %ebp, %edx
movl %ebx, %ecx
pushq $-0x1
popq %r8
callq 0x21612
movl 0xc(%rsp), %ebx
jmp 0x1365f
movq %r15, %rdi
leaq 0xb0(%rsp), %rsi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
callq 0x20b58
movq %r13, %rdi
callq 0xb09c
movq %r13, %rdi
callq 0xb0a8
cmpl $-0x1, %ebx
je 0x13684
divsd 0x78(%rsp), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
mulsd %xmm1, %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movapd 0x60(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
movsd %xmm1, (%rsp)
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x163a4
movq %r12, %rdi
callq 0x18d3c
movq %rax, %r15
movsd 0x58(%r12), %xmm0
movaps %xmm0, 0x130(%rsp)
movl 0x38(%rsp), %ebx
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r12, %rdi
callq 0x18d3c
movq %r15, %xmm0
movq 0x14af9(%rip), %xmm3    # 0x281f0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x14afd(%rip), %xmm1    # 0x28200
subpd %xmm1, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
movdqa 0x130(%rsp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm1, %xmm0
movapd %xmm1, %xmm4
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
cvtsi2sd %ebx, %xmm0
addsd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm5
addsd %xmm0, %xmm5
divsd %xmm5, %xmm1
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x14bb9(%rip), %rdi     # 0x28335
movl %ebx, %esi
movb $0x1, %al
callq 0x2030
movl 0x3c(%rsp), %esi
cvtsi2sd %esi, %xmm0
movsd 0x58(%r12), %xmm1
unpcklps 0x14a55(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x14a5d(%rip), %xmm1    # 0x28200
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x14b98(%rip), %rdi     # 0x28352
movb $0x1, %al
callq 0x2030
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x2300
movsd 0x30(%rsp), %xmm0
leaq 0x14ad7(%rip), %rdi     # 0x282af
movb $0x1, %al
callq 0x2030
movsd (%rsp), %xmm0
leaq 0x14b84(%rip), %rdi     # 0x2836f
movaps 0x10(%rsp), %xmm1
movb $0x2, %al
callq 0x2030
movl %ebx, %edi
callq 0x2300
movsd 0x30(%rsp), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
divsd %xmm1, %xmm0
leaq 0x14b76(%rip), %rdi     # 0x2838b
movb $0x1, %al
callq 0x2030
movsd 0x28(%rsp), %xmm0
leaq 0x14a6b(%rip), %rdi     # 0x28294
movb $0x1, %al
callq 0x2030
leaq 0xe0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1637e
movsd 0x48(%rsp), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x50(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xb070
incq %r14
jmp 0x1356a
cvtsi2sdl 0x58(%rsp), %xmm2
movsd 0x48(%rsp), %xmm0
divsd %xmm2, %xmm0
movsd 0x50(%rsp), %xmm1
movsd %xmm2, 0x10(%rsp)
divsd %xmm2, %xmm1
leaq 0x14b00(%rip), %rdi     # 0x283a5
movb $0x2, %al
callq 0x2030
leaq 0xf8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x163c6
leaq 0xf8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %rsp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0xa8(%rsp), %r8
leaq 0xa0(%rsp), %r9
callq 0x26ad4
leaq 0xf8(%rsp), %rdi
callq 0x5754
movsd (%rsp), %xmm1
mulsd 0x148b0(%rip), %xmm1    # 0x281b8
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm2
ucomisd %xmm0, %xmm2
movapd %xmm2, %xmm0
jb 0x13922
sqrtsd %xmm0, %xmm0
jmp 0x13933
movsd %xmm1, 0x10(%rsp)
callq 0x21d0
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0xb0(%rsp), %xmm2
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
addsd %xmm2, %xmm1
leaq 0x14a77(%rip), %rdi     # 0x283ca
movb $0x2, %al
callq 0x2030
leaq 0x80(%rsp), %rdi
callq 0x5754
leaq 0xe0(%rsp), %rdi
callq 0x16772
leaq 0x140(%rsp), %rdi
callq 0xb070
leaq 0x170(%rsp), %rdi
callq 0x1173e
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x5754
jmp 0x139f1
jmp 0x139d6
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0xb070
jmp 0x13a0b
jmp 0x139cf
jmp 0x139ca
movq %rax, %rbx
jmp 0x13a18
movq %rax, %rbx
jmp 0x13a0b
jmp 0x139d6
movq %rax, %rbx
jmp 0x139f1
jmp 0x139e1
jmp 0x139e1
jmp 0x139e1
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0xb070
leaq 0x80(%rsp), %rdi
callq 0x5754
leaq 0xe0(%rsp), %rdi
callq 0x16772
leaq 0x140(%rsp), %rdi
callq 0xb070
leaq 0x170(%rsp), %rdi
callq 0x1173e
movq %rbx, %rdi
callq 0x23d0