// File: aalto-speech[P]ftk/src/EM.cc
// Function: backward(MultiStringFactorGraph const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::map<unsigned int, double, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, double>>> const&, 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>>>>>>&, double)
flt_type
backward(const MultiStringFactorGraph &msfg,
         const string &text,
         const map<msfg_node_idx_t, flt_type> &fw,
         transitions_t &stats,
         flt_type text_weight)
{

    int text_end_node = msfg.string_end_nodes.at(text);
    map<int, flt_type> bw; bw[text_end_node] = 0.0;
    set<int> nodes_to_process; nodes_to_process.insert(text_end_node);

    while(nodes_to_process.size() > 0) {

        int i = *(nodes_to_process.rbegin());

        const MultiStringFactorGraph::Node &node = msfg.nodes[i];

        for (auto arc = node.incoming.begin(); arc != node.incoming.end(); ++arc) {
            int src_node = (**arc).source_node;
            flt_type curr_cost = *(**arc).cost + fw.at(src_node) - fw.at(i) + bw[i];
            stats[msfg.nodes.at(src_node).factor][node.factor] += text_weight * exp(curr_cost);
            if (bw.find(src_node) == bw.end()) {
                bw[src_node] = curr_cost;
                nodes_to_process.insert(src_node);
            }
            else bw[src_node] = add_log_domain_probs(bw[src_node], curr_cost);
        }

        nodes_to_process.erase(i);
    }

    return fw.at(msfg.string_end_nodes.at(text));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x10(%rbp), %rsi
callq 0x30970
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x49450
movq -0x120(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x49470
movq %rax, -0x118(%rbp)
jmp 0x4655f
movq -0x118(%rbp), %rax
movq $0x0, (%rax)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x492d0
movq -0x138(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x492f0
movb %dl, -0x129(%rbp)
movq %rax, -0x128(%rbp)
jmp 0x4659f
movb -0x129(%rbp), %al
movq -0x128(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x49340
cmpq $0x0, %rax
jbe 0x469aa
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
leaq -0xa0(%rbp), %rsi
callq 0x49360
movq -0x148(%rbp), %rdi
callq 0x49390
movq %rax, -0x140(%rbp)
jmp 0x465fe
movq -0x140(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movslq -0xb4(%rbp), %rsi
callq 0x30c00
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x30260
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
addq $0x20, %rdi
callq 0x302c0
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x30290
testb $0x1, %al
jne 0x46672
jmp 0x46990
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x302f0
movq -0x160(%rbp), %rdi
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
callq 0x302f0
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x158(%rbp)
movq -0x18(%rbp), %rdi
movl -0xdc(%rbp), %eax
movl %eax, -0xec(%rbp)
leaq -0xec(%rbp), %rsi
callq 0x49680
movq %rax, -0x150(%rbp)
jmp 0x466d4
movsd -0x158(%rbp), %xmm0
movq -0x150(%rbp), %rax
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x170(%rbp)
movq -0x18(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, -0xf0(%rbp)
leaq -0xf0(%rbp), %rsi
callq 0x49680
movq %rax, -0x168(%rbp)
jmp 0x46718
movsd -0x170(%rbp), %xmm0
movq -0x168(%rbp), %rax
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x180(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x49470
movq %rax, -0x178(%rbp)
jmp 0x46750
movsd -0x180(%rbp), %xmm0
movq -0x178(%rbp), %rax
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0xe8(%rbp), %xmm0
callq 0x4710
movsd %xmm0, -0x198(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movslq -0xdc(%rbp), %rsi
callq 0x49550
movq %rax, -0x188(%rbp)
jmp 0x467b9
movq -0x188(%rbp), %rsi
movq -0x190(%rbp), %rdi
callq 0x48700
movq %rax, -0x1a8(%rbp)
jmp 0x467d5
movq -0x1a8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x47db0
movq %rax, -0x1b0(%rbp)
jmp 0x467f1
movq -0x1b0(%rbp), %rax
movsd -0x198(%rbp), %xmm2
movsd -0x1a0(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x495c0
movq %rax, -0x1b8(%rbp)
jmp 0x46831
movq -0x1b8(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x495f0
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x49590
testb $0x1, %al
jne 0x4686b
jmp 0x46916
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0x1c8(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1c0(%rbp)
jmp 0x46894
movq -0x1c0(%rbp), %rax
movsd -0x1c8(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq -0xa0(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x492f0
movb %dl, -0x1d1(%rbp)
movq %rax, -0x1d0(%rbp)
jmp 0x468c9
movb -0x1d1(%rbp), %al
movq -0x1d0(%rbp), %rcx
movq %rcx, -0x110(%rbp)
movb %al, -0x108(%rbp)
jmp 0x4697d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x46a24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x493f0
jmp 0x46a24
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1e0(%rbp)
jmp 0x4692f
movq -0x1e0(%rbp), %rax
movsd (%rax), %xmm0
movsd -0xe8(%rbp), %xmm1
callq 0x412e0
movsd %xmm0, -0x1e8(%rbp)
jmp 0x46951
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1f0(%rbp)
jmp 0x4696a
movq -0x1f0(%rbp), %rax
movsd -0x1e8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x4697f
leaq -0xd0(%rbp), %rdi
callq 0x30320
jmp 0x4663f
leaq -0xa0(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x493c0
jmp 0x469a5
jmp 0x465b9
movq -0x18(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x10(%rbp), %rsi
callq 0x30970
movq %rax, -0x1f8(%rbp)
jmp 0x469cf
movq -0x1f8(%rbp), %rsi
movq -0x200(%rbp), %rdi
callq 0x49680
movq %rax, -0x208(%rbp)
jmp 0x469eb
movq -0x208(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x210(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x493f0
leaq -0x60(%rbp), %rdi
callq 0x49660
movsd -0x210(%rbp), %xmm0
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x49660
movq -0x68(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)