// File: 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:
subq $0xf4308, %rsp          # imm = 0xF4308
movq %rdi, 0xf42f8(%rsp)
movq 0xf42f8(%rsp), %rdi
leaq 0xa4e81(%rip), %rsi     # 0x10781f
callq 0x13240
movq %rax, 0xf42f0(%rsp)
movl $0x20, %edi
callq 0x647b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0x62dc0
jmp 0x629c7
movq 0x40(%rsp), %rax
movq %rax, 0xf42e8(%rsp)
cmpq $0x0, 0xf42f0(%rsp)
je 0x62d33
leaq 0x70(%rsp), %rdi
callq 0xd2dc0
movl $0x0, 0x6c(%rsp)
movq 0xf42f0(%rsp), %rdx
leaq 0x90(%rsp), %rdi
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x132c0
movq %rax, 0x38(%rsp)
jmp 0x62a16
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
je 0x62bc6
leaq 0x90(%rsp), %rdi
callq 0x13370
cmpq $0x5, %rax
ja 0x62a76
jmp 0x629f5
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf42e0(%rsp)
movl %eax, 0xf42dc(%rsp)
callq 0x63910
jmp 0x62d75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf42e0(%rsp)
movl %eax, 0xf42dc(%rsp)
jmp 0x62d27
leaq 0x90(%rsp), %rdi
callq 0x13370
movq %rax, %rcx
andq $0x3f, %rcx
movl $0x40, %eax
subq %rcx, %rax
movl %eax, 0x68(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x13370
movl %eax, 0x64(%rsp)
cmpl $0x0, 0x68(%rsp)
jle 0x62ad2
movl 0x64(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x64(%rsp)
cltq
movb $0x64, 0x90(%rsp,%rax)
movl 0x68(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x62aa7
movslq 0x64(%rsp), %rax
movb $0x0, 0x90(%rsp,%rax)
leaq 0x90(%rsp), %rdi
callq 0x13370
cmpq $0x5, %rax
jbe 0x62bc1
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xd4680
jmp 0x62b0a
movl $0x20, %edi
callq 0x647b0
movq %rax, 0x30(%rsp)
jmp 0x62b1b
movq 0x30(%rsp), %rdi
callq 0x62d90
jmp 0x62b27
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
movslq 0x6c(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x8(%rax)
leaq 0x90(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x13370
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x10(%rax)
callq 0x13370
movq %rax, %rcx
movl 0x6c(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x6c(%rsp)
movq 0xf42e8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x62df0
jmp 0x62b88
movq 0xf42e8(%rsp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
jmp 0x62bc1
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf42e0(%rsp)
movl %eax, 0xf42dc(%rsp)
callq 0x63910
jmp 0x62d27
jmp 0x629f5
leaq 0x70(%rsp), %rdi
callq 0xd30f0
movq %rax, %rdx
shlq $0x0, %rdx
leaq 0x50(%rsp), %rdi
movl $0x40, %esi
callq 0x13130
cmpl $0x0, %eax
je 0x62c1a
leaq 0xdb88e(%rip), %rdi     # 0x13e480
leaq 0xa04a5(%rip), %rsi     # 0x10309e
callq 0xccb00
movq %rax, 0x20(%rsp)
jmp 0x62c05
movq 0x20(%rsp), %rdi
leaq 0x697cf(%rip), %rsi     # 0xcc3e0
callq 0xcb5f0
jmp 0x62c18
jmp 0x62c1a
movl $0x0, 0x4c(%rsp)
movslq 0x4c(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xf42e8(%rsp), %rdi
callq 0x62e60
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x62cfc
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xf42e8(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x62e80
movq 0x10(%rsp), %rcx
movq (%rax), %rax
movq %rcx, (%rax)
cmpl $0x0, 0x4c(%rsp)
jne 0x62cab
movq 0xf42e8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x62e80
movq (%rax), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
callq 0xd4910
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x135c0
movslq 0x4c(%rsp), %rax
movq %rax, (%rsp)
movq 0xf42e8(%rsp), %rdi
callq 0x62e60
movq %rax, %rcx
movq (%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x62cea
movq 0xf42e8(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x62e80
movq (%rax), %rax
movb $0x0, 0x18(%rax)
jmp 0x62cec
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x62c22
movq 0xf42f0(%rsp), %rdi
callq 0x13600
jmp 0x62d0b
movq 0xf42e8(%rsp), %rax
movq %rax, 0xf4300(%rsp)
leaq 0x70(%rsp), %rdi
callq 0xd2ed0
jmp 0x62d65
leaq 0x70(%rsp), %rdi
callq 0xd2ed0
jmp 0x62d75
leaq 0xdb746(%rip), %rdi     # 0x13e480
leaq 0xa0398(%rip), %rsi     # 0x1030d9
callq 0xccb00
movq %rax, %rdi
leaq 0x69690(%rip), %rsi     # 0xcc3e0
callq 0xcb5f0
movq 0xf42e8(%rsp), %rax
movq %rax, 0xf4300(%rsp)
movq 0xf4300(%rsp), %rax
addq $0xf4308, %rsp          # imm = 0xF4308
retq
movq 0xf42e0(%rsp), %rdi
callq 0x13710
nopw %cs:(%rax,%rax)
nopl (%rax)