// File: /workspace/github_cpp_makefile_star2_O2/AutomataLab[P]JSONSki/src/RecordLoader.cpp
// Function: RecordLoader::loadRecords(char*)
RecordSet* RecordLoader::loadRecords(char* file_path) {
    FILE *fp = fopen(file_path, "r");
    RecordSet* rs = new RecordSet();
    if (fp) {
        char line[MAX_RECORD_SIZE];
        string str;
        int start_pos = 0;
        while (fgets(line, sizeof(line), fp) != NULL) {
            if (strlen(line) <= MIN_RECORD_SIZE) continue;
            int remain = 64 - strlen(line) % 64;
            int top = strlen(line);
            while (remain > 0) {
                line[top++] = 'd';
                --remain;
            }
            line[top] = '\0';
            if (strlen(line) > MIN_RECORD_SIZE) {
                // concating a sequence of record texts into one single string generates the best performance for indexing and querying
                str.append(line);
                Record* record = new Record();
                record->rec_start_pos = start_pos;
                record->rec_length = strlen(line);
                start_pos += strlen(line);
                rs->recs.push_back(record);
                ++rs->num_recs;
            }
        }
        void* p;
        if(posix_memalign(&p, 64, str.size()*sizeof(char)) != 0) {
            cout<<"Fail to allocate memory space for records from input file."<<endl;
        }
        for (int i = 0; i < rs->recs.size(); ++i) {
            // all record objects points to the same input text which contacts a sequence of JSON records
            rs->recs[i]->text = (char*) p;
            if (i == 0) strcpy(rs->recs[0]->text, str.c_str());
            // deconstructor in the last record object can delete input text
            if (i < rs->recs.size() - 1) rs->recs[i]->can_delete_text = false;
        }
        fclose(fp);
        return rs;
    }
    cout<<"Fail open the file."<<endl;
    return rs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf4288, %rsp          # imm = 0xF4288
leaq 0xa3ad3(%rip), %rsi     # 0xc889f
callq 0x13240
movq %rax, %r14
pushq $0x20
popq %rdi
callq 0x25f30
movq %rax, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rax)
testq %r14, %r14
je 0x24f86
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %ebp, %ebp
leaq 0x40(%rsp), %r15
pushq $0x40
popq %r13
movabsq $0x100000000, %r12      # imm = 0x100000000
movq %r15, %rdi
movl $0xf4240, %esi          # imm = 0xF4240
movq %r14, %rdx
vzeroupper
callq 0x132c0
testq %rax, %rax
je 0x24ee1
movq %r15, %rdi
callq 0x13370
cmpq $0x6, %rax
jb 0x24e16
movslq %eax, %rdx
movl %eax, %esi
andl $0x3f, %esi
movl %r13d, %ecx
subl %esi, %ecx
shlq $0x20, %rax
addq %rsp, %rdx
addq $0x40, %rdx
testl %ecx, %ecx
jle 0x24e69
movb $0x64, (%rdx)
decl %ecx
addq %r12, %rax
incq %rdx
jmp 0x24e58
sarq $0x20, %rax
movb $0x0, 0x40(%rsp,%rax)
movq %r15, %rdi
callq 0x13370
cmpq $0x6, %rax
jb 0x24e16
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x95da0
pushq $0x20
popq %rdi
callq 0x25f30
andq $0x0, (%rax)
movb $0x1, 0x18(%rax)
movq %rax, 0x8(%rsp)
movq %rax, %rcx
movq %rax, 0x18(%rsp)
movslq %ebp, %rax
movq %rax, 0x8(%rcx)
movq %r15, %rdi
callq 0x13370
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x24fd2
addl 0x10(%rsp), %ebp
incq 0x18(%rbx)
jmp 0x24e16
movq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rdi
pushq $0x40
popq %rsi
callq 0x13130
testl %eax, %eax
je 0x24f12
leaq 0xda582(%rip), %rdi     # 0xff480
leaq 0x9f228(%rip), %rsi     # 0xc412d
callq 0x8e220
movq %rax, %rdi
callq 0x8db00
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
xorl %r15d, %r15d
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x24f72
movq 0x8(%rsp), %rsi
movq (%rax,%r15,8), %rdi
movq %rsi, (%rdi)
testq %r15, %r15
jne 0x24f5d
movq (%rax), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0x135d0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
decq %rdx
cmpq %r15, %rdx
jbe 0x24f6d
movq (%rax,%r15,8), %rdx
movb $0x0, 0x18(%rdx)
incq %r15
jmp 0x24f1c
movq %r14, %rdi
callq 0x13610
leaq 0x20(%rsp), %rdi
callq 0x945f0
jmp 0x24fa4
leaq 0xda4f3(%rip), %rdi     # 0xff480
leaq 0x9f1d4(%rip), %rsi     # 0xc4168
vzeroupper
callq 0x8e220
movq %rax, %rdi
callq 0x8db00
movq %rbx, %rax
addq $0xf4288, %rsp          # imm = 0xF4288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24fbd
jmp 0x24fbd
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x945f0
movq %rbx, %rdi
callq 0x13720