// File: /workspace/github_cpp_makefile_star2_O3/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, 0x44(%rsp)
movss %xmm0, 0x40(%rsp)
movl %edx, %ebp
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), %rdi
incl %esi
movq %rdi, 0x10(%rsp)
callq 0xb36a
leaq 0xa20(%r15), %rdi
movl 0x9c8(%r15), %esi
incl %esi
movq %rdi, 0x20(%rsp)
callq 0xb36a
movl $0x1, %ebx
cmpl $0x0, 0x9c8(%r15)
je 0xdc61
xorl %r14d, %r14d
leaq 0x50(%rsp), %r12
leaq 0xc(%rsp), %r13
movl $0x4, %edx
movq %r12, %rdi
movq %r13, %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 0xdbef
testl %eax, %eax
je 0xdc61
xorl %r14d, %r14d
leaq 0x50(%rsp), %r12
leaq 0xc(%rsp), %r13
movl $0x4, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x3400
movl 0xc(%rsp), %eax
movq 0xa20(%r15), %rcx
movl %eax, 0x4(%rcx,%r14,4)
movl 0x9c8(%r15), %ebx
incq %r14
cmpq %rbx, %r14
jb 0xdc2f
incq %rbx
leaq 0x9d8(%r15), %rdi
movq %rdi, 0x18(%rsp)
movq %rbx, %rsi
callq 0x12798
leaq 0xa88(%r15), %r13
movl 0x9c8(%r15), %esi
incl %esi
movq %r13, %rdi
callq 0x12798
cmpl $0x0, 0x9c8(%r15)
movq %r15, 0x30(%rsp)
je 0xddad
movl %ebp, 0x3c(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl $0x1, %r15d
leaq 0x50(%rsp), %rbp
leaq 0xc(%rsp), %rbx
leaq (%r15,%r15,2), %r12
leaq (,%r12,8), %r14
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
addq %r14, %rdi
movslq (%rax,%r15,4), %rsi
callq 0xb36a
addq (%r13), %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
movq %r14, %rdi
callq 0xb36a
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r15,4)
jle 0xdd3a
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbp, %rdi
movq %rbx, %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 0xdd06
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r15,4)
jle 0xdd84
xorl %r14d, %r14d
movl $0x4, %edx
movq %rbp, %rdi
movq %rbx, %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), %rcx
cmpq %rcx, %r14
jl 0xdd4c
leaq 0x1(%r15), %rcx
movq 0x30(%rsp), %rdx
movl 0x9c8(%rdx), %esi
cmpq %rsi, %r15
movq %rcx, %r15
jb 0xdcbc
incq %rsi
movq 0x30(%rsp), %r15
movl 0x3c(%rsp), %ebp
jmp 0xddb2
movl $0x1, %esi
leaq 0xaa0(%r15), %rdi
movq %rdi, 0x18(%rsp)
callq 0x1281a
leaq 0x9f0(%r15), %r13
movl 0x9c8(%r15), %esi
incl %esi
movq %r13, %rdi
callq 0x1281a
testb %bpl, %bpl
je 0xde00
movq %r15, %rdi
movss 0x40(%rsp), %xmm0
movss 0x44(%rsp), %xmm1
callq 0xed76
movl 0x9c8(%r15), %eax
jmp 0xdf14
cmpl $0x0, 0x9c8(%r15)
je 0xdf12
movl $0x1, %r14d
leaq 0x50(%rsp), %rbx
leaq 0x2c(%rsp), %rbp
movl %r14d, %r14d
leaq (%r14,%r14,2), %r15
leaq (,%r15,8), %r12
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %r12, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r14,4), %rsi
callq 0x125f2
addq (%r13), %r12
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r14,4), %rsi
movq %r12, %rdi
callq 0x125f2
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r14,4)
jle 0xdeaf
xorl %r12d, %r12d
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3400
movss 0x2c(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rax
movss %xmm0, (%rax,%r12,4)
incq %r12
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %r12
jl 0xde73
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax,%r14,4)
jle 0xdef9
xorl %r12d, %r12d
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3400
movss 0x2c(%rsp), %xmm0
movq (%r13), %rax
movq (%rax,%r15,8), %rax
movss %xmm0, (%rax,%r12,4)
incq %r12
movq 0x20(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %r12
jl 0xdec1
incl %r14d
movq 0x30(%rsp), %rax
movl 0x9c8(%rax), %eax
cmpl %eax, %r14d
jbe 0xde1e
jmp 0xdf14
xorl %eax, %eax
testl %eax, %eax
movq 0x10(%rsp), %rcx
je 0xdf71
movq (%rcx), %rcx
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
incl %eax
movl $0x1, %esi
movss 0x9185(%rip), %xmm0     # 0x170bc
movslq (%rcx,%rsi,4), %rdi
testq %rdi, %rdi
jle 0xdf69
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 0xdf4e
incq %rsi
cmpq %rax, %rsi
jne 0xdf37
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 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
jmp 0xdfa9
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x35c0