// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movq %rsi, %rax
movq %rdi, %r14
movl $0x0, -0x30(%rbp)
leaq 0x2621(%rip), %rsi      # 0x5182
movq %rax, -0x40(%rbp)
movq %rax, %rdi
callq 0x2190
testl %eax, %eax
je 0x2c04
leaq 0x26d4(%rip), %rsi      # 0x5250
movq -0x40(%rbp), %rdi
callq 0x2190
testl %eax, %eax
setne %al
testl %r13d, %r13d
setle %cl
orb %al, %cl
jne 0x2bf2
movl %r13d, %r13d
leaq -0x4e90(%rbp), %r14
leaq 0x25f4(%rip), %r12      # 0x5199
xorl %r15d, %r15d
movb $0x0, -0x4e8c(%rbp)
movl $0x74697865, -0x4e90(%rbp) # imm = 0x74697865
movl (%rbx,%r15,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r14, %rsi
callq 0x2090
testq %rax, %rax
jle 0x2bb9
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 0x2ba8
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, -0x2c(%rbp)
movq %r14, %rdi
callq 0x2d75
testl %eax, %eax
je 0x2d4a
xorl %eax, %eax
movb %al, -0x9cd0(%rbp)
movb %al, -0x2770(%rbp)
movl -0x2c(%rbp), %r13d
testl %r13d, %r13d
jle 0x2b75
movl %r13d, %eax
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
leaq -0x75b0(%rbp), %rax
leaq -0x2770(%rbp), %r12
movq %r14, -0x50(%rbp)
movq %rax, %r13
movq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x2050
movq %r13, %rdi
callq 0x20d0
movw $0x20, -0x75b0(%rbp,%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2310
leaq -0x9cd0(%rbp), %rdi
movq %rdi, %r14
movq %r13, %rsi
callq 0x2050
movl (%rbx,%r15,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r14, %rsi
callq 0x2090
testq %rax, %rax
jg 0x2cbc
movq %r12, %rdi
leaq 0x24dc(%rip), %rsi      # 0x5189
callq 0x23a0
testq %rax, %rax
je 0x2c8d
jmp 0x2bf2
movb $0x0, -0x2770(%rbp)
movq %r12, %rdi
leaq 0x24bc(%rip), %rsi      # 0x5189
callq 0x23a0
testq %rax, %rax
jne 0x2d29
movq -0x58(%rbp), %rax
movl (%rax,%r15,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r12, %rsi
callq 0x2160
testq %rax, %rax
jle 0x2cd7
movb $0x0, -0x60(%rbp)
movq %r12, %rdi
leaq 0x248a(%rip), %r13      # 0x5189
movq %r13, %rsi
callq 0x23a0
testq %rax, %rax
jne 0x2d21
movq %r12, %rdi
callq 0x20d0
movl -0x34(%rbp), %edi
movq %r12, %rsi
movl %eax, %edx
callq 0x3694
movq %r12, %rdi
movq %r13, %rsi
jmp 0x2ccd
incq %r15
cmpq -0x48(%rbp), %r15
movl -0x2c(%rbp), %r13d
movq -0x50(%rbp), %r14
leaq -0x75b0(%rbp), %rax
jne 0x2c4f
jmp 0x2b75
leaq 0x2479(%rip), %rdi      # 0x51ca
xorl %eax, %eax
callq 0x20e0
leaq 0x2480(%rip), %rdi      # 0x51df
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2070