// File: /workspace/github_c_makefile_star2_O0/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
subq $0x18760, %rsp          # imm = 0x18760
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq $0x0, -0x186d8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x1050(%rip), %rsi      # 0x562a
callq 0x22d0
movq %rax, -0x18760(%rbp)
cmpq $0x0, -0x18760(%rbp)
jne 0x4606
leaq 0xfa7(%rip), %rdi       # 0x559e
callq 0x22e0
movl $0x1, %edi
callq 0x2350
jmp 0x4608
leaq -0x186d0(%rbp), %rdi
movq -0x18760(%rbp), %rdx
movl $0x1869f, %esi          # imm = 0x1869F
callq 0x2180
cmpq $0x0, %rax
je 0x4633
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4608
movq -0x18760(%rbp), %rdi
callq 0x2100
movq -0x18(%rbp), %rax
movl (%rax), %esi
leaq 0xf5a(%rip), %rdi       # 0x55a6
movb $0x0, %al
callq 0x20e0
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
shlq $0x4, %rdi
callq 0x2250
movq %rax, -0x18758(%rbp)
movq -0x18760(%rbp), %rdi
movq -0x18758(%rbp), %rsi
callq 0x2890
cmpl $0x0, %eax
jne 0x46ba
leaq 0xf40(%rip), %rdi       # 0x55c9
movb $0x0, %al
callq 0x20e0
movq -0x18758(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0x29f0
leaq 0xf3b(%rip), %rdi       # 0x55e4
movb $0x0, %al
callq 0x20e0
movl $0x1, %edi
callq 0x2350
movq -0x18760(%rbp), %rdi
callq 0x20a0
movl -0xc(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x46da
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movslq -0xc(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2250
movq %rax, %rcx
leaq 0x3f7f(%rip), %rax      # 0x8670
movq %rcx, (%rax)
movslq -0xc(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2250
movq %rax, %rcx
leaq 0x3f6d(%rip), %rax      # 0x8678
movq %rcx, (%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x47cd
leaq -0x18750(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0xecb(%rip), %rsi       # 0x55fd
movb $0x0, %al
callq 0x2340
leaq -0x18750(%rbp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21e0
leaq -0x18750(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
movb $0x0, %al
callq 0x22c0
movl %eax, %edx
leaq 0x3f0a(%rip), %rax      # 0x8670
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0x18710(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0xe8c(%rip), %rsi       # 0x560d
movb $0x0, %al
callq 0x2340
leaq -0x18710(%rbp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x21e0
leaq -0x18710(%rbp), %rdi
movl $0x802, %esi            # imm = 0x802
movb $0x0, %al
callq 0x22c0
movl %eax, %edx
leaq 0x3ec3(%rip), %rax      # 0x8678
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4715
movslq -0xc(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2250
movq %rax, %rcx
leaq 0x3e9c(%rip), %rax      # 0x8680
movq %rcx, (%rax)
movslq -0xc(%rbp), %rdi
shlq $0x0, %rdi
callq 0x2250
movq %rax, -0x186d8(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %edi
movl -0xc(%rbp), %esi
leaq -0x24(%rbp), %rdx
callq 0x2a40
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %edi
movl -0xc(%rbp), %edx
leaq -0x24(%rbp), %rsi
callq 0x2aa0
movq %rax, -0x30(%rbp)
movq -0x18758(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq 0x3e38(%rip), %rax      # 0x8670
movq (%rax), %rcx
callq 0x2b20
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x4917
callq 0x23b0
movl %eax, %edx
leaq 0x3e1f(%rip), %rax      # 0x8680
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq 0x3e0e(%rip), %rax      # 0x8680
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x4895
leaq 0xd98(%rip), %rdi       # 0x561e
callq 0x22e0
movl $0x1, %edi
callq 0x2350
leaq 0x3de4(%rip), %rax      # 0x8680
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x4903
movq -0x186d8(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x8d1(%rip), %rsi       # 0x518b
movb $0x0, %al
callq 0x2340
movq -0x186d8(%rbp), %rdx
leaq 0xd54(%rip), %rdi       # 0x5623
leaq 0xd4f(%rip), %rsi       # 0x5625
xorl %eax, %eax
movl %eax, %ecx
movb $0x0, %al
callq 0x2380
cmpl $-0x1, %eax
jne 0x48fc
leaq 0xd3f(%rip), %rdi       # 0x562c
callq 0x22e0
movl $0x1, %edi
callq 0x2350
xorl %edi, %edi
callq 0x2350
jmp 0x4905
jmp 0x4907
jmp 0x4909
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x4847
movq -0x18758(%rbp), %rax
addq $0x18760, %rsp          # imm = 0x18760
popq %rbp
retq
nopw (%rax,%rax)