// File: /workspace/github_cpp_makefile_star2_O1/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 0x1aee6
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 0x1b220
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 0x1b952
movq -0x98(%rbp), %r12
movq $0x0, (%r12)
movq 0x20(%r15), %rax
movq 0x28(%r15), %rcx
movq %rax, -0x38(%rbp)
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
je 0x1adc4
sarq $0x7, -0x40(%rbp)
xorl %r13d, %r13d
movsd (%r12,%r13,8), %xmm0
ucomisd 0xbaa9(%rip), %xmm0     # 0x26800
jne 0x1ad5b
jnp 0x1adb8
movq %r13, %rax
shlq $0x7, %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rax), %rbx
addq $0x58, %rbx
movq 0x10(%rbx), %rax
cmpq %rbx, %rax
je 0x1adb8
movq -0x80(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq 0x20(%rax), %rcx
movsd (%r12,%r13,8), %xmm0
movq 0x8(%rcx), %rdx
addsd (%rdx), %xmm0
movslq 0x4(%rcx), %rcx
ucomisd (%r12,%rcx,8), %xmm0
jbe 0x1adab
movsd %xmm0, (%r12,%rcx,8)
movq -0x48(%rbp), %rdx
movl %r13d, (%rdx,%rcx,4)
movq %rax, %rdi
callq 0x4410
cmpq %rbx, %rax
jne 0x1ad7f
incq %r13
movl %r13d, %eax
cmpq %rax, -0x40(%rbp)
ja 0x1ad49
movq 0x50(%r15), %rax
leaq 0x40(%r15), %rcx
movq %rcx, -0x58(%rbp)
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0x1aef0
movsd %xmm0, -0x38(%rbp)
leaq 0x20(%rax), %r12
movq %rax, -0x60(%rbp)
movl 0x40(%rax), %eax
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x1ae8d
movq -0x40(%rbp), %rbx
movslq %ebx, %r13
movq -0x80(%rbp), %rax
movslq (%rax,%r13,4), %rbx
testq %rbx, %rbx
js 0x1af28
movq %r14, %rdi
movq %r12, %rsi
callq 0x1b4d8
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
subq %rsi, %rdx
sarq $0x7, %rdx
cmpq %rbx, %rdx
jbe 0x1af71
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq %rbx, %rax
shlq $0x7, %rax
addq %rax, %rsi
movq -0x68(%rbp), %rdi
callq 0x1b3fa
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
subq %rsi, %rdx
sarq $0x7, %rdx
cmpq %r13, %rdx
jbe 0x1af6e
shlq $0x7, %r13
addq %r13, %rsi
movq %rax, %rdi
callq 0x1b05e
movsd -0x48(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
testl %ebx, %ebx
jne 0x1adfe
movq %r14, %rdi
movq %r12, %rsi
callq 0x1b4d8
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq -0x40(%rbp), %rsi
cmpq %rsi, %rdx
movq -0x60(%rbp), %rdi
jbe 0x1af82
movsd (%rax), %xmm0
mulsd (%rcx,%rsi,8), %xmm0
movsd -0x38(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0x4410
cmpq -0x58(%rbp), %rax
jne 0x1ade2
jmp 0x1aef5
movsd 0xb912(%rip), %xmm0     # 0x26800
jmp 0x1af19
movsd %xmm0, -0x38(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x1af03
callq 0x4350
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x1af14
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)
movb $0x1, %r15b
leaq 0xb8ea(%rip), %rsi      # 0x26830
leaq 0xb908(%rip), %rdx      # 0x26855
movq %r14, %rdi
callq 0x5fa4
xorl %r15d, %r15d
leaq 0x18df1(%rip), %rsi     # 0x33d50
movq 0x1903a(%rip), %rdx     # 0x33fa0
movq %r14, %rdi
callq 0x4610
movq %r13, %rbx
leaq 0xb8de(%rip), %rdi      # 0x26856
movq %rbx, %rsi
xorl %eax, %eax
callq 0x4530
leaq 0xb8cd(%rip), %rdi      # 0x26856
xorl %eax, %eax
callq 0x4530
movq %rax, %rbx
jmp 0x1afbe
jmp 0x1afad
jmp 0x1afad
movq %rax, %rbx
testb %r15b, %r15b
je 0x1afb0
movq %r14, %rdi
callq 0x42b0
jmp 0x1afb0
jmp 0x1afad
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x1afbe
callq 0x4350
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x1afcf
callq 0x4350
movq %rbx, %rdi
callq 0x4650
nop