// File: /workspace/github_c_makefile_star2_O0/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
subq $0x2800, %rsp           # imm = 0x2800
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movb $0x0, %al
callq 0x1ca0
movq %rax, -0x27f0(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x131f
leaq 0x1cf6(%rip), %rdi      # 0x3004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1190
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1160
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1080
movq %rax, -0x27f8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1080
movq -0x27f8(%rbp), %rdi
addq %rax, %rdi
callq 0x1110
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x1170
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1170
leaq -0x80(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x1c9f(%rip), %rsi      # 0x3030
movb $0x0, %al
callq 0x1180
leaq -0xc0(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x1c97(%rip), %rsi      # 0x3040
movb $0x0, %al
callq 0x1180
leaq -0x80(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
movb $0x0, %al
callq 0x1120
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x13d6
leaq 0x1d66(%rip), %rdi      # 0x3135
callq 0x1140
jmp 0x13dd
movl $0x1, -0x30(%rbp)
leaq -0xc0(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
movb $0x0, %al
callq 0x1120
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
jne 0x1404
leaq 0x1d36(%rip), %rdi      # 0x3135
callq 0x1140
cmpl $0x1, -0x30(%rbp)
jne 0x14b9
jmp 0x1410
movl -0x18(%rbp), %edi
leaq -0xc5(%rbp), %rsi
movl $0x4, %edx
callq 0x10c0
cmpq $0x0, %rax
jle 0x14b7
movb $0x0, -0xc1(%rbp)
leaq -0xc5(%rbp), %rdi
callq 0x1160
movl %eax, -0x20(%rbp)
movslq -0x20(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1110
movq %rax, -0x27e8(%rbp)
movl -0x18(%rbp), %edi
movq -0x27e8(%rbp), %rsi
movslq -0x20(%rbp), %rdx
callq 0x10c0
cmpq $-0x1, %rax
jne 0x147d
leaq 0x1bd9(%rip), %rdi      # 0x3051
callq 0x1140
movq -0x27e8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x27e8(%rbp), %rdi
movq -0x27f0(%rbp), %rsi
leaq -0x24(%rbp), %rdx
leaq -0x28(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x1570
movl $0x0, -0x30(%rbp)
jmp 0x1410
jmp 0x14c3
movl $0x1, %edi
callq 0x1190
jmp 0x14c5
jmp 0x14c7
movl -0x18(%rbp), %edi
leaq -0x27e0(%rbp), %rsi
movl $0x2711, %edx           # imm = 0x2711
callq 0x10c0
cmpq $0x0, %rax
jg 0x14e3
jmp 0x14c7
movb $0x0, -0xd0(%rbp)
leaq -0x27e0(%rbp), %rdi
leaq 0x1b6f(%rip), %rsi      # 0x3067
callq 0x11a0
cmpq $0x0, %rax
je 0x152a
leaq -0x27e0(%rbp), %rdi
addq $0x7, %rdi
movq -0x27f0(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movl $0x1, %edx
callq 0x1870
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x1547
leaq -0x27e0(%rbp), %rdi
leaq 0x1b36(%rip), %rsi      # 0x306e
callq 0x11a0
cmpq $0x0, %rax
je 0x1545
jmp 0x154c
jmp 0x1547
jmp 0x14c5
movl -0x18(%rbp), %edi
callq 0x10a0
movl -0x1c(%rbp), %edi
callq 0x10a0
movq -0x27f0(%rbp), %rdi
callq 0x2220
movl -0x34(%rbp), %edi
callq 0x1190