// File: /workspace/github_cpp_makefile_star2_O2/aalto-speech[P]ftk/src/strscore.cc
// Function: main
int main(int argc, char* argv[]) {

    conf::Config config;
    config("usage: strscore [OPTION...] ARPAFILE INPUT OUTPUT\n")
    ('8', "utf-8", "", "", "Utf-8 character encoding in use")
    ('h', "help", "", "", "display help");
    config.default_parse(argc, argv);
    if (config.arguments.size() != 3) config.print_help(stderr, 1);

    string arpafname = config.arguments[0];
    string infname = config.arguments[1];
    string outfname = config.arguments[2];
    bool utf8_encoding = config["utf-8"].specified;

    Ngram lm;
    lm.read_arpa(arpafname);

    ifstream infile(infname);
    if (!infile) {
        cerr << "Something went wrong opening the input file." << endl;
        exit(1);
    }

    ofstream outfile(outfname);
    if (!outfile) {
        cerr << "Something went wrong opening the output file." << endl;
        exit(1);
    }

    float count;
    string line, lstr;
    vector<pair<string, float> > scores;
    float normalizer = SMALL_LP;
    while (getline(infile, line)) {
        stringstream ss(line);
        ss >> count;
        ss >> lstr;

        float total_prob = 0.0;
        int node_id = lm.root_node;

        vector<unsigned int> char_positions;
        get_character_positions(lstr, char_positions, utf8_encoding);
        for (unsigned int i=0; i<char_positions.size()-1; i++) {
            unsigned int start_pos = char_positions[i];
            unsigned int end_pos = char_positions[i+1];
            int sym = lm.vocabulary_lookup[lstr.substr(start_pos, end_pos-start_pos)];
            node_id = lm.score(node_id, sym, total_prob);
        }

        total_prob *= log(10.0); // Convert from log10 (ARPA default) to ln
        normalizer = add_log_domain_probs(normalizer, total_prob);
        scores.push_back(make_pair(lstr, total_prob));
    }

    for (auto it=scores.begin(); it != scores.end(); ++it)
        outfile << it->second-normalizer << "\t" << it->first << endl;

    infile.close();
    outfile.close();

    exit(EXIT_SUCCESS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9f8, %rsp            # imm = 0x9F8
movq %rsi, %rbx
movl %edi, %r14d
leaq -0x398(%rbp), %rdi
callq 0x6a50
leaq 0x1f509(%rip), %rsi     # 0x24032
leaq -0x2e0(%rbp), %rdi
leaq -0x698(%rbp), %rdx
callq 0x4140
leaq -0x398(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x6abc
movq %rax, %r15
leaq 0x1f50c(%rip), %rsi     # 0x24065
leaq -0x2c0(%rbp), %rdi
leaq -0x898(%rbp), %rdx
callq 0x4140
leaq 0x1f8c4(%rip), %rsi     # 0x24437
leaq -0x2a0(%rbp), %rdi
leaq -0xa20(%rbp), %rdx
callq 0x4140
leaq 0x1f8aa(%rip), %rsi     # 0x24437
leaq -0x280(%rbp), %rdi
leaq -0x490(%rbp), %rdx
callq 0x4140
leaq 0x1f4c4(%rip), %rsi     # 0x2406b
leaq -0x260(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x4140
pushq $0x38
popq %rsi
leaq -0x2c0(%rbp), %rdx
leaq -0x2a0(%rbp), %rcx
leaq -0x280(%rbp), %r8
leaq -0x260(%rbp), %r9
movq %r15, %rdi
callq 0x6ad4
movq %rax, %r15
leaq 0x1f4a8(%rip), %rsi     # 0x24093
leaq -0x240(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x4140
leaq 0x1f832(%rip), %rsi     # 0x24437
leaq -0x220(%rbp), %rdi
leaq -0x1a0(%rbp), %rdx
callq 0x4140
leaq 0x1f818(%rip), %rsi     # 0x24437
leaq -0x200(%rbp), %rdi
leaq -0x180(%rbp), %rdx
callq 0x4140
leaq 0x1f452(%rip), %rsi     # 0x2408b
leaq -0x1e0(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x4140
pushq $0x68
popq %rsi
leaq -0x240(%rbp), %rdx
leaq -0x220(%rbp), %rcx
leaq -0x200(%rbp), %r8
leaq -0x1e0(%rbp), %r9
movq %r15, %rdi
callq 0x6ad4
leaq -0x1e0(%rbp), %rdi
callq 0x46b8
leaq -0x200(%rbp), %rdi
callq 0x46b8
leaq -0x220(%rbp), %rdi
callq 0x46b8
leaq -0x240(%rbp), %rdi
callq 0x46b8
leaq -0x260(%rbp), %rdi
callq 0x46b8
leaq -0x280(%rbp), %rdi
callq 0x46b8
leaq -0x2a0(%rbp), %rdi
callq 0x46b8
leaq -0x2c0(%rbp), %rdi
callq 0x46b8
leaq -0x2e0(%rbp), %rdi
callq 0x46b8
leaq -0x398(%rbp), %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x733a
movq -0x300(%rbp), %rsi
movq -0x2f8(%rbp), %rax
subq %rsi, %rax
cmpq $0x60, %rax
je 0x4d28
movq 0x312b1(%rip), %rax     # 0x35fc0
movq (%rax), %rsi
leaq -0x398(%rbp), %rdi
pushq $0x1
popq %rdx
callq 0x7822
movq -0x300(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
callq 0x4460
movq -0x300(%rbp), %rsi
addq $0x20, %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x4460
movq -0x300(%rbp), %rsi
addq $0x40, %rsi
leaq -0x180(%rbp), %rdi
callq 0x4460
leaq 0x1f2fc(%rip), %rsi     # 0x24065
leaq -0x160(%rbp), %rdi
leaq -0x698(%rbp), %rdx
callq 0x4140
leaq -0x398(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x73da
movb 0x4a(%rax), %al
movb %al, -0x29(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x46b8
leaq -0x490(%rbp), %rdi
callq 0x5384
leaq -0x140(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x4460
leaq -0x490(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x9b1c
leaq -0x140(%rbp), %rdi
callq 0x46b8
leaq -0x698(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x4540
movq -0x698(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x678(%rbp,%rax)
jne 0x511b
leaq -0x898(%rbp), %rdi
leaq -0x180(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x4640
movq -0x898(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x878(%rbp,%rax)
jne 0x5138
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movss 0x1f187(%rip), %xmm0    # 0x24004
movss %xmm0, -0x60(%rbp)
leaq -0xf0(%rbp), %r14
leaq -0xa20(%rbp), %r15
leaq -0xd0(%rbp), %rbx
leaq -0x88(%rbp), %r12
leaq -0x698(%rbp), %rdi
movq %r14, %rsi
callq 0x4090
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x515b
movq %r15, %rdi
movq %r14, %rsi
pushq $0x18
popq %rdx
callq 0x4520
movq %r15, %rdi
leaq -0x94(%rbp), %rsi
callq 0x46a0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x43f0
andl $0x0, -0x30(%rbp)
movl -0x490(%rbp), %r13d
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
cmpb $0x0, -0x29(%rbp)
je 0x4f75
andl $0x0, -0x88(%rbp)
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x5776
andl $0x0, -0x88(%rbp)
xorl %eax, %eax
pushq $0x1
popq %r14
pushq $0x4
popq %r15
movl %eax, %eax
cmpq %rax, -0xc8(%rbp)
jbe 0x4fc4
movq -0xd0(%rbp), %rcx
movb (%rcx,%rax), %dl
movl %r14d, %ecx
testb %dl, %dl
jns 0x4f59
movl %r15d, %ecx
cmpb $-0x11, %dl
ja 0x4f59
cmpb $-0x20, %dl
pushq $0x2
popq %rcx
sbbl $-0x1, %ecx
addl %ecx, %eax
movl %eax, -0x88(%rbp)
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x5750
movl -0x88(%rbp), %eax
jmp 0x4f28
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4460
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x4f8e
movq %rax, -0x48(%rbp)
xorl %eax, %eax
leaq -0x34(%rbp), %r14
movl %eax, -0x34(%rbp)
movl %eax, %eax
movq -0x80(%rbp), %rcx
incq %rcx
cmpq %rax, %rcx
jbe 0x4fb8
leaq -0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x5750
movl -0x34(%rbp), %eax
incl %eax
jmp 0x4f94
movq %r12, %rdi
callq 0x46b8
pushq $0x1
popq %r14
movl %r14d, %r15d
leal -0x1(%r15), %ecx
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
decq %rdx
cmpq %rcx, %rdx
jbe 0x5030
movl (%rax,%rcx,4), %edx
movl %r15d, %ecx
movl (%rax,%rcx,4), %ecx
subl %edx, %ecx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4360
leaq -0x448(%rbp), %rdi
movq %r12, %rsi
callq 0x54b8
movl (%rax), %r14d
movq %r12, %rdi
callq 0x46b8
incl %r15d
leaq -0x490(%rbp), %rdi
movl %r13d, %esi
movl %r14d, %edx
leaq -0x30(%rbp), %rcx
callq 0x9a00
movl %eax, %r13d
jmp 0x4fc7
cvtss2sd -0x30(%rbp), %xmm0
mulsd 0x1efcb(%rip), %xmm0    # 0x24008
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
cvtss2sd -0x60(%rbp), %xmm1
cvtss2sd %xmm0, %xmm0
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x110(%rbp)
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
xorpd 0x1efa6(%rip), %xmm0    # 0x24010
movapd %xmm0, -0x120(%rbp)
movapd %xmm1, -0x100(%rbp)
minsd %xmm1, %xmm0
callq 0x45f0
callq 0x4660
movsd %xmm0, -0x90(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4460
movss -0x30(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
leaq -0xb0(%rbp), %rdi
movq %r12, %rsi
callq 0x5ef4
leaq -0xf0(%rbp), %r14
leaq -0xa20(%rbp), %r15
movapd -0x120(%rbp), %xmm1
cmpltsd -0x100(%rbp), %xmm1
movapd -0x60(%rbp), %xmm0
andpd %xmm1, %xmm0
andnpd -0x110(%rbp), %xmm1
orpd %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x60(%rbp)
movq %r12, %rdi
callq 0x46b8
leaq -0x50(%rbp), %rdi
callq 0x5aa4
movq %r15, %rdi
callq 0x42b0
jmp 0x4e9e
movq 0x30ec6(%rip), %rdi     # 0x35fe8
leaq 0x1ef6f(%rip), %rsi     # 0x24098
callq 0x4340
movq %rax, %rdi
callq 0x4180
jmp 0x5153
movq 0x30ea9(%rip), %rdi     # 0x35fe8
leaq 0x1ef7f(%rip), %rsi     # 0x240c5
callq 0x4340
movq %rax, %rdi
callq 0x4180
pushq $0x1
popq %rdi
callq 0x43c0
movq -0xb0(%rbp), %rbx
leaq -0x898(%rbp), %r14
leaq 0x1f2c6(%rip), %r15     # 0x24436
cmpq -0xa8(%rbp), %rbx
je 0x51af
movss 0x20(%rbx), %xmm0
subss -0x60(%rbp), %xmm0
movq %r14, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4340
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x4180
addq $0x28, %rbx
jmp 0x5170
leaq -0x698(%rbp), %rdi
callq 0x40a0
leaq -0x898(%rbp), %rdi
callq 0x4470
xorl %edi, %edi
callq 0x43c0
jmp 0x51da
jmp 0x51da
jmp 0x51ea
jmp 0x51fb
jmp 0x51fb
jmp 0x51da
movq %rax, %rbx
jmp 0x5213
movq %rax, %rbx
jmp 0x5207
jmp 0x51ea
jmp 0x51fb
jmp 0x51fb
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x46b8
jmp 0x51fe
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x5aa4
leaq -0xa20(%rbp), %rdi
callq 0x42b0
leaq -0xb0(%rbp), %rdi
callq 0x5522
leaq -0xd0(%rbp), %rdi
callq 0x46b8
leaq -0xf0(%rbp), %rdi
callq 0x46b8
jmp 0x523c
movq %rax, %rbx
leaq -0x898(%rbp), %rdi
callq 0x44c0
jmp 0x524f
jmp 0x524c
movq %rax, %rbx
leaq -0x698(%rbp), %rdi
callq 0x40d0
jmp 0x5273
jmp 0x5270
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x46b8
jmp 0x5273
movq %rax, %rbx
leaq -0x490(%rbp), %rdi
callq 0x554e
jmp 0x5297
jmp 0x5294
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x46b8
jmp 0x5297
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
callq 0x46b8
jmp 0x52a8
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
callq 0x46b8
jmp 0x52b9
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
jmp 0x5365
jmp 0x536c
jmp 0x536c
movq %rax, %rbx
leaq -0x1e0(%rbp), %rdi
callq 0x46b8
jmp 0x52e3
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
callq 0x46b8
jmp 0x52f4
movq %rax, %rbx
leaq -0x220(%rbp), %rdi
callq 0x46b8
jmp 0x5305
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
callq 0x46b8
jmp 0x5318
jmp 0x5315
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x46b8
jmp 0x5329
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x46b8
jmp 0x533a
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
callq 0x46b8
jmp 0x534b
movq %rax, %rbx
leaq -0x2c0(%rbp), %rdi
callq 0x46b8
jmp 0x535e
jmp 0x535b
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x46b8
jmp 0x536f
movq %rax, %rbx
leaq -0x398(%rbp), %rdi
callq 0x55a8
movq %rbx, %rdi
callq 0x45e0
nop