// File: aalto-speech[P]ftk/src/EM.cc
// Function: forward_backward(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::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>>>>>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
flt_type forward_backward(const transitions_t &transitions,
                          FactorGraph &text,
                          transitions_t &stats,
                          const string &block)
{
    if (text.nodes.size() == 0) return MIN_FLOAT;
    stats.clear();

    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;

    string source_node_str, target_node_str;

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

        if (fw[i] == MIN_FLOAT) continue;

        FactorGraph::Node &node = text.nodes[i];
        source_node_str = text.get_factor(node);
        if (source_node_str == block) continue;

        for (auto arc = node.outgoing.begin(); arc != node.outgoing.end(); ++arc) {

            int tgt_node = (**arc).target_node;
            target_node_str = text.get_factor(tgt_node);
            if (target_node_str == block) continue;
            try {
                (**arc).cost = transitions.at(source_node_str).at(target_node_str);
            }
            catch (std::out_of_range &oor) {
                (**arc).cost = SMALL_LP;
            }

            flt_type cost = fw[i] + (**arc).cost;
            if (fw[tgt_node] == MIN_FLOAT) fw[tgt_node] = cost;
            else fw[tgt_node] = add_log_domain_probs(fw[tgt_node], cost);
        }
    }

    backward(text, fw, bw, stats);

    return fw.back();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
cmpq $0x0, %rax
jne 0x43324
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x43898
movq -0x20(%rbp), %rdi
callq 0x48490
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x150(%rbp)
callq 0x47bf0
movaps 0x23443(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x48(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x48220
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rcx
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x48240
jmp 0x43383
leaq -0x49(%rbp), %rdi
callq 0x482c0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0x160(%rbp)
callq 0x47bf0
movaps 0x233e4(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x80(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x48220
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rcx
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x48240
jmp 0x433e5
leaq -0x81(%rbp), %rdi
callq 0x482c0
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rsi
subq $0x1, %rsi
leaq -0x78(%rbp), %rdi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq -0xa8(%rbp), %rdi
callq 0x4560
leaq -0xc8(%rbp), %rdi
callq 0x4560
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movq %rax, -0x168(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rcx
movq -0x168(%rbp), %rax
cmpq %rcx, %rax
jae 0x43817
movl -0xcc(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x170(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x170(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x43500
jp 0x43500
jmp 0x43803
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x482c0
jmp 0x438a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x482c0
jmp 0x4388d
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movl -0xcc(%rbp), %eax
movl %eax, %esi
callq 0x23ae0
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x24450
jmp 0x43535
leaq -0xa8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x4480
leaq -0xf8(%rbp), %rdi
callq 0x4828
movq -0x28(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x2cc30
testb $0x1, %al
jne 0x4356a
jmp 0x43580
jmp 0x43803
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4386c
movq -0xd8(%rbp), %rdi
addq $0x20, %rdi
callq 0x24310
movq %rax, -0x100(%rbp)
movq -0xd8(%rbp), %rdi
addq $0x20, %rdi
callq 0x24340
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x245f0
testb $0x1, %al
jne 0x435ca
jmp 0x43801
leaq -0x100(%rbp), %rdi
callq 0x24630
movq (%rax), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x10c(%rbp)
movq -0x18(%rbp), %rsi
movl -0x10c(%rbp), %edx
leaq -0x130(%rbp), %rdi
callq 0x24510
jmp 0x435fb
leaq -0xc8(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x4480
leaq -0x130(%rbp), %rdi
callq 0x4828
movq -0x28(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x2cc30
testb $0x1, %al
jne 0x43630
jmp 0x43635
jmp 0x437f0
movq -0x10(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x487e0
movq %rax, -0x178(%rbp)
jmp 0x4364e
movq -0x178(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x48880
movq %rax, -0x180(%rbp)
jmp 0x4366a
movq -0x180(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x188(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x24630
movsd -0x188(%rbp), %xmm0
movq (%rax), %rax
movsd %xmm0, 0x8(%rax)
jmp 0x436ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x4386c
movq -0x58(%rbp), %rdi
callq 0x4180
movq %rax, -0x138(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x24630
movq (%rax), %rax
movabsq $-0x3fa7000000000000, %rcx # imm = 0xC059000000000000
movq %rcx, 0x8(%rax)
callq 0x4660
jmp 0x436eb
jmp 0x436ed
movl -0xcc(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x198(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x24630
movsd -0x198(%rbp), %xmm0
movq (%rax), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, -0x140(%rbp)
movslq -0x10c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x190(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x190(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x437a7
jp 0x437a7
movsd -0x140(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movslq -0x10c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd -0x1a0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x437ee
movslq -0x10c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd -0x140(%rbp), %xmm1
callq 0x412e0
movsd %xmm0, -0x1a8(%rbp)
jmp 0x437d2
movslq -0x10c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47eb0
movsd -0x1a8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x437f0
leaq -0x100(%rbp), %rdi
callq 0x24650
jmp 0x43597
jmp 0x43803
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x4344b
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rcx
leaq -0x40(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x42980
jmp 0x4382e
leaq -0x40(%rbp), %rdi
callq 0x482e0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x4828
leaq -0xa8(%rbp), %rdi
callq 0x4828
leaq -0x78(%rbp), %rdi
callq 0x48320
leaq -0x40(%rbp), %rdi
callq 0x48320
jmp 0x43898
leaq -0xc8(%rbp), %rdi
callq 0x4828
leaq -0xa8(%rbp), %rdi
callq 0x4828
leaq -0x78(%rbp), %rdi
callq 0x48320
leaq -0x40(%rbp), %rdi
callq 0x48320
jmp 0x438a6
movsd -0x8(%rbp), %xmm0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x46e0
nop