// File: /workspace/github_c_makefile_star2_O0/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
subq $0x2790, %rsp           # imm = 0x2790
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq $0x0, -0x2768(%rbp)
movl $0x0, -0x2774(%rbp)
movl $0x0, -0x2778(%rbp)
movl $0x0, -0x277c(%rbp)
movl $0x0, -0x2784(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1b20
movl %eax, -0x2780(%rbp)
cmpl $0x0, -0x2780(%rbp)
jne 0x18e0
movl $0x1, -0x4(%rbp)
jmp 0x1b0e
movslq -0x2780(%rbp), %rdi
shlq $0x4, %rdi
callq 0x1110
movq %rax, -0x2760(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x177f(%rip), %rsi      # 0x3081
callq 0x1150
movq %rax, -0x28(%rbp)
movl -0x2774(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2774(%rbp)
cmpl -0x2780(%rbp), %eax
jge 0x1a9a
movq -0x2768(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2360
cmpl $0x0, %eax
jne 0x1a81
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2140
movq %rax, %rcx
movq %rcx, -0x2758(%rbp)
movq -0x2760(%rbp), %rax
movslq -0x2778(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x2760(%rbp), %rax
movl -0x2778(%rbp), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, -0x2778(%rbp)
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
cmpq $0x0, -0x2758(%rbp)
je 0x1a7f
movq -0x2758(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2770(%rbp)
cmpq $0x0, -0x2770(%rbp)
je 0x1a7d
movq -0x2770(%rbp), %rax
movq (%rax), %rdi
movq -0x2770(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x26e0
movq %rax, -0x30(%rbp)
movq -0x2768(%rbp), %rdi
movq -0x2770(%rbp), %rax
movq (%rax), %rsi
movq -0x2770(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %r9
leaq -0x277c(%rbp), %r8
callq 0x23e0
movq %rax, -0x2768(%rbp)
leaq -0x2750(%rbp), %rdi
movq -0x2770(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x2770(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %r8
leaq 0x164b(%rip), %rsi      # 0x308c
movb $0x0, %al
callq 0x1180
movl -0x20(%rbp), %edi
leaq -0x2750(%rbp), %rsi
callq 0x1be0
movl -0x2784(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2784(%rbp)
movq -0x2770(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2770(%rbp)
jmp 0x19ba
jmp 0x1a7f
jmp 0x1a81
xorl %eax, %eax
movl %eax, %edi
leaq 0x15f5(%rip), %rsi      # 0x3081
callq 0x1150
movq %rax, -0x28(%rbp)
jmp 0x190b
movl $0x0, -0x2788(%rbp)
leaq -0x2750(%rbp), %rdi
leaq 0x15f5(%rip), %rsi      # 0x30a7
callq 0x1040
movl -0x20(%rbp), %edi
leaq -0x2750(%rbp), %rsi
movl $0x2711, %edx           # imm = 0x2711
callq 0x1050
movl %eax, -0x2788(%rbp)
cmpl $0x0, %eax
jg 0x1ad8
jmp 0x1ab7
cmpl $-0x1, -0x2788(%rbp)
jne 0x1aed
leaq 0x15cf(%rip), %rdi      # 0x30b7
callq 0x1140
movq -0x2760(%rbp), %rdi
callq 0x1030
movq -0x2768(%rbp), %rdi
callq 0x2670
movl -0x2784(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2790, %rsp           # imm = 0x2790
popq %rbp
retq
nopw (%rax,%rax)