// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/EM.cc
// Function: forward(StringSet const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::vector<Token, std::allocator<Token>>, std::allocator<std::vector<Token, std::allocator<Token>>>>&, std::vector<double, std::allocator<double>>&, bool)
void forward(const StringSet &vocab,
             const string &text,
             vector<vector<Token> > &search,
             vector<flt_type> &fw,
             bool utf8)
{
    int len = text.length();

    vector<unsigned int> char_positions;
    get_character_positions(text, char_positions, utf8);

    for (unsigned int cpi=0; cpi<char_positions.size(); cpi++) {

        int i = char_positions[cpi];
        if (i>0 && search[i-1].size() == 0) continue;

        if (i>0) {
            fw[i-1] = search[i-1][0].cost;
            for (unsigned int t=1; t<search[i-1].size(); t++)
                fw[i-1] = add_log_domain_probs(fw[i-1], search[i-1][t].cost);
        }

        // Iterate all factors starting from this position
        const StringSet::Node *node = &vocab.root_node;
        for (unsigned int j=i; j<text.length(); j++) {

            StringSet::Arc *arc = vocab.find_arc(text[j], node);

            if (arc == nullptr) break;
            node = arc->target_node;

            // Factor associated with this node
            if (arc->factor.length() > 0) {
                flt_type cost = arc->cost;
                if (i>0) cost += fw[i-1];

                Token tok(i-1, cost);
                search[j].push_back(tok);
            }
        }
    }

    if (search[len-1].size() == 0) return;

    fw[len-1] = search[len-1][0].cost;
    for (unsigned int j=1; j<search[len-1].size(); j++)
        fw[len-1] = add_log_domain_probs(fw[len-1], search[len-1][j].cost);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %r14
movq 0x8(%rsi), %rax
movq %rax, -0x48(%rbp)
xorpd %xmm0, %xmm0
leaq -0x70(%rbp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %r15, %rdi
movl %r8d, %edx
callq 0x154a1
xorl %edx, %edx
movq %r14, -0x50(%rbp)
movl %edx, %edx
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rdx, %rcx
jbe 0x159aa
movq %rdx, -0x58(%rbp)
movslq (%rax,%rdx,4), %rbx
leal -0x1(%rbx), %eax
movl %eax, -0x3c(%rbp)
testq %rbx, %rbx
jle 0x1592f
movl -0x3c(%rbp), %r12d
movq -0x38(%rbp), %rax
movq (%rax), %rcx
imulq $0x18, %r12, %r13
movq (%rcx,%r13), %rax
movq 0x8(%rcx,%r13), %rcx
cmpq %rax, %rcx
je 0x1599f
movsd 0x8(%rax), %xmm0
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movsd %xmm0, (%rdx,%r12,8)
pushq $0x1
popq %rdx
movl %edx, %r14d
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r14, %rcx
jbe 0x1592f
movq %r14, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm1
callq 0x15a53
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%r12,8)
leal 0x1(%r14), %edx
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq (%rcx,%r13), %rax
movq 0x8(%rcx,%r13), %rcx
jmp 0x158eb
movq -0x50(%rbp), %r14
movq %r14, %r12
movl %ebx, %r13d
movl %r13d, %r13d
cmpq %r13, 0x8(%r15)
jbe 0x1599f
movq (%r15), %rax
movsbl (%rax,%r13), %esi
movq %r14, %rdi
movq %r12, %rdx
callq 0xbb42
testq %rax, %rax
je 0x1599f
movq 0x28(%rax), %r12
cmpq $0x0, 0x10(%rax)
je 0x1599a
movsd 0x30(%rax), %xmm0
testl %ebx, %ebx
jle 0x1597b
movq -0x30(%rbp), %rax
movq (%rax), %rax
addsd -0x8(%rax,%rbx,8), %xmm0
movl -0x3c(%rbp), %eax
movl %eax, -0x80(%rbp)
movsd %xmm0, -0x78(%rbp)
imulq $0x18, %r13, %rdi
movq -0x38(%rbp), %rax
addq (%rax), %rdi
leaq -0x80(%rbp), %rsi
callq 0x18dfa
incl %r13d
jmp 0x15939
movq -0x58(%rbp), %rdx
incl %edx
jmp 0x15888
movq -0x48(%rbp), %rax
decl %eax
movslq %eax, %rbx
movq -0x38(%rbp), %rax
movq (%rax), %rcx
imulq $0x18, %rbx, %r15
movq (%rcx,%r15), %rax
movq 0x8(%rcx,%r15), %rcx
cmpq %rax, %rcx
je 0x15a23
movsd 0x8(%rax), %xmm0
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movsd %xmm0, (%rdx,%rbx,8)
pushq $0x1
popq %rdx
movl %edx, %r14d
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %r14, %rcx
jbe 0x15a23
movq %r14, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm1
callq 0x15a53
movq -0x30(%rbp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%rbx,8)
leal 0x1(%r14), %edx
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq (%rcx,%r15), %rax
movq 0x8(%rcx,%r15), %rcx
jmp 0x159e0
leaq -0x70(%rbp), %rdi
callq 0xed7a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15a3f
jmp 0x15a3f
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0xed7a
movq %rbx, %rdi
callq 0x45f0