// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
movq %rdi, -0x70(%rbp)
movq %rcx, -0x68(%rbp)
movq %rcx, %rdi
callq 0x82e8
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x16a12
movl %r14d, -0x3c(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x1afd8
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %rsi
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movzbl %bl, %edx
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0x16b01
movq -0x60(%rbp), %rcx
cmpq %rcx, -0x58(%rbp)
je 0x1697b
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xc0(%rbp)
xorl %esi, %esi
leaq -0xa8(%rbp), %r13
xorl %edi, %edi
movl %edi, %edi
cmpl $0x0, -0x30(%rbp)
je 0x1695e
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rdi, %rdx
jbe 0x1695e
movl (%rcx,%rsi,4), %ebx
leal -0x1(%rbx), %ecx
movl $0x1, %r12d
movq %rcx, -0xb0(%rbp)
shlq $0x4, %rcx
movq %rcx, -0xb8(%rbp)
movq %rdi, %rcx
movq %rdi, -0xc8(%rbp)
decq %rdx
cmpq %rcx, %rdx
jbe 0x1689d
leal (%rdi,%r12), %ecx
movl (%rax,%rcx,4), %r14d
movq -0x38(%rbp), %rsi
jmp 0x168a5
movq -0x38(%rbp), %rsi
movl 0x8(%rsi), %r14d
movl %r14d, %ecx
subl %ebx, %ecx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x43b0
movq -0x70(%rbp), %rdi
movq %r13, %r15
movq %r13, %rsi
callq 0xd700
movq %rax, %r13
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x168df
callq 0x4350
cmpq -0xc0(%rbp), %r13
movq -0xc8(%rbp), %rdi
je 0x16936
movsd 0x40(%r13), %xmm0
testq %rbx, %rbx
je 0x1690e
movq -0x88(%rbp), %rax
movq -0xb8(%rbp), %rcx
addsd 0x8(%rax,%rcx), %xmm0
decl %r14d
movq -0x88(%rbp), %rax
shlq $0x4, %r14
ucomisd 0x8(%rax,%r14), %xmm0
jbe 0x16936
addq %r14, %rax
movsd %xmm0, 0x8(%rax)
movq -0xb0(%rbp), %rcx
movl %ecx, (%rax)
cmpl -0x30(%rbp), %r12d
movq %r15, %r13
jae 0x1695e
leal (%rdi,%r12), %ecx
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
incl %r12d
cmpq %rcx, %rdx
ja 0x16887
incl %edi
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
sarq $0x2, %rax
movq %rdi, %rsi
cmpq %rdi, %rax
ja 0x1683b
movq -0x88(%rbp), %r14
movq -0x80(%rbp), %r15
subq %r14, %r15
shrq $0x4, %r15
decl %r15d
movslq %r15d, %rax
shlq $0x4, %rax
movsd 0x8(%r14,%rax), %xmm1
movsd 0xfe5a(%rip), %xmm0     # 0x26800
ucomisd %xmm0, %xmm1
jne 0x169b2
jnp 0x16a65
addq %rax, %r14
leaq -0x98(%rbp), %r12
leaq -0xa8(%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 0x43b0
movq -0x68(%rbp), %rdi
movq %rbx, %rsi
callq 0x832e
movq -0xa8(%rbp), %rdi
cmpq %r12, %rdi
je 0x169ff
callq 0x4350
cmpl $-0x1, %r15d
je 0x16a1c
shlq $0x4, %r14
addq -0x88(%rbp), %r14
jmp 0x169c3
movsd 0xfde6(%rip), %xmm0     # 0x26800
jmp 0x16a93
cmpb $0x0, -0x3c(%rbp)
je 0x16a5c
movq -0x68(%rbp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r14
cmpq %r14, %rbx
setne %al
addq $-0x20, %r14
cmpq %rbx, %r14
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x16a5c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42a0
addq $0x20, %rbx
addq $-0x20, %r14
cmpq %r14, %rbx
jb 0x16a44
movq -0x80(%rbp), %rax
movsd -0x8(%rax), %xmm0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
movsd %xmm0, -0x30(%rbp)
je 0x16a7d
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x16a93
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x16ad7
jmp 0x16ad7
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r12, %rdi
jne 0x16ad0
jmp 0x16ada
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x16ada
callq 0x4350
jmp 0x16ada
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x16ae8
callq 0x4350
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x16af9
callq 0x4350
movq %rbx, %rdi
callq 0x4650