// File: /workspace/github_c_makefile_star2_O3/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 $0x2748, %rsp           # imm = 0x2748
movl %ecx, %ebx
movq %rsi, -0x40(%rbp)
movq %rdi, %r14
movl $0x0, -0x34(%rbp)
callq 0x18f1
testl %eax, %eax
je 0x18a1
movl %eax, %r15d
leaq 0x18db(%rip), %rsi      # 0x3087
movq %r14, %rdi
callq 0x1150
testl %r15d, %r15d
jle 0x18a9
movl $0x1, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %r15d, -0x2c(%rbp)
movq %rax, %r15
movl %ebx, -0x30(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1da4
testl %eax, %eax
movq %r12, -0x48(%rbp)
je 0x180b
xorl %edi, %edi
leaq 0x1898(%rip), %rsi      # 0x3087
callq 0x1150
movq %rax, %r15
movq -0x48(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl -0x2c(%rbp), %ecx
movl %eax, %r12d
jne 0x17d3
jmp 0x18af
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x1c76
testq %rax, %rax
je 0x17e6
movq %r15, %rcx
movq 0x10(%rax), %r15
testq %r15, %r15
je 0x17e6
movq %rcx, -0x50(%rbp)
movq (%r15), %rdi
movl 0x8(%r15), %esi
callq 0x1fc2
movq %rax, %rbx
movq (%r15), %rsi
movl 0x8(%r15), %edx
movq %r13, %rdi
movq %rax, %rcx
leaq -0x34(%rbp), %r8
movq -0x50(%rbp), %r9
callq 0x1de1
movl %r14d, %r12d
movq %rax, %r14
movl 0x8(%r15), %edx
movq (%r15), %rcx
leaq -0x2770(%rbp), %r13
movq %r13, %rdi
leaq 0x1817(%rip), %rsi      # 0x308a
movq %rbx, %r8
movl -0x30(%rbp), %ebx
xorl %eax, %eax
callq 0x1180
movl %ebx, %edi
movq %r13, %rsi
movq %r14, %r13
movl %r12d, %r14d
callq 0x1954
incl %r14d
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x182c
jmp 0x17e6
movl $0x1, %r14d
jmp 0x18dc
xorl %r13d, %r13d
xorl %r14d, %r14d
movups 0x17ef(%rip), %xmm0     # 0x30a5
leaq -0x2770(%rbp), %r15
movaps %xmm0, (%r15)
movl $0x2711, %edx           # imm = 0x2711
movl %ebx, %edi
movq %r15, %rsi
callq 0x1060
testl %eax, %eax
jle 0x18c1
movq %r13, %rdi
callq 0x1f80
movl %r14d, %eax
addq $0x2748, %rsp           # imm = 0x2748
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq