// File: aalto-speech[P]ftk/src/EM.cc
// Function: backward(FactorGraph const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>>&, 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>>>>>>&)
void backward(const FactorGraph &text,
              const vector<flt_type> &fw,
              vector<flt_type> &bw,
              transitions_t &stats)
{
    for (int i=text.nodes.size()-1; i>0; i--) {

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

        const FactorGraph::Node &node = text.nodes[i];
        string target_node_str = text.get_factor(node);

        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[text.get_factor(src_node)][target_node_str] += exp(curr_cost);
            if (bw[src_node] == MIN_FLOAT) bw[src_node] = curr_cost;
            else bw[src_node] = add_log_domain_probs(bw[src_node], curr_cost);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
subq $0x1, %rax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x42ce9
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0xa8(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x42a06
jp 0x42a06
jmp 0x42cd0
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movslq -0x24(%rbp), %rsi
callq 0x23ff0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x24450
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
callq 0x24010
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
callq 0x24080
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x24040
testb $0x1, %al
jne 0x42a64
jmp 0x42cc7
leaq -0x58(%rbp), %rdi
callq 0x240b0
movq (%rax), %rax
movzwl (%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x48050
movsd (%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0xb0(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x42ac3
jp 0x42ac3
jmp 0x42cb9
leaq -0x58(%rbp), %rdi
callq 0x240b0
movq (%rax), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x48050
movsd -0xd8(%rbp), %xmm0
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x48050
movsd -0xd0(%rbp), %xmm0
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x47eb0
movsd -0xc8(%rbp), %xmm0
movsd (%rax), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x4710
movsd %xmm0, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rsi
movl -0x64(%rbp), %edx
leaq -0x90(%rbp), %rdi
callq 0x24510
jmp 0x42b7a
movq -0xb8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x48920
movq %rax, -0xe0(%rbp)
jmp 0x42b96
movq -0xe0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x47db0
movq %rax, -0xe8(%rbp)
jmp 0x42baf
movq -0xe8(%rbp), %rax
movsd -0xc0(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
leaq -0x90(%rbp), %rdi
callq 0x4828
movq -0x18(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0xf0(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0xf0(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x42c79
jp 0x42c79
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xf8(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x47eb0
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x42cb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x42cde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4828
jmp 0x42cde
movq -0x18(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x47eb0
movsd (%rax), %xmm0
movsd -0x70(%rbp), %xmm1
callq 0x412e0
movsd %xmm0, -0x100(%rbp)
jmp 0x42c9e
movq -0x18(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x47eb0
movsd -0x100(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x42cb9
leaq -0x58(%rbp), %rdi
callq 0x240d0
jmp 0x42a3d
leaq -0x50(%rbp), %rdi
callq 0x4828
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x429af
leaq -0x50(%rbp), %rdi
callq 0x4828
jmp 0x42cf2
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x46e0
nop