// File: /workspace/github_cpp_makefile_star2_O3/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 0xa3d15(%rip), %rsi     # 0xc98df
callq 0x13240
movq %rax, %r14
movl $0x20, %edi
callq 0x26e80
movq %rax, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rax)
testq %r14, %r14
je 0x25db1
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
xorl %r12d, %r12d
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0xf4240, %esi          # imm = 0xF4240
movq %r14, %rdx
vzeroupper
callq 0x132c0
testq %rax, %rax
je 0x25ce3
movq %r15, %rdi
callq 0x13370
cmpq $0x6, %rax
jb 0x25c0e
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 0x25c0e
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x96cf0
movl $0x20, %edi
callq 0x26e80
movq %rax, %rbp
movq $0x0, (%rax)
movb $0x1, 0x18(%rax)
movq %rax, (%rsp)
movslq %r12d, %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 0x25cca
movq %rbp, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x25cd5
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x25e2a
addl 0x8(%rsp), %r12d
incq 0x18(%rbx)
jmp 0x25c0e
movq 0x18(%rsp), %rdx
movq %rsp, %rdi
movl $0x40, %esi
callq 0x13130
testl %eax, %eax
je 0x25d3f
leaq 0xd9780(%rip), %r15     # 0xff480
leaq 0x9f450(%rip), %rsi     # 0xc5157
movl $0x3a, %edx
movq %r15, %rdi
callq 0x8ec40
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x6fd90
movsbl %al, %esi
leaq 0xd974e(%rip), %rdi     # 0xff480
callq 0x8e740
movq %rax, %rdi
callq 0x8dd10
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq %rax, %rcx
je 0x25d98
xorl %r15d, %r15d
movq (%rsp), %rdx
movq (%rax,%r15,8), %rsi
movq %rdx, (%rsi)
testq %r15, %r15
jne 0x25d75
movq (%rax), %rax
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
callq 0x135d0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
leaq -0x1(%rdx), %rsi
cmpq %r15, %rsi
jbe 0x25d90
movq (%rax,%r15,8), %rsi
movb $0x0, 0x18(%rsi)
incq %r15
cmpq %r15, %rdx
ja 0x25d4e
movq %r14, %rdi
callq 0x13610
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x25df6
callq 0x26180
jmp 0x25df6
leaq 0xd96c8(%rip), %r14     # 0xff480
leaq 0x9f3d3(%rip), %rsi     # 0xc5192
movl $0x13, %edx
movq %r14, %rdi
vzeroupper
callq 0x8ec40
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x6fd90
movsbl %al, %esi
movq %r14, %rdi
callq 0x8e740
movq %rax, %rdi
callq 0x8dd10
movq %rbx, %rax
addq $0xf4278, %rsp          # imm = 0xF4278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x25e0f
jmp 0x25e0f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x25e21
callq 0x26180
movq %rbx, %rdi
callq 0x13720
nop