// File: aalto-speech[P]ftk/src/EM.cc
// Function: viterbi(MultiStringFactorGraph const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&)
flt_type
viterbi(const MultiStringFactorGraph &msfg,
        const string &text,
        vector<string> &best_path)
{
    map<msfg_node_idx_t, flt_type> scores;
    map<msfg_node_idx_t, msfg_node_idx_t> sources;
    unsigned int text_end_node = msfg.string_end_nodes.at(text);
    set<unsigned int> nodes_to_process; nodes_to_process.insert(text_end_node);
    scores[text_end_node] = 0.0;

    while(nodes_to_process.size() > 0) {

        int i = *(nodes_to_process.rbegin());

        const MultiStringFactorGraph::Node &node = msfg.nodes[i];

        for (auto arc = node.incoming.begin(); arc != node.incoming.end(); ++arc) {
            int src_node = (**arc).source_node;
            flt_type cost = scores[i] + *(**arc).cost;
            if (scores.find(src_node) == scores.end() || (cost > scores[src_node])) {
                scores[src_node] = cost;
                sources[src_node] = i;
            }
            nodes_to_process.insert(src_node);
        }

        nodes_to_process.erase(i);
    }

    best_path.clear();

    msfg_node_idx_t curr_node = 0;
    while (true) {
        best_path.push_back(msfg.nodes[curr_node].factor);
        if (curr_node == text_end_node) break;
        curr_node = sources[curr_node];
    }

    return scores.at(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x49290
leaq -0x78(%rbp), %rdi
callq 0x49750
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x10(%rbp), %rsi
callq 0x30970
movq %rax, -0x160(%rbp)
jmp 0x46e23
movq -0x160(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x30b10
movq -0x178(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x30b30
movb %dl, -0x169(%rbp)
movq %rax, -0x168(%rbp)
jmp 0x46e61
movb -0x169(%rbp), %al
movq -0x168(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movb %al, -0xc8(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x48e60
movq %rax, -0x180(%rbp)
jmp 0x46e91
movq -0x180(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq -0xc0(%rbp), %rdi
callq 0x30b80
cmpq $0x0, %rax
jbe 0x471da
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x190(%rbp)
leaq -0xc0(%rbp), %rsi
callq 0x30ba0
movq -0x190(%rbp), %rdi
callq 0x30bd0
movq %rax, -0x188(%rbp)
jmp 0x46ee4
movq -0x188(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd4(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movslq -0xd4(%rbp), %rsi
callq 0x30c00
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x30260
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x302c0
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x30290
testb $0x1, %al
jne 0x46f58
jmp 0x471b4
leaq -0xf0(%rbp), %rdi
callq 0x302f0
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0xfc(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x10c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
callq 0x48fd0
movq %rax, -0x198(%rbp)
jmp 0x46f94
movq -0x198(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x1a8(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x302f0
movsd -0x1a8(%rbp), %xmm0
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x11c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x11c(%rbp), %rsi
callq 0x48f70
movq %rax, -0x1a0(%rbp)
jmp 0x46ff7
movq -0x1a0(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x48fa0
movq %rax, -0x128(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x48f40
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x1a9(%rbp)
jne 0x47088
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x12c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x12c(%rbp), %rsi
callq 0x48fd0
movq %rax, -0x1b8(%rbp)
jmp 0x4706c
movsd -0x1c0(%rbp), %xmm0
movq -0x1b8(%rbp), %rax
ucomisd (%rax), %xmm0
seta %al
movb %al, -0x1a9(%rbp)
movb -0x1a9(%rbp), %al
testb $0x1, %al
jne 0x47097
jmp 0x4715b
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x1d0(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x130(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x48fd0
movq %rax, -0x1c8(%rbp)
jmp 0x470cc
movq -0x1c8(%rbp), %rax
movsd -0x1d0(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0xd4(%rbp), %eax
movl %eax, -0x1dc(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x134(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x134(%rbp), %rsi
callq 0x49770
movq %rax, -0x1d8(%rbp)
jmp 0x47110
movq -0x1d8(%rbp), %rax
movl -0x1dc(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x4715b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x472bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x30d30
jmp 0x472bc
movl -0xfc(%rbp), %eax
movl %eax, -0x138(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x49850
movb %dl, -0x1e9(%rbp)
movq %rax, -0x1e8(%rbp)
jmp 0x47189
movb -0x1e9(%rbp), %al
movq -0x1e8(%rbp), %rcx
movq %rcx, -0x148(%rbp)
movb %al, -0x140(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x30320
jmp 0x46f25
movl -0xd4(%rbp), %eax
movl %eax, -0x14c(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x14c(%rbp), %rsi
callq 0x30c20
jmp 0x471d5
jmp 0x46e9f
movq -0x18(%rbp), %rdi
callq 0xb7e0
movl $0x0, -0x150(%rbp)
jmp 0x471ef
movq -0x18(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movl -0x150(%rbp), %eax
movl %eax, %esi
callq 0x30c00
movq -0x1f8(%rbp), %rdi
movq %rax, %rsi
callq 0xabb0
jmp 0x47220
movl -0x150(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jne 0x4722d
jmp 0x47257
leaq -0x78(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x498a0
movq %rax, -0x200(%rbp)
jmp 0x47246
movq -0x200(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x150(%rbp)
jmp 0x471ed
movl $0x0, -0x154(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x154(%rbp), %rsi
callq 0x490b0
movq %rax, -0x208(%rbp)
jmp 0x4727a
movq -0x208(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x210(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x30d30
leaq -0x78(%rbp), %rdi
callq 0x49980
leaq -0x48(%rbp), %rdi
callq 0x492b0
movsd -0x210(%rbp), %xmm0
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x49980
leaq -0x48(%rbp), %rdi
callq 0x492b0
movq -0x88(%rbp), %rdi
callq 0x46e0
nopw (%rax,%rax)