// File: /workspace/github_c_makefile_star2_O2/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, -0x30(%rbp)
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, -0x40(%rbp)
movq %rsi, %r14
movq %rdi, %r15
andl $0x0, -0x2c(%rbp)
leaq 0x16e6(%rip), %rsi      # 0x4182
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
movq %r14, %rax
je 0x2ad4
leaq 0x179e(%rip), %rsi      # 0x4250
movq %rax, %rdi
callq 0x2190
testl %eax, %eax
je 0x2c08
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x2c8f
testl %eax, %eax
je 0x2c76
movq %r15, -0x50(%rbp)
xorl %r14d, %r14d
leaq -0x9cd0(%rbp), %rax
movb %r14b, (%rax)
leaq -0x75b0(%rbp), %r15
movb %r14b, (%r15)
movq -0x40(%rbp), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
leaq 0x1671(%rip), %r12      # 0x4189
movq -0x38(%rbp), %rax
movq %rcx, -0x48(%rbp)
cmpq %rcx, %r14
je 0x2aab
leaq -0x2770(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
leaq -0x2770(%rbp), %rdi
callq 0x20d0
movw $0x20, -0x2770(%rbp,%rax)
leaq -0x2770(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x2310
leaq -0x9cd0(%rbp), %rdi
leaq -0x2770(%rbp), %rsi
callq 0x2050
movl (%rbx,%r14,4), %edi
movl $0x2711, %edx           # imm = 0x2711
leaq -0x9cd0(%rbp), %rsi
callq 0x2090
testq %rax, %rax
jg 0x2b9c
movq %r15, %rdi
movq %r12, %rsi
callq 0x23a0
testq %rax, %rax
je 0x2b6d
jmp 0x2ac2
movb $0x0, -0x75b0(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x23a0
testq %rax, %rax
jne 0x2bf8
movl (%r13,%r14,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r15, %rsi
callq 0x2160
testq %rax, %rax
jle 0x2bb3
movb $0x0, -0x4ea0(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x23a0
testq %rax, %rax
jne 0x2ba3
movq %r15, %rdi
callq 0x20d0
movl -0x30(%rbp), %edi
movq %r15, %rsi
movl %eax, %edx
callq 0x34bb
jmp 0x2ba3
incq %r14
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
jmp 0x2b20
xorl %eax, %eax
movq -0x40(%rbp), %r12
testl %r12d, %r12d
cmovlel %eax, %r12d
leaq -0x4e90(%rbp), %r15
leaq 0x1576(%rip), %r13      # 0x4199
xorl %r14d, %r14d
cmpq %r12, %r14
je 0x2ac2
movb $0x0, -0x4e8c(%rbp)
movl $0x74697865, -0x4e90(%rbp) # imm = 0x74697865
movl (%rbx,%r14,4), %edi
movl $0x2711, %edx           # imm = 0x2711
movq %r15, %rsi
callq 0x2090
testq %rax, %rax
jle 0x2c40
leaq -0x2c(%rbp), %rdi
callq 0x2380
movl -0x2c(%rbp), %edx
sarl $0x8, %edx
movq %r13, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x20e0
incq %r14
jmp 0x2c26
pushq $0x1
popq %rdi
addq $0x9ca8, %rsp           # imm = 0x9CA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2cdc