// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rdi, -0x80(%rbp)
movq 0x48(%rsi), %rax
cmpq 0x40(%rsi), %rax
je 0x17bff
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdx, %rdi
callq 0x82e8
movq 0x48(%r15), %rsi
subq 0x40(%r15), %rsi
sarq $0x3, %rsi
movabsq $0x6db6db6db6db6db7, %r12 # imm = 0x6DB6DB6DB6DB6DB7
imulq %r12, %rsi
movabsq $-0x10000000000001, %rax # imm = 0xFFEFFFFFFFFFFFFF
leaq -0x58(%rbp), %rdx
movq %rax, (%rdx)
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rcx
callq 0x1b220
movq 0x48(%r15), %rsi
subq 0x40(%r15), %rsi
sarq $0x3, %rsi
imulq %r12, %rsi
leaq -0x70(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x101a2
movl %r14d, -0x34(%rbp)
movq %rbx, -0x78(%rbp)
movq -0xa8(%rbp), %rax
movq $0x0, (%rax)
movq -0x70(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x40(%r15), %rax
movq %r15, -0x30(%rbp)
movq 0x48(%r15), %rbx
subq %rax, %rbx
je 0x17c0c
xorl %edx, %edx
leaq -0x58(%rbp), %r12
imulq $0x38, %rdx, %rcx
movq 0x20(%rax,%rcx), %r14
cmpq 0x28(%rax,%rcx), %r14
je 0x17bd1
addq %rcx, %rax
addq $0x20, %rax
movq %rax, -0x88(%rbp)
movq %rdx, -0x90(%rbp)
movq (%r14), %rax
movzwl (%rax), %edx
movzwl 0x2(%rax), %ebx
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x10378
movq -0x80(%rbp), %rdi
movq %r12, %r15
movq %r12, %rsi
callq 0x1b47a
movq %rax, %r12
leaq -0xc8(%rbp), %r13
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0x10378
movq %r12, %rdi
movq %r13, %rsi
callq 0x1b4d8
movsd (%rax), %xmm0
movq (%r14), %rax
movsd %xmm0, 0x8(%rax)
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x17b03
callq 0x4350
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x17b15
callq 0x4350
movq -0xa8(%rbp), %rax
movq -0x90(%rbp), %rdx
movsd (%rax,%rdx,8), %xmm0
movq (%r14), %rcx
addsd 0x8(%rcx), %xmm0
movl %ebx, %ecx
ucomisd (%rax,%rcx,8), %xmm0
movq %r15, %r12
jbe 0x17b48
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movl %edx, (%rax,%rcx,4)
addq $0x8, %r14
movq -0x88(%rbp), %rax
cmpq 0x8(%rax), %r14
jne 0x17a97
jmp 0x17bd1
movq %rdx, %r12
movq %rax, %r13
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x17b87
callq 0x4350
jmp 0x17b87
jmp 0x17b81
movq %rdx, %r12
movq %rax, %r13
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x17ba4
callq 0x4350
jmp 0x17ba4
movq %r12, %r15
movq %rdx, %r12
movq %rax, %r13
cmpl $0x1, %r12d
jne 0x17d50
movq %r13, %rdi
callq 0x4160
movq (%r14), %rax
movabsq $-0x3fa7000000000000, %rcx # imm = 0xC059000000000000
movq %rcx, 0x8(%rax)
callq 0x45c0
jmp 0x17b15
incl %edx
movq -0x30(%rbp), %rcx
movq 0x40(%rcx), %rax
movq 0x48(%rcx), %rbx
subq %rax, %rbx
sarq $0x3, %rbx
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rbx
cmpq %rdx, %rbx
ja 0x17a6e
jmp 0x17c14
movsd 0xebf9(%rip), %xmm0     # 0x26800
jmp 0x17d08
sarq $0x3, %rbx
imulq %r12, %rbx
decl %ebx
leaq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0x10378
leaq -0x58(%rbp), %rsi
movq -0x78(%rbp), %r13
movq %r13, %rdi
callq 0x832e
leaq -0x48(%rbp), %r12
movq -0x10(%r12), %rdi
cmpq %r12, %rdi
je 0x17c48
callq 0x4350
movslq %ebx, %rax
movq -0x70(%rbp), %rcx
movl (%rcx,%rax,4), %ebx
cmpl $-0x1, %ebx
je 0x17c91
leaq -0x58(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0x10378
movq %r13, %rdi
movq %r14, %rsi
callq 0x832e
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x17c82
callq 0x4350
movslq %ebx, %rax
movq -0x70(%rbp), %rcx
movl (%rcx,%rax,4), %ebx
cmpl $-0x1, %ebx
jne 0x17c5b
cmpb $0x0, -0x34(%rbp)
je 0x17cce
movq (%r13), %rbx
movq 0x8(%r13), %r14
cmpq %r14, %rbx
setne %al
addq $-0x20, %r14
cmpq %rbx, %r14
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x17cce
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42a0
addq $0x20, %rbx
addq $-0x20, %r14
cmpq %r14, %rbx
jb 0x17cb6
movq -0xa0(%rbp), %rax
movsd -0x8(%rax), %xmm0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
movsd %xmm0, -0x30(%rbp)
je 0x17cf2
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x17d08
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17d3a
movq %rax, %r13
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x17d4b
jmp 0x17d50
jmp 0x17d3a
movq %rax, %r13
jmp 0x17d5e
movq %rax, %r13
jmp 0x17d6f
movq %rax, %r13
jmp 0x17d50
movq %rax, %r13
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x17d50
callq 0x4350
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x17d5e
callq 0x4350
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x17d6f
callq 0x4350
movq %r13, %rdi
callq 0x4650