// File: /workspace/github_c_makefile_star2_O2/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 $0x18748, %rsp          # imm = 0x18748
movq %rdx, %r12
movl %esi, %r15d
andl $0x0, -0x34(%rbp)
leaq 0x9fe(%rip), %rsi       # 0x45e3
callq 0x22b0
testq %rax, %rax
je 0x3e41
movq %rax, %rbx
leaq -0x18770(%rbp), %r14
movq %r14, %rdi
movl $0x1869f, %esi          # imm = 0x1869F
movq %rbx, %rdx
callq 0x2170
testq %rax, %rax
je 0x3c18
incl (%r12)
jmp 0x3bfd
movq %rbx, %rdi
callq 0x2100
movl (%r12), %esi
leaq 0x968(%rip), %rdi       # 0x4593
xorl %eax, %eax
callq 0x20e0
movslq (%r12), %rdi
shlq $0x4, %rdi
callq 0x2240
movq %rbx, %rdi
movq %rax, -0x40(%rbp)
movq %rax, %rsi
callq 0x2754
testl %eax, %eax
je 0x3e4a
movq %rbx, %rdi
callq 0x20b0
movq %r12, -0x50(%rbp)
movl (%r12), %eax
cmpl %r15d, %eax
cmovll %eax, %r15d
movslq %r15d, %rbx
leaq (,%rbx,4), %r14
movq %r14, %rdi
callq 0x2240
leaq 0x39d9(%rip), %rcx      # 0x7660
movq %rax, (%rcx)
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x2240
leaq 0x39cb(%rip), %rcx      # 0x7668
movq %rax, (%rcx)
testl %ebx, %ebx
movl $0x0, %r13d
movq %rbx, -0x48(%rbp)
cmovgl %ebx, %r13d
leaq -0x90(%rbp), %rbx
leaq 0x908(%rip), %r12       # 0x45c6
leaq -0xd0(%rbp), %r15
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x3d51
movq %rbx, %rdi
leaq 0x8db(%rip), %rsi       # 0x45b6
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 0x3958(%rip), %rcx      # 0x7660
movq (%rcx), %rcx
movl %eax, (%rcx,%r14,4)
movq %r15, %rdi
movq %r12, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2320
movq %r15, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21d0
movq %r15, %rdi
movl $0x802, %esi            # imm = 0x802
xorl %eax, %eax
callq 0x22a0
leaq 0x3926(%rip), %rcx      # 0x7668
movq (%rcx), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
jmp 0x3cc8
movq -0x30(%rbp), %rdi
callq 0x2240
leaq 0x390f(%rip), %r14      # 0x7670
movq %rax, (%r14)
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x2240
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %edi
leaq -0x34(%rbp), %rbx
movl %r12d, %esi
movq %rbx, %rdx
callq 0x2896
movl %eax, %edi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x28b7
leaq 0x38c3(%rip), %rcx      # 0x7660
movq (%rcx), %rcx
movq -0x40(%rbp), %r15
movq %r15, %rdi
movq %rax, %rsi
movl %r12d, %edx
callq 0x290b
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x3dd2
callq 0x2390
movq (%r14), %rcx
movl %eax, (%rcx,%rbx,4)
testl %eax, %eax
je 0x3de7
cmpl $-0x1, %eax
je 0x3e26
incq %rbx
jmp 0x3db4
movq %r15, %rax
addq $0x18748, %rsp          # imm = 0x18748
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38b(%rip), %rsi       # 0x4179
movq -0x30(%rbp), %r14
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2320
leaq 0x7d7(%rip), %rdi       # 0x45dc
leaq 0x7d2(%rip), %rsi       # 0x45de
movq %r14, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2360
cmpl $-0x1, %eax
jne 0x3e3a
leaq 0x7c1(%rip), %rdi       # 0x45e5
jmp 0x3e2d
leaq 0x7aa(%rip), %rdi       # 0x45d7
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x2330
xorl %edi, %edi
callq 0x2330
leaq 0x743(%rip), %rdi       # 0x458b
jmp 0x3e2d
leaq 0x79b(%rip), %rdi       # 0x45ec
callq 0x2070
movl (%r12), %esi
movq -0x40(%rbp), %rdi
callq 0x2853
leaq 0x79c(%rip), %rdi       # 0x4606
callq 0x2070
jmp 0x3e32