// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %esi, 0x18(%rsp)
movq %rdi, %rbx
movq (%rdi), %rdi
leaq 0xc1d4(%rip), %rsi      # 0x16986
callq 0x55c0
movq %rax, 0x10(%rsp)
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 0xa7e9
movq 0xd0(%rsp), %r14
movq %r14, 0x121c0(%rip)     # 0x1c9b0
cmpq $0x0, 0x10(%rsp)
sete %cl
testl %ebp, %ebp
sete %dl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0xaaf6
movb $0x1, 0x121a6(%rip)     # 0x1c9b8
movq 0x1218f(%rip), %rdi     # 0x1c9a8
testq %rdi, %rdi
je 0xa826
movq %r14, %rsi
callq 0x5280
movq 0x12183(%rip), %rsi     # 0x1c9b0
xorl %edi, %edi
movl $0x1, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x58f0
movq %rax, 0x1215d(%rip)     # 0x1c9a8
movq %rax, 0x88(%rsp)
testq %rax, %rax
je 0xaad8
movq %r12, 0x78(%rsp)
movq %r15, 0x80(%rsp)
movq 0x12100(%rip), %rsi     # 0x1c970
leaq 0x120f9(%rip), %rdi     # 0x1c970
callq 0xe570
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r12
leaq 0xa0(%rsp), %r14
movq %r12, %rdi
callq 0x5800
testl %eax, %eax
jne 0xa9ef
subl $0x1, %r13d
jb 0xa9ef
movb $0x0, 0xa0(%rsp)
movq %r14, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r12, %rdx
callq 0x5840
movq %rax, %r15
testq %rax, %rax
je 0xa9e6
movq %r14, %rdi
callq 0x5210
movq %rax, %rbp
testl %ebp, %ebp
je 0xa8f8
movl %ebp, %eax
decl %eax
cltq
cmpb $0xa, 0xa0(%rsp,%rax)
jne 0xa8f8
movb $0x0, 0xa0(%rsp,%rax)
xorl %edx, %edx
movl %edx, 0xc(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq %rdx, 0x90(%rsp)
movl %edx, 0x98(%rsp)
movq 0x12080(%rip), %rax     # 0x1c9a8
movslq 0x8(%rsp), %rcx
movl %edx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %r14, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
leaq 0xc(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x40(%rsp), %rdi
callq 0xc3fa
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0xa9bc
movl %ebx, 0x1c(%rsp)
movq 0x20(%rsp), %r12
xorl %ebx, %ebx
movsbl (%r12,%rbx), %edi
callq 0x5710
movb %al, (%r12,%rbx)
incq %rbx
cmpq %rbx, %r14
jne 0xa980
movl 0xc(%rsp), %esi
leaq 0x11fcf(%rip), %rdi     # 0x1c970
leaq 0x20(%rsp), %rdx
leaq 0x90(%rsp), %rcx
callq 0xbf1c
movl 0x1c(%rsp), %ebx
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xa9d8
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x54c0
incl %ebx
addl %ebp, 0x8(%rsp)
leaq 0xa0(%rsp), %r14
testq %r15, %r15
jne 0xa893
movl %ebx, %ebp
movq %r12, %rdi
callq 0x54a0
leaq 0x11f70(%rip), %rdi     # 0x1c970
movl 0x18(%rsp), %esi
callq 0xbfe2
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x11f4f(%rip), %rdx     # 0x1c970
cmpq %rdx, 0x11f50(%rip)     # 0x1c978
je 0xaa8a
xorl %r14d, %r14d
leaq 0x40(%rsp), %rbx
movabsq $0x6db6db6db6db6db7, %r15 # imm = 0x6DB6DB6DB6DB6DB7
xorl %r12d, %r12d
addq %r14, %rdx
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0xaa5d
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rsp)
jmp 0xaa65
movq %rbx, %rdi
callq 0xe494
incq %r12
movq 0x11f09(%rip), %rax     # 0x1c978
movq 0x11efa(%rip), %rdx     # 0x1c970
subq %rdx, %rax
sarq $0x3, %rax
imulq %r15, %rax
addq $0x38, %r14
cmpq %rax, %r12
jb 0xaa3f
leaq 0x11ef7(%rip), %rdi     # 0x1c988
leaq 0x40(%rsp), %rsi
callq 0xbbce
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
subq %rdi, %rax
shrq $0x2, %rax
movq 0x78(%rsp), %rcx
movl %eax, (%rcx)
movq 0x80(%rsp), %rax
movl %ebp, (%rax)
movb $0x0, 0x11ef4(%rip)     # 0x1c9b8
testq %rdi, %rdi
je 0xaae9
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x54c0
jmp 0xaae9
movq 0x10(%rsp), %rdi
callq 0x54a0
movl %ebp, %edi
callq 0x59c0
xorl %eax, %eax
cmpq $0x1, 0x88(%rsp)
sbbl %eax, %eax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xab28
jmp 0xab0c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xab42
movq 0x30(%rsp), %rsi
incq %rsi
jmp 0xab3d
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0xab42
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x54c0
movq %rbx, %rdi
callq 0x58e0