// File: /workspace/github_cpp_makefile_star2_O1/dtsaras[P]GCW/rwgraph.cpp
// Function: Graph::readGraph_LT(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, float, float)
void Graph::readGraph_LT(const string filename, bool rand_edges, float m, float s){
    ifstream infile(filename, ios::in|ios::binary|ios::ate);
    int v;
    long long u;
    float prob;
    infile.seekg(0, ios::beg);

    // Read Num Nodes + Num Edges
    infile.read(reinterpret_cast<char*>(&v), sizeof(int));
    infile.read(reinterpret_cast<char*>(&u), sizeof(long long));

    numNodes = v;
    numEdges = u;

    // Read Node degrees
    inv_node_deg.resize(numNodes + 1);
    node_deg.resize(numNodes + 1);
    for (int i = 1; i <= numNodes; i++){
        infile.read(reinterpret_cast<char*>(&v), sizeof(int));
        inv_node_deg[i] = v;
    }
    for (int i = 1; i <= numNodes; i++){
        infile.read(reinterpret_cast<char*>(&v), sizeof(int));
        node_deg[i] = v;
    }


    // Read Neighbors
    adjList.resize(numNodes + 1);
    invAdjList.resize(numNodes + 1);
    for (int i = 1; i <= numNodes; i++){
        adjList[i].resize(node_deg[i]);
        invAdjList[i].resize(inv_node_deg[i]);
        // Incoming neighbors
        for (int j = 0; j < inv_node_deg[i]; ++j){
            infile.read(reinterpret_cast<char*>(&v), sizeof(int));
            invAdjList[i][j] = v;
        }
        // Outgoing neighbors
        for (int j = 0; j < node_deg[i]; ++j){
            infile.read(reinterpret_cast<char*>(&v), sizeof(int));
            adjList[i][j] = v;
        }
    }
    weights.resize(numNodes + 1);
    probabilities.resize(numNodes + 1);

    if (rand_edges)
        rand_graph(m, s);
    else{
        float tmp;
        // vector<UI> b;
        // invProbabilities.resize(numNodes + 1);
        for (unsigned int i = 1; i <= numNodes; ++i){
            weights[i].resize(inv_node_deg[i]);
            // invProbabilities[i].resize(inv_node_deg[i]);
            probabilities[i].resize(node_deg[i]);

            // vector<UI> tmp1(inv_node_deg[i] + 1, 0);
            for(int j = 0; j < inv_node_deg[i]; ++j){
                infile.read(reinterpret_cast<char*>(&tmp), sizeof(float));
                weights[i][j] = tmp;
                // weights[i][j] = tmp*UI_MAX;
                // invProbabilities[i][j] = tmp;
                // if (weights[i][j] <= 0)
                //     weights[i][j] = UI_MAX;
            }

            for(int j = 0; j < node_deg[i]; ++j){
                infile.read(reinterpret_cast<char*>(&tmp), sizeof(float));
                probabilities[i][j] = tmp;
            }
        }

    }

    float total_weight;
    for (int i = 1; i <= numNodes; ++i){
        total_weight = 0;
        for (int j = 0; j < inv_node_deg[i]; ++j){
            total_weight += weights[i][j];
            if (total_weight >= 1){
                weights[i][j] = 1;
            }
            else {
                weights[i][j] = total_weight;
            }
        }
    }


    // size_t probs_pos = infile.tellg();
    // vector<UI> b;
    // weights.push_back(b);
    // for (unsigned int i = 1; i <= numNodes; ++i){
    //     vector<float> tmp(node_deg[i] + 1, 0);
    //     vector<UI> tmp1(node_deg[i] + 1, 0);
    //     for(int j = 1;j < node_deg[i] + 1; ++j){
    //         infile.read(reinterpret_cast<char*>(&tmp[j]), sizeof(float));
    //     }
    //     for(int j = 1;j < node_deg[i] + 1; ++j){
    //         tmp[j] += tmp[j-1];
    //         if (tmp[j] >= 1){
    //             tmp1[j] = UI_MAX;
    //         } else {
    //             tmp1[j] = tmp[j]*UI_MAX;
    //         }
    //     }
    //     weights.push_back(tmp1);
    //     node_deg[i]++;
    // }

    // // Read Probabilities
    // infile.seekg(probs_pos);
    // probabilities.resize(numNodes + 1);
    // for (int i = 1; i <= numNodes; i++){
    //     // probabilities[i].resize(node_deg[i]);
    //     for (int j = 0; j < node_deg[i] - 1; ++j){
    //         infile.read(reinterpret_cast<char*>(&prob), sizeof(float));
    //         // probabilities[i][j] = prob;
    //         probabilities[adjList[i][j]].push_back(prob);
    //     }
    // }
    // for (int i = 1; i <= numNodes; i++)
    //     probabilities[i].resize(node_deg[i] - 1);

    infile.close();

    // for (int i = 1; i <= numNodes; i++){
    //     cout << "In deg: " << inv_node_deg[i] << " w_size: " << weights[i].size() << endl;
    // }

    
    
    // cout << "  " << adjList.size() << "  " << probabilities.size() << "  " << weights.size() << endl;
    // for (int v = 1; v <= numNodes - 1; v++){
    //     for (int u = 0; u < node_deg[v]; u++){
    //         cout << v << "  " << adjList[v][u] << "  " << weights[v][u + 1] << endl;
    //     }
    // }

    // cout << " Read the graph *********" << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movss %xmm1, 0x40(%rsp)
movss %xmm0, 0x3c(%rsp)
movl %edx, 0x44(%rsp)
movq %rdi, %r15
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movl $0xe, %edx
callq 0x3530
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3430
leaq 0x50(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x3400
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movl $0x8, %edx
callq 0x3400
movl 0xc(%rsp), %esi
movl %esi, 0x9c8(%r15)
movl 0x48(%rsp), %eax
movl %eax, 0x9cc(%r15)
leaq 0xa70(%r15), %rbp
incl %esi
movq %rbp, %rdi
callq 0xb4e8
leaq 0xa20(%r15), %rdi
movl 0x9c8(%r15), %esi
incl %esi
movq %rdi, 0x20(%rsp)
callq 0xb4e8
cmpl $0x0, 0x9c8(%r15)
je 0xddcd
xorl %r14d, %r14d
leaq 0x50(%rsp), %rbx
leaq 0xc(%rsp), %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3400
movl 0xc(%rsp), %eax
movq 0xa70(%r15), %rcx
movl %eax, 0x4(%rcx,%r14,4)
movl 0x9c8(%r15), %eax
incq %r14
cmpq %rax, %r14
jb 0xdd9e
movl 0x9c8(%r15), %esi
testl %esi, %esi
je 0xde14
xorl %r14d, %r14d
leaq 0x50(%rsp), %rbx
leaq 0xc(%rsp), %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3400
movl 0xc(%rsp), %eax
movq 0xa20(%r15), %rcx
movl %eax, 0x4(%rcx,%r14,4)
movl 0x9c8(%r15), %esi
incq %r14
cmpq %rsi, %r14
jb 0xdde5
leaq 0x9d8(%r15), %rdi
incl %esi
movq %rdi, 0x18(%rsp)
callq 0x129be
leaq 0xa88(%r15), %r13
movl 0x9c8(%r15), %esi
incl %esi
movq %r13, %rdi
callq 0x129be
movl 0x9c8(%r15), %esi
testl %esi, %esi
movq %rbp, 0x10(%rsp)
movq %r15, 0x30(%rsp)
je 0xdf5d
movl $0x1, %r15d
leaq 0x50(%rsp), %rbx
leaq 0xc(%rsp), %rbp
leaq (%r15,%r15,2), %r12
leaq (,%r12,8), %r14
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %r14, %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
callq 0xb4e8
addq (%r13), %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
movq %r14, %rdi
callq 0xb4e8
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r15,4)
jle 0xdeee
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3400
movl 0xc(%rsp), %eax
movq (%r13), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r14
jl 0xdeba
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r15,4)
jle 0xdf38
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3400
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r14
jl 0xdf00
leaq 0x1(%r15), %rax
movq 0x30(%rsp), %rcx
movl 0x9c8(%rcx), %esi
cmpq %rsi, %r15
movq %rax, %r15
jb 0xde68
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rbp
leaq 0xaa0(%r15), %rdi
incl %esi
movq %rdi, 0x18(%rsp)
callq 0x12a40
leaq 0x9f0(%r15), %r13
movl 0x9c8(%r15), %esi
incl %esi
movq %r13, %rdi
callq 0x12a40
cmpb $0x0, 0x44(%rsp)
je 0xdfa8
movq %r15, %rdi
movss 0x3c(%rsp), %xmm0
movss 0x40(%rsp), %xmm1
callq 0xef20
jmp 0xe0c0
cmpl $0x0, 0x9c8(%r15)
je 0xe0b6
movl $0x1, %r12d
leaq 0x50(%rsp), %rbp
leaq 0x2c(%rsp), %rbx
movl %r12d, %r12d
leaq (%r12,%r12,2), %r15
leaq (,%r15,8), %r14
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %r14, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r12,4), %rsi
callq 0x12824
addq (%r13), %r14
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r12,4), %rsi
movq %r14, %rdi
callq 0x12824
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r12,4)
jle 0xe057
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3400
movss 0x2c(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rax
movss %xmm0, (%rax,%r14,4)
incq %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r12,4), %rax
cmpq %rax, %r14
jl 0xe01b
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r12,4)
jle 0xe0a1
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3400
movss 0x2c(%rsp), %xmm0
movq (%r13), %rax
movq (%rax,%r15,8), %rax
movss %xmm0, (%rax,%r14,4)
incq %r14
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r12,4), %rax
cmpq %rax, %r14
jl 0xe069
incl %r12d
movq 0x30(%rsp), %rax
cmpl 0x9c8(%rax), %r12d
jbe 0xdfc6
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rbp
movl 0x9c8(%r15), %eax
testl %eax, %eax
je 0xe120
movq (%rbp), %rcx
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
incl %eax
movl $0x1, %esi
movss 0x9fd6(%rip), %xmm0     # 0x180bc
movslq (%rcx,%rsi,4), %rdi
testq %rdi, %rdi
jle 0xe118
leaq (%rsi,%rsi,2), %r8
movq (%rdx,%r8,8), %r8
xorps %xmm1, %xmm1
xorl %r9d, %r9d
addss (%r8,%r9,4), %xmm1
movaps %xmm0, %xmm2
minss %xmm1, %xmm2
movss %xmm2, (%r8,%r9,4)
incq %r9
cmpq %r9, %rdi
jne 0xe0fd
incq %rsi
cmpq %rax, %rsi
jne 0xe0e6
leaq 0x50(%rsp), %rdi
callq 0x30b0
leaq 0x50(%rsp), %rdi
callq 0x30d0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
jmp 0xe158
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x35c0
nop