// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/EM.cc
// Function: viterbi(MultiStringFactorGraph const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double, 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, double>>> const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double, 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, double>>>, 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, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double, 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, double>>>>>>&)
flt_type viterbi(const MultiStringFactorGraph &msfg,
                 const map<string, flt_type> &word_freqs,
                 transitions_t &stats)
{
    if (msfg.nodes.size() == 0) return MIN_FLOAT;

    vector<flt_type> fw(msfg.nodes.size(), MIN_FLOAT);
    vector<int> source_nodes(msfg.nodes.size(), -1);
    fw[0] = 0.0;

    for (unsigned int i=0; i<msfg.nodes.size(); i++) {
        if (fw[i] == MIN_FLOAT) continue;
        const MultiStringFactorGraph::Node &node = msfg.nodes[i];
        for (auto arc = node.outgoing.begin(); arc != node.outgoing.end(); ++arc) {
            int tgt_node = (**arc).target_node;
            flt_type cost = fw[i] + *(**arc).cost;
            if (cost > fw[tgt_node]) {
                fw[tgt_node] = cost;
                source_nodes[tgt_node] = i;
            }
        }
    }

    flt_type total_lp = 0.0;
    for (auto it = msfg.string_end_nodes.begin(); it != msfg.string_end_nodes.end(); ++it) {
        msfg_node_idx_t text_end_node = it->second;
        int curr_node = text_end_node;
        while (curr_node != 0) {
            if (source_nodes[curr_node] < 0) throw string("Problem in backtracking Viterbi path.");
            msfg_node_idx_t src_node = source_nodes[curr_node];
            stats[msfg.nodes.at(src_node).factor][msfg.nodes.at(curr_node).factor] += word_freqs.at(it->first);
            curr_node = src_node;
        }
        total_lp += word_freqs.at(it->first) * fw.at(text_end_node);
    }

    return total_lp;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, -0x70(%rbp)
movq %rsi, -0x60(%rbp)
movq 0x28(%rdi), %rsi
subq 0x20(%rdi), %rsi
je 0x18cba
movq %rdi, %r14
sarq $0x7, %rsi
movabsq $-0x10000000000001, %rax # imm = 0xFFEFFFFFFFFFFFFF
leaq -0x88(%rbp), %rdx
movq %rax, (%rdx)
leaq -0xa0(%rbp), %rdi
leaq -0x44(%rbp), %rbx
movq %rbx, %rcx
callq 0x18ee6
movq 0x28(%r14), %rsi
subq 0x20(%r14), %rsi
sarq $0x7, %rsi
orl $-0x1, (%rbx)
leaq -0x88(%rbp), %rdi
leaq -0x44(%rbp), %rdx
leaq -0x29(%rbp), %rcx
callq 0x19504
movq -0xa0(%rbp), %rbx
andq $0x0, (%rbx)
leaq 0x20(%r14), %r12
movq 0x20(%r14), %rax
movq %r14, -0x50(%rbp)
movq 0x28(%r14), %rcx
movq %rax, -0x58(%rbp)
subq %rax, %rcx
sarq $0x7, %rcx
movq %rcx, -0x38(%rbp)
xorl %r13d, %r13d
movl %r13d, %r14d
cmpq %r14, -0x38(%rbp)
jbe 0x18baf
movsd (%rbx,%r14,8), %xmm0
ucomisd 0xac45(%rip), %xmm0     # 0x23790
jne 0x18b54
jp 0x18b54
incl %r13d
jmp 0x18b34
movq %r14, %rax
shlq $0x7, %rax
movq -0x58(%rbp), %rcx
leaq (%rcx,%rax), %r15
addq $0x58, %r15
movq 0x10(%r15), %rax
movq -0x88(%rbp), %rcx
movq %rcx, -0x40(%rbp)
cmpq %r15, %rax
je 0x18b4f
movq 0x20(%rax), %rcx
movsd (%rbx,%r14,8), %xmm0
movq 0x8(%rcx), %rdx
addsd (%rdx), %xmm0
movslq 0x4(%rcx), %rcx
ucomisd (%rbx,%rcx,8), %xmm0
jbe 0x18ba5
movsd %xmm0, (%rbx,%rcx,8)
movq -0x40(%rbp), %rdx
movl %r13d, (%rdx,%rcx,4)
movq %rax, %rdi
callq 0x43b0
jmp 0x18b76
movq -0x50(%rbp), %rcx
movq 0x50(%rcx), %rax
addq $0x40, %rcx
movq %rcx, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
cmpq -0x50(%rbp), %rax
je 0x18c9b
leaq 0x20(%rax), %r15
movq %rax, -0x68(%rbp)
movl 0x40(%rax), %r13d
movq %r13, -0x58(%rbp)
testl %r13d, %r13d
je 0x18c51
movslq %r13d, %rbx
movq -0x88(%rbp), %rax
movslq (%rax,%rbx,4), %r13
testq %r13, %r13
js 0x18cd1
movq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x19102
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x193e4
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0x19048
movq %rax, %r14
movq %r12, %rdi
movq %rbx, %rsi
callq 0x193e4
movq %r14, %rdi
movq %rax, %rsi
callq 0x18d90
movsd -0x40(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x18be1
movq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x19102
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq -0xa0(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x19370
movsd -0x40(%rbp), %xmm1
mulsd (%rax), %xmm1
movsd -0x38(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x68(%rbp), %rdi
callq 0x43b0
jmp 0x18bc7
leaq -0x88(%rbp), %rdi
callq 0xa2cc
leaq -0xa0(%rbp), %rdi
callq 0x1a10a
movsd -0x38(%rbp), %xmm0
jmp 0x18cc2
movsd 0xaace(%rip), %xmm0     # 0x23790
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x4190
movq %rax, %r14
leaq 0xaadd(%rip), %rsi      # 0x237c0
leaq -0x44(%rbp), %rdx
movq %rax, %rdi
callq 0x4140
leaq 0x1c062(%rip), %rsi     # 0x34d58
movq 0x1c2a3(%rip), %rdx     # 0x34fa0
movq %r14, %rdi
callq 0x45c0
movq %rax, %rbx
jmp 0x18d2a
jmp 0x18d1b
jmp 0x18d1b
movq %rax, %rbx
movq %r14, %rdi
callq 0x4290
jmp 0x18d1e
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0xa2cc
leaq -0xa0(%rbp), %rdi
callq 0x1a10a
movq %rbx, %rdi
callq 0x45f0