// File: /workspace/github_cpp_makefile_star2_O3/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, -0x30(%rbp)
movq %rdx, -0x40(%rbp)
movq %rdx, %rdi
callq 0x82a2
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x16d09
leaq -0x58(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x1aefe
xorpd %xmm0, %xmm0
leaq -0x70(%rbp), %rsi
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movzbl %r13b, %edx
movq %r15, %rdi
callq 0x169dd
movl %ebx, -0x34(%rbp)
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rax
cmpq %rdi, %rax
je 0x16c79
xorl %r13d, %r13d
movl (%rdi,%r13,4), %r12d
cmpq %r12, 0x8(%r15)
jbe 0x16c63
leal -0x1(%r12), %eax
leal 0x1(%r12), %ebx
movq %rax, -0x78(%rbp)
shlq $0x4, %rax
movq %rax, -0x80(%rbp)
movq %r12, %r14
movq -0x30(%rbp), %rdx
movq (%r15), %rax
movsbl (%rax,%r14), %esi
movq -0x30(%rbp), %rdi
callq 0xcd82
testq %rax, %rax
je 0x16c5b
movq 0x28(%rax), %rdx
cmpq $0x0, 0x10(%rax)
je 0x16c50
movsd 0x30(%rax), %xmm0
movq -0x58(%rbp), %rax
testq %r12, %r12
je 0x16c35
movq -0x80(%rbp), %rcx
addsd 0x8(%rax,%rcx), %xmm0
shlq $0x4, %r14
ucomisd 0x8(%rax,%r14), %xmm0
jbe 0x16c50
addq %r14, %rax
movsd %xmm0, 0x8(%rax)
movq -0x78(%rbp), %rcx
movl %ecx, (%rax)
movl %ebx, %r14d
incl %ebx
cmpq %r14, 0x8(%r15)
ja 0x16bfc
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rax
incl %r13d
movq %rax, %rcx
subq %rdi, %rcx
sarq $0x2, %rcx
cmpq %r13, %rcx
ja 0x16bd1
movq -0x58(%rbp), %r14
movq -0x50(%rbp), %r13
subq %r14, %r13
shrq $0x4, %r13
decl %r13d
movslq %r13d, %rax
shlq $0x4, %rax
movsd 0x8(%r14,%rax), %xmm1
movsd 0xfb5f(%rip), %xmm0     # 0x26800
ucomisd %xmm0, %xmm1
jne 0x16cad
jnp 0x16d60
addq %rax, %r14
leaq -0x90(%rbp), %rbx
leaq -0xa0(%rbp), %r12
movl %r13d, %eax
movl (%r14), %r13d
movslq %r13d, %r14
leaq 0x1(%r14), %rdx
subl %r14d, %eax
movslq %eax, %rcx
movq %r12, %rdi
movq %r15, %rsi
callq 0x43b0
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x82e8
movq -0xa0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x16cf9
callq 0x4350
cmpl $-0x1, %r13d
je 0x16d13
shlq $0x4, %r14
addq -0x58(%rbp), %r14
jmp 0x16cbe
movsd 0xfaef(%rip), %xmm0     # 0x26800
jmp 0x16d87
cmpb $0x0, -0x34(%rbp)
je 0x16d53
movq -0x40(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rbx
cmpq %rbx, %r14
setne %al
addq $-0x20, %rbx
cmpq %r14, %rbx
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x16d53
movq %r14, %rdi
movq %rbx, %rsi
callq 0x42a0
addq $0x20, %r14
addq $-0x20, %rbx
cmpq %rbx, %r14
jb 0x16d3b
movq -0x50(%rbp), %rax
movsd -0x8(%rax), %xmm0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
movsd %xmm0, -0x30(%rbp)
je 0x16d74
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x16d87
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x16db0
jmp 0x16db0
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x16db3
callq 0x4350
jmp 0x16db3
movq %rax, %r14
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x16dc1
callq 0x4350
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x16dcf
callq 0x4350
movq %r14, %rdi
callq 0x4650