// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movq %rcx, %rbp
movl %edx, 0xc(%rsp)
movl %esi, %r15d
movq %rdi, %r14
movq (%rdi), %rdi
leaq 0xb3a6(%rip), %rsi      # 0x13983
callq 0x55a0
movq %rax, %r12
movq (%r14), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5320
movl %eax, %ebx
movq (%r14), %rdi
callq 0x76da
movq %rax, 0x123be(%rip)     # 0x1a9c0
testq %r12, %r12
sete %cl
testl %ebx, %ebx
sete %dl
orb %cl, %dl
jne 0x879f
movb $0x1, 0x123ac(%rip)     # 0x1a9c8
movq 0x12395(%rip), %rdi     # 0x1a9b8
testq %rdi, %rdi
movl %r15d, 0x14(%rsp)
je 0x863c
movq %rax, %rsi
callq 0x5280
movq 0x12384(%rip), %rax     # 0x1a9c0
xorl %r15d, %r15d
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %rax, %rsi
movl %ecx, %edx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x58f0
movq %rax, %r14
movq %rax, 0x1235a(%rip)     # 0x1a9b8
testq %rax, %rax
je 0x87a7
movq %r14, 0x18(%rsp)
movq %rbp, 0x28(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x12303(%rip), %rdi     # 0x1a980
callq 0x9b7e
leaq 0xa0(%rsp), %r14
leaq 0x40(%rsp), %rbp
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x5800
testl %eax, %eax
jne 0x87c2
cmpl %r15d, 0xc(%rsp)
je 0x87bb
movb $0x0, 0xa0(%rsp)
movq %r14, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r12, %rdx
callq 0x5840
testq %rax, %rax
je 0x87c2
movq %r14, %rdi
callq 0x51e0
movq %rax, %rbx
testl %ebx, %ebx
je 0x86f5
movl %ebx, %eax
decl %eax
cltq
cmpb $0xa, 0xa0(%rsp,%rax)
jne 0x86f5
movb $0x0, 0xa0(%rsp,%rax)
andl $0x0, 0x10(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
andq $0x0, 0x48(%rsp)
andq $0x0, 0x30(%rsp)
andl $0x0, 0x38(%rsp)
movb $0x0, 0x50(%rsp)
andl $0x0, 0x60(%rsp)
movq 0x12292(%rip), %rax     # 0x1a9b8
movslq %r13d, %rcx
movq %rax, 0x68(%rsp)
movq %r14, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %rbp, 0x88(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x9b86
cmpq $0x0, 0x48(%rsp)
je 0x878c
movq %rbp, %rdi
callq 0x9cd9
movl 0x10(%rsp), %esi
leaq 0x12201(%rip), %rdi     # 0x1a980
movq %rbp, %rdx
leaq 0x30(%rsp), %rcx
callq 0x9790
addl %ebx, %r13d
movq %rbp, %rdi
callq 0x5210
incl %r15d
jmp 0x8692
pushq $-0x1
popq %rax
jmp 0x886d
movq %r12, %rdi
callq 0x5490
movl %ebx, %edi
callq 0x59e0
jmp 0x8865
movl 0xc(%rsp), %eax
movl %eax, %r15d
movq %r12, %rdi
callq 0x5490
leaq 0x121af(%rip), %rdi     # 0x1a980
movl 0x14(%rsp), %esi
callq 0x9828
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rbx
andq $0x0, 0x10(%rbx)
movaps %xmm0, (%rbx)
xorl %r14d, %r14d
pushq $0x38
popq %r12
xorl %r13d, %r13d
movq 0x28(%rsp), %rbp
movq 0x12188(%rip), %rax     # 0x1a988
movq 0x12179(%rip), %rsi     # 0x1a980
subq %rsi, %rax
cqto
idivq %r12
cmpq %rax, %r13
jae 0x8828
addq %r14, %rsi
movq %rbx, %rdi
callq 0x98be
incq %r13
addq $0x38, %r14
jmp 0x87f9
leaq 0x12169(%rip), %rdi     # 0x1a998
leaq 0x60(%rsp), %rsi
callq 0x94b8
leaq 0x60(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x2, %rax
movl %eax, (%rbp)
movq 0x20(%rsp), %rax
movl %r15d, (%rax)
movb $0x0, 0x1216d(%rip)     # 0x1a9c8
callq 0xb1a2
movq 0x18(%rsp), %r14
xorl %eax, %eax
cmpq $0x1, %r14
sbbl %eax, %eax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8892
jmp 0x8883
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5210
jmp 0x889f
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xb1a2
movq %rbx, %rdi
callq 0x58e0