// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
cmpq $0x0, %rax
jne 0x41f17
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x42498
movq -0x20(%rbp), %rdi
callq 0xb7e0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x160(%rbp)
callq 0x47bf0
movaps 0x24850(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x48(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x48220
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rcx
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x48240
jmp 0x41f76
leaq -0x49(%rbp), %rdi
callq 0x482c0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x170(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0xc290
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x23f80
jmp 0x41fbc
leaq -0x79(%rbp), %rdi
callq 0xc910
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq -0x78(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13820
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x42320
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movl -0x80(%rbp), %eax
movl %eax, %esi
callq 0x23ae0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
callq 0x24310
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
callq 0x24340
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x245f0
testb $0x1, %al
jne 0x42080
jmp 0x42310
leaq -0x90(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x24630
movq -0x188(%rbp), %rdi
movq (%rax), %rax
movzwl (%rax), %eax
movl %eax, -0x9c(%rbp)
callq 0x24630
movq (%rax), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x18(%rbp), %rsi
movl -0x9c(%rbp), %edx
leaq -0xc0(%rbp), %rdi
callq 0x24510
jmp 0x420db
movq -0x180(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x487e0
movq %rax, -0x190(%rbp)
jmp 0x420f7
movq -0x18(%rbp), %rsi
movl -0xa0(%rbp), %edx
leaq -0xe0(%rbp), %rdi
callq 0x24510
jmp 0x4210f
movq -0x190(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x48880
movq %rax, -0x198(%rbp)
jmp 0x4212b
movq -0x198(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x1a0(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x24630
movsd -0x1a0(%rbp), %xmm0
movq (%rax), %rax
movsd %xmm0, 0x8(%rax)
leaq -0xe0(%rbp), %rdi
callq 0x4828
leaq -0xc0(%rbp), %rdi
callq 0x4828
jmp 0x42231
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x482c0
jmp 0x424a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x79(%rbp), %rdi
callq 0xc910
jmp 0x4248d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x421eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x421df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x4828
leaq -0xc0(%rbp), %rdi
callq 0x4828
movl -0x5c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x42484
movq -0x58(%rbp), %rdi
callq 0x4180
movq %rax, -0xe8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x24630
movq (%rax), %rax
movabsq $-0x3fa7000000000000, %rcx # imm = 0xC059000000000000
movq %rcx, 0x8(%rax)
callq 0x4660
jmp 0x4222f
jmp 0x42231
movslq -0xa0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0xf0(%rbp)
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x1a8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x24630
movsd -0x1a8(%rbp), %xmm0
movq (%rax), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0xf8(%rbp), %xmm0
ucomisd -0xf0(%rbp), %xmm0
jbe 0x422fd
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x1b8(%rbp)
movslq -0xa0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd -0x1b8(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x80(%rbp), %eax
movl %eax, -0x1ac(%rbp)
movslq -0xa0(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x13820
movl -0x1ac(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x422fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x42484
jmp 0x422ff
leaq -0x90(%rbp), %rdi
callq 0x24650
jmp 0x4204d
jmp 0x42312
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x41ff3
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
decl %eax
movl %eax, -0xfc(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x18(%rbp), %rsi
movl -0xfc(%rbp), %edx
leaq -0x120(%rbp), %rdi
callq 0x24510
jmp 0x42358
movq -0x1c0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0xb800
jmp 0x4236d
leaq -0x120(%rbp), %rdi
callq 0x4828
jmp 0x4237b
movslq -0xfc(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x13820
movl (%rax), %eax
movl %eax, -0xfc(%rbp)
cmpl $-0x1, -0xfc(%rbp)
jne 0x423be
jmp 0x42421
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4828
jmp 0x42484
movq -0x20(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x18(%rbp), %rsi
movl -0xfc(%rbp), %edx
leaq -0x140(%rbp), %rdi
callq 0x24510
jmp 0x423e1
movq -0x1c8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xb800
jmp 0x423f6
leaq -0x140(%rbp), %rdi
callq 0x4828
jmp 0x42379
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x4828
jmp 0x42484
testb $0x1, -0x21(%rbp)
je 0x4245e
movq -0x20(%rbp), %rdi
callq 0xb2f0
movq %rax, -0x148(%rbp)
movq -0x20(%rbp), %rdi
callq 0xb320
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0x30db0
jmp 0x4245c
jmp 0x4245e
leaq -0x40(%rbp), %rdi
callq 0x482e0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x14140
leaq -0x40(%rbp), %rdi
callq 0x48320
jmp 0x42498
leaq -0x78(%rbp), %rdi
callq 0x14140
leaq -0x40(%rbp), %rdi
callq 0x48320
jmp 0x424a6
movsd -0x8(%rbp), %xmm0
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x46e0
nop