// File: aalto-speech[P]ftk/util/Ngram.cc
// Function: Ngram::read_arpa_insert_order_to_tree(std::vector<Ngram::NgramInfo, std::allocator<Ngram::NgramInfo>>&, int&, int&, int)
void
Ngram::read_arpa_insert_order_to_tree(std::vector<NgramInfo> &order_ngrams,
                                      int &curr_node_idx,
                                      int &curr_arc_idx,
                                      int curr_order)
{
    for (unsigned int ni=0; ni<order_ngrams.size(); ni++) {

        int node_idx_traversal = root_node;
        for (unsigned int i=0; i<order_ngrams[ni].ngram.size()-1; i++) {
            node_idx_traversal = find_node(node_idx_traversal, order_ngrams[ni].ngram[i]);
            if (node_idx_traversal == -1) throw string("Missing lower order n-gram");
        }
        int tmp = find_node(node_idx_traversal, order_ngrams[ni].ngram.back());
        if (tmp != -1)
            throw string("Duplicate n-gram in model");

        if (nodes[node_idx_traversal].first_arc == -1)
            nodes[node_idx_traversal].first_arc = curr_arc_idx;
        nodes[node_idx_traversal].last_arc = curr_arc_idx;

        arc_words[curr_arc_idx] = order_ngrams[ni].ngram.back();
        arc_target_nodes[curr_arc_idx] = curr_node_idx;
        nodes[curr_node_idx].prob = order_ngrams[ni].prob;
        nodes[curr_node_idx].backoff_prob = order_ngrams[ni].backoff_prob;

        int ctxt_start = 1;
        while (true) {
            int bo_traversal = root_node;
            unsigned int i = ctxt_start;
            for (; i<order_ngrams[ni].ngram.size(); i++) {
                int tmp = find_node(bo_traversal, order_ngrams[ni].ngram[i]);
                if (tmp == -1) break;
                bo_traversal = tmp;
            }
            if (i >= order_ngrams[ni].ngram.size()) {
                nodes[curr_node_idx].backoff_node = bo_traversal;
                break;
            }
            else ctxt_start++;
        }

        curr_arc_idx++;
        curr_node_idx++;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x141c0
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jae 0x136c3
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
callq 0x14210
movq %rax, %rcx
movq -0x78(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x13384
movl -0x2c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
movl -0x30(%rbp), %eax
movl %eax, %esi
callq 0x13820
movq -0x68(%rbp), %rdi
movl -0x7c(%rbp), %esi
movl (%rax), %edx
callq 0x11fd0
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x13374
movb $0x1, -0x45(%rbp)
movl $0x20, %edi
callq 0x41e0
movq %rax, -0x90(%rbp)
movq %rax, -0x98(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0x46f0
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rdx
leaq 0x531dc(%rip), %rsi     # 0x664f8
callq 0x4550
jmp 0x13323
movq -0x98(%rbp), %rdi
movb $0x0, -0x45(%rbp)
leaq 0x80a1b(%rip), %rsi     # 0x93d50
movq 0x80c64(%rip), %rdx     # 0x93fa0
callq 0x46b0
jmp 0x136d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x4420
testb $0x1, -0x45(%rbp)
jne 0x13363
jmp 0x1336f
movq -0x98(%rbp), %rdi
callq 0x42f0
jmp 0x136cc
jmp 0x13376
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1326e
movl -0x2c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
callq 0x14230
movq -0x68(%rbp), %rdi
movl -0x9c(%rbp), %esi
movl (%rax), %edx
callq 0x11fd0
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
je 0x1345a
movb $0x1, -0x4e(%rbp)
movl $0x20, %edi
callq 0x41e0
movq %rax, -0xb0(%rbp)
movq %rax, -0xb8(%rbp)
leaq -0x4d(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x46f0
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rdx
leaq 0x53111(%rip), %rsi     # 0x66513
callq 0x4550
jmp 0x13409
movq -0xb8(%rbp), %rdi
movb $0x0, -0x4e(%rbp)
leaq 0x80935(%rip), %rsi     # 0x93d50
movq 0x80b7e(%rip), %rdx     # 0x93fa0
callq 0x46b0
jmp 0x136d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x4d(%rbp), %rdi
callq 0x4420
testb $0x1, -0x4e(%rbp)
jne 0x13449
jmp 0x13455
movq -0xb8(%rbp), %rdi
callq 0x42f0
jmp 0x136cc
movq -0x68(%rbp), %rdi
addq $0x78, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x136e0
cmpl $-0x1, 0xc(%rax)
jne 0x13497
movq -0x68(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xbc(%rbp)
addq $0x78, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x136e0
movl -0xbc(%rbp), %ecx
movl %ecx, 0xc(%rax)
movq -0x68(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd0(%rbp)
addq $0x78, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x136e0
movl -0xd0(%rbp), %ecx
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
callq 0x14230
movq -0x68(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
addq $0x90, %rdi
movq -0x20(%rbp), %rax
movslq (%rax), %rsi
callq 0x13820
movl -0xcc(%rbp), %ecx
movq -0x68(%rbp), %rdi
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc8(%rbp)
addq $0xa8, %rdi
movq -0x20(%rbp), %rax
movslq (%rax), %rsi
callq 0x13820
movl -0xc8(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq -0x68(%rbp), %rdi
movsd 0x18(%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xc4(%rbp)
addq $0x78, %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
callq 0x136e0
movss -0xc4(%rbp), %xmm0
movss %xmm0, (%rax)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq -0x68(%rbp), %rdi
movsd 0x20(%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xc0(%rbp)
addq $0x78, %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
callq 0x136e0
movss -0xc0(%rbp), %xmm0
movss %xmm0, 0x4(%rax)
movl $0x1, -0x54(%rbp)
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
callq 0x14210
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1363a
movl -0x58(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
callq 0x13820
movq -0x68(%rbp), %rdi
movl -0xdc(%rbp), %esi
movl (%rax), %edx
callq 0x11fd0
movl %eax, -0x60(%rbp)
cmpl $-0x1, -0x60(%rbp)
jne 0x13629
jmp 0x1363a
movl -0x60(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x135ba
movl -0x5c(%rbp), %eax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x141f0
movq %rax, %rdi
callq 0x14210
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jb 0x13691
movq -0x68(%rbp), %rdi
movl -0x58(%rbp), %eax
movl %eax, -0xec(%rbp)
addq $0x78, %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
callq 0x136e0
movl -0xec(%rbp), %ecx
movl %ecx, 0x8(%rax)
jmp 0x1369f
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x135ab
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1323e
addq $0xf0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)
nop