// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/EM.cc
// Function: viterbi(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>>>>>> const&, FactorGraph&, 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>>>>&, bool)
flt_type viterbi(const transitions_t &transitions,
                 FactorGraph &text,
                 vector<string> &best_path,
                 bool reverse)
{
    if (text.nodes.size() == 0) return MIN_FLOAT;
    best_path.clear();

    // Initialize node scores
    vector<flt_type> costs(text.nodes.size(), MIN_FLOAT);
    vector<int> source_nodes(text.nodes.size());
    costs[0] = 0.0; source_nodes[0] = -1;

    // Traverse paths
    for (unsigned int i=0; i<text.nodes.size(); i++) {

        FactorGraph::Node &node = text.nodes[i];
        for (auto arc = node.outgoing.begin(); arc != node.outgoing.end(); ++arc) {

            int src_node = (**arc).source_node;
            int tgt_node = (**arc).target_node;

            try {
                (**arc).cost = transitions.at(text.get_factor(src_node)).at(text.get_factor(tgt_node));
            }
            catch (std::out_of_range &oor) {
                (**arc).cost = SMALL_LP;
            }

            flt_type curr_cost = costs[tgt_node];
            flt_type new_cost = costs[i] + (**arc).cost;
            if (new_cost > curr_cost) {
                costs[tgt_node] = new_cost;
                source_nodes[tgt_node] = i;
            }
        }
    }

    // Find best path
    int node = text.nodes.size()-1;
    best_path.push_back(text.get_factor(node));
    while (true) {
        node = source_nodes[node];
        if (node == -1) break;
        best_path.push_back(text.get_factor(node));
    }
    if (reverse) std::reverse(best_path.begin(), best_path.end());

    return costs.back();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, -0x68(%rbp)
movq 0x48(%rsi), %rax
cmpq 0x40(%rsi), %rax
je 0x16327
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdx, %rdi
callq 0x7532
movq 0x48(%r15), %rax
subq 0x40(%r15), %rax
pushq $0x38
popq %r12
cqto
idivq %r12
movabsq $-0x10000000000001, %rcx # imm = 0xFFEFFFFFFFFFFFFF
leaq -0x58(%rbp), %rdx
movq %rcx, (%rdx)
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rcx
movq %rax, %rsi
callq 0x18ee6
movl %r14d, -0x38(%rbp)
movq %rbx, -0x60(%rbp)
movq 0x48(%r15), %rax
movq %r15, -0x30(%rbp)
subq 0x40(%r15), %rax
cqto
idivq %r12
leaq -0x88(%rbp), %rdi
leaq -0x58(%rbp), %rdx
movq %rax, %rsi
callq 0xe8ca
movq -0xa0(%rbp), %rax
andq $0x0, (%rax)
movq -0x88(%rbp), %rax
orl $-0x1, (%rax)
xorl %esi, %esi
movl %esi, %r14d
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rcx
movq 0x48(%rax), %rax
subq %rcx, %rax
cqto
idivq %r12
cmpq %r14, %rax
jbe 0x16283
movl %esi, -0x34(%rbp)
imulq $0x38, %r14, %rax
addq %rcx, %rax
addq $0x20, %rax
movq %rax, -0x70(%rbp)
movq (%rax), %r15
movq -0x70(%rbp), %rax
cmpq 0x8(%rax), %r15
je 0x16275
movq (%r15), %rax
movzwl (%rax), %edx
movzwl 0x2(%rax), %ebx
leaq -0x58(%rbp), %r12
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0xea5a
movq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x190b2
movq %rax, %r12
leaq -0xc0(%rbp), %r13
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0xea5a
movq %r12, %rdi
movq %r13, %rsi
callq 0x19102
movsd (%rax), %xmm0
movq (%r15), %rax
movsd %xmm0, 0x8(%rax)
movq %r13, %rdi
callq 0x46b8
leaq -0x58(%rbp), %rdi
callq 0x46b8
movq -0xa0(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
movq (%r15), %rcx
addsd 0x8(%rcx), %xmm0
movl %ebx, %ecx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x16216
movsd %xmm0, (%rax,%rcx,8)
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %edx
movl %edx, (%rax,%rcx,4)
addq $0x8, %r15
jmp 0x16172
movq %rax, %r12
movq %r13, %rdi
movq %rdx, %r13
callq 0x46b8
jmp 0x16237
jmp 0x16231
movq %rdx, %r13
movq %rax, %r12
leaq -0x58(%rbp), %rdi
callq 0x46b8
jmp 0x16248
movq %rdx, %r13
movq %rax, %r12
cmpl $0x1, %r13d
jne 0x16364
movq %r12, %rdi
callq 0x4150
movq (%r15), %rax
movabsq $-0x3fa7000000000000, %rcx # imm = 0xC059000000000000
movq %rcx, 0x8(%rax)
callq 0x4560
jmp 0x161e6
movl -0x34(%rbp), %esi
incl %esi
pushq $0x38
popq %r12
jmp 0x1613d
movq %rax, %rbx
decl %ebx
leaq -0x58(%rbp), %rdi
movq -0x30(%rbp), %r13
movq %r13, %rsi
movl %ebx, %edx
callq 0xea5a
leaq -0x58(%rbp), %rsi
movq -0x60(%rbp), %r15
movq %r15, %rdi
callq 0x7570
movl -0x38(%rbp), %r12d
leaq -0x58(%rbp), %r14
movq %r14, %rdi
callq 0x46b8
movslq %ebx, %rax
movq -0x88(%rbp), %rcx
movl (%rcx,%rax,4), %ebx
cmpl $-0x1, %ebx
je 0x162e6
movq %r14, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0xea5a
movq %r15, %rdi
movq %r14, %rsi
callq 0x7570
jmp 0x162b2
testb %r12b, %r12b
je 0x162f7
movq (%r15), %rdi
movq 0x8(%r15), %rsi
callq 0x1423d
leaq -0xa0(%rbp), %rbx
movq 0x8(%rbx), %rax
movsd -0x8(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xa2cc
movq %rbx, %rdi
callq 0x1a10a
movsd -0x30(%rbp), %xmm0
jmp 0x1632f
movsd 0xd461(%rip), %xmm0     # 0x23790
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x16353
jmp 0x16353
jmp 0x16358
jmp 0x16353
movq %rax, %r12
jmp 0x16370
movq %rax, %r12
jmp 0x1637c
movq %rax, %r12
jmp 0x16364
movq %rax, %r12
leaq -0x58(%rbp), %rdi
callq 0x46b8
leaq -0x88(%rbp), %rdi
callq 0xa2cc
leaq -0xa0(%rbp), %rdi
callq 0x1a10a
movq %r12, %rdi
callq 0x45f0