// File: /workspace/github_c_makefile_star2_O1/devkot[P]SearchEngine/Crawler/jobexec.c
// Function: prepareJobExec
DocumentNode *prepareJobExec(char *docfile, int numWorkers, int *nDocs) {
    int i, j, remainder = 0, increment = 0, *incrementArray;
    char lineBuffer[100000], *buffer = NULL;
    char pipeOut[50], pipeIn[50];
    DocumentNode *documentArray;
    FILE *inputFile;

    // open input file to create data structures
    inputFile = fopen(docfile, "r");
    if (inputFile == NULL) {
        perror("Error: ");
        exit(EXIT_FAILURE);
    }

    // count how many documents the input file has
    while (fgets(lineBuffer, 99999, inputFile))
        (*nDocs)++;
    rewind(inputFile);

    printf("Allocating %d cells for the array\n", *nDocs);
    // allocate nDocs cells for the array of the documents
    documentArray = (DocumentNode *) malloc(sizeof(DocumentNode) * (*nDocs));

    // initialize all the data structures with the contents of the input file
    if (!initialize(inputFile, documentArray)) {
        printf("Wrong document line read.\n");
        destroyDocuments(documentArray, *nDocs);
        printf("Successfully destroyed.\n");
        exit(EXIT_FAILURE);
    }
    fclose(inputFile);

    if (numWorkers > *nDocs)         // in case we have more workers than files
        numWorkers = *nDocs;         // set worker number equal to lines (each gets one)


// handler for sigchild
//signal(SIGCHLD, handler);

    fdIn = (int *) malloc(sizeof(int) * numWorkers);
    fdOut = (int *) malloc(sizeof(int) * numWorkers);

    for (i = 0; i < numWorkers; i++) {
        sprintf(pipeIn, "/tmp/fifo_%d_in", i);
        mkfifo(pipeIn, S_IRWXU);
        fdIn[i] = open(pipeIn, O_NONBLOCK | O_RDWR);

        sprintf(pipeOut, "/tmp/fifo_%d_out", i);
        mkfifo(pipeOut, S_IRWXU);
        fdOut[i] = open(pipeOut, O_NONBLOCK | O_RDWR);
    }

    pids    = (int *) malloc(sizeof(int) * numWorkers);
    buffer  = (char *) malloc(sizeof(char) * numWorkers);

    increment = determineWorkload(*nDocs, numWorkers, &remainder);

    incrementArray = splitWorkload(increment, &remainder, numWorkers);

    distributeDocs(documentArray, incrementArray, numWorkers, fdIn);

    for (j = 0; j < numWorkers; j++) {
        pids[j] = fork();

        if (pids[j] == -1) {
            perror("fork");
            exit(1);
        } else if (pids[j] == 0) {           // child
            sprintf(buffer, "%d", j);
            if (execlp("./worker", "worker", buffer, NULL) == -1) {
                perror("execlp");
                exit(EXIT_FAILURE);
            }
            exit(0);
        } else {                            // parent
            //printf("Im the parent with pid %d\n", getpid());
        }
    }

    return documentArray;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18738, %rsp          # imm = 0x18738
movq %rdx, %r14
movl %esi, %r15d
movl $0x0, -0x2c(%rbp)
leaq 0x17b2(%rip), %rsi      # 0x55e6
callq 0x22b0
testq %rax, %rax
je 0x406f
movq %rax, %r12
leaq -0x18760(%rbp), %rdi
movl $0x1869f, %esi          # imm = 0x1869F
movq %rax, %rdx
callq 0x2170
testq %rax, %rax
je 0x3e7d
leaq -0x18760(%rbp), %rbx
incl (%r14)
movq %rbx, %rdi
movl $0x1869f, %esi          # imm = 0x1869F
movq %r12, %rdx
callq 0x2170
testq %rax, %rax
jne 0x3e65
movq %r12, %rdi
callq 0x2100
movl (%r14), %esi
leaq 0x1707(%rip), %rdi      # 0x5596
xorl %eax, %eax
callq 0x20e0
movslq (%r14), %rdi
shlq $0x4, %rdi
callq 0x2240
movq %r12, %rdi
movq %rax, -0x38(%rbp)
movq %rax, %rsi
callq 0x2840
testl %eax, %eax
je 0x4074
movq %r12, %rdi
callq 0x20b0
movq %r14, -0x48(%rbp)
movl (%r14), %eax
cmpl %r15d, %eax
cmovll %eax, %r15d
movslq %r15d, %r15
leaq (,%r15,4), %rbx
movq %rbx, %rdi
callq 0x2240
leaq 0x4777(%rip), %rcx      # 0x8660
movq %rax, (%rcx)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x2240
leaq 0x4769(%rip), %r12      # 0x8668
movq %rax, (%r12)
testl %r15d, %r15d
jle 0x3f98
leaq -0x80(%rbp), %r14
leaq -0xc0(%rbp), %rbx
xorl %r13d, %r13d
movq %r14, %rdi
leaq 0x1695(%rip), %rsi      # 0x55b9
movl %r13d, %edx
xorl %eax, %eax
callq 0x2320
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21d0
movq %r14, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x22a0
leaq 0x470f(%rip), %rcx      # 0x8660
movq (%rcx), %rcx
movl %eax, (%rcx,%r13,4)
movq %rbx, %rdi
leaq 0x1667(%rip), %rsi      # 0x55c9
movl %r13d, %edx
xorl %eax, %eax
callq 0x2320
movq %rbx, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21d0
movq %rbx, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x22a0
movq (%r12), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
cmpq %r13, %r15
jne 0x3f1a
movq -0x40(%rbp), %rdi
callq 0x2240
leaq 0x46c8(%rip), %r14      # 0x8670
movq %rax, (%r14)
movq %r15, %rdi
callq 0x2240
movq %rax, %r12
movq -0x48(%rbp), %rax
movl (%rax), %edi
leaq -0x2c(%rbp), %rbx
movl %r15d, %esi
movq %rbx, %rdx
callq 0x298b
movl %eax, %edi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x29ac
leaq 0x4681(%rip), %rcx      # 0x8660
movq (%rcx), %rcx
movq -0x38(%rbp), %r13
movq %r13, %rdi
movq %rax, %rsi
movl %r15d, %edx
callq 0x2a02
testl %r15d, %r15d
jle 0x4017
xorl %ebx, %ebx
callq 0x2390
movq (%r14), %rcx
movl %eax, (%rcx,%rbx,4)
testl %eax, %eax
je 0x402c
cmpl $-0x1, %eax
je 0x4063
incq %rbx
cmpq %rbx, %r15
jne 0x3ffb
movq %r13, %rax
addq $0x18738, %rsp          # imm = 0x18738
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1155(%rip), %rsi      # 0x5188
movq %r12, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2320
leaq 0x1599(%rip), %rdi      # 0x55df
leaq 0x1594(%rip), %rsi      # 0x55e1
movq %r12, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2360
cmpl $-0x1, %eax
jne 0x4068
callq 0x2440
callq 0x2458
xorl %edi, %edi
callq 0x2330
callq 0x2470
leaq 0x1574(%rip), %rdi      # 0x55ef
callq 0x2070
movl (%r14), %esi
movq -0x38(%rbp), %rdi
callq 0x294c
leaq 0x1576(%rip), %rdi      # 0x5609
callq 0x2070
movl $0x1, %edi
callq 0x2330