// File: 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:
subq $0x2e8, %rsp            # imm = 0x2E8
movq %rsi, 0x58(%rsp)
movb %dl, %al
movq %rdi, 0x2e0(%rsp)
movq %rsi, 0x2d8(%rsp)
andb $0x1, %al
movb %al, 0x2d7(%rsp)
movss %xmm0, 0x2d0(%rsp)
movss %xmm1, 0x2cc(%rsp)
movq 0x2e0(%rsp), %rax
movq %rax, 0x50(%rsp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x24a30
movl %eax, %edi
movl $0x2, %esi
callq 0x24a30
movq 0x58(%rsp), %rsi
movl %eax, %edx
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x45d0
movq 0x60(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x44e0
jmp 0x1d39a
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d3b6
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movl $0x8, %edx
callq 0x44a0
jmp 0x1d3d2
movq 0x50(%rsp), %rax
movl 0xbc(%rsp), %ecx
movl %ecx, 0x9c8(%rax)
movl 0xb0(%rsp), %ecx
movl %ecx, 0x9cc(%rax)
movq %rax, %rdi
addq $0xa70, %rdi            # imm = 0xA70
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x12730
jmp 0x1d40c
movq 0x50(%rsp), %rax
movq %rax, %rdi
addq $0xa20, %rdi            # imm = 0xA20
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x12730
jmp 0x1d42c
movl $0x1, 0x98(%rsp)
movq 0x50(%rsp), %rcx
movl 0x98(%rsp), %eax
cmpl 0x9c8(%rcx), %eax
ja 0x1d4ca
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d467
movq 0x50(%rsp), %rdi
movl 0xbc(%rsp), %eax
movl %eax, 0x4c(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movslq 0x98(%rsp), %rsi
callq 0xe3b0
movl 0x4c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x1d437
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4100
jmp 0x1db62
movl $0x1, 0x94(%rsp)
movq 0x50(%rsp), %rcx
movl 0x94(%rsp), %eax
cmpl 0x9c8(%rcx), %eax
ja 0x1d542
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d505
movq 0x50(%rsp), %rdi
movl 0xbc(%rsp), %eax
movl %eax, 0x48(%rsp)
addq $0xa20, %rdi            # imm = 0xA20
movslq 0x94(%rsp), %rsi
callq 0xe3b0
movl 0x48(%rsp), %ecx
movl %ecx, (%rax)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x1d4d5
movq 0x50(%rsp), %rax
movq %rax, %rdi
addq $0x9d8, %rdi            # imm = 0x9D8
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x24a50
jmp 0x1d562
movq 0x50(%rsp), %rax
movq %rax, %rdi
addq $0xa88, %rdi            # imm = 0xA88
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x24a50
jmp 0x1d582
movl $0x1, 0x90(%rsp)
movq 0x50(%rsp), %rcx
movl 0x90(%rsp), %eax
cmpl 0x9c8(%rcx), %eax
ja 0x1d795
movq 0x50(%rsp), %rdi
addq $0x9d8, %rdi            # imm = 0x9D8
movslq 0x90(%rsp), %rsi
callq 0x24850
movq 0x50(%rsp), %rdi
movq %rax, 0x40(%rsp)
addq $0xa20, %rdi            # imm = 0xA20
movslq 0x90(%rsp), %rsi
callq 0xe3b0
movq 0x40(%rsp), %rdi
movslq (%rax), %rsi
callq 0x12730
jmp 0x1d5eb
movq 0x50(%rsp), %rdi
addq $0xa88, %rdi            # imm = 0xA88
movslq 0x90(%rsp), %rsi
callq 0x24850
movq 0x50(%rsp), %rdi
movq %rax, 0x38(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movslq 0x90(%rsp), %rsi
callq 0xe3b0
movq 0x38(%rsp), %rdi
movslq (%rax), %rsi
callq 0x12730
jmp 0x1d631
movl $0x0, 0x8c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x8c(%rsp), %eax
movl %eax, 0x34(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movslq 0x90(%rsp), %rsi
callq 0xe3b0
movq %rax, %rcx
movl 0x34(%rsp), %eax
cmpl (%rcx), %eax
jge 0x1d6d7
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d687
movq 0x50(%rsp), %rdi
movl 0xbc(%rsp), %eax
movl %eax, 0x30(%rsp)
addq $0xa88, %rdi            # imm = 0xA88
movslq 0x90(%rsp), %rsi
callq 0x24850
movq %rax, %rdi
movslq 0x8c(%rsp), %rsi
callq 0xe3b0
movl 0x30(%rsp), %ecx
movl %ecx, (%rax)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x1d63c
movl $0x0, 0x88(%rsp)
movq 0x50(%rsp), %rdi
movl 0x88(%rsp), %eax
movl %eax, 0x2c(%rsp)
addq $0xa20, %rdi            # imm = 0xA20
movslq 0x90(%rsp), %rsi
callq 0xe3b0
movq %rax, %rcx
movl 0x2c(%rsp), %eax
cmpl (%rcx), %eax
jge 0x1d77d
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d72d
movq 0x50(%rsp), %rdi
movl 0xbc(%rsp), %eax
movl %eax, 0x28(%rsp)
addq $0x9d8, %rdi            # imm = 0x9D8
movslq 0x90(%rsp), %rsi
callq 0x24850
movq %rax, %rdi
movslq 0x88(%rsp), %rsi
callq 0xe3b0
movl 0x28(%rsp), %ecx
movl %ecx, (%rax)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x1d6e2
jmp 0x1d77f
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x1d58d
movq 0x50(%rsp), %rax
movq %rax, %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x24af0
jmp 0x1d7b5
movq 0x50(%rsp), %rax
movq %rax, %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl 0x9c8(%rax), %eax
incl %eax
movl %eax, %esi
callq 0x24af0
jmp 0x1d7d5
testb $0x1, 0x2d7(%rsp)
je 0x1d802
movq 0x50(%rsp), %rdi
movss 0x2d0(%rsp), %xmm0
movss 0x2cc(%rsp), %xmm1
callq 0x1f310
jmp 0x1d7fd
jmp 0x1da11
movl $0x1, 0x80(%rsp)
movq 0x50(%rsp), %rcx
movl 0x80(%rsp), %eax
cmpl 0x9c8(%rcx), %eax
ja 0x1da0f
movq 0x50(%rsp), %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0x24890
movq 0x50(%rsp), %rdi
movq %rax, 0x20(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0xe3b0
movq 0x20(%rsp), %rdi
movslq (%rax), %rsi
callq 0x244a0
jmp 0x1d86d
movq 0x50(%rsp), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0x24890
movq 0x50(%rsp), %rdi
movq %rax, 0x18(%rsp)
addq $0xa20, %rdi            # imm = 0xA20
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0xe3b0
movq 0x18(%rsp), %rdi
movslq (%rax), %rsi
callq 0x244a0
jmp 0x1d8b5
movl $0x0, 0x7c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x14(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0xe3b0
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpl (%rcx), %eax
jge 0x1d956
leaq 0xc0(%rsp), %rdi
leaq 0x84(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d906
movq 0x50(%rsp), %rdi
movss 0x84(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
addq $0xaa0, %rdi            # imm = 0xAA0
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0x24890
movq %rax, %rdi
movslq 0x7c(%rsp), %rsi
callq 0xf1a0
movss 0x10(%rsp), %xmm0
movss %xmm0, (%rax)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1d8bd
movl $0x0, 0x78(%rsp)
movq 0x50(%rsp), %rdi
movl 0x78(%rsp), %eax
movl %eax, 0xc(%rsp)
addq $0xa20, %rdi            # imm = 0xA20
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0xe3b0
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl (%rcx), %eax
jge 0x1d9f7
leaq 0xc0(%rsp), %rdi
leaq 0x84(%rsp), %rsi
movl $0x4, %edx
callq 0x44a0
jmp 0x1d9a7
movq 0x50(%rsp), %rdi
movss 0x84(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
addq $0x9f0, %rdi            # imm = 0x9F0
movl 0x80(%rsp), %eax
movl %eax, %esi
callq 0x24890
movq %rax, %rdi
movslq 0x78(%rsp), %rsi
callq 0xf1a0
movss 0x8(%rsp), %xmm0
movss %xmm0, (%rax)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x1d95e
jmp 0x1d9f9
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x1d80d
jmp 0x1da11
movl $0x1, 0x70(%rsp)
movq 0x50(%rsp), %rcx
movl 0x70(%rsp), %eax
cmpl 0x9c8(%rcx), %eax
ja 0x1db3e
xorps %xmm0, %xmm0
movss %xmm0, 0x74(%rsp)
movl $0x0, 0x6c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x6c(%rsp), %eax
movl %eax, 0x4(%rsp)
addq $0xa70, %rdi            # imm = 0xA70
movslq 0x70(%rsp), %rsi
callq 0xe3b0
movq %rax, %rcx
movl 0x4(%rsp), %eax
cmpl (%rcx), %eax
jge 0x1db2c
movq 0x50(%rsp), %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
movslq 0x70(%rsp), %rsi
callq 0x24890
movq %rax, %rdi
movslq 0x6c(%rsp), %rsi
callq 0xf1a0
movss (%rax), %xmm0
addss 0x74(%rsp), %xmm0
movss %xmm0, 0x74(%rsp)
movss 0x74(%rsp), %xmm0
movss 0x1459f(%rip), %xmm1    # 0x3204c
ucomiss %xmm1, %xmm0
jb 0x1dae3
movq 0x50(%rsp), %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
movslq 0x70(%rsp), %rsi
callq 0x24890
movq %rax, %rdi
movslq 0x6c(%rsp), %rsi
callq 0xf1a0
movss 0x1456f(%rip), %xmm0    # 0x3204c
movss %xmm0, (%rax)
jmp 0x1db1a
movq 0x50(%rsp), %rdi
movss 0x74(%rsp), %xmm0
movss %xmm0, (%rsp)
addq $0xaa0, %rdi            # imm = 0xAA0
movslq 0x70(%rsp), %rsi
callq 0x24890
movq %rax, %rdi
movslq 0x6c(%rsp), %rsi
callq 0xf1a0
movss (%rsp), %xmm0
movss %xmm0, (%rax)
jmp 0x1db1c
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1da3f
jmp 0x1db2e
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x1da19
leaq 0xc0(%rsp), %rdi
callq 0x40d0
jmp 0x1db4d
leaq 0xc0(%rsp), %rdi
callq 0x4100
addq $0x2e8, %rsp            # imm = 0x2E8
retq
movq 0xa0(%rsp), %rdi
callq 0x4690
nop