// File: 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
subq $0x190, %rsp            # imm = 0x190
movl %r8d, -0x11c(%rbp)
movq %rcx, %rax
movl -0x11c(%rbp), %ecx
movq %rax, -0x118(%rbp)
movq %rdx, %rax
movq -0x118(%rbp), %rdx
movq %rax, -0x110(%rbp)
movl %esi, %eax
movq -0x110(%rbp), %rsi
movl %eax, -0x104(%rbp)
movq %rdi, %r8
movl -0x104(%rbp), %edi
movb %r9b, %al
movq %r8, -0x10(%rbp)
movl %edi, -0x14(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
andb $0x1, %cl
movb %cl, -0x29(%rbp)
andb $0x1, %al
movb %al, -0x2a(%rbp)
movq -0x28(%rbp), %rdi
callq 0xb7e0
movq -0x20(%rbp), %rdi
callq 0x45e0
cmpq $0x0, %rax
jne 0x402b6
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x407be
movq -0x20(%rbp), %rdi
callq 0x45e0
movq %rax, -0x130(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x47c00
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x47c20
jmp 0x402ef
leaq -0x49(%rbp), %rdi
callq 0x47c90
leaq -0x78(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x23470
movq -0x138(%rbp), %rsi
movq -0x20(%rbp), %rdi
movzbl -0x2a(%rbp), %edx
andl $0x1, %edx
callq 0x407e0
jmp 0x40321
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq %rax, -0x140(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x23490
movq %rax, %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
jae 0x405ec
movl -0x7c(%rbp), %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
callq 0x234b0
movl (%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq %rax, -0x150(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x23490
movq -0x150(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x141(%rbp)
jae 0x403ac
movl -0x84(%rbp), %eax
subl -0x7c(%rbp), %eax
cmpl -0x14(%rbp), %eax
setb %al
movb %al, -0x141(%rbp)
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x403bb
jmp 0x405dc
movq -0x20(%rbp), %rdi
callq 0x42d0
movl %eax, -0x88(%rbp)
movl -0x84(%rbp), %eax
movq %rax, -0x158(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x23490
movq %rax, %rcx
movq -0x158(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x4043e
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
callq 0x234b0
movl (%rax), %eax
movl %eax, -0x88(%rbp)
jmp 0x4043e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x47c90
jmp 0x407cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x407aa
movq -0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x20(%rbp), %rsi
movl -0x80(%rbp), %eax
movl %eax, %edx
movl %edx, %ecx
movl -0x88(%rbp), %eax
subl %ecx, %eax
movl %eax, %ecx
leaq -0xb0(%rbp), %rdi
callq 0x43e0
jmp 0x4046c
movq -0x160(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x1b5a0
movq %rax, -0x168(%rbp)
jmp 0x40488
movq -0x168(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x4828
movq -0x10(%rbp), %rdi
callq 0x1b480
movq %rax, -0xb8(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1b570
testb $0x1, %al
jne 0x404cb
jmp 0x404ed
jmp 0x405c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x4828
jmp 0x407aa
leaq -0x90(%rbp), %rdi
callq 0x1b2f0
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0xc0(%rbp)
cmpl $0x0, -0x80(%rbp)
jbe 0x40534
movl -0x80(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movsd 0x8(%rax), %xmm0
addsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x170(%rbp)
movl -0x88(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movsd -0x170(%rbp), %xmm0
ucomisd 0x8(%rax), %xmm0
jbe 0x405c6
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movl -0x88(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movsd -0x180(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl -0x80(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x174(%rbp)
movl -0x88(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movl -0x174(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x405c8
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x4036a
jmp 0x405de
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x40328
leaq -0x48(%rbp), %rdi
callq 0x47cd0
subq $0x1, %rax
movl %eax, -0xc4(%rbp)
movslq -0xc4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x188(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x188(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4067b
jp 0x4067b
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0x40796
movslq -0xc4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x40695
movq -0x28(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x20(%rbp), %rsi
movl -0xcc(%rbp), %ecx
movl %ecx, %eax
incl %eax
movslq %eax, %rdx
movl -0xc4(%rbp), %eax
subl %ecx, %eax
movslq %eax, %rcx
leaq -0xf0(%rbp), %rdi
callq 0x43e0
jmp 0x406ca
movq -0x190(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xb800
jmp 0x406df
leaq -0xf0(%rbp), %rdi
callq 0x4828
cmpl $-0x1, -0xcc(%rbp)
jne 0x40713
jmp 0x4073c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x4828
jmp 0x407aa
movl -0xcc(%rbp), %eax
movl %eax, -0xc4(%rbp)
movslq -0xc4(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x47cb0
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x40693
testb $0x1, -0x29(%rbp)
je 0x40779
movq -0x28(%rbp), %rdi
callq 0xb2f0
movq %rax, -0xf8(%rbp)
movq -0x28(%rbp), %rdi
callq 0xb320
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x30db0
jmp 0x40777
jmp 0x40779
leaq -0x48(%rbp), %rdi
callq 0x47cf0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xc8(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x23550
leaq -0x48(%rbp), %rdi
callq 0x47d30
jmp 0x407be
leaq -0x78(%rbp), %rdi
callq 0x23550
leaq -0x48(%rbp), %rdi
callq 0x47d30
jmp 0x407cc
movsd -0x8(%rbp), %xmm0
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)