// File: /workspace/github_cpp_makefile_star2_O3/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, -0x50(%rbp)
movq %rcx, -0x48(%rbp)
movq %rcx, %rdi
callq 0x82a2
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x168f1
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0x1aefe
xorpd %xmm0, %xmm0
leaq -0xb0(%rbp), %rsi
movapd %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movzbl %bl, %edx
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0x169dd
movl %r14d, -0x3c(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rax
cmpq %rdi, %rax
je 0x16863
movq -0x50(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0xc8(%rbp)
xorl %edx, %edx
leaq -0x90(%rbp), %r13
xorl %esi, %esi
movq %rax, %rcx
subq %rdi, %rcx
sarq $0x2, %rcx
movl %esi, %esi
cmpl $0x0, -0x30(%rbp)
je 0x16855
cmpq %rsi, %rcx
jbe 0x16855
movl (%rdi,%rdx,4), %ebx
leal -0x1(%rbx), %eax
movl $0x1, %r12d
movq %rax, -0xb8(%rbp)
shlq $0x4, %rax
movq %rax, -0xc0(%rbp)
movq %rsi, %rdx
movq %rsi, -0x58(%rbp)
decq %rcx
cmpq %rdx, %rcx
jbe 0x16792
leal (%rsi,%r12), %eax
movl (%rdi,%rax,4), %r14d
movq -0x38(%rbp), %rsi
jmp 0x1679a
movq -0x38(%rbp), %rsi
movl 0x8(%rsi), %r14d
movl %r14d, %ecx
subl %ebx, %ecx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x43b0
movq -0x50(%rbp), %rdi
movq %r13, %r15
movq %r13, %rsi
callq 0xd62a
movq %rax, %r13
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x167d1
callq 0x4350
cmpq -0xc8(%rbp), %r13
je 0x16817
movsd 0x40(%r13), %xmm0
movq -0x70(%rbp), %rax
testq %rbx, %rbx
je 0x167f6
movq -0xc0(%rbp), %rcx
addsd 0x8(%rax,%rcx), %xmm0
decl %r14d
shlq $0x4, %r14
ucomisd 0x8(%rax,%r14), %xmm0
jbe 0x16817
addq %r14, %rax
movsd %xmm0, 0x8(%rax)
movq -0xb8(%rbp), %rcx
movl %ecx, (%rax)
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq %rax, %rcx
subq %rdi, %rcx
sarq $0x2, %rcx
cmpl -0x30(%rbp), %r12d
jae 0x1684e
movq -0x58(%rbp), %rsi
leal (%rsi,%r12), %edx
incl %r12d
cmpq %rdx, %rcx
movq %r15, %r13
ja 0x1677c
jmp 0x16855
movq %r15, %r13
movq -0x58(%rbp), %rsi
incl %esi
movq %rsi, %rdx
cmpq %rsi, %rcx
ja 0x16738
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), %xmm1
movsd 0xff75(%rip), %xmm0     # 0x26800
ucomisd %xmm0, %xmm1
jne 0x16897
jnp 0x1694b
addq %rax, %r14
leaq -0x80(%rbp), %r12
leaq -0x90(%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 -0x48(%rbp), %rdi
movq %rbx, %rsi
callq 0x82e8
movq -0x90(%rbp), %rdi
cmpq %r12, %rdi
je 0x168e1
callq 0x4350
cmpl $-0x1, %r15d
je 0x168fb
shlq $0x4, %r14
addq -0x70(%rbp), %r14
jmp 0x168a5
movsd 0xff07(%rip), %xmm0     # 0x26800
jmp 0x16972
cmpb $0x0, -0x3c(%rbp)
je 0x1693b
movq -0x48(%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 0x1693b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42a0
addq $0x20, %rbx
addq $-0x20, %r14
cmpq %r14, %rbx
jb 0x16923
movq -0x68(%rbp), %rax
movsd -0x8(%rax), %xmm0
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
movsd %xmm0, -0x30(%rbp)
je 0x1695f
callq 0x4350
movsd -0x30(%rbp), %xmm0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x16972
callq 0x4350
movsd -0x30(%rbp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x169b3
jmp 0x169b3
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r12, %rdi
jne 0x169ac
jmp 0x169b6
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x169b6
callq 0x4350
jmp 0x169b6
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x169c7
callq 0x4350
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x169d5
callq 0x4350
movq %rbx, %rdi
callq 0x4650