// File: aalto-speech[P]ftk/src/substrings.cc
// Function: get_substrings(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, float, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, float>>>&, bool)
int get_substrings(const string &infname,
                   unsigned int maxlen,
                   map<string,float> &substrs,
                   bool utf8) {

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

    float count;
    string line, word;

    while (getline(infile, line)) {
        stringstream ss(line);
        ss >> count;
        ss >> word;

        vector<unsigned int> char_positions;
        get_character_positions(word, char_positions, utf8);

        for (unsigned int i=0; i<char_positions.size()-1; i++)
            for (unsigned int j=i+1; j<char_positions.size() && j-i <= maxlen; j++) {
                unsigned int start_pos = char_positions[i];
                unsigned int end_pos = char_positions[j];
                substrs[word.substr(start_pos, end_pos-start_pos)] += count;
            }
    }

    infile.close();
    return substrs.size();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movb %cl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x228(%rbp), %rdi
movl $0x8, %edx
callq 0x4650
movq -0x228(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x228(%rbp,%rax), %rdi
callq 0x41e0
movb %al, -0x449(%rbp)
jmp 0x4f72
movb -0x449(%rbp), %al
testb $0x1, %al
jne 0x4f7e
jmp 0x4fd0
movq 0x95063(%rip), %rdi     # 0x99fe8
leaq 0x6507a(%rip), %rsi     # 0x6a006
callq 0x43d0
movq %rax, -0x458(%rbp)
jmp 0x4f9a
movq -0x458(%rbp), %rdi
movq 0x94ff0(%rip), %rsi     # 0x99f98
callq 0x4410
jmp 0x4faf
movl $0x1, %edi
callq 0x4480
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x5346
leaq -0x258(%rbp), %rdi
callq 0x4570
leaq -0x278(%rbp), %rdi
callq 0x4570
leaq -0x228(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x40b0
movq %rax, -0x460(%rbp)
jmp 0x5004
movq -0x460(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x44b0
movb %al, -0x461(%rbp)
jmp 0x5022
movb -0x461(%rbp), %al
testb $0x1, %al
jne 0x5031
jmp 0x52de
movl $0x10, %edi
movl $0x8, %esi
callq 0x62f0
movl %eax, -0x468(%rbp)
jmp 0x5048
movl -0x468(%rbp), %edx
leaq -0x400(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x4640
jmp 0x5063
leaq -0x400(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x40c0
jmp 0x5078
leaq -0x400(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x44c0
jmp 0x508d
leaq -0x418(%rbp), %rdi
movq %rdi, -0x470(%rbp)
callq 0x6310
movq -0x470(%rbp), %rsi
movzbl -0x19(%rbp), %edx
andl $0x1, %edx
leaq -0x278(%rbp), %rdi
callq 0x5360
jmp 0x50bc
movl $0x0, -0x41c(%rbp)
movl -0x41c(%rbp), %eax
movq %rax, -0x478(%rbp)
leaq -0x418(%rbp), %rdi
callq 0x6330
movq %rax, %rcx
movq -0x478(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x52a7
movl -0x41c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x420(%rbp)
movl -0x420(%rbp), %eax
movq %rax, -0x488(%rbp)
leaq -0x418(%rbp), %rdi
callq 0x6330
movq -0x488(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x479(%rbp)
jae 0x514d
movl -0x420(%rbp), %eax
subl -0x41c(%rbp), %eax
cmpl -0xc(%rbp), %eax
setbe %al
movb %al, -0x479(%rbp)
movb -0x479(%rbp), %al
testb $0x1, %al
jne 0x515c
jmp 0x5291
movl -0x41c(%rbp), %eax
movl %eax, %esi
leaq -0x418(%rbp), %rdi
movq %rdi, -0x4a0(%rbp)
callq 0x6350
movq -0x4a0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x424(%rbp)
movl -0x420(%rbp), %eax
movl %eax, %esi
callq 0x6350
movl (%rax), %eax
movl %eax, -0x428(%rbp)
movss -0x238(%rbp), %xmm0
movss %xmm0, -0x494(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x490(%rbp)
movl -0x424(%rbp), %eax
movl %eax, %edx
movl %edx, %ecx
movl -0x428(%rbp), %eax
subl %ecx, %eax
movl %eax, %ecx
leaq -0x448(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x43f0
jmp 0x51df
movq -0x490(%rbp), %rdi
leaq -0x448(%rbp), %rsi
callq 0x6370
movq %rax, -0x4a8(%rbp)
jmp 0x51fb
movq -0x4a8(%rbp), %rax
movss -0x494(%rbp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
leaq -0x448(%rbp), %rdi
callq 0x4828
movl -0x420(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x420(%rbp)
jmp 0x5105
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x532e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x52d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x52c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x4828
jmp 0x52c4
jmp 0x5293
movl -0x41c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x41c(%rbp)
jmp 0x50c6
leaq -0x418(%rbp), %rdi
callq 0x6450
leaq -0x400(%rbp), %rdi
callq 0x4320
jmp 0x4fe8
leaq -0x418(%rbp), %rdi
callq 0x6450
leaq -0x400(%rbp), %rdi
callq 0x4320
jmp 0x532e
leaq -0x228(%rbp), %rdi
callq 0x40d0
jmp 0x52ec
movq -0x18(%rbp), %rdi
callq 0x64b0
movl %eax, -0x4ac(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x4828
leaq -0x258(%rbp), %rdi
callq 0x4828
leaq -0x228(%rbp), %rdi
callq 0x4110
movl -0x4ac(%rbp), %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
leaq -0x278(%rbp), %rdi
callq 0x4828
leaq -0x258(%rbp), %rdi
callq 0x4828
leaq -0x228(%rbp), %rdi
callq 0x4110
movq -0x230(%rbp), %rdi
callq 0x46f0
nop