// File: /workspace/github_c_makefile_star2_O1/devkot[P]SearchEngine/Crawler/worker.c
// Function: main
int main(int argc, char **argv) {
    int i, fdIn, fdOut, bytesToRead = 0, nWords = 0, nChars = 0, nLines = 0, flag = 0, queries = 0;
    char *workerID, pipeIn[50], pipeOut[50], temp[5], msgbuf[MSGSIZE + 1], *lineBuffer;
    // In and Out follow jobExecutor's order

    TrieNode *trie = createTrieNode();
    // the Trie data structure that will contain all the words

    if (argc != 2) {                    // Two arguments for worker + i
        printf("Wrong number of arguments used in execlp()\n");
        exit(EXIT_FAILURE);
    }

    i = atoi(argv[1]);                  // Get the pipe ID corresponding to this worker

    workerID = (char *) malloc(strlen(argv[0]) + strlen(argv[1]));  // Allocate memory for the worker ID

    strcat(workerID, argv[0]);
    strcat(workerID, argv[1]);

    sprintf(pipeIn, "/tmp/fifo_%d_in", i);
    sprintf(pipeOut, "/tmp/fifo_%d_out", i);

    if ((fdIn = open(pipeIn, O_NONBLOCK | O_RDWR)) == -1)
        perror("open");
    else
        flag = 1;

    if ((fdOut = open(pipeOut, O_NONBLOCK | O_RDWR)) == -1)
        perror("open");

    if (flag == 1) {                                   // Initialize data structures
        while ((read(fdIn, temp, 4)) > 0) {            // Read 4 byte digits to determine the payload's size
            *(temp + 4) = '\0';                        // Append terminating character
            bytesToRead = atoi(temp);

            lineBuffer = (char *) malloc(sizeof(char) * bytesToRead);

            if ((read(fdIn, lineBuffer, bytesToRead)) == -1)      // Read the payload
                perror("Error reading docfile");

            *(lineBuffer + bytesToRead) = '\0';

            readFolder(lineBuffer, trie, &nWords, &nChars, &nLines);        // Get path contents

            flag = 0;
        }
    } else
        exit(EXIT_FAILURE);

    //signal(SIGALRM, alarmHandler);

    while (1) {
        while ((read(fdIn, msgbuf, MSGSIZE + 1)) <= 0) {            // Read query command, maxcount is the longest so we read 8 bytes
        }
        msgbuf[MSGSIZE] = '\0';

        if (strstr(msgbuf, "search")) {                    // if this is a search command
            queries += searchQuery(msgbuf + 7, trie, 1, fdOut);
        }
        else if (strstr(msgbuf, "exit")) {                 // Exit
            break;
        }
    }

    // Close pipes and free memory
    close(fdIn);
    close(fdOut);

    destroyTrie(trie);

    // Exit with the number of queries
    exit(queries);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x27b8, %rsp           # imm = 0x27B8
movq %rsi, %rbx
movl %edi, %r14d
xorl %eax, %eax
movl %eax, -0x4c(%rbp)
movl %eax, -0x48(%rbp)
movl %eax, -0x44(%rbp)
xorl %eax, %eax
callq 0x19d0
movq %rax, -0x40(%rbp)
cmpl $0x2, %r14d
jne 0x1422
movq 0x8(%rbx), %rdi
callq 0x1150
movl %eax, %ebx
leaq 0x1cec(%rip), %rsi      # 0x3004
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1170
leaq 0x1ce2(%rip), %rsi      # 0x3014
leaq -0x80(%rbp), %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1170
movq %r14, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x1110
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x1362
leaq 0x1df4(%rip), %rdi      # 0x3151
callq 0x1130
leaq -0x80(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
jne 0x1386
leaq 0x1dd0(%rip), %rdi      # 0x3151
callq 0x1130
cmpl $-0x1, %r14d
je 0x142e
leaq -0x31(%rbp), %rsi
movl $0x4, %edx
movl %r14d, %edi
callq 0x10c0
testq %rax, %rax
jle 0x1438
leaq -0x31(%rbp), %r12
leaq -0x44(%rbp), %rbx
movb $0x0, -0x2d(%rbp)
movq %r12, %rdi
callq 0x1150
movslq %eax, %r15
movq %r15, %rdi
callq 0x1100
movq %rax, %r13
movl %r14d, %edi
movq %rax, %rsi
movq %r15, %rdx
callq 0x10c0
cmpq $-0x1, %rax
je 0x1414
movb $0x0, (%r13,%r15)
movq %r13, %rdi
movq -0x40(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x48(%rbp), %rcx
movq %rbx, %r8
callq 0x14cc
movl $0x4, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x10c0
testq %rax, %rax
jg 0x13b2
jmp 0x1438
leaq 0x1c0a(%rip), %rdi      # 0x3025
callq 0x1130
jmp 0x13e0
leaq 0x1c1e(%rip), %rdi      # 0x3047
callq 0x1050
movl $0x1, %edi
callq 0x1180
xorl %r12d, %r12d
leaq -0x27e0(%rbp), %r15
leaq 0x1bf2(%rip), %r13      # 0x303b
leaq 0x1bf2(%rip), %rbx      # 0x3042
movl $0x2711, %edx           # imm = 0x2711
movl %r14d, %edi
movq %r15, %rsi
callq 0x10c0
testq %rax, %rax
jle 0x1450
movb $0x0, -0xd0(%rbp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x1190
testq %rax, %rax
jne 0x148e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1190
testq %rax, %rax
je 0x1450
jmp 0x14ab
leaq -0x27d9(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x1, %edx
movl -0x2c(%rbp), %ecx
callq 0x176e
addl %eax, %r12d
jmp 0x1450
movl %r14d, %edi
callq 0x10a0
movl -0x2c(%rbp), %edi
callq 0x10a0
movq -0x40(%rbp), %rdi
callq 0x1d18
movl %r12d, %edi
callq 0x1180