// File: aalto-speech[P]ftk/src/MSFG.cc
// Function: MultiStringFactorGraph::add(FactorGraph const&, bool)
void
MultiStringFactorGraph::add(const FactorGraph &text,
                            bool lookahead)
{
    map<fg_node_idx_t, msfg_node_idx_t> visited_nodes;
    multimap<fg_node_idx_t, pair<msfg_node_idx_t, FactorGraph::Arc*> > arcs_to_process;
    // multimap key is target index in fg, value pair source index in msfg, arc in fg
    // ordering arcs by target index ensures topological order for text nodes in msfg

    const FactorGraph::Node &fg_node = text.nodes[0];
    for (auto fg_arcit = fg_node.outgoing.cbegin(); fg_arcit != fg_node.outgoing.cend(); ++fg_arcit)
        arcs_to_process.insert(make_pair((**fg_arcit).target_node, make_pair(0, *fg_arcit)));

    while (arcs_to_process.size() > 0) {

        auto arc_to_process = arcs_to_process.begin();
        FactorGraph::Arc *arc = (*arc_to_process).second.second;
        fg_node_idx_t fg_target_node = arc->target_node;
        msfg_node_idx_t msfg_source_node = (*arc_to_process).second.first;
        arcs_to_process.erase(arc_to_process);

        string target_node_factor = text.get_factor(fg_target_node);
        msfg_node_idx_t msfg_target_node = 0;

        // Check if have connected to this fg node already from some other node
        // Just find or add arc and continue
        if (visited_nodes.find(fg_target_node) != visited_nodes.end()) {
            msfg_target_node = visited_nodes[fg_target_node];
            find_or_create_arc(msfg_source_node, msfg_target_node);
            continue;
        }

        // Check if node exists in msfg, just not visited yet
        if (lookahead) {
            auto flait = factor_lookahead.find(msfg_source_node);
            if (flait != factor_lookahead.end()) {
                auto flait2 = flait->second.find(target_node_factor);
                if (flait2 != flait->second.end())
                    msfg_target_node = flait2->second;
            }
        }
        else {
            MultiStringFactorGraph::Node &msfg_node = nodes[msfg_source_node];
            for (auto msfg_arcit = msfg_node.outgoing.begin(); msfg_arcit != msfg_node.outgoing.end(); ++msfg_arcit)
                if (nodes[(**msfg_arcit).target_node].factor == target_node_factor) {
                    msfg_target_node = (**msfg_arcit).target_node;
                    break;
                }
        }

        if (msfg_target_node != 0) {
            visited_nodes[fg_target_node] = msfg_target_node;
            const FactorGraph::Node &fg_node = text.nodes[fg_target_node];
            for (auto fg_arcit = fg_node.outgoing.cbegin(); fg_arcit != fg_node.outgoing.cend(); ++fg_arcit)
                arcs_to_process.insert(make_pair((**fg_arcit).target_node, make_pair(msfg_target_node, *fg_arcit)));
            continue;
        }

        // Create new node and arc
        nodes.push_back(Node(target_node_factor));
        msfg_target_node = nodes.size()-1;
        visited_nodes[fg_target_node] = msfg_target_node;
        if (lookahead) factor_lookahead[msfg_source_node][target_node_factor] = msfg_target_node;
        const FactorGraph::Node &fg_node = text.nodes[fg_target_node];
        for (auto fg_arcit = fg_node.outgoing.cbegin(); fg_arcit != fg_node.outgoing.cend(); ++fg_arcit)
            arcs_to_process.insert(make_pair((**fg_arcit).target_node, make_pair(msfg_target_node, *fg_arcit)));
        create_arc(msfg_source_node, msfg_target_node);
    }


    string_end_nodes[text.text] = visited_nodes[text.nodes.size()-1];
    reverse_string_end_nodes[visited_nodes[text.nodes.size()-1]] = text.text;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3c0, %rsp            # imm = 0x3C0
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x298(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2fdc0
leaq -0x78(%rbp), %rdi
callq 0x2fde0
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x23ff0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
addq $0x20, %rdi
callq 0x2c9e0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
addq $0x20, %rdi
callq 0x2fe00
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x24040
testb $0x1, %al
jne 0x2cf80
jmp 0x2d05a
leaq -0x88(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x240b0
movq -0x2b8(%rbp), %rdi
movq (%rax), %rax
addq $0x2, %rax
movq %rax, -0x2b0(%rbp)
movl $0x0, -0xbc(%rbp)
callq 0x240b0
movq %rax, %rsi
leaq -0xbc(%rbp), %rdi
callq 0x2fe90
movq %rdx, -0x2a8(%rbp)
movl %eax, -0x29c(%rbp)
jmp 0x2cfd5
movq -0x2b0(%rbp), %rsi
movq -0x2a8(%rbp), %rax
movl -0x29c(%rbp), %ecx
movl %ecx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x2fe60
jmp 0x2d00b
leaq -0x78(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2fe30
movq %rax, -0x2c0(%rbp)
jmp 0x2d024
movq -0x2c0(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x240d0
jmp 0x2cf50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x2d8b3
jmp 0x2d05c
leaq -0x78(%rbp), %rdi
callq 0x2fec0
cmpq $0x0, %rax
jbe 0x2d7b5
leaq -0x78(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x2fee0
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x2ff10
movq -0x2d8(%rbp), %rdi
movq 0x10(%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movw 0x2(%rax), %ax
movw %ax, -0xea(%rbp)
callq 0x2ff10
movq -0x2d0(%rbp), %rdi
movl 0x8(%rax), %eax
movl %eax, -0xf0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rsi
callq 0x2ff30
movq %rax, -0x2c8(%rbp)
jmp 0x2d0f5
movq -0x2c8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rsi
movzwl -0xea(%rbp), %edx
leaq -0x120(%rbp), %rdi
callq 0x24510
jmp 0x2d11c
movl $0x0, -0x124(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xea(%rbp), %rsi
callq 0x2ffa0
movq %rax, -0x2e0(%rbp)
jmp 0x2d13f
movq -0x2e0(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2ffd0
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x2ff70
testb $0x1, %al
jne 0x2d176
jmp 0x2d1de
leaq -0x48(%rbp), %rdi
leaq -0xea(%rbp), %rsi
callq 0x30000
movq %rax, -0x2e8(%rbp)
jmp 0x2d18f
movq -0x298(%rbp), %rdi
movq -0x2e8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x124(%rbp)
movl -0xf0(%rbp), %esi
movl -0x124(%rbp), %edx
callq 0x2d8e0
jmp 0x2d1b8
movl $0x5, -0x13c(%rbp)
jmp 0x2d782
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0x2d7a4
testb $0x1, -0x11(%rbp)
je 0x2d2d9
movq -0x298(%rbp), %rdi
addq $0xc8, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x300e0
movq %rax, -0x2f0(%rbp)
jmp 0x2d20b
movq -0x298(%rbp), %rdi
movq -0x2f0(%rbp), %rax
movq %rax, -0x148(%rbp)
addq $0xc8, %rdi
callq 0x30140
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x30110
testb $0x1, %al
jne 0x2d24f
jmp 0x2d2d4
leaq -0x148(%rbp), %rdi
callq 0x30170
movq %rax, %rdi
addq $0x8, %rdi
leaq -0x120(%rbp), %rsi
callq 0x30190
movq %rax, -0x2f8(%rbp)
jmp 0x2d277
movq -0x2f8(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x30170
movq %rax, %rdi
addq $0x8, %rdi
callq 0x301f0
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x301c0
testb $0x1, %al
jne 0x2d2bd
jmp 0x2d2d2
leaq -0x158(%rbp), %rdi
callq 0x30220
movl 0x20(%rax), %eax
movl %eax, -0x124(%rbp)
jmp 0x2d2d4
jmp 0x2d3b5
movq -0x298(%rbp), %rdi
addq $0x20, %rdi
movl -0xf0(%rbp), %eax
movl %eax, %esi
callq 0x30240
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rdi
addq $0x50, %rdi
callq 0x30260
movq %rax, -0x170(%rbp)
movq -0x168(%rbp), %rdi
addq $0x50, %rdi
callq 0x302c0
movq %rax, -0x178(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x30290
testb $0x1, %al
jne 0x2d33f
jmp 0x2d3b3
movq -0x298(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x300(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x302f0
movq -0x300(%rbp), %rdi
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, %esi
callq 0x30240
movq %rax, %rdi
leaq -0x120(%rbp), %rsi
callq 0x2cc30
testb $0x1, %al
jne 0x2d386
jmp 0x2d3a0
leaq -0x170(%rbp), %rdi
callq 0x302f0
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x124(%rbp)
jmp 0x2d3b3
jmp 0x2d3a2
leaq -0x170(%rbp), %rdi
callq 0x30320
jmp 0x2d30f
jmp 0x2d3b5
cmpl $0x0, -0x124(%rbp)
je 0x2d525
movl -0x124(%rbp), %eax
movl %eax, -0x30c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xea(%rbp), %rsi
callq 0x30000
movq %rax, -0x308(%rbp)
jmp 0x2d3e7
movq -0x308(%rbp), %rax
movl -0x30c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movzwl -0xea(%rbp), %eax
movl %eax, %esi
callq 0x23ff0
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rdi
addq $0x20, %rdi
callq 0x2c9e0
movq %rax, -0x188(%rbp)
movq -0x180(%rbp), %rdi
addq $0x20, %rdi
callq 0x2fe00
movq %rax, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x24040
testb $0x1, %al
jne 0x2d45d
jmp 0x2d516
leaq -0x188(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x240b0
movq -0x328(%rbp), %rdi
movq (%rax), %rax
addq $0x2, %rax
movq %rax, -0x320(%rbp)
callq 0x240b0
movq %rax, %rsi
leaq -0x124(%rbp), %rdi
callq 0x303b0
movq %rdx, -0x318(%rbp)
movl %eax, -0x310(%rbp)
jmp 0x2d4a8
movq -0x320(%rbp), %rsi
movq -0x318(%rbp), %rax
movl -0x310(%rbp), %ecx
movl %ecx, -0x1b8(%rbp)
movq %rax, -0x1b0(%rbp)
leaq -0x1a8(%rbp), %rdi
leaq -0x1b8(%rbp), %rdx
callq 0x30380
jmp 0x2d4de
leaq -0x78(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x30350
movq %rax, -0x330(%rbp)
jmp 0x2d4f7
movq -0x330(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x240d0
jmp 0x2d42a
movl $0x5, -0x13c(%rbp)
jmp 0x2d782
movq -0x298(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x338(%rbp)
leaq -0x240(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x30410
jmp 0x2d54c
movq -0x338(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x303e0
jmp 0x2d561
leaq -0x240(%rbp), %rdi
callq 0x304a0
movq -0x298(%rbp), %rdi
addq $0x20, %rdi
callq 0x30500
decl %eax
movl %eax, -0x124(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x344(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xea(%rbp), %rsi
callq 0x30000
movq %rax, -0x340(%rbp)
jmp 0x2d5aa
movq -0x340(%rbp), %rax
movl -0x344(%rbp), %ecx
movl %ecx, (%rax)
testb $0x1, -0x11(%rbp)
je 0x2d63e
movq -0x298(%rbp), %rdi
movl -0x124(%rbp), %eax
movl %eax, -0x354(%rbp)
addq $0xc8, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x30520
movq %rax, -0x350(%rbp)
jmp 0x2d5ee
movq -0x350(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x30600
movq %rax, -0x360(%rbp)
jmp 0x2d60a
movq -0x360(%rbp), %rax
movl -0x354(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x2d63e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x304a0
jmp 0x2d7a4
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movzwl -0xea(%rbp), %eax
movl %eax, %esi
callq 0x23ff0
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rdi
addq $0x20, %rdi
callq 0x2c9e0
movq %rax, -0x250(%rbp)
movq -0x248(%rbp), %rdi
addq $0x20, %rdi
callq 0x2fe00
movq %rax, -0x258(%rbp)
leaq -0x250(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x24040
testb $0x1, %al
jne 0x2d6a5
jmp 0x2d75e
leaq -0x250(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x240b0
movq -0x380(%rbp), %rdi
movq (%rax), %rax
addq $0x2, %rax
movq %rax, -0x378(%rbp)
callq 0x240b0
movq %rax, %rsi
leaq -0x124(%rbp), %rdi
callq 0x303b0
movq %rdx, -0x370(%rbp)
movl %eax, -0x364(%rbp)
jmp 0x2d6f0
movq -0x378(%rbp), %rsi
movq -0x370(%rbp), %rax
movl -0x364(%rbp), %ecx
movl %ecx, -0x280(%rbp)
movq %rax, -0x278(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x280(%rbp), %rdx
callq 0x30380
jmp 0x2d726
leaq -0x78(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x30350
movq %rax, -0x388(%rbp)
jmp 0x2d73f
movq -0x388(%rbp), %rax
movq %rax, -0x288(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x240d0
jmp 0x2d672
movq -0x298(%rbp), %rdi
movl -0xf0(%rbp), %esi
movl -0x124(%rbp), %edx
callq 0x2d990
jmp 0x2d778
movl $0x0, -0x13c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4828
movl -0x13c(%rbp), %eax
testl %eax, %eax
je 0x2d79f
jmp 0x2d79a
jmp 0x2d05c
jmp 0x2d05c
leaq -0x120(%rbp), %rdi
callq 0x4828
jmp 0x2d8b3
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
decl %eax
movw %ax, -0x28a(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x28a(%rbp), %rsi
callq 0x306e0
movq %rax, -0x390(%rbp)
jmp 0x2d7e4
movq -0x298(%rbp), %rdi
movq -0x390(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x39c(%rbp)
addq $0x38, %rdi
movq -0x10(%rbp), %rsi
callq 0x30600
movq %rax, -0x398(%rbp)
jmp 0x2d810
movq -0x298(%rbp), %rax
movq -0x398(%rbp), %rcx
movl -0x39c(%rbp), %edx
movl %edx, (%rcx)
movq -0x10(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
addq $0x68, %rax
movq %rax, -0x3b0(%rbp)
addq $0x40, %rdi
callq 0x23a90
decl %eax
movw %ax, -0x28c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x28c(%rbp), %rsi
callq 0x306e0
movq %rax, -0x3a8(%rbp)
jmp 0x2d867
movq -0x3a8(%rbp), %rsi
movq -0x3b0(%rbp), %rdi
callq 0x307c0
movq %rax, -0x3c0(%rbp)
jmp 0x2d883
movq -0x3b8(%rbp), %rsi
movq -0x3c0(%rbp), %rdi
callq 0x4040
jmp 0x2d898
leaq -0x78(%rbp), %rdi
callq 0x308a0
leaq -0x48(%rbp), %rdi
callq 0x308c0
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x308a0
leaq -0x48(%rbp), %rdi
callq 0x308c0
movq -0xc8(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)