// File: JohnPapad[P]Mini-Search-Engine/minisearch.cpp
// Function: parse_docs(char**, _IO_FILE*)
bool parse_docs(char **docs, FILE * fp)
{// storing docfile's lines into docs array - checking docfile for errors
    char * line = NULL;
    size_t len = 0;
    ssize_t nchars;
    int line_index = 0;

    while ((nchars = getline(&line, &len, fp)) != -1)
    {
        if ( line[nchars - 1] == '\n')
        { // removing newline character from string
            line[nchars - 1] = '\0';
            nchars--;
        }

        char *content;
        char *line_id_str = strtok_r(line, " \t", &content);

        if(line_id_str == NULL)
        { // checking if there is a line id
            cout<<"-! ERROR line is empty (without id) !-"<<endl;
            free(line);
            return 1;
        }

        int line_id = strtol(line_id_str, NULL, 10);

        if(is_number(line_id_str) == 1)
        { //checking if line id is a number
            if(line_id != line_index)
            { //checking if indexing is correct
                cout<<"-! ERROR at line indexing !-"<<endl;
                free(line);
                return 1;
            }
        }
        else
        {
            cout<<"-! ERROR line id is not a number !-"<<endl;
            free(line);
            return 1;
        }

        docs[line_index] = (char *) malloc((strlen(content) + 1) * sizeof(char));  //plus one for \0
        strcpy(docs[line_index], content);  //storing docfile's line into doc array
        line_index++;
    }

    free(line);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2150
movq %rax, -0x30(%rbp)
cmpq $-0x1, %rax
je 0x2f1c
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x2de7
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x343e(%rip), %rsi      # 0x6230
leaq -0x40(%rbp), %rdx
callq 0x2270
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2e3a
movq 0x61c3(%rip), %rdi      # 0x8fd0
leaq 0x341f(%rip), %rsi      # 0x6233
callq 0x20c0
movq %rax, %rdi
movq 0x619d(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x2130
movb $0x1, -0x1(%rbp)
jmp 0x2f29
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rdi
callq 0x2d00
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x2ea0
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x2e9e
movq 0x615f(%rip), %rdi      # 0x8fd0
leaq 0x33e2(%rip), %rsi      # 0x625a
callq 0x20c0
movq %rax, %rdi
movq 0x6139(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x2130
movb $0x1, -0x1(%rbp)
jmp 0x2f29
jmp 0x2ed1
movq 0x6129(%rip), %rdi      # 0x8fd0
leaq 0x33c9(%rip), %rsi      # 0x6277
callq 0x20c0
movq %rax, %rdi
movq 0x6103(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x2130
movb $0x1, -0x1(%rbp)
jmp 0x2f29
movq -0x40(%rbp), %rdi
callq 0x2070
movq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x2160
movq %rax, %rdx
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x40(%rbp), %rsi
callq 0x20b0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2d97
movq -0x20(%rbp), %rdi
callq 0x2130
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)