// File: 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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x30500
cmpq $0x0, %rax
jne 0x47470
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x47a5d
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x30500
movq %rax, -0xe0(%rbp)
callq 0x47bf0
movaps 0x1f300(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x40(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x48220
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rcx
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x48240
jmp 0x474c6
leaq -0x41(%rbp), %rdi
callq 0x482c0
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x30500
movq %rax, -0xf0(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
leaq -0x75(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0xc290
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x74(%rbp), %rdx
callq 0x499a0
jmp 0x47517
leaq -0x75(%rbp), %rdi
callq 0xc910
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq %rax, -0xf8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x30500
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x47741
movl -0x7c(%rbp), %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x100(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x100(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x475e7
jp 0x475e7
jmp 0x47733
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x482c0
jmp 0x47a6b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x75(%rbp), %rdi
callq 0xc910
jmp 0x47a52
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x30c00
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
addq $0x50, %rdi
callq 0x30260
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
addq $0x50, %rdi
callq 0x302c0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x30290
testb $0x1, %al
jne 0x4764a
jmp 0x47731
leaq -0x90(%rbp), %rdi
callq 0x302f0
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x9c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x302f0
movsd -0x110(%rbp), %xmm0
movq (%rax), %rax
movq 0x8(%rax), %rax
addsd (%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd -0x108(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x4771e
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x120(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd -0x120(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x7c(%rbp), %eax
movl %eax, -0x114(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x13820
movl -0x114(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x47720
leaq -0x90(%rbp), %rdi
callq 0x30320
jmp 0x47617
jmp 0x47733
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x4753b
xorps %xmm0, %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x49720
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x30940
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x31430
testb $0x1, %al
jne 0x47790
jmp 0x47a28
leaq -0xb8(%rbp), %rdi
callq 0x31490
movl 0x20(%rax), %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xc8(%rbp)
cmpl $0x0, -0xc8(%rbp)
je 0x47990
movslq -0xc8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x13820
cmpl $0x0, (%rax)
jge 0x4787f
movb $0x1, -0xca(%rbp)
movl $0x20, %edi
callq 0x41e0
movq %rax, -0x130(%rbp)
movq %rax, -0x138(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x46f0
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rdx
leaq 0x1ef82(%rip), %rsi     # 0x667a0
callq 0x4550
jmp 0x47825
movq -0x138(%rbp), %rdi
movb $0x0, -0xca(%rbp)
leaq 0x4c516(%rip), %rsi     # 0x93d50
movq 0x4c75f(%rip), %rdx     # 0x93fa0
callq 0x46b0
jmp 0x47a74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc9(%rbp), %rdi
callq 0x4420
testb $0x1, -0xca(%rbp)
jne 0x4786e
jmp 0x4787a
movq -0x138(%rbp), %rdi
callq 0x42f0
jmp 0x47a49
movslq -0xc8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x13820
movl (%rax), %eax
movl %eax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x31490
movq -0x148(%rbp), %rdi
movq %rax, %rsi
callq 0x48880
movq %rax, -0x140(%rbp)
jmp 0x478c6
movq -0x140(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x160(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
movl -0xd0(%rbp), %eax
movl %eax, %esi
callq 0x49550
movq %rax, -0x150(%rbp)
jmp 0x47902
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdi
callq 0x48700
movq %rax, -0x168(%rbp)
jmp 0x4791e
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
movslq -0xc8(%rbp), %rsi
callq 0x49550
movq %rax, -0x170(%rbp)
jmp 0x4793b
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdi
callq 0x47db0
movq %rax, -0x178(%rbp)
jmp 0x47957
movq -0x178(%rbp), %rax
movsd -0x160(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl -0xd0(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x477b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x47a49
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x31490
movq -0x188(%rbp), %rdi
movq %rax, %rsi
callq 0x48880
movq %rax, -0x180(%rbp)
jmp 0x479bf
movq -0x180(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x198(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x49220
movq %rax, -0x190(%rbp)
jmp 0x479ec
movsd -0x198(%rbp), %xmm0
movq -0x190(%rbp), %rax
movsd (%rax), %xmm2
movsd -0xb0(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x314b0
jmp 0x47760
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x14140
leaq -0x38(%rbp), %rdi
callq 0x48320
jmp 0x47a5d
leaq -0x70(%rbp), %rdi
callq 0x14140
leaq -0x38(%rbp), %rdi
callq 0x48320
jmp 0x47a6b
movsd -0x8(%rbp), %xmm0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)