// File: /workspace/github_c_makefile_star2_O3/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 0x19e0
movq %rax, -0x40(%rbp)
cmpl $0x2, %r14d
jne 0x1432
movq 0x8(%rbx), %rdi
callq 0x1160
movl %eax, %ebx
leaq 0x1cdc(%rip), %rsi      # 0x3004
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1180
leaq 0x1cd2(%rip), %rsi      # 0x3014
leaq -0x80(%rbp), %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1180
movq %r14, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x1120
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x1372
leaq 0x1db8(%rip), %rdi      # 0x3125
callq 0x1140
leaq -0x80(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x1120
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
jne 0x1396
leaq 0x1d94(%rip), %rdi      # 0x3125
callq 0x1140
cmpl $-0x1, %r14d
je 0x143e
leaq -0x31(%rbp), %rsi
movl $0x4, %edx
movl %r14d, %edi
callq 0x10c0
testq %rax, %rax
jle 0x1448
leaq -0x31(%rbp), %r12
leaq -0x44(%rbp), %rbx
movb $0x0, -0x2d(%rbp)
movq %r12, %rdi
callq 0x1160
movslq %eax, %r15
movq %r15, %rdi
callq 0x1110
movq %rax, %r13
movl %r14d, %edi
movq %rax, %rsi
movq %r15, %rdx
callq 0x10c0
cmpq $-0x1, %rax
je 0x1424
movb $0x0, (%r13,%r15)
movq %r13, %rdi
movq -0x40(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x48(%rbp), %rcx
movq %rbx, %r8
callq 0x14dc
movl $0x4, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x10c0
testq %rax, %rax
jg 0x13c2
jmp 0x1448
leaq 0x1bfa(%rip), %rdi      # 0x3025
callq 0x1140
jmp 0x13f0
leaq 0x1c0e(%rip), %rdi      # 0x3047
callq 0x1050
movl $0x1, %edi
callq 0x1190
xorl %r12d, %r12d
leaq -0x27e0(%rbp), %r15
leaq 0x1be2(%rip), %r13      # 0x303b
leaq 0x1be2(%rip), %rbx      # 0x3042
movl $0x2711, %edx           # imm = 0x2711
movl %r14d, %edi
movq %r15, %rsi
callq 0x10c0
testq %rax, %rax
jle 0x1460
movb $0x0, -0xd0(%rbp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x11a0
testq %rax, %rax
jne 0x149e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x11a0
testq %rax, %rax
je 0x1460
jmp 0x14bb
leaq -0x27d9(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x1, %edx
movl -0x2c(%rbp), %ecx
callq 0x1771
addl %eax, %r12d
jmp 0x1460
movl %r14d, %edi
callq 0x10a0
movl -0x2c(%rbp), %edi
callq 0x10a0
movq -0x40(%rbp), %rdi
callq 0x1cde
movl %r12d, %edi
callq 0x1190