// File: 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:
subq $0x2118, %rsp           # imm = 0x2118
movq %rdi, 0x20(%rsp)
movq %rdi, 0x2108(%rsp)
movl %esi, 0x2104(%rsp)
movl %edx, 0x2100(%rsp)
movq %rcx, 0x20f8(%rsp)
movq %r8, 0x20f0(%rsp)
callq 0x5200
movq %rax, %rdi
leaq 0x1831d(%rip), %rsi     # 0x2398e
callq 0x56b0
movq 0x20(%rsp), %rdi
movq %rax, 0x20e8(%rsp)
callq 0x5200
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x53b0
movq 0x20(%rsp), %rdi
movl %eax, 0x20e4(%rsp)
callq 0x5200
movq %rax, %rdi
callq 0x9ac0
movq %rax, 0x2047c(%rip)     # 0x2bb30
cmpq $0x0, 0x20e8(%rsp)
je 0xb6c9
cmpl $0x0, 0x20e4(%rsp)
jne 0xb6d9
movl $0xffffffff, 0x2114(%rsp) # imm = 0xFFFFFFFF
jmp 0xba5f
movb $0x1, 0x20458(%rip)     # 0x2bb38
movl $0x0, 0x20e0(%rsp)
movl $0x0, 0x20dc(%rsp)
cmpq $0x0, 0x2042a(%rip)     # 0x2bb28
je 0xb713
movq 0x20421(%rip), %rdi     # 0x2bb28
movq 0x20422(%rip), %rsi     # 0x2bb30
callq 0x52b0
movq 0x20416(%rip), %rsi     # 0x2bb30
movl 0x20e4(%rsp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x5a60
movq %rax, 0x203eb(%rip)     # 0x2bb28
cmpq $0x0, 0x203e3(%rip)     # 0x2bb28
jne 0xb770
movq 0x20e8(%rsp), %rdi
callq 0x5560
movl 0x20e4(%rsp), %edi
callq 0x5b70
movl $0xffffffff, 0x2114(%rsp) # imm = 0xFFFFFFFF
jmp 0xba5f
leaq 0x20379(%rip), %rdi     # 0x2baf0
callq 0xea90
movq 0x20e8(%rsp), %rdi
callq 0x5940
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x1f(%rsp)
jne 0xb7b3
movl 0x2100(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x2100(%rsp)
cmpl $0x0, %eax
setne %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0xb7c0
jmp 0xb961
movb $0x0, 0xd0(%rsp)
leaq 0xd0(%rsp), %rdi
movq 0x20e8(%rsp), %rdx
movl $0x2000, %esi           # imm = 0x2000
callq 0x5980
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0xb7fa
jmp 0xb961
leaq 0xd0(%rsp), %rdi
callq 0x5230
movl %eax, 0xc4(%rsp)
cmpl $0x0, 0xc4(%rsp)
je 0xb845
movl 0xc4(%rsp), %eax
subl $0x1, %eax
cltq
movsbl 0xd0(%rsp,%rax), %eax
cmpl $0xa, %eax
jne 0xb845
movl 0xc4(%rsp), %eax
subl $0x1, %eax
cltq
movb $0x0, 0xd0(%rsp,%rax)
movl 0x20e0(%rsp), %eax
incl %eax
movl %eax, 0x20e0(%rsp)
movl $0x0, 0xc0(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x57f0
xorl %edx, %edx
movl %edx, %esi
leaq 0x90(%rsp), %rdi
callq 0xde90
jmp 0xb880
movq 0x202a1(%rip), %rsi     # 0x2bb28
movslq 0x20dc(%rsp), %rdx
movq %rsp, %rax
leaq 0x90(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x48(%rsp), %rdi
leaq 0xd0(%rsp), %rcx
leaq 0xc0(%rsp), %r8
leaq 0xa0(%rsp), %r9
callq 0xeab0
jmp 0xb8c1
leaq 0x48(%rsp), %rdi
callq 0xeb20
jmp 0xb8cd
movl 0xc4(%rsp), %eax
addl 0x20dc(%rsp), %eax
movl %eax, 0x20dc(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x5a00
testb $0x1, %al
jne 0xb94f
leaq 0xa0(%rsp), %rdi
callq 0xed60
jmp 0xb902
movl 0xc0(%rsp), %esi
leaq 0x201e0(%rip), %rdi     # 0x2baf0
leaq 0xa0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
callq 0xdd50
jmp 0xb927
jmp 0xb94f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x5320
jmp 0xba6e
leaq 0xa0(%rsp), %rdi
callq 0x5320
jmp 0xb77c
movq 0x20e8(%rsp), %rdi
callq 0x5560
movl 0x2104(%rsp), %esi
leaq 0x20174(%rip), %rdi     # 0x2baf0
callq 0xdec0
leaq 0x30(%rsp), %rdi
callq 0xd3d0
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x2014b(%rip), %rdi     # 0x2baf0
callq 0xe000
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0xba09
movq 0x28(%rsp), %rsi
leaq 0x2012d(%rip), %rdi     # 0x2baf0
callq 0xe110
movq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0xe360
jmp 0xb9d7
jmp 0xb9d9
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0xb994
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xd550
jmp 0xba6e
leaq 0x200f8(%rip), %rdi     # 0x2bb08
leaq 0x30(%rsp), %rsi
callq 0xd460
jmp 0xba1c
leaq 0x30(%rsp), %rdi
callq 0xd4e0
movl %eax, %ecx
movq 0x20f8(%rsp), %rax
movl %ecx, (%rax)
movl 0x20e0(%rsp), %ecx
movq 0x20f0(%rsp), %rax
movl %ecx, (%rax)
movb $0x0, 0x200ee(%rip)     # 0x2bb38
leaq 0x30(%rsp), %rdi
callq 0xd550
movl $0x0, 0x2114(%rsp)
movl 0x2114(%rsp), %eax
addq $0x2118, %rsp           # imm = 0x2118
retq
movq 0x88(%rsp), %rdi
callq 0x5a40
nopl (%rax,%rax)