// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
leaq 0x50(%rsp), %rbx
pushq $0xe
popq %rdx
movq %rbx, %rdi
callq 0x3500
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3410
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x33e0
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x33e0
movl 0x8(%rsp), %esi
movl %esi, 0x9c8(%r13)
movl 0x48(%rsp), %eax
movl %eax, 0x9cc(%r13)
leaq 0xa70(%r13), %rdi
incl %esi
movq %rdi, 0x18(%rsp)
callq 0x9252
leaq 0xa20(%r13), %rdi
movl 0x9c8(%r13), %esi
incl %esi
movq %rdi, 0x30(%rsp)
callq 0x9252
pushq $0x1
popq %r15
leaq 0x50(%rsp), %rbx
leaq 0x8(%rsp), %r14
pushq $0x4
popq %r12
movl 0x9c8(%r13), %esi
cmpq %rsi, %r15
ja 0xb73c
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x33e0
movl 0x8(%rsp), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
jmp 0xb70d
pushq $0x1
popq %r15
leaq 0x50(%rsp), %rbx
leaq 0x8(%rsp), %r14
pushq $0x4
popq %r12
movl %esi, %eax
cmpq %rax, %r15
ja 0xb77e
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x33e0
movl 0x8(%rsp), %eax
movq 0xa20(%r13), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
movl 0x9c8(%r13), %esi
jmp 0xb74e
leaq 0x9d8(%r13), %rdi
incl %esi
movq %rdi, 0x20(%rsp)
callq 0xebd4
leaq 0xa88(%r13), %rdi
movl 0x9c8(%r13), %esi
incl %esi
movq %rdi, 0x10(%rsp)
callq 0xebd4
movl %ebp, 0xc(%rsp)
movq %r13, 0x38(%rsp)
pushq $0x1
popq %r15
leaq 0x50(%rsp), %rbp
leaq 0x8(%rsp), %rbx
pushq $0x4
popq %r14
movq 0x38(%rsp), %rax
movl 0x9c8(%rax), %esi
cmpq %rsi, %r15
ja 0xb894
imulq $0x18, %r15, %r12
movq 0x20(%rsp), %rax
movq (%rax), %rdi
addq %r12, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
callq 0x9252
movq 0x10(%rsp), %rax
movq (%rax), %rdi
addq %r12, %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
callq 0x9252
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r13
jge 0xb851
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x33e0
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0xb819
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r13
jge 0xb88c
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x33e0
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0xb854
incq %r15
jmp 0xb7c6
movq 0x38(%rsp), %rbx
leaq 0xaa0(%rbx), %rdi
incq %rsi
movq %rdi, 0x10(%rsp)
callq 0xec0a
movl 0xc(%rsp), %ebp
leaq 0x9f0(%rbx), %rdi
movl 0x9c8(%rbx), %esi
incl %esi
movq %rdi, 0x20(%rsp)
callq 0xec0a
testb %bpl, %bpl
je 0xb96e
movq %rbx, %rdi
movss 0x40(%rsp), %xmm0
movss 0x44(%rsp), %xmm1
callq 0xc67e
movl 0x9c8(%rbx), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
incl %eax
pushq $0x1
popq %rdx
xorl %esi, %esi
movss 0x7788(%rip), %xmm0     # 0x1308c
cmpq %rax, %rdx
je 0xb948
movl (%rcx,%rdx,4), %edi
imulq $0x18, %rdx, %r8
movq 0x10(%rsp), %r9
addq (%r9), %r8
testl %edi, %edi
cmovlel %esi, %edi
xorps %xmm1, %xmm1
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0xb943
movq (%r8), %r10
addss (%r10,%r9,4), %xmm1
movaps %xmm0, %xmm2
minss %xmm1, %xmm2
movss %xmm2, (%r10,%r9,4)
incq %r9
jmp 0xb923
incq %rdx
jmp 0xb904
leaq 0x50(%rsp), %rdi
callq 0x30b0
leaq 0x50(%rsp), %rdi
callq 0x30e0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rcx
leaq 0x50(%rsp), %rbp
leaq 0x2c(%rsp), %rbx
pushq $0x4
popq %r12
movq 0x38(%rsp), %rax
movl 0x9c8(%rax), %eax
cmpl %eax, %ecx
ja 0xb8ed
movl %ecx, 0xc(%rsp)
movl %ecx, %r15d
imulq $0x18, %r15, %r14
movq 0x10(%rsp), %rax
movq (%rax), %rdi
addq %r14, %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
callq 0xea98
movq 0x20(%rsp), %rax
movq (%rax), %rdi
addq %r14, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rsi
callq 0xea98
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r13
jge 0xba14
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x33e0
movss 0x2c(%rsp), %xmm0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r14), %rax
movss %xmm0, (%rax,%r13,4)
incq %r13
jmp 0xb9d8
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%r15,4), %rax
cmpq %rax, %r13
jge 0xba53
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x33e0
movss 0x2c(%rsp), %xmm0
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r14), %rax
movss %xmm0, (%rax,%r13,4)
incq %r13
jmp 0xba17
movl 0xc(%rsp), %ecx
incl %ecx
jmp 0xb97f
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
jmp 0xba70
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3590
nop