// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, -0x44(%rbp)
movq %rsi, %rbx
andl $0x0, -0x40(%rbp)
movl %edi, %r14d
andl $0x0, -0x3c(%rbp)
xorl %eax, %eax
callq 0x190c
movq %rax, -0x38(%rbp)
cmpl $0x2, %r14d
jne 0x13e6
movq 0x8(%rbx), %rdi
callq 0x1160
movl %eax, %ebx
leaq 0xd0b(%rip), %rsi       # 0x2004
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1180
leaq 0xd01(%rip), %rsi       # 0x2014
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 0x1343
leaq 0xde7(%rip), %rdi       # 0x2125
callq 0x1140
leaq -0x80(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x1120
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
jne 0x1367
leaq 0xdc3(%rip), %rdi       # 0x2125
callq 0x1140
cmpl $-0x1, %r14d
je 0x13f2
leaq -0x49(%rbp), %r12
leaq -0x3c(%rbp), %r15
movl %r14d, %edi
movq %r12, %rsi
pushq $0x4
popq %rdx
callq 0x10c0
testq %rax, %rax
jle 0x13fa
movb $0x0, -0x45(%rbp)
movq %r12, %rdi
callq 0x1160
movslq %eax, %r13
movq %r13, %rdi
callq 0x1110
movq %rax, %rbx
movl %r14d, %edi
movq %rax, %rsi
movq %r13, %rdx
callq 0x10c0
cmpq $-0x1, %rax
je 0x13d8
movb $0x0, (%rbx,%r13)
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
leaq -0x44(%rbp), %rdx
leaq -0x40(%rbp), %rcx
movq %r15, %r8
callq 0x148c
jmp 0x1379
leaq 0xc46(%rip), %rdi       # 0x2025
callq 0x1140
jmp 0x13ba
leaq 0xc5a(%rip), %rdi       # 0x2047
callq 0x1050
pushq $0x1
popq %rdi
callq 0x1190
xorl %ebx, %ebx
leaq -0x27e0(%rbp), %r15
leaq 0xc31(%rip), %r13       # 0x203b
leaq 0xc31(%rip), %r12       # 0x2042
movl $0x2711, %edx           # imm = 0x2711
movl %r14d, %edi
movq %r15, %rsi
callq 0x10c0
testq %rax, %rax
jle 0x1411
movb $0x0, -0xd0(%rbp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x11a0
testq %rax, %rax
jne 0x144f
movq %r15, %rdi
movq %r12, %rsi
callq 0x11a0
testq %rax, %rax
je 0x1411
jmp 0x1469
leaq -0x27d9(%rbp), %rdi
movq -0x38(%rbp), %rsi
pushq $0x1
popq %rdx
movl -0x2c(%rbp), %ecx
callq 0x16c2
addl %eax, %ebx
jmp 0x1411
movl %r14d, %edi
callq 0x10a0
movl -0x2c(%rbp), %edi
callq 0x10a0
movq -0x38(%rbp), %rdi
callq 0x1be1
movl %ebx, %edi
callq 0x1190
nopl (%rax)