// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movl %r8d, %r13d
movl %ecx, %ebx
movq %rsi, %r15
movq %rdi, %r12
movq %rdx, -0x40(%rbp)
movq %rdx, %rdi
callq 0x82e8
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x16e2c
movl %ebx, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x1afd8
xorpd %xmm0, %xmm0
leaq -0x70(%rbp), %rsi
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movzbl %r13b, %edx
movq %r15, %rdi
callq 0x16b01
movq -0x70(%rbp), %rax
cmpq %rax, -0x68(%rbp)
je 0x16d9e
xorl %edx, %edx
movq %rdx, -0x78(%rbp)
movl (%rax,%rdx,4), %eax
movq %rax, -0x30(%rbp)
cmpq %rax, 0x8(%r15)
jbe 0x16d80
movq -0x30(%rbp), %rbx
leal -0x1(%rbx), %eax
leal 0x1(%rbx), %r14d
movq %rax, -0x80(%rbp)
shlq $0x4, %rax
movq %rax, -0x88(%rbp)
movq %r12, %r13
movq (%r15), %rax
movsbl (%rax,%rbx), %esi
movq %r12, %rdi
movq %r13, %rdx
callq 0xcdf2
testq %rax, %rax
je 0x16d6f
movq 0x28(%rax), %r13
cmpq $0x0, 0x10(%rax)
je 0x16d6f
movsd 0x30(%rax), %xmm0
cmpq $0x0, -0x30(%rbp)
je 0x16d51
movq -0x58(%rbp), %rcx
movq -0x88(%rbp), %rdx
addsd 0x8(%rcx,%rdx), %xmm0
movq -0x58(%rbp), %rcx
shlq $0x4, %rbx
ucomisd 0x8(%rcx,%rbx), %xmm0
jbe 0x16d6f
addq %rbx, %rcx
movsd %xmm0, 0x8(%rcx)
movq -0x80(%rbp), %rdx
movl %edx, (%rcx)
testq %rax, %rax
je 0x16d80
movl %r14d, %ebx
incl %r14d
cmpq %rbx, 0x8(%r15)
ja 0x16d12
movq -0x78(%rbp), %rdx
incl %edx
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rdx, %rcx
ja 0x16ce0
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), %xmm1
movsd 0xfa3b(%rip), %xmm0     # 0x26800
ucomisd %xmm0, %xmm1
jne 0x16dd1
jnp 0x16e7f
addq %rax, %rbx
leaq -0x98(%rbp), %r13
leaq -0xa8(%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 0x43b0
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x832e
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x16e1c
callq 0x4350
cmpl $-0x1, %r14d
je 0x16e36
shlq $0x4, %rbx
addq -0x58(%rbp), %rbx
jmp 0x16de2
movsd 0xf9cc(%rip), %xmm0     # 0x26800
jmp 0x16eaa
cmpb $0x0, -0x34(%rbp)
je 0x16e76
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 0x16e76
movq %r14, %rdi
movq %rbx, %rsi
callq 0x42a0
addq $0x20, %r14
addq $-0x20, %rbx
cmpq %rbx, %r14
jb 0x16e5e
movq -0x50(%rbp), %rax
movsd -0x8(%rax), %xmm0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
movsd %xmm0, -0x30(%rbp)
je 0x16e97
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x16eaa
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x16ed6
jmp 0x16ed6
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x16ed9
callq 0x4350
jmp 0x16ed9
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x16ee7
callq 0x4350
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x16ef5
callq 0x4350
movq %rbx, %rdi
callq 0x4650