// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/EM.cc
// Function: viterbi(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&, unsigned int, 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 map<string, flt_type> &vocab,
                 unsigned int maxlen,
                 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];
        for (unsigned int j=i; j<char_positions.size() && (j-i < maxlen); j++) {

            unsigned int end_pos = text.size();
            if (j < (char_positions.size()-1)) end_pos = char_positions[j+1];
            auto vit = vocab.find(text.substr(start_pos, end_pos-start_pos));
            if (vit == vocab.end()) continue;

            flt_type cost = vit->second;
            if (start_pos > 0) cost += search[start_pos-1].cost;

            if (cost > search[end_pos-1].cost) {
                search[end_pos-1].cost = cost;
                search[end_pos-1].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 $0xa8, %rsp
movl %r9d, %ebx
movl %r8d, %r14d
movq %rdx, %r15
movl %esi, -0x40(%rbp)
movq %rdi, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
movq %rcx, %rdi
callq 0x7532
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x1540e
movl %r14d, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x18d3e
xorpd %xmm0, %xmm0
leaq -0x90(%rbp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movzbl %bl, %edx
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0x154a1
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %rcx
xorl %edi, %edi
pushq $0x1
popq %rdx
movq %rdx, -0x30(%rbp)
leaq -0xd0(%rbp), %r14
movl %edi, %edi
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rdi, %rdx
jbe 0x15391
movl (%rax,%rdi,4), %ebx
leal -0x1(%rbx), %edx
xorl %r12d, %r12d
movq %rdx, -0x98(%rbp)
shlq $0x4, %rdx
movq %rdx, -0xa0(%rbp)
movq %rdi, -0xb0(%rbp)
cmpl -0x40(%rbp), %r12d
jae 0x15380
leal (%rdi,%r12), %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
cmpq %rdx, %rsi
jbe 0x15380
decq %rsi
cmpq %rdx, %rsi
jbe 0x152ee
movq -0x30(%rbp), %rcx
addl %r12d, %ecx
movl (%rax,%rcx,4), %r15d
movq -0x38(%rbp), %rsi
jmp 0x152f6
movq -0x38(%rbp), %rsi
movl 0x8(%rsi), %r15d
movl %r15d, %ecx
subl %ebx, %ecx
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4360
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0xc3dc
movq %rax, %r13
movq %r14, %rdi
callq 0x46b8
cmpq -0xa8(%rbp), %r13
movq -0xb0(%rbp), %rdi
je 0x1536a
movsd 0x40(%r13), %xmm0
movq -0x70(%rbp), %rax
testq %rbx, %rbx
je 0x15349
movq -0xa0(%rbp), %rcx
addsd 0x8(%rax,%rcx), %xmm0
decl %r15d
shlq $0x4, %r15
ucomisd 0x8(%rax,%r15), %xmm0
jbe 0x1536a
addq %r15, %rax
movsd %xmm0, 0x8(%rax)
movq -0x98(%rbp), %rcx
movl %ecx, (%rax)
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %rcx
incl %r12d
jmp 0x152b4
incl %edi
movq -0x30(%rbp), %rdx
incl %edx
movq %rdx, -0x30(%rbp)
jmp 0x1527d
movq -0x70(%rbp), %r14
movq -0x68(%rbp), %r15
subq %r14, %r15
shrq $0x4, %r15
decl %r15d
movslq %r15d, %rax
shlq $0x4, %rax
movsd 0x8(%r14,%rax), %xmm0
movsd 0xe3d7(%rip), %xmm1     # 0x23790
ucomisd %xmm1, %xmm0
jne 0x153c1
jnp 0x15437
addq %rax, %r14
leaq -0xd0(%rbp), %rbx
movl %r15d, %eax
movl (%r14), %r15d
movslq %r15d, %r14
leaq 0x1(%r14), %rdx
subl %r14d, %eax
movslq %eax, %rcx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x4360
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x7570
movq %rbx, %rdi
callq 0x46b8
cmpl $-0x1, %r15d
je 0x15418
shlq $0x4, %r14
addq -0x70(%rbp), %r14
jmp 0x153cb
movsd 0xe37a(%rip), %xmm0     # 0x23790
jmp 0x15456
cmpb $0x0, -0x44(%rbp)
je 0x1542e
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x1423d
movq -0x68(%rbp), %rax
movsd -0x8(%rax), %xmm1
movsd %xmm1, -0x40(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xed7a
leaq -0x70(%rbp), %rdi
callq 0x19c1c
movsd -0x40(%rbp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15481
jmp 0x15481
jmp 0x15481
jmp 0x15470
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x46b8
jmp 0x15484
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0xed7a
leaq -0x70(%rbp), %rdi
callq 0x19c1c
movq %rbx, %rdi
callq 0x45f0