// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rsi, %r12
movq %rdi, %rbp
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
leaq 0x20(%rdi), %rcx
movq %rcx, 0x28(%rsp)
movups %xmm0, 0x20(%rdi)
movq %rax, 0x30(%rdi)
movb $0x0, 0x3c(%rdi)
movups %xmm0, 0x40(%rdi)
movq (%rsi), %rdi
leaq 0xe7a7(%rip), %rsi      # 0x19596
callq 0x2230
testq %rax, %rax
movq %rbp, 0x18(%rsp)
je 0xb3bc
movq %rax, %r15
movl $0x3, %ebx
leaq 0x60(%rsp), %r13
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0x22e0
testq %rax, %rax
je 0xb357
decl %ebx
jne 0xae0f
leaq 0xe79f(%rip), %rsi      # 0x195d2
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x2050
leaq 0x7(%rax), %rdi
callq 0x22d0
movl %eax, %ebx
leaq 0xe787(%rip), %rsi      # 0x195d9
movq %r14, %rdi
callq 0x2050
leaq 0x7(%rax), %rdi
callq 0x22d0
movl %eax, %r13d
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0x22e0
testq %rax, %rax
je 0xb3f3
movl %ebx, %r14d
movq %rbp, %rdi
movq %r14, %rsi
callq 0xbce0
movl $0x18, %edi
callq 0x21e0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0x18(%rbp)
addl %r13d, %r13d
movq %rax, %rdi
movq %r13, %rsi
callq 0xbd9c
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r14, %rsi
callq 0xc5be
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, 0x40(%rsp)
movq %r15, %rdx
callq 0x22e0
testq %rax, %rax
je 0xb139
xorl %ebp, %ebp
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movabsq $0x100000601, %r15      # imm = 0x100000601
xorl %r12d, %r12d
movl %ebx, 0x3c(%rsp)
movb $0x1, %al
leaq 0x14(%rsp), %rdx
movq %r14, %rcx
leaq 0x24(%rsp), %r14
xorl %esi, %esi
movzbl (%rcx), %edi
cmpq $0x20, %rdi
ja 0xaf25
btq %rdi, %r15
jb 0xaf37
movsbl %dil, %edi
leal (%rsi,%rsi,4), %esi
leal (%rdi,%rsi,2), %esi
addl $-0x30, %esi
incq %rcx
jmp 0xaf16
movl %esi, (%rdx)
movzbl (%rcx), %edx
cmpl $0x20, %edx
je 0xaf46
cmpl $0x9, %edx
jne 0xaf4b
incq %rcx
jmp 0xaf39
movq %r14, %rdx
testb $0x1, %al
movl $0x0, %eax
jne 0xaf14
movl 0x14(%rsp), %eax
cmpl %ebx, %eax
jae 0xb38e
cmpl %ebx, 0x24(%rsp)
jae 0xb38e
cmpl $-0x1, %r13d
setne %cl
testl %eax, %eax
sete %dl
orb %cl, %dl
jne 0xaf96
movq 0x18(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0xb5b0
jmp 0xb0a5
cmpl %eax, %r13d
jae 0xb0a5
movl %r12d, %ebx
movl %r13d, %eax
movq 0x48(%rsp), %rcx
leaq (%rax,%rax,2), %rax
movq 0x8(%rcx,%rax,8), %r12
subq (%rcx,%rax,8), %r12
shrq $0x2, %r12
addl %ebp, %r12d
movl %ebx, 0x8(%rsp)
movl %r12d, 0xc(%rsp)
movb $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rbp
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0xaff1
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rbp)
jmp 0xaffe
movq %rbp, %rdi
leaq 0x8(%rsp), %rdx
callq 0xdb48
testl %r12d, %r12d
jne 0xb060
movq 0x30(%rsp), %rax
cmpb $0x0, (%rax)
jne 0xb060
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
imull $0xaaaaaaab, %edx, %edx # imm = 0xAAAAAAAB
decl %edx
movl %edx, 0x8(%rsp)
movq 0x28(%rbp), %rsi
cmpq 0x30(%rbp), %rsi
je 0xb041
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbp)
jmp 0xb05b
movq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0xf5ac
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movb $0x1, -0x4(%rax,%rcx)
leal 0x1(%r13), %r14d
movq 0x18(%rbp), %rdi
movq 0x8(%rdi), %rsi
movl %r14d, %eax
movq 0x48(%rsp), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rdx
movq 0x8(%rcx,%rax,8), %rcx
callq 0xe46c
addl %ebx, %r12d
addl $0x2, %r13d
xorl %ebp, %ebp
movl %r12d, %ebx
cmpl 0x14(%rsp), %r13d
movl %r14d, %r13d
leaq 0x24(%rsp), %r14
jbe 0xafa2
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0xb0c8
movl 0x24(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0xb0d4
movq %r14, %rdx
callq 0xd7b4
movl 0x24(%rsp), %eax
movl %eax, %eax
movq 0x48(%rsp), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0xb0ff
movl 0x14(%rsp), %r13d
movl %r13d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0xb10e
leaq 0x14(%rsp), %rdx
callq 0xd7b4
movl 0x14(%rsp), %r13d
incl %ebp
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x40(%rsp), %rdx
callq 0x22e0
testq %rax, %rax
movl 0x3c(%rsp), %ebx
jne 0xaf05
movl %r13d, %eax
jmp 0xb143
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x48(%rsp), %rcx
leaq (%rax,%rax,2), %rax
movq 0x8(%rcx,%rax,8), %r15
subq (%rcx,%rax,8), %r15
shrq $0x2, %r15
addl %ebp, %r15d
movl %r12d, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
movb $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rbp
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0xb190
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rbp)
jmp 0xb19d
leaq 0x8(%rsp), %rdx
movq %rbp, %rdi
callq 0xdb48
testl %r15d, %r15d
je 0xb1ac
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
jmp 0xb20f
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
movq 0x30(%rsp), %rdx
cmpb $0x0, (%rdx)
jne 0xb20f
movq %rax, %rdx
subq %rcx, %rdx
movq %rdx, %rsi
shrq $0x2, %rsi
imull $0xaaaaaaab, %esi, %edi # imm = 0xAAAAAAAB
decl %edi
movl %edi, 0x8(%rsp)
movq 0x28(%rbp), %rsi
cmpq 0x30(%rbp), %rsi
je 0xb1ed
movl %edi, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbp)
jmp 0xb20a
leaq 0x8(%rsp), %rdx
movq 0x28(%rsp), %rdi
callq 0xf5ac
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
movq %rax, %rdx
subq %rcx, %rdx
movb $0x1, -0x4(%rcx,%rdx)
subq %rcx, %rax
sarq $0x2, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
subl %ecx, %ebx
jbe 0xb32e
movl %ebx, %r13d
addl %r12d, %r15d
movl %ecx, %eax
leaq (%rax,%rax,2), %rax
leaq 0x8(,%rax,8), %rbx
leaq 0x8(%rsp), %r14
movq 0x48(%rsp), %rax
movq (%rax,%rbx), %r12
subq -0x8(%rax,%rbx), %r12
shrq $0x2, %r12
movl %r15d, 0x8(%rsp)
movl %r12d, 0xc(%rsp)
movb $0x0, 0x10(%rsp)
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0xb286
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rbp)
jmp 0xb291
movq %rbp, %rdi
movq %r14, %rdx
callq 0xdb48
testl %r12d, %r12d
jne 0xb2f1
movq 0x30(%rsp), %rax
cmpb $0x0, (%rax)
jne 0xb2f1
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
imull $0xaaaaaaab, %edx, %edx # imm = 0xAAAAAAAB
decl %edx
movl %edx, 0x8(%rsp)
movq 0x28(%rbp), %rsi
cmpq 0x30(%rbp), %rsi
je 0xb2d4
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbp)
jmp 0xb2ec
movq 0x28(%rsp), %rdi
movq %r14, %rdx
callq 0xf5ac
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
movb $0x1, -0x4(%rax,%rcx)
movq 0x18(%rbp), %rdi
movq 0x8(%rdi), %rsi
movq 0x48(%rsp), %rax
movq -0x8(%rax,%rbx), %rdx
movq (%rax,%rbx), %rcx
callq 0xe46c
addl %r12d, %r15d
addq $0x18, %rbx
decl %r13d
jne 0xb245
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x2, %rax
imull $0xaaaaaaab, %eax, %ecx # imm = 0xAAAAAAAB
movl %ecx, 0x38(%rbp)
movq 0x40(%rsp), %rdi
callq 0x21d0
leaq 0x48(%rsp), %rdi
callq 0xc63c
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16c92(%rip), %rdi     # 0x21ff0
leaq 0xe250(%rip), %rsi      # 0x195b5
movl $0x1c, %edx
callq 0x2200
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x16c71(%rip), %rdi     # 0x21ff0
callq 0x2200
movq %rax, %rdi
callq 0x20b0
jmp 0xb3b2
movq 0x16c5b(%rip), %rdi     # 0x21ff0
leaq 0xe277(%rip), %rsi      # 0x19613
movl $0x6c, %edx
callq 0x2200
movq 0x16c43(%rip), %rdi     # 0x21ff0
callq 0x20b0
movl $0x1, %edi
callq 0x2240
movq 0x16c2d(%rip), %rdi     # 0x21ff0
leaq 0xe1ce(%rip), %rsi      # 0x19598
movl $0x1c, %edx
callq 0x2200
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x16c0c(%rip), %rdi     # 0x21ff0
callq 0x2200
movq %rax, %rdi
callq 0x20b0
jmp 0xb3b2
movq 0x16bf6(%rip), %rdi     # 0x21ff0
leaq 0xe1b4(%rip), %rsi      # 0x195b5
movl $0x1c, %edx
callq 0x2200
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x16bd5(%rip), %rdi     # 0x21ff0
callq 0x2200
movq %rax, %rdi
callq 0x20b0
jmp 0xb3b2
jmp 0xb523
jmp 0xb523
jmp 0xb517
jmp 0xb500
movq %rax, %r14
cmpl $0x1, %edx
jne 0xb51a
movq %r14, %rdi
callq 0x2090
movq %rax, %r14
movq 0x16b94(%rip), %rdi     # 0x21ff0
leaq 0xe196(%rip), %rsi      # 0x195f9
movl $0x19, %edx
callq 0x2200
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq 0x16b73(%rip), %rdi     # 0x21ff0
movq %rax, %rsi
callq 0x21c0
movq %rax, %rdi
callq 0x20b0
jmp 0xb3b2
movq %rax, %r14
callq 0x2300
jmp 0xb51a
movq %rax, %r14
cmpl $0x1, %edx
jne 0xb51a
movq %r14, %rdi
callq 0x2090
movq %rax, %r14
movq 0x16b3a(%rip), %rdi     # 0x21ff0
leaq 0xe123(%rip), %rsi      # 0x195e0
movl $0x18, %edx
callq 0x2200
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq 0x16b19(%rip), %rdi     # 0x21ff0
movq %rax, %rsi
callq 0x21c0
movq %rax, %rdi
callq 0x20b0
jmp 0xb3b2
movq %rax, %r14
callq 0x2300
jmp 0xb51a
movq %rax, %rdi
callq 0xc5b3
jmp 0xb523
movq %rax, %r14
jmp 0xb530
jmp 0xb523
jmp 0xb523
jmp 0xb50d
jmp 0xb523
movq %rax, %r14
movq 0x18(%rsp), %rbp
jmp 0xb526
movq %rax, %r14
movq 0x18(%rsp), %rbp
jmp 0xb530
jmp 0xb523
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0xc63c
movq 0x28(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0xb542
callq 0x21a0
movq (%rbp), %rdi
testq %rdi, %rdi
je 0xb550
callq 0x21a0
movq %r14, %rdi
callq 0x2320