// File: /workspace/github_c_makefile_star2_O0/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
subq $0x9cd0, %rsp           # imm = 0x9CD0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x9ca8(%rbp)
movl $0x0, -0x9cb0(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x23c3(%rip), %rsi      # 0x5194
callq 0x21a0
cmpl $0x0, %eax
jne 0x2f90
movq -0x8(%rbp), %rdi
callq 0x3070
movl %eax, -0x9cb4(%rbp)
cmpl $0x0, -0x9cb4(%rbp)
jne 0x2e06
movl $0x1, %edi
callq 0x3130
jmp 0x3059
leaq -0x2740(%rbp), %rdi
callq 0x3170
leaq -0x7580(%rbp), %rdi
callq 0x3170
movl $0x0, -0x9cac(%rbp)
movl -0x9cac(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2f8e
leaq -0x4e60(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2050
leaq -0x4e60(%rbp), %rdi
leaq 0x26e6(%rip), %rsi      # 0x553b
callq 0x2330
leaq -0x4e60(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x2330
leaq -0x2740(%rbp), %rdi
leaq -0x4e60(%rbp), %rsi
callq 0x2050
movq -0x20(%rbp), %rax
movslq -0x9cac(%rbp), %rcx
movl (%rax,%rcx,4), %edi
leaq -0x2740(%rbp), %rsi
movl $0x2711, %edx           # imm = 0x2711
callq 0x2080
cmpq $0x0, %rax
jg 0x2ec2
leaq -0x7580(%rbp), %rdi
leaq 0x22eb(%rip), %rsi      # 0x519b
callq 0x23c0
cmpq $0x0, %rax
je 0x2ec0
jmp 0x3059
jmp 0x2e7d
leaq -0x7580(%rbp), %rdi
callq 0x3170
leaq -0x7580(%rbp), %rdi
leaq 0x22bf(%rip), %rsi      # 0x519b
callq 0x23c0
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2ef3
jmp 0x2f78
jmp 0x2ef5
movq -0x28(%rbp), %rax
movslq -0x9cac(%rbp), %rcx
movl (%rax,%rcx,4), %edi
leaq -0x7580(%rbp), %rsi
movl $0x2711, %edx           # imm = 0x2711
callq 0x2170
cmpq $0x0, %rax
jg 0x2f1c
jmp 0x2ef5
movb $0x0, -0x4e70(%rbp)
leaq -0x7580(%rbp), %rdi
leaq 0x226a(%rip), %rsi      # 0x519b
callq 0x23c0
cmpq $0x0, %rax
jne 0x2f73
movl -0x2c(%rbp), %eax
movl %eax, -0x9cc4(%rbp)
leaq -0x7580(%rbp), %rax
movq %rax, -0x9cc0(%rbp)
leaq -0x7580(%rbp), %rdi
callq 0x20c0
movl -0x9cc4(%rbp), %edi
movq -0x9cc0(%rbp), %rsi
movl %eax, %edx
callq 0x3bc0
jmp 0x2ece
jmp 0x2f7a
movl -0x9cac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9cac(%rbp)
jmp 0x2e28
jmp 0x2f90
movq -0x10(%rbp), %rdi
leaq 0x2210(%rip), %rsi      # 0x51ab
callq 0x21a0
cmpl $0x0, %eax
jne 0x3059
leaq -0x9ca0(%rbp), %rdi
callq 0x3170
movl $0x0, -0x9cac(%rbp)
movl -0x9cac(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3057
leaq -0x9ca0(%rbp), %rdi
leaq 0x21cf(%rip), %rsi      # 0x51ab
callq 0x2050
movq -0x20(%rbp), %rax
movslq -0x9cac(%rbp), %rcx
movl (%rax,%rcx,4), %edi
leaq -0x9ca0(%rbp), %rsi
movl $0x2711, %edx           # imm = 0x2711
callq 0x2080
cmpq $0x0, %rax
jg 0x3008
jmp 0x2fe1
leaq -0x9cb0(%rbp), %rdi
callq 0x23a0
movl %eax, -0x9ca4(%rbp)
movl -0x9cb0(%rbp), %eax
sarl $0x8, %eax
movl %eax, -0x9ca8(%rbp)
movl -0x9ca4(%rbp), %esi
movl -0x9ca8(%rbp), %edx
leaq 0x2174(%rip), %rdi      # 0x51b0
movb $0x0, %al
callq 0x20e0
movl -0x9cac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9cac(%rbp)
jmp 0x2fbf
jmp 0x3059
addq $0x9cd0, %rsp           # imm = 0x9CD0
popq %rbp
retq
nopw %cs:(%rax,%rax)