// 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::vector<double, std::allocator<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 vector<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;
            if (fw[src_node] == MIN_FLOAT) continue;
            flt_type curr_cost = *(**arc).cost + fw[src_node] - fw[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 $0x200, %rsp            # imm = 0x200
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, -0x118(%rbp)
callq 0x49450
movq -0x118(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x49470
movq %rax, -0x110(%rbp)
jmp 0x4600f
movq -0x110(%rbp), %rax
movq $0x0, (%rax)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x492d0
movq -0x130(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x492f0
movb %dl, -0x121(%rbp)
movq %rax, -0x120(%rbp)
jmp 0x4604f
movb -0x121(%rbp), %al
movq -0x120(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x49340
cmpq $0x0, %rax
jbe 0x46465
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x140(%rbp)
leaq -0xa0(%rbp), %rsi
callq 0x49360
movq -0x140(%rbp), %rdi
callq 0x49390
movq %rax, -0x138(%rbp)
jmp 0x460ae
movq -0x138(%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 0x46122
jmp 0x4644b
leaq -0xd0(%rbp), %rdi
callq 0x302f0
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
callq 0x48050
movsd (%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x148(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x461b7
jp 0x461b7
jmp 0x4643a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x464e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x493f0
jmp 0x464e3
leaq -0xd0(%rbp), %rdi
callq 0x302f0
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x168(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
callq 0x48050
movsd -0x168(%rbp), %xmm0
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x160(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xb4(%rbp), %rsi
callq 0x48050
movsd -0x160(%rbp), %xmm0
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x158(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x49470
movq %rax, -0x150(%rbp)
jmp 0x4623f
movsd -0x158(%rbp), %xmm0
movq -0x150(%rbp), %rax
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x188(%rbp)
movsd -0xe8(%rbp), %xmm0
callq 0x4710
movsd %xmm0, -0x180(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movslq -0xdc(%rbp), %rsi
callq 0x49550
movq %rax, -0x170(%rbp)
jmp 0x462a8
movq -0x170(%rbp), %rsi
movq -0x178(%rbp), %rdi
callq 0x48700
movq %rax, -0x190(%rbp)
jmp 0x462c4
movq -0x190(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x47db0
movq %rax, -0x198(%rbp)
jmp 0x462e0
movq -0x198(%rbp), %rax
movsd -0x180(%rbp), %xmm2
movsd -0x188(%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, -0x1a0(%rbp)
jmp 0x46320
movq -0x1a0(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x495f0
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x49590
testb $0x1, %al
jne 0x46357
jmp 0x463d1
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0x1b0(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1a8(%rbp)
jmp 0x46380
movq -0x1a8(%rbp), %rax
movsd -0x1b0(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq -0xa0(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x492f0
movb %dl, -0x1b9(%rbp)
movq %rax, -0x1b8(%rbp)
jmp 0x463b5
movb -0x1b9(%rbp), %al
movq -0x1b8(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movb %al, -0x100(%rbp)
jmp 0x46438
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1c8(%rbp)
jmp 0x463ea
movq -0x1c8(%rbp), %rax
movsd (%rax), %xmm0
movsd -0xe8(%rbp), %xmm1
callq 0x412e0
movsd %xmm0, -0x1d0(%rbp)
jmp 0x4640c
leaq -0x60(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x49470
movq %rax, -0x1d8(%rbp)
jmp 0x46425
movq -0x1d8(%rbp), %rax
movsd -0x1d0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x4643a
leaq -0xd0(%rbp), %rdi
callq 0x30320
jmp 0x460ef
leaq -0xa0(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x493c0
jmp 0x46460
jmp 0x46069
movq -0x18(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x10(%rbp), %rsi
callq 0x30970
movq %rax, -0x1e0(%rbp)
jmp 0x4648a
movq -0x1e8(%rbp), %rdi
movq -0x1e0(%rbp), %rax
movl (%rax), %eax
movl %eax, %esi
callq 0x49620
movq %rax, -0x1f0(%rbp)
jmp 0x464aa
movq -0x1f0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x1f8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x493f0
leaq -0x60(%rbp), %rdi
callq 0x49660
movsd -0x1f8(%rbp), %xmm0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x49660
movq -0x68(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)