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

    transitions_t stats;
    vector<flt_type> fw(text.nodes.size(), MIN_FLOAT);
    vector<flt_type> bw(text.nodes.size(), MIN_FLOAT);
    fw[0] = 0.0; bw[text.nodes.size()-1] = 0.0;

    forward(transitions, text, fw);
    backward(text, fw, bw, stats);

    // 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 tgt_node = (**arc).target_node;
            flt_type curr_cost = costs[tgt_node];
            flt_type new_cost = costs[i] + bw[tgt_node];
            if (new_cost > curr_cost) {
                costs[tgt_node] = new_cost;
                source_nodes[tgt_node] = i;
            }
        }
    }

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

    return costs.back();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
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 0x43db7
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x44433
movq -0x20(%rbp), %rdi
callq 0xb7e0
leaq -0x58(%rbp), %rdi
callq 0x48ab0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x198(%rbp)
callq 0x47bf0
movaps 0x229a7(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x78(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x48220
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x48240
jmp 0x43e1f
leaq -0x79(%rbp), %rdi
callq 0x482c0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x1a8(%rbp)
callq 0x47bf0
movaps 0x22948(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0xb0(%rbp)
leaq -0xb1(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x48220
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rcx
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x48240
jmp 0x43e8a
leaq -0xb1(%rbp), %rdi
callq 0x482c0
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x47eb0
movq $0x0, (%rax)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rsi
decq %rsi
leaq -0xa8(%rbp), %rdi
callq 0x47eb0
movq -0x1b0(%rbp), %rdx
movq $0x0, (%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x42640
jmp 0x43eed
movq -0x18(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
leaq -0x58(%rbp), %rcx
callq 0x42980
jmp 0x43f07
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x1c0(%rbp)
callq 0x47bf0
movaps 0x22869(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0xd8(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x48220
movq -0x1c0(%rbp), %rsi
movq -0x1b8(%rbp), %rcx
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x48240
jmp 0x43f69
leaq -0xd9(%rbp), %rdi
callq 0x482c0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x1d0(%rbp)
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0xc290
movq -0x1d0(%rbp), %rsi
movq -0x1c8(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x23f80
jmp 0x43fb8
leaq -0xf9(%rbp), %rdi
callq 0xc910
leaq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq -0xf8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13820
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x100(%rbp)
movl -0x100(%rbp), %eax
movq %rax, -0x1d8(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
cmpq %rcx, %rax
jae 0x44247
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0x23ae0
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rdi
addq $0x20, %rdi
callq 0x24310
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
addq $0x20, %rdi
callq 0x24340
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x245f0
testb $0x1, %al
jne 0x4408e
jmp 0x44231
leaq -0x110(%rbp), %rdi
callq 0x24630
movq (%rax), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x11c(%rbp)
movslq -0x11c(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x128(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %esi
leaq -0xd0(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x1e0(%rbp)
movslq -0x11c(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x47eb0
movsd -0x1e0(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, -0x130(%rbp)
movsd -0x130(%rbp), %xmm0
ucomisd -0x128(%rbp), %xmm0
jbe 0x4421e
movsd -0x130(%rbp), %xmm0
movsd %xmm0, -0x1f0(%rbp)
movslq -0x11c(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x47eb0
movsd -0x1f0(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x100(%rbp), %eax
movl %eax, -0x1e4(%rbp)
movslq -0x11c(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x13820
movl -0x1e4(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x4421e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x482c0
jmp 0x44428
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xb1(%rbp), %rdi
callq 0x482c0
jmp 0x4441f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x44413
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x482c0
jmp 0x44413
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xf9(%rbp), %rdi
callq 0xc910
jmp 0x44407
jmp 0x44220
leaq -0x110(%rbp), %rdi
callq 0x24650
jmp 0x4405b
jmp 0x44233
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x43ffb
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
decl %eax
movl %eax, -0x134(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x18(%rbp), %rsi
movl -0x134(%rbp), %edx
leaq -0x158(%rbp), %rdi
callq 0x24510
jmp 0x4427f
movq -0x1f8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0xb800
jmp 0x44294
leaq -0x158(%rbp), %rdi
callq 0x4828
jmp 0x442a2
movslq -0x134(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x13820
movl (%rax), %eax
movl %eax, -0x134(%rbp)
cmpl $-0x1, -0x134(%rbp)
jne 0x44305
jmp 0x44371
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x443fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x4828
jmp 0x443fb
movq -0x20(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x18(%rbp), %rsi
movl -0x134(%rbp), %edx
leaq -0x178(%rbp), %rdi
callq 0x24510
jmp 0x44328
movq -0x200(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0xb800
jmp 0x4433d
leaq -0x178(%rbp), %rdi
callq 0x4828
jmp 0x442a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x4828
jmp 0x443fb
testb $0x1, -0x21(%rbp)
je 0x443ae
movq -0x20(%rbp), %rdi
callq 0xb2f0
movq %rax, -0x180(%rbp)
movq -0x20(%rbp), %rdi
callq 0xb320
movq %rax, -0x188(%rbp)
movq -0x180(%rbp), %rdi
movq -0x188(%rbp), %rsi
callq 0x30db0
jmp 0x443ac
jmp 0x443ae
leaq -0xd0(%rbp), %rdi
callq 0x482e0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x14140
leaq -0xd0(%rbp), %rdi
callq 0x48320
leaq -0xa8(%rbp), %rdi
callq 0x48320
leaq -0x70(%rbp), %rdi
callq 0x48320
leaq -0x58(%rbp), %rdi
callq 0x48ad0
jmp 0x44433
leaq -0xf8(%rbp), %rdi
callq 0x14140
leaq -0xd0(%rbp), %rdi
callq 0x48320
leaq -0xa8(%rbp), %rdi
callq 0x48320
leaq -0x70(%rbp), %rdi
callq 0x48320
leaq -0x58(%rbp), %rdi
callq 0x48ad0
jmp 0x44441
movsd -0x8(%rbp), %xmm0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x46e0
nopl (%rax)