// File: /workspace/github_cpp_makefile_star2_O3/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, -0x98(%rbp)
movq 0x48(%rsi), %rax
cmpq 0x40(%rsi), %rax
je 0x17aa8
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdx, %rdi
callq 0x82a2
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 -0x88(%rbp), %rdi
leaq -0xc8(%rbp), %rcx
callq 0x1b134
movq 0x48(%r15), %rsi
subq 0x40(%r15), %rsi
sarq $0x3, %rsi
imulq %r12, %rsi
leaq -0x70(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x100b0
movl %r14d, -0x34(%rbp)
movq %rbx, -0x90(%rbp)
movq -0x88(%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), %rcx
movq %rcx, %rbx
subq %rax, %rbx
je 0x17ab5
xorl %edi, %edi
leaq -0x58(%rbp), %r12
movq -0x30(%rbp), %rsi
imulq $0x38, %rdi, %rdx
movq 0x20(%rax,%rdx), %r14
cmpq 0x28(%rax,%rdx), %r14
je 0x17a83
addq %rdx, %rax
addq $0x20, %rax
movq %rax, -0xa0(%rbp)
movq %rdi, -0xa8(%rbp)
movq (%r14), %rax
movzwl (%rax), %edx
movzwl 0x2(%rax), %ebx
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x10288
movq -0x98(%rbp), %rdi
movq %r12, %r15
movq %r12, %rsi
callq 0x1b398
movq %rax, %r12
leaq -0xc8(%rbp), %r13
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0x10288
movq %r12, %rdi
movq %r13, %rsi
callq 0x1b3f6
movsd (%rax), %xmm0
movq (%r14), %rax
movsd %xmm0, 0x8(%rax)
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x179a9
callq 0x4350
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x179bb
callq 0x4350
movq -0x88(%rbp), %rax
movq -0xa8(%rbp), %rdi
movsd (%rax,%rdi,8), %xmm0
movq (%r14), %rcx
addsd 0x8(%rcx), %xmm0
movl %ebx, %ecx
ucomisd (%rax,%rcx,8), %xmm0
movq %r15, %r12
jbe 0x179ee
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movl %edi, (%rax,%rcx,4)
addq $0x8, %r14
movq -0xa0(%rbp), %rax
cmpq 0x8(%rax), %r14
jne 0x1793a
jmp 0x17a77
movq %rdx, %r12
movq %rax, %r13
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x17a2d
callq 0x4350
jmp 0x17a2d
jmp 0x17a27
movq %rdx, %r12
movq %rax, %r13
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x17a4a
callq 0x4350
jmp 0x17a4a
movq %r12, %r15
movq %rdx, %r12
movq %rax, %r13
cmpl $0x1, %r12d
jne 0x17c09
movq %r13, %rdi
callq 0x4160
movq (%r14), %rax
movabsq $-0x3fa7000000000000, %rcx # imm = 0xC059000000000000
movq %rcx, 0x8(%rax)
callq 0x45c0
jmp 0x179bb
movq -0x30(%rbp), %rsi
movq 0x40(%rsi), %rax
movq 0x48(%rsi), %rcx
incl %edi
movq %rcx, %rbx
subq %rax, %rbx
sarq $0x3, %rbx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rbx
cmpq %rdi, %rbx
ja 0x17911
jmp 0x17ac1
movsd 0xed50(%rip), %xmm0     # 0x26800
jmp 0x17bc1
sarq $0x3, %rbx
imulq %r12, %rbx
movq -0x30(%rbp), %rsi
decl %ebx
leaq -0x58(%rbp), %rdi
movl %ebx, %edx
callq 0x10288
leaq -0x58(%rbp), %rsi
movq -0x90(%rbp), %r13
movq %r13, %rdi
callq 0x82e8
leaq -0x48(%rbp), %r12
movq -0x10(%r12), %rdi
cmpq %r12, %rdi
je 0x17af4
callq 0x4350
movslq %ebx, %rax
movq -0x70(%rbp), %rdi
movl (%rdi,%rax,4), %ebx
cmpl $-0x1, %ebx
je 0x17b3d
leaq -0x58(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl %ebx, %edx
callq 0x10288
movq %r13, %rdi
movq %r14, %rsi
callq 0x82e8
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x17b2e
callq 0x4350
movslq %ebx, %rax
movq -0x70(%rbp), %rdi
movl (%rdi,%rax,4), %ebx
cmpl $-0x1, %ebx
jne 0x17b07
cmpb $0x0, -0x34(%rbp)
je 0x17b8e
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 0x17b8e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42a0
addq $0x20, %rbx
addq $-0x20, %r14
cmpq %r14, %rbx
jb 0x17b62
movq -0x70(%rbp), %rdi
movq -0x80(%rbp), %rax
movsd -0x8(%rax), %xmm0
testq %rdi, %rdi
jne 0x17b97
jmp 0x17ba6
movq -0x80(%rbp), %rax
movsd -0x8(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x17bc1
movsd %xmm0, -0x30(%rbp)
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17bf3
movq %rax, %r13
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x17c04
jmp 0x17c09
jmp 0x17bf3
movq %rax, %r13
jmp 0x17c17
movq %rax, %r13
jmp 0x17c28
movq %rax, %r13
jmp 0x17c09
movq %rax, %r13
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x17c09
callq 0x4350
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x17c17
callq 0x4350
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x17c28
callq 0x4350
movq %r13, %rdi
callq 0x4650