// 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>>, 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>>>>>>&)
flt_type forward_backward(const map<string, flt_type> &vocab,
                          FactorGraph &text,
                          transitions_t &stats)
{
    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;

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

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

        FactorGraph::Node &node = text.nodes[i];
        for (auto arc = node.outgoing.begin(); arc != node.outgoing.end(); ++arc) {
            int tgt_node = (**arc).target_node;
            (**arc).cost = vocab.at(text.get_factor(tgt_node));
            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 $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
cmpq $0x0, %rax
jne 0x43900
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x43d48
movq -0x20(%rbp), %rdi
callq 0x48490
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0xd8(%rbp)
callq 0x47bf0
movaps 0x22e67(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x40(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x48220
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rcx
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x48240
jmp 0x4395f
leaq -0x41(%rbp), %rdi
callq 0x482c0
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, -0xe8(%rbp)
callq 0x47bf0
movaps 0x22e08(%rip), %xmm1    # 0x66790
pxor %xmm1, %xmm0
movlpd %xmm0, -0x78(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x48220
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x48240
jmp 0x439be
leaq -0x79(%rbp), %rdi
callq 0x482c0
leaq -0x38(%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 -0x70(%rbp), %rdi
callq 0x47eb0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x23a90
movq %rax, %rcx
movq -0xf0(%rbp), %rax
cmpq %rcx, %rax
jae 0x43cf7
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0xf8(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x43ab2
jp 0x43ab2
jmp 0x43ce9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x482c0
jmp 0x43d56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x482c0
jmp 0x43d3d
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movl -0x80(%rbp), %eax
movl %eax, %esi
callq 0x23ae0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
callq 0x24310
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
callq 0x24340
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x245f0
testb $0x1, %al
jne 0x43b15
jmp 0x43ce7
leaq -0x90(%rbp), %rdi
callq 0x24630
movq (%rax), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rsi
movl -0x9c(%rbp), %edx
leaq -0xc0(%rbp), %rdi
callq 0x24510
jmp 0x43b51
movq -0x100(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x48880
movq %rax, -0x108(%rbp)
jmp 0x43b6d
movq -0x108(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x120(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x24630
movsd -0x120(%rbp), %xmm0
movq (%rax), %rax
movsd %xmm0, 0x8(%rax)
leaq -0xc0(%rbp), %rdi
callq 0x4828
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x118(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x24630
movsd -0x118(%rbp), %xmm0
movq (%rax), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, -0xc8(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x110(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x43c8d
jp 0x43c8d
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd -0x128(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x43cd4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x43d34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4828
jmp 0x43d34
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd (%rax), %xmm0
movsd -0xc8(%rbp), %xmm1
callq 0x412e0
movsd %xmm0, -0x130(%rbp)
jmp 0x43cb8
movslq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x47eb0
movsd -0x130(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x43cd6
leaq -0x90(%rbp), %rdi
callq 0x24650
jmp 0x43ae2
jmp 0x43ce9
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x43a06
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rcx
leaq -0x38(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x42980
jmp 0x43d0e
leaq -0x38(%rbp), %rdi
callq 0x482e0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x48320
leaq -0x38(%rbp), %rdi
callq 0x48320
jmp 0x43d48
leaq -0x70(%rbp), %rdi
callq 0x48320
leaq -0x38(%rbp), %rdi
callq 0x48320
jmp 0x43d56
movsd -0x8(%rbp), %xmm0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x46e0
nop