// File: /workspace/github_cpp_makefile_star2_O3/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, -0x68(%rbp)
movq %rsi, %r14
movq 0x28(%rdi), %rsi
subq 0x20(%rdi), %rsi
je 0x1ae12
movq %rdi, %r15
sarq $0x7, %rsi
movabsq $-0x10000000000001, %rax # imm = 0xFFEFFFFFFFFFFFFF
leaq -0x80(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x98(%rbp), %rdi
leaq -0x4c(%rbp), %r12
movq %r12, %rcx
callq 0x1b134
movq 0x28(%r15), %rsi
subq 0x20(%r15), %rsi
sarq $0x7, %rsi
movl $0xffffffff, (%r12)     # imm = 0xFFFFFFFF
leaq -0x80(%rbp), %rdi
leaq -0x4c(%rbp), %rdx
leaq -0x29(%rbp), %rcx
callq 0x1b864
movq -0x98(%rbp), %r13
movq $0x0, (%r13)
movq 0x20(%r15), %rax
movq 0x28(%r15), %rcx
movq %rax, -0x38(%rbp)
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
je 0x1acee
sarq $0x7, -0x40(%rbp)
xorl %r12d, %r12d
movsd (%r13,%r12,8), %xmm0
ucomisd 0xbb82(%rip), %xmm0     # 0x26800
jne 0x1ac82
jnp 0x1ace2
movq %r12, %rax
shlq $0x7, %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rax), %rbx
addq $0x58, %rbx
movq 0x10(%rbx), %rax
cmpq %rbx, %rax
je 0x1ace2
movq -0x80(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq 0x20(%rax), %rcx
movsd (%r13,%r12,8), %xmm0
movq 0x8(%rcx), %rdx
addsd (%rdx), %xmm0
movslq 0x4(%rcx), %rcx
ucomisd (%r13,%rcx,8), %xmm0
jbe 0x1acd5
movsd %xmm0, (%r13,%rcx,8)
movq -0x48(%rbp), %rdx
movl %r12d, (%rdx,%rcx,4)
movq %rax, %rdi
callq 0x4410
cmpq %rbx, %rax
jne 0x1aca6
incq %r12
movl %r12d, %eax
cmpq %rax, -0x40(%rbp)
ja 0x1ac6f
movq 0x50(%r15), %rax
leaq 0x40(%r15), %rcx
movq %rcx, -0x58(%rbp)
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0x1ae1c
movsd %xmm0, -0x38(%rbp)
leaq 0x20(%rax), %r12
movq %rax, -0x60(%rbp)
movl 0x40(%rax), %eax
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x1adb7
movq -0x40(%rbp), %rbx
movslq %ebx, %r13
movq -0x80(%rbp), %rax
movslq (%rax,%r13,4), %rbx
testq %rbx, %rbx
js 0x1ae57
movq %r14, %rdi
movq %r12, %rsi
callq 0x1b3f6
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
subq %rsi, %rdx
sarq $0x7, %rdx
cmpq %rbx, %rdx
jbe 0x1ae9a
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq %rbx, %rax
shlq $0x7, %rax
addq %rax, %rsi
movq -0x68(%rbp), %rdi
callq 0x1b318
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
subq %rsi, %rdx
sarq $0x7, %rdx
cmpq %r13, %rdx
jbe 0x1ae97
shlq $0x7, %r13
addq %r13, %rsi
movq %rax, %rdi
callq 0x1af86
movsd -0x48(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
testl %ebx, %ebx
jne 0x1ad28
movq %r14, %rdi
movq %r12, %rsi
callq 0x1b3f6
movq -0x98(%rbp), %r13
movq -0x90(%rbp), %rdx
subq %r13, %rdx
sarq $0x3, %rdx
movq -0x40(%rbp), %rsi
cmpq %rsi, %rdx
movq -0x60(%rbp), %rdi
jbe 0x1aeab
movsd (%rax), %xmm0
mulsd (%r13,%rsi,8), %xmm0
movsd -0x38(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0x4410
cmpq -0x58(%rbp), %rax
jne 0x1ad0c
jmp 0x1ae21
movsd 0xb9e6(%rip), %xmm0     # 0x26800
jmp 0x1ae48
movsd %xmm0, -0x38(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x1ae3b
callq 0x4350
movq -0x98(%rbp), %r13
testq %r13, %r13
je 0x1ae43
movq %r13, %rdi
callq 0x4350
movsd -0x38(%rbp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x20, %edi
callq 0x4190
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
leaq 0xb9be(%rip), %rsi      # 0x26830
leaq 0xb9dc(%rip), %rdx      # 0x26855
movq %r14, %rdi
callq 0x5fac
leaq 0x18ec8(%rip), %rsi     # 0x33d50
movq 0x19111(%rip), %rdx     # 0x33fa0
movq %r14, %rdi
callq 0x4610
movq %r13, %rbx
leaq 0xb9b5(%rip), %rdi      # 0x26856
movq %rbx, %rsi
xorl %eax, %eax
callq 0x4530
leaq 0xb9a4(%rip), %rdi      # 0x26856
xorl %eax, %eax
callq 0x4530
movq %rax, %rbx
jmp 0x1aee4
jmp 0x1aed3
jmp 0x1aed3
jmp 0x1aed3
movq %rax, %rbx
movq %r14, %rdi
callq 0x42b0
jmp 0x1aed6
jmp 0x1aed3
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x1aee4
callq 0x4350
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x1aef5
callq 0x4350
movq %rbx, %rdi
callq 0x4650
nop