// File: 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
subq $0x140, %rsp            # imm = 0x140
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %cl
movb %cl, -0x21(%rbp)
andb $0x1, %al
movb %al, -0x22(%rbp)
movq -0x20(%rbp), %rdi
callq 0xb7e0
movq -0x18(%rbp), %rdi
callq 0x45e0
cmpq $0x0, %rax
jne 0x408c3
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x40d10
movq -0x18(%rbp), %rdi
callq 0x45e0
movq %rax, -0xe8(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x47c00
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x47c20
jmp 0x408fc
leaq -0x41(%rbp), %rdi
callq 0x47c90
leaq -0x70(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x23470
movq -0xf0(%rbp), %rsi
movq -0x18(%rbp), %rdi
movzbl -0x22(%rbp), %edx
andl $0x1, %edx
callq 0x407e0
jmp 0x4092e
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq %rax, -0xf8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x23490
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x40b3e
movl -0x74(%rbp), %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x234b0
movl (%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rdi
callq 0x45e0
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x40b2e
movq -0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x18(%rbp), %rdi
movl -0x84(%rbp), %eax
movl %eax, %esi
callq 0x4430
movq -0x110(%rbp), %rdi
movsbl (%rax), %esi
movq -0x80(%rbp), %rdx
callq 0x1a730
movq %rax, -0x108(%rbp)
jmp 0x409e0
movq -0x108(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x40a28
jmp 0x40b2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x47c90
jmp 0x40d1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x40cfc
movq -0x90(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
callq 0x45e0
cmpq $0x0, %rax
jbe 0x40b18
movq -0x90(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
cmpl $0x0, -0x78(%rbp)
jbe 0x40a93
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movsd 0x8(%rax), %xmm0
addsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
movl -0x84(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movsd -0x118(%rbp), %xmm0
ucomisd 0x8(%rax), %xmm0
jbe 0x40b16
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
movl -0x84(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movsd -0x128(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x11c(%rbp)
movl -0x84(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movl -0x11c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x40b18
jmp 0x40b1a
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x4097f
jmp 0x40b30
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x40935
leaq -0x40(%rbp), %rdi
callq 0x47cd0
subq $0x1, %rax
movl %eax, -0x9c(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x130(%rbp)
callq 0x47bf0
movaps %xmm0, %xmm1
movsd -0x130(%rbp), %xmm0
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x40bcd
jp 0x40bcd
callq 0x47bf0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x40ce8
movslq -0x9c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x40be7
movq -0x20(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x18(%rbp), %rsi
movl -0xa4(%rbp), %ecx
movl %ecx, %eax
incl %eax
movslq %eax, %rdx
movl -0x9c(%rbp), %eax
subl %ecx, %eax
movslq %eax, %rcx
leaq -0xc8(%rbp), %rdi
callq 0x43e0
jmp 0x40c1c
movq -0x138(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0xb800
jmp 0x40c31
leaq -0xc8(%rbp), %rdi
callq 0x4828
cmpl $-0x1, -0xa4(%rbp)
jne 0x40c65
jmp 0x40c8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x4828
jmp 0x40cfc
movl -0xa4(%rbp), %eax
movl %eax, -0x9c(%rbp)
movslq -0x9c(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x47cb0
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0x40be5
testb $0x1, -0x21(%rbp)
je 0x40ccb
movq -0x20(%rbp), %rdi
callq 0xb2f0
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rdi
callq 0xb320
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x30db0
jmp 0x40cc9
jmp 0x40ccb
leaq -0x40(%rbp), %rdi
callq 0x47cf0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xa0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x23550
leaq -0x40(%rbp), %rdi
callq 0x47d30
jmp 0x40d10
leaq -0x70(%rbp), %rdi
callq 0x23550
leaq -0x40(%rbp), %rdi
callq 0x47d30
jmp 0x40d1e
movsd -0x8(%rbp), %xmm0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x46e0
nopw (%rax,%rax)