// File: /workspace/github_cpp_makefile_star2_O3/nilnilnil[P]autocomplete/src/main.cpp
// Function: do_import(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, unsigned int, int&, int&)
int
do_import(std::string file, int sorted, uint_t limit, 
          int &rnadded, int &rnlines) {
#if defined USE_CXX_IO
    std::ifstream fin(file.c_str());
#else
    FILE *fin = fopen(file.c_str(), "r");
#endif

    int fd = open(file.c_str(), O_RDONLY);

    // Potential race condition + not checking for return value
    if_length = file_size(file.c_str());

    DCERR("handle_import::file:"<<file<<endl);

    if (!fin || !fd) {
        return -IMPORT_FILE_NOT_FOUND;
    }
    else {
        building = true;
        int nlines = 0;
        int foffset = 0;

        if (if_mmap_addr) {
            munmap(if_mmap_addr, if_length);
        }

        // mmap() the input file in
        if_mmap_addr = (char*)mmap(NULL, if_length, PROT_READ, MAP_SHARED, fd, 0);
        if (!if_mmap_addr) {
            fclose(fin);
            close(fd);
            return -IMPORT_FILE_NOT_FOUND;
        }

        pm.repr.clear();
        char buff[INPUT_LINE_SIZE];

        while (
#if defined USE_CXX_IO
               fin
#else
            !feof(fin)
#endif
               && limit--) {

            buff[0] = '\0';

#if defined USE_CXX_IO
            fin.getline(buff, INPUT_LINE_SIZE);
            const int llen = fin.gcount();
            buff[INPUT_LINE_SIZE - 1] = '\0';
#else
            char *got = fgets(buff, INPUT_LINE_SIZE, fin);
            if (!got) {
                break;
            }
            const int llen = strlen(buff);
            if (llen && buff[llen-1] == '\n') {
                buff[llen-1] = '\0';
            }
#endif

            ++nlines;

            int weight = 0;
            std::string phrase;
            StringProxy snippet;
            InputLineParser(if_mmap_addr, foffset, buff, &weight, &phrase, &snippet).start_parsing();

            foffset += llen;

            if (!phrase.empty()) {
                str_lowercase(phrase);
                DCERR("Adding: "<<weight<<", "<<phrase<<", "<<std::string(snippet)<<endl);
                pm.insert(weight, phrase, snippet);
            }
        }

        fclose(fin);
        pm.finalize(sorted);
        vui_t weights;
        for (size_t i = 0; i < pm.repr.size(); ++i) {
            weights.push_back(pm.repr[i].weight);
        }
        st.initialize(weights);

        rnadded = weights.size();
        rnlines = nlines;

        building = false;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20a8, %rsp           # imm = 0x20A8
movq %r8, %r15
movq %rcx, %r12
movl %edx, 0x4(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movq (%rdi), %rdi
leaq 0xc1de(%rip), %rsi      # 0x16983
callq 0x55c0
movq %rax, %r13
movq (%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x5330
movl %eax, %ebp
movq (%rbx), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x5420
testl %eax, %eax
js 0xa7da
movq 0xd0(%rsp), %r14
movq %r14, 0x121cf(%rip)     # 0x1c9b0
testq %r13, %r13
sete %cl
testl %ebp, %ebp
sete %dl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0xa990
movb $0x1, 0x121b8(%rip)     # 0x1c9b8
movq 0x121a1(%rip), %rdi     # 0x1c9a8
testq %rdi, %rdi
je 0xa81b
movq %r14, %rsi
callq 0x5280
movq 0x12195(%rip), %r14     # 0x1c9b0
xorl %edi, %edi
movq %r14, %rsi
movl $0x1, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x58f0
movq %rax, %rbx
movq %rax, 0x12169(%rip)     # 0x1c9a8
testq %rax, %rax
je 0xa979
movq %rbx, 0x68(%rsp)
movq %r12, 0x70(%rsp)
movq %r15, 0x78(%rsp)
movq 0x12112(%rip), %rsi     # 0x1c970
leaq 0x1210b(%rip), %rdi     # 0x1c970
callq 0xe4ca
movq %r13, %rdi
callq 0x5800
testl %eax, %eax
movl $0x0, %r15d
leaq 0xa0(%rsp), %r12
je 0xa9a2
movq %r13, %rdi
callq 0x54a0
leaq 0x120d9(%rip), %rdi     # 0x1c970
movl 0xc(%rsp), %esi
callq 0xbfe4
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x120c0(%rip), %rax     # 0x1c978
movq 0x120b1(%rip), %rcx     # 0x1c970
cmpq %rcx, %rax
je 0xa928
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbx
movabsq $0x6db6db6db6db6db7, %r12 # imm = 0x6DB6DB6DB6DB6DB7
xorl %r13d, %r13d
leaq (%rcx,%r14), %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xa8f8
movl (%rdx), %edx
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xa90e
movq %rbx, %rdi
callq 0xe3ee
movq 0x12071(%rip), %rax     # 0x1c978
movq 0x12062(%rip), %rcx     # 0x1c970
incq %r13
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %r12, %rdx
addq $0x38, %r14
cmpq %rdx, %r13
jb 0xa8d9
leaq 0x12059(%rip), %rdi     # 0x1c988
leaq 0x30(%rsp), %rsi
callq 0xbbea
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rax
subq %rdi, %rax
shrq $0x2, %rax
movq 0x70(%rsp), %rcx
movl %eax, (%rcx)
movq 0x78(%rsp), %rax
movl %r15d, (%rax)
movb $0x0, 0x12058(%rip)     # 0x1c9b8
testq %rdi, %rdi
movq 0x68(%rsp), %rbx
je 0xa988
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x54c0
jmp 0xa988
movq %r13, %rdi
callq 0x54a0
movl %ebp, %edi
callq 0x59c0
xorl %eax, %eax
cmpq $0x1, %rbx
sbbl %eax, %eax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r13, 0x80(%rsp)
subl $0x1, %r14d
jb 0xab13
movb $0x0, 0xa0(%rsp)
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r13, %rdx
callq 0x5840
testq %rax, %rax
je 0xa888
movq %r12, %rdi
callq 0x5210
movq %rax, %rbp
testl %ebp, %ebp
je 0xaa07
movl %ebp, %ecx
decl %ecx
movslq %ecx, %rcx
cmpb $0xa, 0xa0(%rsp,%rcx)
jne 0xaa07
movb $0x0, 0xa0(%rsp,%rcx)
xorl %edx, %edx
movl %edx, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq %rdx, 0x90(%rsp)
movl %edx, 0x98(%rsp)
movq 0x11f71(%rip), %rax     # 0x1c9a8
movslq %ebx, %rcx
movl %edx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %r12, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xc3f6
movq 0x18(%rsp), %r13
testq %r13, %r13
je 0xaad5
movq %r15, 0x88(%rsp)
movq 0x10(%rsp), %r12
xorl %r15d, %r15d
movsbl (%r12,%r15), %edi
callq 0x5710
movb %al, (%r12,%r15)
incq %r15
cmpq %r15, %r13
jne 0xaa92
movl 0x8(%rsp), %esi
leaq 0x11ebd(%rip), %rdi     # 0x1c970
leaq 0x10(%rsp), %rdx
leaq 0x90(%rsp), %rcx
callq 0xbf1e
movq 0x88(%rsp), %r15
leaq 0xa0(%rsp), %r12
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xaaf1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x54c0
incl %r15d
addl %ebp, %ebx
movq 0x80(%rsp), %r13
movq %r13, %rdi
callq 0x5800
testl %eax, %eax
je 0xa9b4
jmp 0xa888
movl 0x4(%rsp), %eax
movl %eax, %r15d
jmp 0xa888
jmp 0xab3f
jmp 0xab23
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xab59
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0xab54
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xab59
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x54c0
movq %rbx, %rdi
callq 0x58e0