// File: /workspace/github_cpp_makefile_star2_O1/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 $0x508, %rsp            # imm = 0x508
movq %rdi, 0x80(%rsp)
leaq 0x300(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x3430
leaq 0x378(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0x4678
leaq 0x108(%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 0x178(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x90(%rsp), %rax
movq %rcx, (%rax)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xa0(%rsp), %rbp
leaq 0x60(%rsp), %rbx
leaq 0x180(%rsp), %r15
leaq 0x98(%rsp), %r13
movq 0x300(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x34c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x460b
movq %r15, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x3460
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
callq 0x3160
movslq 0x4(%rsp), %rbx
testq %rbx, %rbx
jle 0x45e5
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdi
movq %rbx, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq (%rsi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x40c2
movq (%rdx), %r8
movq %rdx, %rax
cmpl 0x8(%r8), %ebx
je 0x40c2
movq %r8, %r9
movq (%r8), %r8
testq %r8, %r8
je 0x40c0
movslq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0x40c2
movq %r9, %rax
cmpl %esi, %ebx
jne 0x4096
jmp 0x40c2
xorl %eax, %eax
testq %rax, %rax
je 0x40e9
cmpq $0x0, (%rax)
je 0x40e9
leaq 0xd8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x59a4
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x4128
movl $0x18, %edi
callq 0x3280
movq %rax, %rdi
movl %ebx, %esi
movq %rax, %rbx
callq 0x96b4
movl 0x4(%rsp), %eax
movl %eax, 0x98(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbx, 0xa0(%rsp)
leaq 0xd8(%rsp), %rdi
movq %r13, %rsi
callq 0x56fe
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movb $0x0, 0x50(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3350
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movl $0x0, 0x8(%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x45a5
cmpl $0x0, 0x8(%rsp)
leaq 0x18(%rsp), %rbx
jne 0x41df
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
movq $-0x1, %rcx
callq 0x32a0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3330
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x41df
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x20(%rsp), %rcx
shlq $0x20, %rcx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %rcx
sarq $0x20, %rcx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32a0
movq %r13, %rsi
movq %rbp, %r15
movq %rbx, %rdi
callq 0x3330
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x4239
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x18(%rsp), %r14
callq 0x3050
movq %rax, %rbx
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r14, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x33a0
cmpq %r14, 0x98(%rsp)
je 0x4660
movq %rax, %r12
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x466c
movl (%rbx), %eax
cmpl $0x22, %eax
je 0x466c
testl %eax, %eax
jne 0x4299
movl %ebp, (%rbx)
movl %r12d, 0xc(%rsp)
movslq %r12d, %rax
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdi
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq (%rsi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
movq %r15, %rbp
leaq 0x180(%rsp), %r15
je 0x430b
movq (%rdx), %r8
movq %rdx, %rax
cmpl %r12d, 0x8(%r8)
je 0x430b
movq %r8, %r9
movq (%r8), %r8
testq %r8, %r8
je 0x4309
movslq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0x430b
movq %r9, %rax
cmpl %r12d, %esi
jne 0x42de
jmp 0x430b
xorl %eax, %eax
testq %rax, %rax
je 0x432d
cmpq $0x0, (%rax)
je 0x432d
leaq 0xd8(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x59a4
movq (%rax), %rbx
jmp 0x4368
movl $0x18, %edi
callq 0x3280
movq %rax, %rbx
movq %rax, %rdi
movl %r12d, %esi
callq 0x96b4
movl 0xc(%rsp), %eax
movl %eax, 0x98(%rsp)
movq %rbx, 0xa0(%rsp)
leaq 0xd8(%rsp), %rdi
movq %r13, %rsi
callq 0x56fe
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %eax
cmpl 0x10(%rbx), %eax
movq %rbx, %rcx
cmovgq %rdx, %rcx
movq %rcx, 0x88(%rsp)
cmpq %rdx, %rcx
movq %rdx, %rax
cmoveq %rbx, %rax
movq %rax, 0x38(%rsp)
movq 0x148(%rsp), %rdi
movq 0x150(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x43f3
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x43f3
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x43f1
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x43f3
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x43c6
jmp 0x43f3
xorl %eax, %eax
testq %rax, %rax
je 0x4402
cmpq $0x0, (%rax)
sete %r14b
jmp 0x4405
movb $0x1, %r14b
testb %r14b, %r14b
je 0x44a8
xorpd %xmm0, %xmm0
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x130(%rsp)
movapd %xmm0, 0x120(%rsp)
movq $0x0, 0x140(%rsp)
leaq 0x140(%rsp), %rax
movq %rax, 0x110(%rsp)
movq $0x1, 0x118(%rsp)
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x130(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %rcx, 0x98(%rsp)
movq %rbp, %rdi
leaq 0x110(%rsp), %r12
movq %r12, %rsi
movq %r12, %rdx
callq 0x5052
leaq 0x148(%rsp), %rdi
movq %r13, %rsi
callq 0x5a2e
movq %rbp, %rdi
callq 0x50cc
movq %r12, %rdi
callq 0x50cc
leaq 0x148(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x5d0a
movq %rax, %rdi
movq 0x38(%rsp), %rcx
movq (%rax), %r8
movq 0x8(%rax), %r9
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq %rdx, %rsi
movq (%r8,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x451e
movq (%rdx), %r10
movq %rdx, %rax
cmpq 0x8(%r10), %rcx
je 0x451e
movq %r10, %r11
movq (%r10), %r10
testq %r10, %r10
je 0x451c
movq 0x8(%r10), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x451e
movq %r11, %rax
cmpq %r8, %rcx
jne 0x44f1
jmp 0x451e
xorl %eax, %eax
testq %rax, %rax
je 0x4530
cmpq $0x0, (%rax)
je 0x4530
testb %r14b, %r14b
jne 0x4545
jmp 0x459c
movq %rdi, 0x98(%rsp)
leaq 0x38(%rsp), %rsi
movq %r13, %rdx
callq 0x5d94
movl $0x20, %edi
callq 0x3280
movq %rax, %r12
leaq 0x90(%rsp), %rdi
callq 0x6011
movsd 0xcb31(%rip), %xmm1     # 0x11098
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3220
xorpd 0xcb34(%rip), %xmm0     # 0x110b0
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x39ea
movq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x47c2
incl 0x8(%rsp)
jmp 0x416d
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x45c1
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
leaq 0x300(%rsp), %r14
je 0x45e5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3290
movq %r15, %rdi
movq 0x129b9(%rip), %rsi     # 0x16fa8
callq 0x33d0
leaq 0x1f8(%rsp), %rdi
callq 0x30c0
leaq 0x60(%rsp), %rbx
jmp 0x3ffd
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x4627
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x148(%rsp), %rdi
callq 0x513c
leaq 0xd8(%rsp), %rdi
callq 0x51d6
leaq 0x300(%rsp), %rdi
callq 0x3090
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcabc(%rip), %rdi      # 0x11123
callq 0x3150
leaq 0xcab0(%rip), %rdi      # 0x11123
callq 0x33e0
leaq 0xc985(%rip), %rdi      # 0x11004
leaq 0xca62(%rip), %rsi      # 0x110e8
leaq 0xca69(%rip), %rcx      # 0x110f6
movl $0x15, %edx
callq 0x3170
jmp 0x46b2
movq %rax, %r14
movl $0x18, %esi
movq %rbx, %rdi
jmp 0x473d
jmp 0x46b2
movq %rax, %r14
jmp 0x4726
jmp 0x46ba
movq %rax, %r14
jmp 0x4742
movq %rax, %r14
jmp 0x4763
jmp 0x4707
movq %rax, %r14
movq %rbp, %rdi
callq 0x50cc
leaq 0x110(%rsp), %rdi
callq 0x50cc
jmp 0x470a
movq %rax, %r14
movl $0x18, %esi
movq %rbx, %rdi
jmp 0x4700
jmp 0x4707
jmp 0x4707
jmp 0x4707
jmp 0x4707
jmp 0x4707
movq %rax, %r14
movl $0x20, %esi
movq %r12, %rdi
callq 0x3290
jmp 0x470a
movq %rax, %r14
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x4726
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4742
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x1285f(%rip), %rsi     # 0x16fa8
leaq 0x180(%rsp), %rdi
callq 0x33d0
leaq 0x1f8(%rsp), %rdi
callq 0x30c0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x477f
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x148(%rsp), %rdi
callq 0x513c
leaq 0xd8(%rsp), %rdi
callq 0x51d6
leaq 0x300(%rsp), %rdi
callq 0x3090
movq %r14, %rdi
callq 0x34a0
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x470a
movl %ebp, (%rbx)
jmp 0x470a
nop