// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/graph.cpp
// Function: build_from_ashiip(Graph*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void build_from_ashiip(Graph* g, std::string file_path)
{
    /*!
     * @brief Builds a graph from scratch, from the nodes and edges generated by 
     * aSHIIP and contained in a specific file format. Adds a random weight drawn 
     * by an exponential distribution to each edge.
     * 
     * @param file_path The path to the file 
     * 
     */

    std::ifstream is(file_path);
    assert(("No such file", is.is_open()));

    std::unordered_map<node_label_t, Node*> created_nodes;
    std::unordered_map<Node*, std::unordered_set<Node*> > links;

    std::default_random_engine generator;
    std::exponential_distribution<double> distribution(1);

    std::string line;

    while (std::getline(is, line)) {

        std::istringstream iss(line);

        // node's label (here, we want positive integers)
        node_label_t label;
        iss >> label;

        if (label <= 0) {
            continue;
        }

        Node* n_in;
        if (created_nodes.find(label) == created_nodes.end()) {
            n_in = new Node(label);
            created_nodes.insert(std::pair<node_label_t, Node*>(label, n_in));
        } else {
            n_in = created_nodes.at(label);
        }

        // type of node (not useful here)
        std::string cat;
        iss >> cat;

        // edges
        std::string other_node;
        int j = 0;
        while (iss >> other_node) {

            if (j == 0) {
                other_node = other_node.substr(1); // eliminating bracket
            }

            int length = other_node.size();
            other_node = other_node.substr(0, length-1); // eliminating comma
            node_label_t other_label = stoi(other_node);

            Node* n_out;
            if (created_nodes.find(other_label) == created_nodes.end()) {
                n_out = new Node(other_label);
                created_nodes.insert(std::pair<node_label_t, Node*>(other_label, n_out));
            } else {
                n_out = created_nodes.at(other_label);
            }

            // avoid double insertion of same edges
            Node* master_node = (n_in->label > n_out->label) ? n_in : n_out;
            Node* slave_node = (master_node == n_in) ? n_out : n_in;

            bool force_insert = false;

            if (links.find(master_node) == links.end()) {
                links.insert(std::pair<Node*, std::unordered_set<Node*> >(master_node, std::unordered_set<Node*>()));
                force_insert = true;
            }

            std::unordered_set<Node*>& linked_nodes = links.at(master_node);
            if (linked_nodes.find(slave_node) == linked_nodes.end()) {
                linked_nodes.insert(slave_node);
                force_insert = true;
            }

            if (force_insert == true) {
                Edge* e = new Edge(n_in, n_out, distribution(generator));
                g->add_edge(e);
            }

            j += 1;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
movq %rdi, 0xb8(%rsp)
leaq 0x2f0(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x3430
leaq 0x368(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0x4626
leaq 0xf8(%rsp), %rax
movq %rax, -0x30(%rax)
movl $0x1, %ecx
movq %rcx, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x130(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0xc0(%rsp), %rax
movq %rcx, (%rax)
leaq 0xa8(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x98(%rsp), %r15
leaq 0x170(%rsp), %rbp
movq 0x2f0(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x34c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x458c
movq %rbp, %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x3460
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3160
movslq 0xc(%rsp), %rbx
testq %rbx, %rbx
jle 0x456b
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdi
movq %rbx, %rax
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %rax
testq %rax, %rax
leaq 0x58(%rsp), %r14
je 0x40e6
movq %rdx, %rcx
movq (%rax), %r8
movl 0x8(%r8), %r9d
movq %r8, %r10
cmpl %r9d, %ebx
jne 0x409d
movq 0x10(%r8), %r13
jmp 0x411a
movq (%r10), %r10
testq %r10, %r10
je 0x40e6
movslq 0x8(%r10), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x40e6
cmpl %esi, %ebx
jne 0x409d
cmpl %r9d, %ebx
je 0x4097
movq (%r8), %r8
testq %r8, %r8
je 0x461a
movslq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x461a
cmpl %esi, %ebx
jne 0x40bf
jmp 0x4097
movl $0x18, %edi
callq 0x3280
movq %rax, %r13
movq %rax, %rdi
movl %ebx, %esi
callq 0x94dc
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rsp)
movq %r13, 0x60(%rsp)
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x5702
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movb $0x0, 0x48(%rsp)
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0x3350
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
xorl %r15d, %r15d
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4523
testl %r15d, %r15d
leaq 0x10(%rsp), %rbx
jne 0x41c1
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rsi
movq $-0x1, %rcx
callq 0x32a0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3330
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x41c1
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x18(%rsp), %rcx
shlq $0x20, %rcx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %rcx
sarq $0x20, %rcx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32a0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3330
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x420f
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x10(%rsp), %r12
callq 0x3050
movq %rax, %rbx
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r12, %rdi
movq %r14, %rsi
movl $0xa, %edx
callq 0x33a0
cmpq %r12, 0x58(%rsp)
je 0x4602
movq %rax, %r14
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x460e
movl (%rbx), %eax
cmpl $0x22, %eax
je 0x460e
testl %eax, %eax
jne 0x426c
movl %ebp, (%rbx)
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdi
movq %r14, %rax
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %rax
testq %rax, %rax
leaq 0x138(%rsp), %rbp
je 0x42f8
movq %rdx, %rcx
movq (%rax), %r8
movl 0x8(%r8), %r9d
movq %r8, %r10
cmpl %r14d, %r9d
jne 0x42ad
movq 0x10(%r8), %rbx
jmp 0x432c
movq (%r10), %r10
testq %r10, %r10
je 0x42f8
movslq 0x8(%r10), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x42f8
cmpl %r14d, %esi
jne 0x42ad
cmpl %r14d, %r9d
je 0x42a7
movq (%r8), %r8
testq %r8, %r8
je 0x45f6
movslq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x45f6
cmpl %r14d, %esi
jne 0x42d0
jmp 0x42a7
movl $0x18, %edi
callq 0x3280
movq %rax, %rbx
movq %rax, %rdi
movl %r14d, %esi
callq 0x94dc
movl %r14d, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5702
movl 0x10(%r13), %eax
cmpl 0x10(%rbx), %eax
movq %rbx, %r14
cmovgq %r13, %r14
cmpq %r13, %r14
movq %r13, %rax
cmoveq %rbx, %rax
movq %rax, 0x30(%rsp)
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdi
movq %r14, %rax
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %r8
testq %r8, %r8
je 0x439f
movq %rdx, %rcx
movq (%r8), %r9
cmpq 0x8(%r9), %r14
je 0x4446
movq (%r9), %r9
testq %r9, %r9
je 0x439f
movq 0x8(%r9), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x439f
cmpq %rsi, %r14
jne 0x437a
xorl %ebp, %ebp
jmp 0x443c
movq $0x0, 0x158(%rsp)
leaq 0x168(%rsp), %rax
movq %rax, 0x138(%rsp)
movq $0x1, 0x140(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x158(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %r14, 0x58(%rsp)
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x503c
leaq 0x100(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5996
movq %r12, %rdi
callq 0x50b2
movq %rbp, %rdi
callq 0x50b2
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdi
movq %r14, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq (%rsi,%rdx,8), %r8
movb $0x1, %bpl
testq %r8, %r8
jne 0x4448
jmp 0x45ea
xorl %ebp, %ebp
movq (%r8), %r12
cmpq 0x8(%r12), %r14
je 0x447a
movq (%r12), %r12
testq %r12, %r12
je 0x45ea
movq 0x8(%r12), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x45ea
cmpq %rsi, %r14
jne 0x4452
addq $0x10, %r12
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x5c70
testq %rax, %rax
je 0x44a4
testb %bpl, %bpl
leaq 0x170(%rsp), %rbp
leaq 0x58(%rsp), %r14
jne 0x44c6
jmp 0x451b
movq %r12, 0x58(%rsp)
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x58(%rsp), %r14
movq %r14, %rdx
callq 0x5ccc
leaq 0x170(%rsp), %rbp
movl $0x20, %edi
callq 0x3280
movq %rax, %r12
leaq 0xc0(%rsp), %rdi
callq 0x5f59
movsd 0xcbb0(%rip), %xmm1     # 0x11098
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3220
xorpd 0xcbb3(%rip), %xmm0     # 0x110b0
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x39ea
movq 0xb8(%rsp), %rdi
movq %r12, %rsi
callq 0x477c
incl %r15d
jmp 0x415a
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x453f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
leaq 0x2f0(%rsp), %r14
leaq 0x98(%rsp), %r15
je 0x456b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbp, %rdi
movq 0x12a33(%rip), %rsi     # 0x16fa8
callq 0x33d0
leaq 0x1e8(%rsp), %rdi
callq 0x30c0
jmp 0x3ffb
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x45b1
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x100(%rsp), %rdi
callq 0x5122
leaq 0xc8(%rsp), %rdi
callq 0x51bc
leaq 0x2f0(%rsp), %rdi
callq 0x3090
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcb99(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0xcb8d(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0xcb4a(%rip), %rdi      # 0x11153
callq 0x3150
leaq 0xcb3e(%rip), %rdi      # 0x11153
callq 0x33e0
leaq 0xcb69(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0xc9d7(%rip), %rdi      # 0x11004
leaq 0xcae4(%rip), %rsi      # 0x11118
leaq 0xcaeb(%rip), %rcx      # 0x11126
movl $0x15, %edx
callq 0x3170
jmp 0x4665
movq %rax, %r14
movl $0x18, %esi
movq %r13, %rdi
jmp 0x4702
jmp 0x4665
movq %rax, %r14
jmp 0x46eb
jmp 0x4665
jmp 0x466d
movq %rax, %r14
jmp 0x4707
movq %rax, %r14
jmp 0x4728
jmp 0x46cc
jmp 0x46cc
movq %rax, %r14
movl $0x18, %esi
movq %rbx, %rdi
jmp 0x46b5
jmp 0x46cc
jmp 0x46cc
movq %rax, %r14
movq %r12, %rdi
callq 0x50b2
leaq 0x138(%rsp), %rdi
callq 0x50b2
jmp 0x46cf
jmp 0x46cc
jmp 0x46cc
jmp 0x46cc
movq %rax, %r14
movl $0x20, %esi
movq %r12, %rdi
callq 0x3290
jmp 0x46cf
jmp 0x46cc
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x46cf
movl %ebp, (%rbx)
jmp 0x46cf
jmp 0x46cc
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x46eb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x4707
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x1289a(%rip), %rsi     # 0x16fa8
leaq 0x170(%rsp), %rdi
callq 0x33d0
leaq 0x1e8(%rsp), %rdi
callq 0x30c0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x474d
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x100(%rsp), %rdi
callq 0x5122
leaq 0xc8(%rsp), %rdi
callq 0x51bc
leaq 0x2f0(%rsp), %rdi
callq 0x3090
movq %r14, %rdi
callq 0x34a0