// File: /workspace/github_c_makefile_star2_O2/devkot[P]SearchEngine/Crawler/functions.c
// Function: searchQuery
int searchQuery(char *query, TrieNode *root, int deadline, int fdOut){
	char *word, *line, payload[MSGSIZE+1];
	TrieNode *wordNode;
	QueryWordInfoNode *wordArray;
	WordSearchNode *docHead = NULL;
	PostingListNode *postingListNode;
	int wordCounter = 0, wordFoundCounter = 0, numOfDocs = 0, numOfWords, queries = 0;

    numOfWords = countWords(query);       // count number of words that will be used for the query

    if (!numOfWords){
        return 1;
    }

    wordArray = (QueryWordInfoNode *) malloc(sizeof(QueryWordInfoNode) * numOfWords); // allocate at most 10 cells for this array

	word = strtok(query, " \t");
    while (wordCounter++ < numOfWords){                         // read every word of the query (up to 10 actually)
        if (!isDuplicate(docHead, word)) {
            wordArray[wordFoundCounter].wordNode = wordNode = searchTrie(root, word);    // add word's trie node to our list
            wordArray[wordFoundCounter++].word = word;                                   // add the word string to our list

            if (wordNode) {                                     // if word actually exists in trie
                postingListNode = wordNode->postingList;        // get it's posting list

                while (postingListNode) {                       // while there are nodes
                    line = readLine(postingListNode->fileName, postingListNode->lineNumber);
                    docHead = addWordSearchNode(docHead, postingListNode->fileName, postingListNode->lineNumber, line,
                                                &numOfDocs, word);
                    
                    sprintf(payload, "Line: %d File: %s Text: %s", postingListNode->lineNumber, postingListNode->fileName, line);
                    writeBack(fdOut, payload);
                    queries++;
                    
                    postingListNode = postingListNode->next;    // go to next node
                }
            }
        }
        word = strtok(NULL, " \t");
    }

    int worked = 0;
    strcpy(payload, "$$$TERMINATE$$$");
    while((worked = write(fdOut, payload, MSGSIZE+1)) <= 0){}

    if(worked == -1)
        perror("write");

    free(wordArray);
    destroyWordSearchNodes(docHead);

    return queries;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2738, %rsp           # imm = 0x2738
movl %ecx, %r14d
movq %rsi, -0x40(%rbp)
movq %rdi, %rbx
andl $0x0, -0x38(%rbp)
callq 0x1832
testl %eax, %eax
je 0x181d
movl %eax, %r13d
movl %r14d, -0x30(%rbp)
leaq 0x988(%rip), %rax       # 0x2087
movq %rbx, %rdi
movq %rax, %rbx
movq %rax, %rsi
callq 0x1150
movq %rax, %rsi
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r14d, %r14d
movl %r13d, -0x2c(%rbp)
cmpl %r13d, %eax
je 0x17e7
movl %eax, -0x34(%rbp)
movq %r15, %rdi
movq %rsi, %r12
callq 0x1c94
testl %eax, %eax
jne 0x17cb
movq -0x40(%rbp), %rdi
movq %r12, -0x48(%rbp)
movq %r12, %rsi
callq 0x1b8d
testq %rax, %rax
je 0x17c0
movq %rax, %rbx
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
je 0x17c0
movq (%rbx), %rdi
movl 0x8(%rbx), %esi
callq 0x1ed8
movq %rax, %r13
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r15, %rdi
movq %rax, %rcx
leaq -0x38(%rbp), %r8
movq -0x48(%rbp), %r9
callq 0x1ccc
movq %rax, %r15
movl 0x8(%rbx), %edx
movq (%rbx), %rcx
leaq -0x2760(%rbp), %r12
movq %r12, %rdi
leaq 0x8e4(%rip), %rsi       # 0x208a
movq %r13, %r8
xorl %eax, %eax
callq 0x1180
movl -0x30(%rbp), %edi
movq %r12, %rsi
callq 0x187f
incl %r14d
jmp 0x175c
movl -0x2c(%rbp), %r13d
leaq 0x8bc(%rip), %rbx       # 0x2087
movl -0x34(%rbp), %r12d
incl %r12d
xorl %edi, %edi
movq %rbx, %rsi
callq 0x1150
movq %rax, %rsi
movl %r12d, %eax
jmp 0x1725
movups 0x8b7(%rip), %xmm0      # 0x20a5
leaq -0x2760(%rbp), %rbx
movaps %xmm0, (%rbx)
movl -0x30(%rbp), %r12d
movl $0x2711, %edx           # imm = 0x2711
movl %r12d, %edi
movq %rbx, %rsi
callq 0x1060
testl %eax, %eax
jle 0x17fc
movq %r15, %rdi
callq 0x1e99
movl %r14d, %eax
jmp 0x1820
pushq $0x1
popq %rax
addq $0x2738, %rsp           # imm = 0x2738
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq