// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, -0x3c(%rbp)
movq %rsi, %r13
movq %rdi, -0x38(%rbp)
leaq -0xa8(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq -0xd8(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x40(%rsi), %r15
movq 0x20(%r15), %r12
leaq -0xe0(%rbp), %rbx
leaq -0x160(%rbp), %r14
cmpq 0x28(%r15), %r12
je 0x104e2
movq (%r12), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x160(%rbp)
andl $0x0, -0x158(%rbp)
movq %rax, -0x150(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x12b32
addq $0x8, %r12
jmp 0x104ae
movq %r13, -0x48(%rbp)
movq -0x38(%rbp), %rax
leaq 0x20(%rax), %r14
leaq 0xc8(%rax), %rcx
movq %rcx, -0x50(%rbp)
addq $0xd0, %rax
movq %rax, -0x60(%rbp)
leaq -0xe0(%rbp), %r13
leaq -0x160(%rbp), %rbx
movq %r14, -0x58(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x1076e
movq -0xc8(%rbp), %rsi
movq 0x30(%rsi), %rax
movzwl 0x2(%rax), %eax
movw %ax, -0x2a(%rbp)
movl 0x28(%rsi), %eax
movl %eax, -0x30(%rbp)
movq %r13, %rdi
callq 0x12c8c
movzwl -0x2a(%rbp), %edx
leaq -0x80(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xea5a
leaq -0xb0(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x12cec
leaq -0xa8(%rbp), %rcx
cmpq %rcx, %rax
je 0x10594
leaq -0xb0(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x11b06
movl (%rax), %edx
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %rdi
callq 0x10872
jmp 0x10760
cmpb $0x0, -0x3c(%rbp)
je 0x105d8
movq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x12fda
movq -0x58(%rbp), %r12
movq %rax, %r14
cmpq -0x60(%rbp), %rax
je 0x1069b
leaq 0x28(%r14), %rdi
leaq -0x80(%rbp), %rsi
callq 0x13032
addq $0x30, %r14
cmpq %r14, %rax
je 0x1069b
addq $0x40, %rax
jmp 0x1062d
movl -0x30(%rbp), %eax
movq -0x58(%rbp), %r12
movq (%r12), %rcx
shlq $0x7, %rax
leaq (%rcx,%rax), %r15
addq $0x58, %r15
movq 0x10(%r15), %r14
cmpq %r15, %r14
je 0x1069b
movq 0x20(%r14), %rax
movl 0x4(%rax), %edi
shlq $0x7, %rdi
addq (%r12), %rdi
leaq -0x80(%rbp), %rsi
callq 0x10328
testb %al, %al
jne 0x10625
movq %r14, %rdi
callq 0x43b0
movq %rax, %r14
jmp 0x105f3
movq 0x20(%r14), %rax
addq $0x4, %rax
movl (%rax), %r14d
testl %r14d, %r14d
je 0x1069b
leaq -0xb0(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x11b06
movl %r14d, (%rax)
movzwl -0x2a(%rbp), %eax
movq -0x48(%rbp), %rcx
movq 0x40(%rcx), %rcx
imulq $0x38, %rax, %rax
leaq (%rcx,%rax), %r15
addq $0x20, %r15
movq (%r15), %r12
cmpq 0x8(%r15), %r12
je 0x10760
movq (%r12), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x160(%rbp)
movl %r14d, -0x158(%rbp)
movq %rax, -0x150(%rbp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x130bc
addq $0x8, %r12
jmp 0x10663
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
callq 0x11b5a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1260e
movq %rbx, %rdi
callq 0x11be4
movq -0x38(%rbp), %rax
movq 0x28(%rax), %r14
subq 0x20(%rax), %r14
leaq -0xb0(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x11b06
shrq $0x7, %r14
decl %r14d
movl %r14d, (%rax)
cmpb $0x0, -0x3c(%rbp)
je 0x10702
movq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x11c2c
movq %rax, %rdi
leaq -0x80(%rbp), %rsi
callq 0x11c7e
movl %r14d, (%rax)
movzwl -0x2a(%rbp), %eax
movq -0x48(%rbp), %rcx
movq 0x40(%rcx), %rcx
imulq $0x38, %rax, %rax
leaq (%rcx,%rax), %r15
addq $0x20, %r15
movq (%r15), %r12
cmpq 0x8(%r15), %r12
je 0x10751
movq (%r12), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x160(%rbp)
movl %r14d, -0x158(%rbp)
movq %rax, -0x150(%rbp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x130bc
addq $0x8, %r12
jmp 0x1071d
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %rdi
movl %r14d, %edx
callq 0x108d2
leaq -0x80(%rbp), %rdi
callq 0x46b8
jmp 0x10515
movq -0x48(%rbp), %r15
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
pushq $0x38
popq %rbx
cqto
idivq %rbx
decl %eax
leaq -0x160(%rbp), %rsi
movw %ax, (%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x11ce8
movl (%rax), %r14d
movq -0x38(%rbp), %rax
leaq 0x38(%rax), %rdi
movq %r15, %rsi
callq 0x11c7e
movl %r14d, (%rax)
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
cqto
idivq %rbx
decl %eax
leaq -0x160(%rbp), %rsi
movw %ax, (%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x11ce8
movq -0x38(%rbp), %rdi
addq $0x68, %rdi
movq %rax, %rsi
callq 0x11d3c
movq %rax, %rdi
movq %r15, %rsi
callq 0x41c0
leaq -0xe0(%rbp), %rdi
callq 0x12262
leaq -0xb0(%rbp), %rdi
callq 0x122b8
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1083d
jmp 0x1083d
jmp 0x10846
jmp 0x10846
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x11be4
jmp 0x10849
jmp 0x10846
jmp 0x10846
jmp 0x1083d
jmp 0x1083d
jmp 0x10846
movq %rax, %rbx
jmp 0x10852
jmp 0x10846
jmp 0x10846
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x46b8
leaq -0xe0(%rbp), %rdi
callq 0x12262
leaq -0xb0(%rbp), %rdi
callq 0x122b8
movq %rbx, %rdi
callq 0x45f0