// File: /workspace/github_c_makefile_star2_O1/devkot[P]SearchEngine/Crawler/utilities.c
// Function: writeQuery
void writeQuery(char *payload, char *command, int numWorkers, int *fdIn, int *fdOut, int sock){
    char msgSearch[MSGSIZE+1], temp[MSGSIZE+1], receiveSearch[MSGSIZE+1], msgExit[MSGSIZE+1];
    int childPid, queries = 0, i, status = 0, numOfWords;

        if(!strcmp(command, "search")){                                 // If it's a search commmand

            numOfWords = countWords(payload);       // count number of words that will be used for the query
            if (!numOfWords){
                printMenu(1);
                return;
            }

            wipeBuffer(msgSearch);                                      // Cleaning buffers
            wipeBuffer(receiveSearch);
            for(i = 0; i < numWorkers; i++) {
                strcpy(temp, command);
                strcat(temp, " ");
                strcat(temp, payload);
                strcpy(msgSearch, temp);

                while ((write(fdIn[i], msgSearch, MSGSIZE + 1)) <= 0){
                    if(strstr(receiveSearch, "$$$TERMINATE$$$"))
                        return;                                          // Added on project3
                }  // Transmit command
                wipeBuffer(receiveSearch);                               // Wipe buffer to remove terminating keyword
// TODO write to socket instead of printing
                while (!strstr(receiveSearch, "$$$TERMINATE$$$")) {
                    while ((read(fdOut[i], receiveSearch, MSGSIZE + 1)) <= 0) {
                    }
                    receiveSearch[MSGSIZE] = '\0';
                    if(!strstr(receiveSearch, "$$$TERMINATE$$$"))
                        customWrite(sock, receiveSearch, strlen(receiveSearch));
                }
            }

        }
        if(!strcmp(command, "exit")){                                          // Exit command

            wipeBuffer(msgExit);
            for(i = 0; i < numWorkers; i++){

                strcpy(msgExit, "exit");

                while((write(fdIn[i], msgExit, MSGSIZE+1)) <= 0){}            // Send exit command to workers and wait

                childPid = wait(&status);
                queries = status >> 8;

                printf("Child with pid: %d exited having found %d words\n", childPid, queries);
            }
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9ca8, %rsp           # imm = 0x9CA8
movl %r9d, -0x34(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, %rbx
movl %edx, -0x2c(%rbp)
movq %rsi, %rax
movq %rdi, %r14
movl $0x0, -0x30(%rbp)
leaq 0x2613(%rip), %rsi      # 0x5191
movq %rax, -0x40(%rbp)
movq %rax, %rdi
callq 0x2190
testl %eax, %eax
je 0x2c24
leaq 0x260f(%rip), %rsi      # 0x51a8
movq -0x40(%rbp), %rdi
callq 0x2190
testl %eax, %eax
jne 0x2c12
movb $0x0, -0x2770(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x2c12
movl -0x2c(%rbp), %r13d
leaq -0x2770(%rbp), %r14
leaq 0x25e8(%rip), %r12      # 0x51ad
xorl %r15d, %r15d
movb $0x0, -0x276c(%rbp)
movl $0x74697865, -0x2770(%rbp) # imm = 0x74697865
movl (%rbx,%r15,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r14, %rsi
callq 0x2090
testq %rax, %rax
jle 0x2bd9
leaq -0x30(%rbp), %rdi
callq 0x2380
movl -0x30(%rbp), %edx
sarl $0x8, %edx
movq %r12, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x20e0
incq %r15
cmpq %r13, %r15
jne 0x2bc8
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2d8f
testl %eax, %eax
je 0x2d64
xorl %eax, %eax
movb %al, -0x9cd0(%rbp)
movb %al, -0x4e90(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0x2b92
movq %r14, %rcx
movl -0x2c(%rbp), %eax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
leaq -0x75b0(%rbp), %r13
leaq 0x2531(%rip), %r15      # 0x5198
leaq -0x4e90(%rbp), %r12
movq %rcx, -0x50(%rbp)
movq %r13, %rdi
movq -0x40(%rbp), %rsi
callq 0x2050
movq %r13, %rdi
callq 0x20d0
movw $0x20, -0x75b0(%rbp,%rax)
movq %r13, %rdi
movq -0x50(%rbp), %rsi
callq 0x2310
leaq -0x9cd0(%rbp), %rdi
movq %r13, %rsi
callq 0x2050
leaq -0x9cd0(%rbp), %r13
movl (%rbx,%r14,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r13, %rsi
callq 0x2090
testq %rax, %rax
jg 0x2cdd
movq %r12, %rdi
movq %r15, %rsi
callq 0x23a0
testq %rax, %rax
je 0x2cb2
jmp 0x2c12
movb $0x0, -0x4e90(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x23a0
testq %rax, %rax
movq -0x58(%rbp), %r13
jne 0x2d4b
movl (%r13,%r14,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r12, %rsi
callq 0x2160
testq %rax, %rax
jle 0x2cf8
movb $0x0, -0x2780(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x23a0
testq %rax, %rax
jne 0x2d3b
movq %r12, %rdi
callq 0x20d0
movl -0x34(%rbp), %edi
movq %r12, %rsi
movl %eax, %edx
callq 0x36b2
movq %r12, %rdi
movq %r15, %rsi
callq 0x23a0
testq %rax, %rax
je 0x2cf8
incq %r14
cmpq -0x48(%rbp), %r14
leaq -0x75b0(%rbp), %r13
jne 0x2c72
jmp 0x2b92
leaq 0x2473(%rip), %rdi      # 0x51de
xorl %eax, %eax
callq 0x20e0
leaq 0x247a(%rip), %rdi      # 0x51f3
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2070