// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/EM.cc
// Function: viterbi(StringSet const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, bool, bool)
flt_type viterbi(const StringSet &vocab,
                 const string &text,
                 vector<string> &best_path,
                 bool reverse,
                 bool utf8)
{
    best_path.clear();
    if (text.length() == 0) return MIN_FLOAT;
    vector<Token> search(text.length());

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

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

        // Iterate all factors starting from this position
        unsigned int start_pos = char_positions[i];
        const StringSet::Node *node = &vocab.root_node;
        for (unsigned int j=start_pos; 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 (start_pos>0) cost += search[start_pos-1].cost;

                if (cost > search[j].cost) {
                    search[j].cost = cost;
                    search[j].source = start_pos-1;
                }
            }
        }
    }

    // Look up the best path
    int target = search.size()-1;
    if (search[target].cost == MIN_FLOAT) return MIN_FLOAT;

    int source = search[target].source;
    while (true) {
        best_path.push_back(text.substr(source+1, target-source));
        if (source == -1) break;
        target = source;
        source = search[target].source;
    }

    if (reverse) std::reverse(best_path.begin(), best_path.end());
    return search.back().cost;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, %r13d
movl %ecx, %ebx
movq %rsi, %r15
movq %rdi, %r12
movq %rdx, -0x40(%rbp)
movq %rdx, %rdi
callq 0x7532
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x15712
movl %ebx, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x18d3e
xorpd %xmm0, %xmm0
leaq -0x80(%rbp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movzbl %r13b, %edx
movq %r15, %rdi
callq 0x154a1
xorl %edx, %edx
movl %edx, %edx
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rdx, %rcx
jbe 0x15698
movq %rdx, -0x60(%rbp)
movl (%rax,%rdx,4), %r13d
leal -0x1(%r13), %ebx
movq %rbx, -0x30(%rbp)
shlq $0x4, %rbx
movq %r12, %rdx
movl %r13d, %r14d
movl %r14d, %r14d
cmpq %r14, 0x8(%r15)
jbe 0x1568d
movq (%r15), %rax
movsbl (%rax,%r14), %esi
movq %r12, %rdi
callq 0xbb42
testq %rax, %rax
je 0x1568d
movq 0x28(%rax), %rdx
cmpq $0x0, 0x10(%rax)
je 0x15688
movsd 0x30(%rax), %xmm0
movq -0x58(%rbp), %rax
testl %r13d, %r13d
je 0x1566b
addsd 0x8(%rax,%rbx), %xmm0
movq %r14, %rcx
shlq $0x4, %rcx
ucomisd 0x8(%rax,%rcx), %xmm0
jbe 0x15688
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movq -0x30(%rbp), %rcx
movl %ecx, (%rax)
incl %r14d
jmp 0x1562e
movq -0x60(%rbp), %rdx
incl %edx
jmp 0x155fa
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %r14
subq %rbx, %r14
shrq $0x4, %r14
decl %r14d
movslq %r14d, %rax
shlq $0x4, %rax
movsd 0x8(%rbx,%rax), %xmm0
movsd 0xe0d1(%rip), %xmm1     # 0x23790
ucomisd %xmm1, %xmm0
jne 0x156c7
jnp 0x1573b
addq %rax, %rbx
leaq -0xa0(%rbp), %r12
movl %r14d, %eax
movl (%rbx), %r14d
movslq %r14d, %rbx
leaq 0x1(%rbx), %rdx
subl %ebx, %eax
movslq %eax, %rcx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4360
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x7570
movq %r12, %rdi
callq 0x46b8
cmpl $-0x1, %r14d
je 0x1571c
shlq $0x4, %rbx
addq -0x58(%rbp), %rbx
jmp 0x156d1
movsd 0xe076(%rip), %xmm0     # 0x23790
jmp 0x15757
cmpb $0x0, -0x34(%rbp)
je 0x15732
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x1423d
movq -0x50(%rbp), %rax
movsd -0x8(%rax), %xmm1
movsd %xmm1, -0x30(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xed7a
leaq -0x58(%rbp), %rdi
callq 0x19c1c
movsd -0x30(%rbp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1577d
jmp 0x1577d
jmp 0x1577d
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x46b8
jmp 0x15780
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0xed7a
leaq -0x58(%rbp), %rdi
callq 0x19c1c
movq %rbx, %rdi
callq 0x45f0