// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf4278, %rsp          # imm = 0xF4278
leaq 0xa2e9f(%rip), %rsi     # 0xc88df
callq 0x13240
movq %rax, %r14
movl $0x20, %edi
callq 0x26d00
movq %rax, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rax)
testq %r14, %r14
je 0x25c39
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0xf4240, %esi          # imm = 0xF4240
movq %r14, %rdx
vzeroupper
callq 0x132c0
testq %rax, %rax
je 0x25b64
movq %r15, %rdi
callq 0x13370
cmpq $0x6, %rax
jb 0x25a86
movslq %eax, %rbp
andl $0x3f, %eax
movl $0x40, %edx
subq %rax, %rdx
leaq (%rsp,%rbp), %rdi
addq $0x30, %rdi
movl $0x64, %esi
callq 0x130e0
movl %ebp, %eax
orl $-0x40, %eax
subl %eax, %ebp
movslq %ebp, %rax
movb $0x0, 0x30(%rsp,%rax)
movq %r15, %rdi
callq 0x13370
cmpq $0x6, %rax
jb 0x25a86
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x96b70
movl $0x20, %edi
callq 0x26d00
movq %rax, %rbp
vxorps %xmm0, %xmm0, %xmm0
vmovups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movb $0x1, 0x18(%rax)
movq %rax, (%rsp)
movslq %r13d, %rax
movq %rax, 0x8(%rbp)
movq %r15, %rdi
callq 0x13370
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x25b4b
movq %rbp, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x25b56
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x25cb2
addl 0x8(%rsp), %r13d
incq 0x18(%rbx)
jmp 0x25a86
movq 0x18(%rsp), %rdx
movq %rsp, %rdi
movl $0x40, %esi
callq 0x13130
testl %eax, %eax
je 0x25bc0
leaq 0xd88ff(%rip), %r15     # 0xfe480
leaq 0x9e5e5(%rip), %rsi     # 0xc416d
movl $0x3a, %edx
movq %r15, %rdi
callq 0x8eac0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x6fc10
movsbl %al, %esi
leaq 0xd88cd(%rip), %rdi     # 0xfe480
callq 0x8e5c0
movq %rax, %rdi
callq 0x8db90
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x25c20
xorl %r15d, %r15d
movq (%rsp), %rcx
movq (%rax,%r15,8), %rdx
movq %rcx, (%rdx)
testq %r15, %r15
jne 0x25bec
movq (%rax), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
callq 0x135d0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
decq %rcx
cmpq %r15, %rcx
jbe 0x25c0a
movq (%rax,%r15,8), %rax
movb $0x0, 0x18(%rax)
incq %r15
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
ja 0x25bcc
movq %r14, %rdi
callq 0x13610
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x25c7e
callq 0x26000
jmp 0x25c7e
leaq 0xd8840(%rip), %r14     # 0xfe480
leaq 0x9e561(%rip), %rsi     # 0xc41a8
movl $0x13, %edx
movq %r14, %rdi
vzeroupper
callq 0x8eac0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x6fc10
movsbl %al, %esi
movq %r14, %rdi
callq 0x8e5c0
movq %rax, %rdi
callq 0x8db90
movq %rbx, %rax
addq $0xf4278, %rsp          # imm = 0xF4278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x25c97
jmp 0x25c97
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x25ca9
callq 0x26000
movq %rbx, %rdi
callq 0x13720
nop