// File: /workspace/github_cpp_makefile_star2_O1/aalto-speech[P]ftk/util/Ngram.cc
// Function: Ngram::read_arpa(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void
Ngram::read_arpa(string arpafname) {

    ifstream arpafile(arpafname);
    string header_error("Invalid ARPA header");
    if (!arpafile) throw string("Problem opening ARPA file: " + arpafname);

    int linei = 0;
    string line;
    _getline(arpafile, line, linei);
    while (line.length() == 0)
        _getline(arpafile, line, linei);

    if (line.find("\\data\\") == string::npos) throw header_error;
    _getline(arpafile, line, linei);

    int curr_ngram_order = 1;
    int total_ngram_count = 0;
    while (line.length() > 0) {
        if (line.find("ngram ") == string::npos) throw header_error;
        stringstream vals(line);
        getline(vals, line, '=');
        getline(vals, line, '=');
        int count = stoi(line);
        ngram_counts_per_order[curr_ngram_order] = count;
        total_ngram_count += count;
        curr_ngram_order++;
        _getline(arpafile, line, linei);
    }

    int max_ngram_order = curr_ngram_order-1;
    int total_ngrams_read = 0;
    curr_ngram_order = 1;
    nodes.resize(total_ngram_count+1);
    arc_words.resize(total_ngram_count);
    arc_target_nodes.resize(total_ngram_count);
    int curr_node_idx = 1;
    int curr_arc_idx = 0;

    while (curr_ngram_order <= max_ngram_order) {

        while (line.length() == 0)
            _getline(arpafile, line, linei);
        if (line.find("-grams") == string::npos) {
            cerr << line;
            throw header_error;
        }

        _getline(arpafile, line, linei);
        vector<NgramInfo> order_ngrams;
        int ngrams_read = read_arpa_read_order(arpafile, order_ngrams, line, curr_ngram_order, linei);

        cerr << "n-grams for order " << curr_ngram_order << ": " << ngrams_read << endl;
        if (ngrams_read != ngram_counts_per_order[curr_ngram_order])
            throw string("Invalid number of n-grams for order: " + curr_ngram_order);

        sort(order_ngrams.begin(), order_ngrams.end());

        read_arpa_insert_order_to_tree(order_ngrams, curr_node_idx, curr_arc_idx, curr_ngram_order);

        max_order = curr_ngram_order;
        curr_ngram_order++;
        total_ngrams_read += ngrams_read;
    }

    sentence_start_symbol_idx = vocabulary_lookup[sentence_start_symbol];
    sentence_start_node = find_node(root_node, sentence_start_symbol_idx);
    if (sentence_start_node == -1) throw string("Sentence start symbol not found.");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %rbx
movq %rdi, %r12
leaq -0x438(%rbp), %rdi
movl $0x8, %edx
callq 0x45a0
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1c2de(%rip), %rsi     # 0x264d2
leaq 0x1c2ea(%rip), %rdx     # 0x264e5
leaq -0x50(%rbp), %rdi
callq 0x5fa4
movq -0x438(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x418(%rbp,%rax)
jne 0xa805
leaq -0x30(%rbp), %rdx
movl $0x0, (%rdx)
leaq -0x68(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x438(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa0d2
leaq -0x438(%rbp), %rbx
leaq -0x78(%rbp), %r14
leaq -0x30(%rbp), %r15
cmpq $0x0, -0x70(%rbp)
jne 0xa270
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xa0d2
jmp 0xa259
leaq 0x1c28b(%rip), %rsi     # 0x26502
leaq -0x78(%rbp), %rdi
movl $0x6, %ecx
xorl %edx, %edx
callq 0x4630
cmpq $-0x1, %rax
je 0xa83f
leaq -0x438(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0xa0d2
movl $0x1, -0x2c(%rbp)
cmpq $0x0, -0x70(%rbp)
movq %r12, -0x58(%rbp)
je 0xa3f5
leaq 0xc0(%r12), %rax
movq %rax, -0xa0(%rbp)
xorl %ebx, %ebx
leaq -0x78(%rbp), %r14
leaq -0x230(%rbp), %r13
movl $0x6, %ecx
movq %r14, %rdi
leaq 0x1c223(%rip), %rsi     # 0x26509
xorl %edx, %edx
callq 0x4630
cmpq $-0x1, %rax
je 0xa699
movq %r13, %rdi
movq %r14, %rsi
movl $0x18, %edx
callq 0x4580
movq %rbx, -0x88(%rbp)
movq %r13, %rdi
movq %r14, %rsi
movl $0x3d, %edx
callq 0x4680
movq %r13, %rdi
movq %r14, %rsi
movl $0x3d, %edx
callq 0x4680
movq %r14, %r15
movq -0x78(%rbp), %r14
callq 0x4050
movq %rax, %r12
movl (%rax), %eax
movl %eax, -0x80(%rbp)
movl $0x0, (%r12)
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
movl $0xa, %edx
callq 0x44b0
cmpq %r14, -0x90(%rbp)
je 0xa6ca
movq %rax, %rbx
movslq %ebx, %rax
cmpq %rbx, %rax
jne 0xa6d6
movl (%r12), %eax
cmpl $0x22, %eax
je 0xa6d6
testl %eax, %eax
movq %r15, %r14
jne 0xa395
movl -0x80(%rbp), %eax
movl %eax, (%r12)
movq -0xa0(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0xb1d4
movl %ebx, (%rax)
incl -0x2c(%rbp)
leaq -0x438(%rbp), %rdi
movq %r14, %rsi
leaq -0x30(%rbp), %rdx
callq 0xa0d2
movq -0x88(%rbp), %rax
addl %ebx, %eax
movq %rax, %rbx
movq %r13, %rdi
movq 0x29bbd(%rip), %rsi     # 0x33f90
callq 0x42c0
leaq -0x1b0(%rbp), %rdi
callq 0x4110
cmpq $0x0, -0x70(%rbp)
movq -0x58(%rbp), %r12
jne 0xa2d7
jmp 0xa3f7
xorl %ebx, %ebx
movl -0x2c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl $0x1, -0x2c(%rbp)
leaq 0x78(%r12), %rdi
leal 0x1(%rbx), %eax
movslq %eax, %rsi
callq 0xb23c
leaq 0x90(%r12), %rdi
movslq %ebx, %rbx
movq %rbx, %rsi
callq 0xb286
leaq 0xa8(%r12), %rdi
movq %rbx, %rsi
callq 0xb286
movl $0x1, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x88(%rbp), %eax
cmpl %eax, -0x2c(%rbp)
jge 0xa62b
movq -0x58(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x80(%rbp)
leaq -0x438(%rbp), %rbx
leaq -0x78(%rbp), %r12
leaq -0x30(%rbp), %r15
cmpq $0x0, -0x70(%rbp)
je 0xa618
movl $0x6, %ecx
movq %r12, %rdi
leaq 0x1c195(%rip), %rsi     # 0x26629
xorl %edx, %edx
callq 0x4630
cmpq $-0x1, %rax
je 0xa6e2
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xa0d2
xorps %xmm0, %xmm0
movaps %xmm0, -0x230(%rbp)
movq $0x0, -0x220(%rbp)
movl -0x2c(%rbp), %r8d
movq -0x58(%rbp), %rdi
movq %rbx, %r13
movq %rbx, %rsi
leaq -0x230(%rbp), %rdx
movq %r12, %rcx
movq %r15, %r14
movq %r15, %r9
callq 0xa9c6
movl %eax, %r15d
movl $0x12, %edx
movq 0x29aee(%rip), %rdi     # 0x33fe8
leaq 0x1c00f(%rip), %rsi     # 0x26510
callq 0x4400
movl -0x2c(%rbp), %esi
movq 0x29ad8(%rip), %rdi     # 0x33fe8
callq 0x4620
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1c25b(%rip), %rsi     # 0x26782
callq 0x4400
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4620
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4370
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4260
movq -0x80(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0xb1d4
cmpl (%rax), %r15d
jne 0xa727
movq -0x230(%rbp), %rbx
movq -0x228(%rbp), %r15
cmpq %r15, %rbx
je 0xa5c5
movq %r15, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb962
movq %rbx, %rdi
movq %r15, %rsi
callq 0xba22
movq -0x58(%rbp), %rdi
leaq -0x230(%rbp), %rsi
leaq -0x90(%rbp), %rdx
leaq -0x94(%rbp), %rcx
callq 0xae78
movl -0x2c(%rbp), %eax
movq -0x58(%rbp), %rcx
movl %eax, 0xf0(%rcx)
incl %eax
movl %eax, -0x2c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0xb2be
movl -0x88(%rbp), %eax
cmpl %eax, -0x2c(%rbp)
movq %r13, %rbx
movq %r14, %r15
jl 0xa47a
jmp 0xa62b
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xa0d2
jmp 0xa47a
movq -0x58(%rbp), %rbx
leaq 0x48(%rbx), %rdi
leaq 0x10(%rbx), %rsi
callq 0x7dde
movl (%rax), %edx
movl %edx, 0x8(%rbx)
movl (%rbx), %esi
movq %rbx, %rdi
callq 0x9f9c
movl %eax, 0x4(%rbx)
cmpl $-0x1, %eax
je 0xa869
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0xa669
callq 0x4350
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xa67b
callq 0x4350
leaq -0x438(%rbp), %rdi
callq 0x40c0
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x20, %edi
callq 0x4190
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rdx
leaq -0x40(%rbp), %rsi
cmpq %rsi, %rdx
jne 0xa781
leaq -0x40(%rbp), %rdx
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
jmp 0xa78c
leaq 0x1befc(%rip), %rdi     # 0x265cd
callq 0x4200
leaq 0x1bef0(%rip), %rdi     # 0x265cd
callq 0x44e0
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq 0x298f7(%rip), %rdi     # 0x33fe8
callq 0x4400
movl $0x20, %edi
callq 0x4190
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rdx
leaq -0x40(%rbp), %rsi
cmpq %rsi, %rdx
jne 0xa7c3
leaq -0x40(%rbp), %rdx
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
jmp 0xa7ce
movl $0x20, %edi
callq 0x4190
movq %rax, %r14
movslq -0x2c(%rbp), %rax
leaq 0x1bde4(%rip), %rbx     # 0x26523
addq %rax, %rbx
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq %rbx, %rdi
callq 0x4170
leaq (%rax,%rbx), %rdx
movb $0x1, %r15b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5fa4
xorl %r15d, %r15d
leaq 0x295e3(%rip), %rsi     # 0x33d50
movq 0x2982c(%rip), %rdx     # 0x33fa0
movq %r14, %rdi
callq 0x4610
jmp 0xa8ee
movq %rdx, (%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
leaq 0x295a1(%rip), %rsi     # 0x33d50
movq 0x297ea(%rip), %rdx     # 0x33fa0
movq %rax, %rdi
callq 0x4610
jmp 0xa8ee
movq %rdx, (%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
leaq 0x2955f(%rip), %rsi     # 0x33d50
movq 0x297a8(%rip), %rdx     # 0x33fa0
movq %rax, %rdi
callq 0x4610
jmp 0xa8ee
movl $0x20, %edi
callq 0x4190
movq %rax, %r14
leaq 0x1bccd(%rip), %rsi     # 0x264e6
movq %rax, %rdi
movq %rbx, %rdx
callq 0xb149
leaq 0x29525(%rip), %rsi     # 0x33d50
movq 0x2976e(%rip), %rdx     # 0x33fa0
movq %r14, %rdi
callq 0x4610
jmp 0xa8ee
movl $0x20, %edi
callq 0x4190
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rdx
leaq -0x40(%rbp), %rsi
cmpq %rsi, %rdx
jne 0xa8b1
leaq -0x40(%rbp), %rdx
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
jmp 0xa8bc
movl $0x20, %edi
callq 0x4190
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movb $0x1, %r15b
leaq 0x1bcc2(%rip), %rsi     # 0x26549
leaq 0x1bcdb(%rip), %rdx     # 0x26569
movq %r14, %rdi
callq 0x5fa4
xorl %r15d, %r15d
leaq 0x294b0(%rip), %rsi     # 0x33d50
movq 0x296f9(%rip), %rdx     # 0x33fa0
movq %r14, %rdi
callq 0x4610
jmp 0xa8ee
movq %rdx, (%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
leaq 0x29471(%rip), %rsi     # 0x33d50
movq 0x296ba(%rip), %rdx     # 0x33fa0
movq %rax, %rdi
callq 0x4610
movq %rax, %rbx
jmp 0xa99f
movq %rax, %rbx
movq %r14, %rdi
callq 0x42b0
jmp 0xa99f
movq %rax, %rbx
testb %r15b, %r15b
je 0xa98d
movq %r14, %rdi
callq 0x42b0
jmp 0xa98d
movq %rax, %rbx
jmp 0xa9b1
jmp 0xa940
jmp 0xa940
jmp 0xa940
jmp 0xa940
movq %rax, %rbx
testb %r15b, %r15b
je 0xa981
movq %r14, %rdi
callq 0x42b0
jmp 0xa981
jmp 0xa940
jmp 0xa940
jmp 0xa940
movq %rax, %rbx
jmp 0xa98d
jmp 0xa947
movq %rax, %rbx
jmp 0xa95d
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0xa95d
movl -0x80(%rbp), %eax
movl %eax, (%r12)
movq 0x2962c(%rip), %rsi     # 0x33f90
leaq -0x230(%rbp), %rdi
callq 0x42c0
leaq -0x1b0(%rbp), %rdi
callq 0x4110
jmp 0xa98d
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0xb2be
leaq -0x68(%rbp), %rax
movq -0x78(%rbp), %rdi
cmpq %rax, %rdi
je 0xa99f
callq 0x4350
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xa9b1
callq 0x4350
leaq -0x438(%rbp), %rdi
callq 0x40c0
movq %rbx, %rdi
callq 0x4650
nop