// File: /workspace/github_cpp_makefile_star2_O2/iPapatsoris[P]Maximum-Independent-Set/src/Graph.cpp
// Function: Graph::Graph(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool const&)
Graph::Graph(const string &inputFile, const bool &checkIndependentSet) : mapping(false), idToPos(NULL), posToId(NULL) {
    /* Open graph input file */
    FILE *f;
    f = fopen(inputFile.c_str(), "r");
    if (f == NULL) {
        cerr << "Error in opening input file " << inputFile << endl;
        exit(EXIT_FAILURE);
    }
    char buf[MAXLINE];

    /* Skip info lines */
    for (int i = 0 ; i < 3 ; i++) {
        if (fgets(buf, MAXLINE, f) == NULL) {
            cerr << "Error in parsing input file " << inputFile << endl;
            exit(EXIT_FAILURE);
        }
    }

    /* Get total node and edge numbers */
    char *ptr = strstr(buf, "Nodes:");
    uint32_t nodes = atoi(ptr + 7);
    ptr = strstr(buf, "Edges:");
    uint32_t edges = atoi(ptr + 7);
    if (fgets(buf, MAXLINE, f) == NULL) {
        cerr << "Error in parsing input file " << inputFile << endl;
        exit(EXIT_FAILURE);
    }

    try {
        nodeIndex.reserve(nodes);
    }
    catch (const length_error &le) {
        cerr << "NodeIndex length error: " << le.what() << endl;
        exit(EXIT_FAILURE);
    }

    edgeBuffer = new vector<uint32_t>();
    try {
        edgeBuffer->reserve(edges * 2);
    }
    catch (const length_error &le) {
        cerr << "EdgeBuffer length error: " << le.what() << endl;
        exit(EXIT_FAILURE);
    }

    /* Hold reverse direction edges temporarily */
    vector<vector<uint32_t> > reverseEdges(nodes);

    /* Build graph with both edge directions, keep them sorted */
    uint32_t sourceNode, targetNode, previousNode, straightEdges, offset;
    previousNode = NONE;
    straightEdges = 0;
    offset = 0;
    while(fgets(buf, MAXLINE, f) != NULL) {
        parseNodeIDs(buf, &sourceNode, &targetNode);
        if (sourceNode >= nodes || targetNode >= nodes) {
            cerr << "Error: received a node id equal or larger than the total nodes number specified at the beginning of the file" << endl;
            exit(EXIT_FAILURE);
        }
        /* Add smaller nodes than first source node */
        if (previousNode == NONE && sourceNode) {
            this->fill(sourceNode, checkIndependentSet);
        }
        if (previousNode != sourceNode && previousNode != NONE) {
            /* At new source node, add previous nodes' (including those who don't appear as source nodes) smaller neighbors */
            for (uint32_t node = previousNode + 1 ; node <= sourceNode ; node++) {
                uint32_t previousEdges = straightEdges + reverseEdges[node - 1].size();
                nodeIndex.push_back(Graph::NodeInfo(offset, previousEdges));
                if (!previousEdges && !checkIndependentSet) {
                    zeroDegreeNodes.push_back(nodeIndex.size()-1);
                    nodeIndex[nodeIndex.size()-1].removed = true;
                }
                offset += previousEdges;
                edgeBuffer->insert(edgeBuffer->end(), reverseEdges[node].begin(), reverseEdges[node].end());
                straightEdges = 0;
                if (node < sourceNode) {
                    //cout << "in between missing node " << node << endl;
                }
            }
        }
        /* Add source node's bigger neighbors */
        edgeBuffer->push_back(targetNode);
        straightEdges++;
        reverseEdges[targetNode].push_back(sourceNode);
        previousNode = sourceNode;
    }
    /* Add final source node's info */
    uint32_t previousEdges = straightEdges + reverseEdges[previousNode].size();
    nodeIndex.push_back(Graph::NodeInfo(offset, previousEdges));
    if (!previousEdges && !checkIndependentSet) {
        zeroDegreeNodes.push_back(nodeIndex.size()-1);
        nodeIndex[nodeIndex.size()-1].removed = true;
    }
    offset += previousEdges;
    /* Add smaller neighbors of the nodes left that don't appear as source nodes */
    for (uint32_t missingNode = nodeIndex.size() ; missingNode < nodes ; missingNode++) {
        //cout << "end missing node " << missingNode << endl;
        previousEdges = reverseEdges[missingNode].size();
        nodeIndex.push_back(Graph::NodeInfo(offset, previousEdges));
        if (!previousEdges && !checkIndependentSet) {
            zeroDegreeNodes.push_back(nodeIndex.size()-1);
            nodeIndex[nodeIndex.size()-1].removed = true;
        }
        offset += previousEdges;
        edgeBuffer->insert(edgeBuffer->end(), reverseEdges[missingNode].begin(), reverseEdges[missingNode].end());
    }
    nextUnusedId = nodeIndex.size();
    fclose(f);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x20(%rdi), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movb $0x0, 0x3c(%rdi)
movups %xmm0, 0x40(%rdi)
movq (%rsi), %rdi
leaq 0xe323(%rip), %rsi      # 0x1759a
callq 0x21d0
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x9674
pushq $0x4
popq %rbp
leaq 0x60(%rsp), %r12
decl %ebp
je 0x92d8
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x28(%rsp), %rdx
callq 0x2280
testq %rax, %rax
jne 0x9292
movq 0x18d3c(%rip), %rdi     # 0x21ff0
leaq 0xe2fe(%rip), %rsi      # 0x175b9
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x966c
leaq 0xe2f7(%rip), %rsi      # 0x175d6
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x2040
leaq 0x7(%rax), %rdi
callq 0x2270
movl %eax, %ebp
leaq 0xe2df(%rip), %rsi      # 0x175dd
movq %r13, %rdi
callq 0x2040
leaq 0x7(%rax), %rdi
callq 0x2270
movl %eax, %r12d
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x28(%rsp), %rdx
callq 0x2280
testq %rax, %rax
je 0x969c
movl %ebp, 0x34(%rsp)
movl %ebp, %esi
movq %rbx, %rdi
movq %rsi, 0x40(%rsp)
callq 0xa06c
pushq $0x18
popq %rdi
callq 0x2190
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x18(%rbx)
addl %r12d, %r12d
movq %rax, %rdi
movq %r12, %rsi
callq 0xa11c
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq 0x40(%rsp), %rsi
callq 0xa944
movq %r15, 0x38(%rsp)
pushq $-0x1
popq %rbp
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
xorl %r14d, %r14d
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x28(%rsp), %rdx
callq 0x2280
testq %rax, %rax
je 0x94ea
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
callq 0x97c8
movl 0xc(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jae 0x9651
cmpl %ecx, 0x1c(%rsp)
jae 0x9651
cmpl $-0x1, %ebp
setne %cl
testl %eax, %eax
sete %dl
orb %cl, %dl
movq 0x38(%rsp), %r15
jne 0x9401
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
movq %r15, %rdx
callq 0x981e
jmp 0x94b9
cmpl %eax, %ebp
sete %al
cmpl $-0x1, %ebp
sete %cl
orb %al, %cl
jne 0x94b9
movl %ebp, %eax
leal 0x1(%rax), %ebp
cmpl 0xc(%rsp), %ebp
ja 0x94b9
movl %eax, %eax
movq 0x48(%rsp), %rcx
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %rdx
subq (%rcx,%rax), %rdx
shrq $0x2, %rdx
addl %edx, %r14d
movl %r13d, 0x10(%rsp)
movl %r14d, 0x14(%rsp)
movb $0x0, 0x18(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc224
testl %r14d, %r14d
jne 0x948d
cmpb $0x0, (%r15)
jne 0x948d
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
decl %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0xde6c
movq 0x8(%rbx), %rax
movb $0x1, -0x4(%rax)
addl %r14d, %r13d
movq 0x18(%rbx), %rdi
movq 0x8(%rdi), %rsi
movl %ebp, %eax
movq 0x48(%rsp), %rcx
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rdx
movq 0x8(%rcx,%rax), %rcx
xorl %r14d, %r14d
callq 0xa81c
jmp 0x9414
movq 0x18(%rbx), %rdi
leaq 0x1c(%rsp), %rsi
callq 0xa1ac
movl 0x1c(%rsp), %eax
imulq $0x18, %rax, %rdi
addq 0x48(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0xa1ac
incl %r14d
movl 0xc(%rsp), %ebp
jmp 0x938c
movl %ebp, %eax
movq 0x48(%rsp), %rcx
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %rdx
subq (%rcx,%rax), %rdx
shrq $0x2, %rdx
addl %edx, %r14d
leaq 0x10(%rsp), %rsi
movl %r13d, (%rsi)
movl %r14d, 0x4(%rsi)
movb $0x0, 0x8(%rsi)
movq %rbx, %rdi
callq 0xc224
movq 0x38(%rsp), %r15
testl %r14d, %r14d
je 0x9530
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
jmp 0x9566
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
cmpb $0x0, (%r15)
jne 0x9566
subq %rcx, %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
decl %eax
leaq 0x10(%rsp), %rsi
movl %eax, (%rsi)
movq 0x20(%rsp), %rdi
callq 0xde6c
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
movb $0x1, -0x4(%rax)
subq %rcx, %rax
addl %r13d, %r14d
cqto
pushq $0xc
popq %rcx
idivq %rcx
movl %eax, %r13d
imulq $0x18, %r13, %rbp
addq $0x8, %rbp
cmpq 0x40(%rsp), %r13
jae 0x9619
movq 0x48(%rsp), %rax
movq (%rax,%rbp), %r12
subq -0x8(%rax,%rbp), %r12
shrq $0x2, %r12
movl %r14d, 0x10(%rsp)
movl %r12d, 0x14(%rsp)
movb $0x0, 0x18(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc224
testl %r12d, %r12d
jne 0x95ef
cmpb $0x0, (%r15)
jne 0x95ef
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
decl %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xde6c
movq 0x8(%rbx), %rax
movb $0x1, -0x4(%rax)
movq 0x18(%rbx), %rdi
movq 0x8(%rdi), %rsi
movq 0x48(%rsp), %rax
movq -0x8(%rax,%rbp), %rdx
movq (%rax,%rbp), %rcx
callq 0xa81c
addl %r12d, %r14d
incq %r13
addq $0x18, %rbp
jmp 0x957f
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
movl %eax, 0x38(%rbx)
movq 0x28(%rsp), %rdi
callq 0x2180
leaq 0x48(%rsp), %rdi
callq 0xa996
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18998(%rip), %rdi     # 0x21ff0
leaq 0xdfb8(%rip), %rsi      # 0x17617
callq 0x2170
movq %rax, %rdi
callq 0x20a0
pushq $0x1
popq %rdi
callq 0x21e0
movq 0x18975(%rip), %rdi     # 0x21ff0
leaq 0xdf1a(%rip), %rsi      # 0x1759c
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x966c
movq 0x1894d(%rip), %rdi     # 0x21ff0
leaq 0xdf0f(%rip), %rsi      # 0x175b9
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x966c
jmp 0x97a1
jmp 0x9792
jmp 0x97a1
jmp 0x9792
movq %rax, %r14
cmpl $0x1, %edx
jne 0x97ae
movq %r14, %rdi
callq 0x2090
movq %rax, %r14
movq 0x188fa(%rip), %rdi     # 0x21ff0
leaq 0xdf00(%rip), %rsi      # 0x175fd
callq 0x2170
movq %rax, %r15
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20a0
jmp 0x966c
movq %rax, %r14
callq 0x22a0
jmp 0x97ae
movq %rax, %r14
cmpl $0x1, %edx
jne 0x97ae
movq %r14, %rdi
callq 0x2090
movq %rax, %r14
movq 0x188a6(%rip), %rdi     # 0x21ff0
leaq 0xde93(%rip), %rsi      # 0x175e4
callq 0x2170
movq %rax, %r15
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20a0
jmp 0x966c
movq %rax, %r14
callq 0x22a0
jmp 0x97ae
movq %rax, %rdi
callq 0xa939
jmp 0x9792
jmp 0x97a1
jmp 0x97a1
movq %rax, %r14
jmp 0x97ae
jmp 0x97a1
jmp 0x97a1
jmp 0x97a1
jmp 0x97a1
jmp 0x97a1
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0xa996
movq 0x20(%rsp), %rdi
callq 0xb872
movq %rbx, %rdi
callq 0xb864
movq %r14, %rdi
callq 0x22c0