// File: /workspace/github_cpp_makefile_star2_O2/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 $0x518, %rsp            # imm = 0x518
movq %rdi, 0x50(%rsp)
leaq 0x310(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x3440
leaq 0x388(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0x4162
leaq 0xb8(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rdx
movq %rdx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %ecx       # imm = 0x3F800000
movl %ecx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x188(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rdx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %ecx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x60(%rsp), %rax
movq %rdx, (%rax)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
leaq 0x58(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movq %rax, (%rdx)
movb $0x0, (%rcx)
leaq 0xc0(%rsp), %rbx
leaq 0x190(%rsp), %r14
leaq 0xc(%rsp), %r12
leaq 0x118(%rsp), %r15
leaq 0x20(%rsp), %r13
leaq 0x310(%rsp), %rdi
movq %rbx, %rsi
callq 0x3070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x411c
movq %r14, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
callq 0x3470
movq %r14, %rdi
movq %r12, %rsi
callq 0x31a0
cmpl $0x0, 0xc(%rsp)
jle 0x410f
leaq 0x88(%rsp), %rdi
movq %r12, %rsi
callq 0x51aa
testq %rax, %rax
je 0x3e21
leaq 0x88(%rsp), %rdi
movq %r12, %rsi
callq 0x5500
movq (%rax), %r12
jmp 0x3e5b
pushq $0x18
popq %rdi
callq 0x32b0
movq %rax, %r12
movl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x8460
movl 0xc(%rsp), %eax
movl %eax, 0x118(%rsp)
movq %r12, 0x120(%rsp)
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x5226
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
movb $0x0, 0x78(%rsp)
movq %r14, %rdi
leaq 0x68(%rsp), %rsi
callq 0x3360
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movl $0x0, 0x1c(%rsp)
movq %r12, 0x48(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3360
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x40e6
subl $0x1, 0x1c(%rsp)
jae 0x3ef3
movq %r15, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x32d0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3340
movq %r15, %rdi
callq 0x3110
movl 0x30(%rsp), %eax
decl %eax
movslq %eax, %rcx
movq %r15, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32d0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3340
movq %r15, %rdi
callq 0x3110
movq %rbx, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x47f6
movl %eax, 0x8(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x51aa
testq %rax, %rax
je 0x3f60
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5500
movq (%rax), %rbp
jmp 0x3f9a
pushq $0x18
popq %rdi
callq 0x32b0
movq %rax, %rbp
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x8460
movl 0x8(%rsp), %eax
movl %eax, 0x118(%rsp)
movq %rbp, 0x120(%rsp)
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x5226
movl 0x10(%r12), %eax
cmpl 0x10(%rbp), %eax
movq %rbp, %rax
cmovgq %r12, %rax
movq %rax, 0x10(%rsp)
cmpq %r12, %rax
movq %r12, %rax
cmoveq %rbp, %rax
movq %rax, 0x20(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x551e
movq %rax, %r14
testq %rax, %rax
jne 0x4071
andq $0x0, 0x100(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq $0x1, 0xe8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0x100(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %r13
leaq 0xe0(%rsp), %r15
movq %r15, %rsi
callq 0x49a8
leaq 0x158(%rsp), %rdi
movq %r13, %r12
movq %r13, %rsi
callq 0x559a
movq %rbx, %rdi
callq 0x4a48
movq %r15, %rdi
callq 0x4a48
movq %r12, %r15
leaq 0x20(%rsp), %r13
movq 0x48(%rsp), %r12
leaq 0x158(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x58a0
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x58be
testq %rax, %rax
je 0x40a1
testq %r14, %r14
jne 0x3e9f
jmp 0x40ac
movq %rbx, %rdi
movq %r13, %rsi
callq 0x593a
pushq $0x20
popq %rdi
callq 0x32b0
movq %rax, %r14
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4812
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x382a
movq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x4250
jmp 0x3e9f
leaq 0x28(%rsp), %rdi
callq 0x3110
leaq 0x68(%rsp), %rdi
callq 0x3110
leaq 0xc0(%rsp), %rbx
leaq 0x190(%rsp), %r14
leaq 0xc(%rsp), %r12
movq %r14, %rdi
callq 0x3120
jmp 0x3db1
leaq 0xc0(%rsp), %rdi
callq 0x3110
leaq 0x158(%rsp), %rdi
callq 0x4af0
leaq 0x88(%rsp), %rdi
callq 0x4bae
leaq 0x310(%rsp), %rdi
callq 0x30a0
addq $0x518, %rsp            # imm = 0x518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xae9b(%rip), %rdi      # 0xf004
leaq 0xaf78(%rip), %rsi      # 0xf0e8
leaq 0xaf7f(%rip), %rcx      # 0xf0f6
pushq $0x15
popq %rdx
callq 0x31b0
jmp 0x419a
movq %rax, %r15
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0x32c0
jmp 0x4206
jmp 0x419a
jmp 0x419a
movq %rax, %r15
jmp 0x41fc
movq %rax, %r15
jmp 0x4206
jmp 0x41a1
movq %rax, %r15
jmp 0x4213
movq %rax, %r15
pushq $0x18
popq %rsi
movq %rbp, %rdi
jmp 0x41e8
movq %rax, %r15
movq %rbx, %rdi
callq 0x4a48
leaq 0xe0(%rsp), %rdi
callq 0x4a48
jmp 0x41f2
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
jmp 0x41ef
movq %rax, %r15
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x32c0
jmp 0x41f2
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x3110
leaq 0x68(%rsp), %rdi
callq 0x3110
leaq 0x190(%rsp), %rdi
callq 0x3120
leaq 0xc0(%rsp), %rdi
callq 0x3110
leaq 0x158(%rsp), %rdi
callq 0x4af0
leaq 0x88(%rsp), %rdi
callq 0x4bae
leaq 0x310(%rsp), %rdi
callq 0x30a0
movq %r15, %rdi
callq 0x34b0
nop