// File: /workspace/github_c_makefile_star2_O3/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 0x17c7(%rip), %rsi      # 0x55e3
callq 0x22b0
testq %rax, %rax
je 0x4067
movq %rax, %r12
leaq -0x18760(%rbp), %rdi
movl $0x1869f, %esi          # imm = 0x1869F
movq %rax, %rdx
callq 0x2170
testq %rax, %rax
je 0x3e65
leaq -0x18760(%rbp), %rbx
incl (%r14)
movq %rbx, %rdi
movl $0x1869f, %esi          # imm = 0x1869F
movq %r12, %rdx
callq 0x2170
testq %rax, %rax
jne 0x3e4d
movq %r12, %rdi
callq 0x2100
movl (%r14), %esi
leaq 0x171c(%rip), %rdi      # 0x5593
xorl %eax, %eax
callq 0x20e0
movslq (%r14), %rdi
shlq $0x4, %rdi
callq 0x2240
movq %r12, %rdi
movq %rax, -0x38(%rbp)
movq %rax, %rsi
callq 0x2820
testl %eax, %eax
je 0x406c
movq %r12, %rdi
callq 0x20b0
movl (%r14), %ebx
cmpl %r15d, %ebx
cmovll %ebx, %r15d
movslq %r15d, %r15
leaq (,%r15,4), %r12
movq %r12, %rdi
callq 0x2240
leaq 0x4793(%rip), %rcx      # 0x8660
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2240
leaq 0x4789(%rip), %rdx      # 0x8668
movq %rax, (%rdx)
testl %r15d, %r15d
jle 0x3f93
movq %r12, -0x40(%rbp)
movq %r14, -0x48(%rbp)
leaq -0x80(%rbp), %rbx
leaq -0xc0(%rbp), %r13
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x16ab(%rip), %rsi      # 0x55b6
movq %rdx, %r12
movl %r14d, %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
leaq 0x4725(%rip), %rcx      # 0x8660
movq (%rcx), %rcx
movl %eax, (%rcx,%r14,4)
movq %r13, %rdi
leaq 0x167a(%rip), %rsi      # 0x55c6
movl %r14d, %edx
xorl %eax, %eax
callq 0x2320
movq %r13, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21d0
movq %r13, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x22a0
movq %r12, %rdx
movq (%r12), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
cmpq %r14, %r15
jne 0x3f01
movq -0x48(%rbp), %rax
movl (%rax), %ebx
movq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x2240
leaq 0x46ce(%rip), %r13      # 0x8670
movq %rax, (%r13)
movq %r15, %rdi
callq 0x2240
movq %rax, %r14
leaq -0x2c(%rbp), %r12
movl %ebx, %edi
movl %r15d, %esi
movq %r12, %rdx
callq 0x296b
movl %eax, %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x298c
leaq 0x468a(%rip), %rcx      # 0x8660
movq (%rcx), %rcx
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
movl %r15d, %edx
callq 0x29e5
testl %r15d, %r15d
jle 0x400f
xorl %ebx, %ebx
callq 0x2390
movq (%r13), %rcx
movl %eax, (%rcx,%rbx,4)
testl %eax, %eax
je 0x4024
cmpl $-0x1, %eax
je 0x405b
incq %rbx
cmpq %rbx, %r15
jne 0x3ff2
movq %r12, %rax
addq $0x18738, %rsp          # imm = 0x18738
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x114e(%rip), %rsi      # 0x5179
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2320
leaq 0x159e(%rip), %rdi      # 0x55dc
leaq 0x1599(%rip), %rsi      # 0x55de
movq %r14, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2360
cmpl $-0x1, %eax
jne 0x4060
callq 0x2440
callq 0x2458
xorl %edi, %edi
callq 0x2330
callq 0x2470
leaq 0x1579(%rip), %rdi      # 0x55ec
callq 0x2070
movl (%r14), %esi
movq -0x38(%rbp), %rdi
callq 0x292c
leaq 0x157b(%rip), %rdi      # 0x5606
callq 0x2070
movl $0x1, %edi
callq 0x2330