// File: /workspace/github_c_makefile_star2_O1/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 0x18ee
testl %eax, %eax
je 0x189e
movl %eax, %r15d
leaq 0x18e1(%rip), %rsi      # 0x308a
movq %r14, %rdi
callq 0x1140
testl %r15d, %r15d
jle 0x18a6
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 0x1ddc
testl %eax, %eax
movq %r12, -0x48(%rbp)
je 0x1808
xorl %edi, %edi
leaq 0x189e(%rip), %rsi      # 0x308a
callq 0x1140
movq %rax, %r15
movq -0x48(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl -0x2c(%rbp), %ecx
movl %eax, %r12d
jne 0x17d0
jmp 0x18ac
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x1ca0
testq %rax, %rax
je 0x17e3
movq %r15, %rcx
movq 0x10(%rax), %r15
testq %r15, %r15
je 0x17e3
movq %rcx, -0x50(%rbp)
movq (%r15), %rdi
movl 0x8(%r15), %esi
callq 0x2044
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 0x1e2c
movl %r14d, %r12d
movq %rax, %r14
movl 0x8(%r15), %edx
movq (%r15), %rcx
leaq -0x2770(%rbp), %r13
movq %r13, %rdi
leaq 0x181d(%rip), %rsi      # 0x308d
movq %rbx, %r8
movl -0x30(%rbp), %ebx
xorl %eax, %eax
callq 0x1170
movl %ebx, %edi
movq %r13, %rsi
movq %r14, %r13
movl %r12d, %r14d
callq 0x1945
incl %r14d
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x1829
jmp 0x17e3
movl $0x1, %r14d
jmp 0x18d9
xorl %r13d, %r13d
xorl %r14d, %r14d
movups 0x17f5(%rip), %xmm0     # 0x30a8
leaq -0x2770(%rbp), %r15
movaps %xmm0, (%r15)
movl $0x2711, %edx           # imm = 0x2711
movl %ebx, %edi
movq %r15, %rsi
callq 0x1060
testl %eax, %eax
jle 0x18be
movq %r13, %rdi
callq 0x2002
movl %r14d, %eax
addq $0x2748, %rsp           # imm = 0x2748
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq