// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movl %edx, -0x3c(%rbp)
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
leaq -0xb0(%rbp), %rcx
xorl %eax, %eax
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq -0xe0(%rbp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq 0x40(%rsi), %r15
movq 0x20(%r15), %r12
cmpq 0x28(%r15), %r12
je 0x11b71
movq %r14, -0x48(%rbp)
leaq -0xe8(%rbp), %rbx
leaq -0x168(%rbp), %r14
movq (%r12), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x168(%rbp)
movl $0x0, -0x160(%rbp)
movq %rax, -0x158(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1469c
addq $0x8, %r12
cmpq 0x28(%r15), %r12
jne 0x117ec
cmpq $0x0, -0xc0(%rbp)
movq -0x48(%rbp), %r14
je 0x11b71
movq -0x38(%rbp), %rax
leaq 0x20(%rax), %rcx
movq %rcx, -0x58(%rbp)
leaq 0xd0(%rax), %rcx
movq %rcx, -0x50(%rbp)
addq $0xc8, %rax
movq %rax, -0x80(%rbp)
leaq -0xe8(%rbp), %r12
movq -0xd0(%rbp), %rsi
movq 0x30(%rsi), %rax
movzwl 0x2(%rax), %eax
movw %ax, -0x2a(%rbp)
movl 0x28(%rsi), %eax
movl %eax, -0x30(%rbp)
movq %r12, %rdi
callq 0x14782
movzwl -0x2a(%rbp), %edx
leaq -0x78(%rbp), %rdi
movq %r14, %rsi
callq 0x10288
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x118e4
movzwl -0x2a(%rbp), %eax
leaq -0xb0(%rbp), %r8
movq %r8, %rcx
movq %rdi, %rdx
movzwl 0x20(%rdi), %esi
xorl %edi, %edi
cmpw %ax, %si
setb %dil
movq %rdx, %r8
cmovbq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x118a3
leaq -0xb0(%rbp), %rdi
cmpq %rdi, %r8
je 0x118e4
cmpw %ax, %si
cmovbq %rcx, %rdx
cmpw 0x20(%rdx), %ax
jae 0x11b2f
cmpb $0x0, -0x3c(%rbp)
je 0x119d5
movq -0x38(%rbp), %rax
movq 0xd8(%rax), %rcx
testq %rcx, %rcx
je 0x11a4f
movl -0x30(%rbp), %eax
movq -0x50(%rbp), %rbx
xorl %edx, %edx
cmpl %eax, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rbx
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x11909
cmpq -0x50(%rbp), %rbx
je 0x11a4f
cmpl 0x20(%rbx), %eax
jb 0x11a4f
leaq 0x28(%rbx), %rdi
leaq -0x78(%rbp), %rsi
callq 0x149d8
addq $0x30, %rbx
cmpq %rbx, %rax
je 0x11a4f
movl 0x40(%rax), %r13d
testl %r13d, %r13d
je 0x11a4f
leaq -0xb8(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x135d2
leaq -0xe8(%rbp), %r12
movl %r13d, (%rax)
movzwl -0x2a(%rbp), %ecx
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rax
imulq $0x38, %rcx, %rcx
movq 0x20(%rax,%rcx), %rbx
cmpq 0x28(%rax,%rcx), %rbx
je 0x11b4d
leaq (%rax,%rcx), %r14
addq $0x20, %r14
movq (%rbx), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x168(%rbp)
movl %r13d, -0x160(%rbp)
movq %rax, -0x158(%rbp)
movq %r12, %rdi
leaq -0x168(%rbp), %rsi
callq 0x14a76
addq $0x8, %rbx
cmpq 0x8(%r14), %rbx
jne 0x1199b
jmp 0x11b4d
movl -0x30(%rbp), %eax
movq -0x58(%rbp), %rcx
movq (%rcx), %r12
shlq $0x7, %rax
leaq (%r12,%rax), %rbx
addq $0x58, %rbx
movq 0x10(%rbx), %r15
cmpq %rbx, %r15
je 0x11a4f
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %r14
movq 0x20(%r15), %rax
movl 0x4(%rax), %r13d
movq %r13, %rax
shlq $0x7, %rax
cmpq %r14, 0x8(%r12,%rax)
jne 0x11a3f
testq %r14, %r14
je 0x11950
addq %r12, %rax
movq (%rax), %rdi
movq -0x88(%rbp), %rsi
movq %r14, %rdx
callq 0x43a0
testl %eax, %eax
je 0x11950
movq %r15, %rdi
callq 0x4410
movq %rax, %r15
cmpq %rbx, %rax
jne 0x11a03
leaq -0x168(%rbp), %rbx
movq %rbx, %rdi
leaq -0x78(%rbp), %rsi
callq 0x1363e
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x1417a
leaq -0xe8(%rbp), %r12
movq %rbx, %rdi
callq 0x136c6
movq -0x38(%rbp), %rax
movq 0x28(%rax), %r15
subq 0x20(%rax), %r15
leaq -0xb8(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x135d2
shrq $0x7, %r15
decl %r15d
movl %r15d, (%rax)
cmpb $0x0, -0x3c(%rbp)
je 0x11ac5
movq -0x80(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1371e
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x13786
movl %r15d, (%rax)
movzwl -0x2a(%rbp), %ecx
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rax
imulq $0x38, %rcx, %rcx
movq 0x20(%rax,%rcx), %rbx
cmpq 0x28(%rax,%rcx), %rbx
je 0x11b1e
leaq (%rax,%rcx), %r14
addq $0x20, %r14
movq (%rbx), %rax
movzwl 0x2(%rax), %ecx
movw %cx, -0x168(%rbp)
movl %r15d, -0x160(%rbp)
movq %rax, -0x158(%rbp)
movq %r12, %rdi
leaq -0x168(%rbp), %rsi
callq 0x14a76
addq $0x8, %rbx
cmpq 0x8(%r14), %rbx
jne 0x11ae9
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %rdi
movl %r15d, %edx
callq 0x11cda
jmp 0x11b4d
leaq -0xb8(%rbp), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x135d2
movl (%rax), %edx
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %rdi
callq 0x11c7a
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x11b5f
callq 0x4350
cmpq $0x0, -0xc0(%rbp)
movq -0x48(%rbp), %r14
jne 0x1185b
movl 0x48(%r14), %eax
subl 0x40(%r14), %eax
shrl $0x3, %eax
imull $0x6db7, %eax, %eax     # imm = 0x6DB7
decl %eax
leaq -0x168(%rbp), %rsi
movw %ax, (%rsi)
leaq -0xb8(%rbp), %rdi
callq 0x13806
movl (%rax), %ebx
movq -0x38(%rbp), %rax
leaq 0x38(%rax), %rdi
movq %r14, %rsi
callq 0x13786
movl %ebx, (%rax)
movl 0x48(%r14), %eax
subl 0x40(%r14), %eax
shrl $0x3, %eax
imull $0x6db7, %eax, %eax     # imm = 0x6DB7
decl %eax
leaq -0x168(%rbp), %rsi
movw %ax, (%rsi)
leaq -0xb8(%rbp), %rdi
callq 0x13806
movq -0x38(%rbp), %rdi
addq $0x68, %rdi
movq %rax, %rsi
callq 0x13872
movq %rax, %rdi
movq %r14, %rsi
callq 0x41c0
leaq -0xe8(%rbp), %rdi
callq 0x13e5a
leaq -0xb8(%rbp), %rdi
callq 0x13eb2
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11c44
jmp 0x11c3b
jmp 0x11c3b
jmp 0x11c44
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0x136c6
jmp 0x11c47
jmp 0x11c44
jmp 0x11c3b
jmp 0x11c3b
movq %rax, %rbx
jmp 0x11c59
jmp 0x11c44
jmp 0x11c44
movq %rax, %rbx
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x11c59
callq 0x4350
leaq -0xe8(%rbp), %rdi
callq 0x13e5a
leaq -0xb8(%rbp), %rdi
callq 0x13eb2
movq %rbx, %rdi
callq 0x4650
nop