// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %r12
movq (%rsi), %rsi
xorl %edi, %edi
movq %rcx, -0x40(%rbp)
movl %edi, %edi
movq 0x8(%r15), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %rdi, %rax
jbe 0x9e14
movl (%r12), %r13d
xorl %eax, %eax
movq %rdi, -0x48(%rbp)
imulq $0x28, %rdi, %rbx
movl %eax, %r14d
movq (%r15), %rcx
movq (%rcx,%rbx), %rax
movq 0x8(%rcx,%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
decq %rdx
cmpq %r14, %rdx
jbe 0x9d0e
movl (%rax,%r14,4), %edx
movq %r12, %rdi
movl %r13d, %esi
callq 0x9194
movl %eax, %r13d
leal 0x1(%r14), %eax
cmpl $-0x1, %r13d
jne 0x9ccc
jmp 0x9e23
movl -0x4(%rcx), %edx
movq %r12, %rdi
movl %r13d, %esi
callq 0x9194
cmpl $-0x1, %eax
jne 0x9e57
movslq %r13d, %rcx
movq 0x78(%r12), %rax
imulq $0x14, %rcx, %rdx
leaq (%rax,%rdx), %rcx
cmpl $-0x1, 0xc(%rax,%rdx)
movq -0x40(%rbp), %r8
movl (%r8), %edx
jne 0x9d46
movl %edx, 0xc(%rcx)
movl %edx, 0x10(%rcx)
movq (%r15), %rcx
movq 0x8(%rcx,%rbx), %rdx
movl -0x4(%rdx), %edx
movslq (%r8), %rsi
movq 0x90(%r12), %rdi
movl %edx, (%rdi,%rsi,4)
movq -0x38(%rbp), %rdx
movslq (%rdx), %rdx
movslq (%r8), %rsi
movq 0xa8(%r12), %rdi
movl %edx, (%rdi,%rsi,4)
cvtsd2ss 0x18(%rcx,%rbx), %xmm0
imulq $0x14, %rdx, %rdx
movss %xmm0, (%rax,%rdx)
cvtsd2ss 0x20(%rcx,%rbx), %xmm0
movss %xmm0, 0x4(%rax,%rdx)
pushq $0x1
popq %rcx
movl (%r12), %eax
movl %ecx, -0x30(%rbp)
movl %eax, %r13d
movl %ecx, %r14d
movq (%r15), %rsi
movq (%rsi,%rbx), %rdx
movq 0x8(%rsi,%rbx), %rax
subq %rdx, %rax
sarq $0x2, %rax
cmpq %r14, %rax
jbe 0x9de2
movl (%rdx,%r14,4), %edx
movq %r12, %rdi
movl %r13d, %esi
callq 0x9194
leal 0x1(%r14), %ecx
cmpl $-0x1, %eax
jne 0x9d9c
movq (%r15), %rsi
movq 0x8(%rsi,%rbx), %rax
subq (%rsi,%rbx), %rax
sarq $0x2, %rax
movl -0x30(%rbp), %ecx
incl %ecx
cmpq %r14, %rax
ja 0x9d95
movq -0x38(%rbp), %rdi
movslq (%rdi), %rax
movq 0x78(%r12), %rdx
imulq $0x14, %rax, %rax
movl %r13d, 0x8(%rdx,%rax)
movq -0x40(%rbp), %rcx
incl (%rcx)
incl (%rdi)
movq -0x48(%rbp), %rdi
incl %edi
jmp 0x9ca4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x4190
movq %rax, %rbx
leaq 0x196f3(%rip), %rsi     # 0x23528
leaq -0x2a(%rbp), %rdx
movq %rax, %rdi
callq 0x4140
leaq 0x2af10(%rip), %rsi     # 0x34d58
movq 0x2b151(%rip), %rdx     # 0x34fa0
movq %rbx, %rdi
callq 0x45c0
pushq $0x20
popq %rdi
callq 0x4190
movq %rax, %rbx
leaq 0x196da(%rip), %rsi     # 0x23543
leaq -0x29(%rbp), %rdx
movq %rax, %rdi
callq 0x4140
jmp 0x9e41
jmp 0x9e79
movq %rax, %r14
movq %rbx, %rdi
callq 0x4290
movq %r14, %rdi
callq 0x45f0