// File: GAStudyGroup[P]GPX2/src/cross/gpx/sources/GPX2Support.cpp
// Function: GPX2Support::removeGhosts(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Node*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, Node*>>>&)
void GPX2Support::removeGhosts(GPX2Structs::NodeMap& graph)
{
    for (auto it = graph.begin();it!=graph.end();) {
        auto index = (*it).first.find(GPX2Structs::ghostToken);
        if (index != string::npos) {
            // gets the id of the node without the ghost token
            string id = (*it).first;
            id.erase(index, GPX2Structs::ghostToken.size());

            //Gets two nodes that are connected to the original node, and also to the ghost, iterating through the edges of the original node
            vector<Node::node> edges = graph[id]->getEdges();
            unsigned edgeToDelete{ 0 };
            Node::node prev, next;
            for (unsigned i = 0; i < edges.size(); i++) {
                //If it found the edge that is connected to the ghost...
                if (!edges[i].first.compare((*it).first)) {
                    edgeToDelete = i;
                } else {
                    prev = edges[i];
                }
            }
            graph[id]->deleteEdge(edgeToDelete);

            edges = graph[(*it).first]->getEdges();

            for (unsigned i = 0; i < edges.size(); i++) {
                //Find the nodes that aren't the original in the edge list 
                if (!(!edges[i].first.compare(id))) {
                    next = edges[i];
                    break;
                }
            }

            //Connect the original node to the next
            graph[id]->addEdge(next);

            //arrange the next node edges
            edges = graph[next.first]->getEdges();
            for (unsigned i = 0; i < edges.size(); i++) {
                //Found the edge that references the ghost
                if (!edges[i].first.compare((*it).first)) {
                    edgeToDelete = i;
                    break;
                }
            }
            graph[next.first]->deleteEdge(edgeToDelete);
            graph[next.first]->addEdge(make_pair(id, next.second));

            //removes the ghost node
            delete graph[(*it).first];
            it = graph.erase(it);
        }else{
            it++;
        }
    }
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq 0x210(%rsp), %rdi
callq 0x60ba0
movq %rax, 0x208(%rsp)
movq 0x210(%rsp), %rdi
callq 0x60bd0
movq %rax, 0x200(%rsp)
leaq 0x208(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x60c00
testb $0x1, %al
jne 0x6ee77
jmp 0x6f567
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq %rax, %rdi
leaq 0x2efa2(%rip), %rsi     # 0x9de30
xorl %eax, %eax
movl %eax, %edx
callq 0x6960
movq %rax, 0x1f8(%rsp)
cmpq $-0x1, 0x1f8(%rsp)
je 0x6f54b
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq %rax, %rsi
leaq 0x1d8(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x6200
movq 0x1f8(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x2ef46(%rip), %rdi     # 0x9de30
callq 0x6370
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq %rax, %rdx
callq 0x60f0
jmp 0x6ef09
movq 0x210(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x60c60
movq %rax, 0xc8(%rsp)
jmp 0x6ef28
movq 0xc8(%rsp), %rax
movq (%rax), %rdi
callq 0x661c0
movq %rax, 0xc0(%rsp)
jmp 0x6ef42
movq 0xc0(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x615a0
jmp 0x6ef59
movl $0x0, 0x1ac(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x70c70
jmp 0x6ef73
leaq 0x158(%rsp), %rdi
callq 0x70c70
jmp 0x6ef82
movl $0x0, 0x154(%rsp)
movl 0x154(%rsp), %eax
movq %rax, 0xb8(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x610e0
movq %rax, %rcx
movq 0xb8(%rsp), %rax
cmpq %rcx, %rax
jae 0x6f0c6
movl 0x154(%rsp), %eax
movl %eax, %esi
leaq 0x1b0(%rsp), %rdi
callq 0x60c40
movq %rax, 0xa8(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0x6060
movl %eax, 0xb4(%rsp)
jmp 0x6f001
movl 0xb4(%rsp), %eax
cmpl $0x0, %eax
jne 0x6f084
movl 0x154(%rsp), %eax
movl %eax, 0x1ac(%rsp)
jmp 0x6f0ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
jmp 0x6f53c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
jmp 0x6f52f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
jmp 0x6f522
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
jmp 0x6f515
movl 0x154(%rsp), %eax
movl %eax, %esi
leaq 0x1b0(%rsp), %rdi
callq 0x60c40
movq %rax, %rsi
leaq 0x180(%rsp), %rdi
callq 0x66f80
jmp 0x6f0ac
jmp 0x6f0ae
jmp 0x6f0b0
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x6ef8d
movq 0x210(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x60c60
movq %rax, 0xa0(%rsp)
jmp 0x6f0e5
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
movl 0x1ac(%rsp), %esi
callq 0x66280
jmp 0x6f0fe
movq 0x210(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0x60c60
movq %rax, 0x98(%rsp)
jmp 0x6f135
movq 0x98(%rsp), %rax
movq (%rax), %rdi
callq 0x661c0
movq %rax, 0x88(%rsp)
jmp 0x6f14f
movq 0x88(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x665f0
jmp 0x6f166
movl $0x0, 0x150(%rsp)
movl 0x150(%rsp), %eax
movq %rax, 0x80(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x610e0
movq %rax, %rcx
movq 0x80(%rsp), %rax
cmpq %rcx, %rax
jae 0x6f214
movl 0x150(%rsp), %eax
movl %eax, %esi
leaq 0x1b0(%rsp), %rdi
callq 0x60c40
movq %rax, %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x6060
movl %eax, 0x7c(%rsp)
jmp 0x6f1c9
movl 0x7c(%rsp), %eax
cmpl $0x0, %eax
je 0x6f1fc
movl 0x150(%rsp), %eax
movl %eax, %esi
leaq 0x1b0(%rsp), %rdi
callq 0x60c40
movq %rax, %rsi
leaq 0x158(%rsp), %rdi
callq 0x66f80
jmp 0x6f1fa
jmp 0x6f214
jmp 0x6f1fe
movl 0x150(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
jmp 0x6f171
movq 0x210(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x60c60
movq %rax, 0x70(%rsp)
jmp 0x6f230
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x68(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x61030
jmp 0x6f254
movq 0x68(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x66250
jmp 0x6f268
leaq 0x128(%rsp), %rdi
callq 0x610b0
movq 0x210(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x60c60
movq %rax, 0x60(%rsp)
jmp 0x6f291
movq 0x60(%rsp), %rax
movq (%rax), %rdi
callq 0x661c0
movq %rax, 0x58(%rsp)
jmp 0x6f2a5
movq 0x58(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x665f0
jmp 0x6f2b9
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
movq %rax, 0x50(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x610e0
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0x6f380
movl 0x124(%rsp), %eax
movl %eax, %esi
leaq 0x1b0(%rsp), %rdi
callq 0x60c40
movq %rax, 0x40(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x6060
movl %eax, 0x4c(%rsp)
jmp 0x6f329
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
jne 0x6f368
movl 0x124(%rsp), %eax
movl %eax, 0x1ac(%rsp)
jmp 0x6f380
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x610b0
jmp 0x6f515
jmp 0x6f36a
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x6f2c4
movq 0x210(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x60c60
movq %rax, 0x38(%rsp)
jmp 0x6f39c
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movl 0x1ac(%rsp), %esi
callq 0x66280
jmp 0x6f3b2
movq 0x210(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x60c60
movq %rax, 0x30(%rsp)
jmp 0x6f3ce
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
leaq 0x178(%rsp), %rdx
leaq 0xf8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x61070
jmp 0x6f3fa
movq 0x28(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x66250
jmp 0x6f40e
leaq 0xf8(%rsp), %rdi
callq 0x610b0
movq 0x210(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x60c20
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x60c60
movq %rax, 0x20(%rsp)
jmp 0x6f449
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x6f475
movq 0x10(%rsp), %rdi
callq 0x61540
movq 0x10(%rsp), %rdi
movl $0x40, %esi
callq 0x64c0
movq 0x210(%rsp), %rdi
movq 0x208(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rsi
callq 0x70ca0
movq %rax, 0x8(%rsp)
jmp 0x6f4a1
movq 0x8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x208(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x610b0
leaq 0x180(%rsp), %rdi
callq 0x610b0
leaq 0x1b0(%rsp), %rdi
callq 0x616a0
leaq 0x1d8(%rsp), %rdi
callq 0x6988
jmp 0x6f562
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x610b0
leaq 0x158(%rsp), %rdi
callq 0x610b0
leaq 0x180(%rsp), %rdi
callq 0x610b0
leaq 0x1b0(%rsp), %rdi
callq 0x616a0
leaq 0x1d8(%rsp), %rdi
callq 0x6988
jmp 0x6f56f
leaq 0x208(%rsp), %rdi
xorl %esi, %esi
callq 0x70c30
movq %rax, 0xe0(%rsp)
jmp 0x6ee44
addq $0x218, %rsp            # imm = 0x218
retq
movq 0x1d0(%rsp), %rdi
callq 0x6850
nopl (%rax)