// File: /workspace/github_c_makefile_star2_O0/stefpant[P]TCPserver/PantoulasStefanosProject2/jobExec/jobExecutor.c
// Function: main
int main(int argc,char* argv[]){
	signal(SIGINT,SIG_IGN);
	signal(SIGTERM,SIG_IGN);
	signal(SIGQUIT,SIG_IGN);//ignore signals before reaching the parent function
	int w = 3;//by default 3 workers
	int sorted = 0,lines = 0;
	char **docmap = NULL;
	char* fifoname=NULL;
	char* fifoname2=NULL;
	pid_t *processids = NULL;

	//signal(SIGINT,SIG_IGN);//ignore interupts ( ctr+C )

	//step 1: read arguments
	if(input_parse(argc,argv,&docmap,&w,&sorted,&lines,&fifoname,&fifoname2) < 0){
		printf("Parsing arguments error,exiting...\n");
		free(fifoname);
		free(fifoname2);
		exit(7);
	}

	if(lines < w){
		w = lines;
		//printf("Too much workers for given docfile.\n");
		//printf("Continue with %d workers!\n",w);
	}
	workers = w;
//	for(int i=0;i<lines;i++)
//		printf("line %d:%s\n",i+1,docmap[i]);

	//step 2: create and init structs for fifo infos
	fifo_info * parentfifos;//parent needs all fifos
	fifo_info * childfifos;//childs just 2 fifos to RD-WR from/to parent
	parentfifos = malloc(2*w*sizeof(fifo_info));
	childfifos = malloc(2*sizeof(fifo_info));

	initNames(&parentfifos,w);

	//step 3: make fifos
	for(int i=0;i<2*w;i++){
		//printf("%s\n",parentfifos[i].name);
		if(mkfifo(parentfifos[i].name,0666) == -1){
			if(errno != EEXIST){
				perror("mkfifo");
				exit(21);
			}
			else{//if fifo exists unlink it and make it again :-)
				unlink(parentfifos[i].name);
				if(mkfifo(parentfifos[i].name,0666) == -1){
					perror("mkfifo isn't working");
					exit(99);
				}
			}
		}
	}

	processids = malloc(w*sizeof(pid_t));
	pid_t pid;
	//step 4: fork childs and open fifos
	for(int i=0;i<w;i++){
		guard(pid = fork(),"fork failed",22);
		processids[i] = pid;//just to save pid in same var
		if(pid>0){//parent
			parentfifos[2*i].fd = open(parentfifos[2*i].name,O_WRONLY);
			parentfifos[2*i+1].fd = open(parentfifos[2*i+1].name,O_RDONLY);
		}
		else{//child
			for(int j=0;j<lines;j++) free(docmap[j]);free(docmap);
			childfifos[0].name = malloc(strlen(parentfifos[2*i].name) + 1);
			strcpy(childfifos[0].name,parentfifos[2*i].name);
			childfifos[0].fd = open(childfifos[0].name,O_RDONLY);
			childfifos[1].name = malloc(strlen(parentfifos[2*i+1].name) + 1);
			strcpy(childfifos[1].name,parentfifos[2*i+1].name);
			childfifos[1].fd = open(childfifos[1].name,O_WRONLY);
			for(int j=0;j<2*w;j++) free(parentfifos[j].name);
			free(parentfifos);//free parent's stuct
			free(fifoname);
			free(fifoname2);
			break ;//exit loop
		}
	}
	if(pid>0) free(childfifos);//for parent 
	else free(processids);//for childs
	//ready for IPC :-)

	if(pid > 0){
		int FIFOfd = open(fifoname,O_RDONLY);//open fifo to read from crawler
		int FIFOfd2 = open(fifoname2,O_WRONLY);//open fifo to write to crawler
		parentExec(parentfifos,docmap,w,lines,processids,sorted,FIFOfd,FIFOfd2);//main function
		for(int j=0;j<lines;j++) free(docmap[j]);free(docmap);
		free(processids);
		for(int i=0;i<2*w;i++)
			close(parentfifos[i].fd);
		close(FIFOfd);
		close(FIFOfd2);
	}
	else childExec(childfifos,sorted);//main function

	pid_t chret;
	int chres;
	while((chret = wait(&chres))>0){}
		//printf("Child %d search %d words!\n",(int)chret,WEXITSTATUS(chres));

	for(int i=0;i<2*w;i++){
		unlink(parentfifos[i].name);
		free(parentfifos[i].name);
	}
	free(parentfifos);
	printf("Job Executor about to exit!\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x2, %edi
movl $0x1, %esi
callq 0x1170
movl $0xf, %edi
movl $0x1, %esi
callq 0x1170
movl $0x3, %edi
movl $0x1, %esi
callq 0x1170
movl $0x3, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq -0x28(%rbp), %rdx
leaq -0x14(%rbp), %rcx
leaq -0x18(%rbp), %r8
leaq -0x1c(%rbp), %r9
leaq -0x30(%rbp), %r10
leaq -0x38(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x15b0
cmpl $0x0, %eax
jge 0x4827
leaq 0x3bb5(%rip), %rdi      # 0x83b9
movb $0x0, %al
callq 0x1120
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x7, %edi
callq 0x12a0
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4835
movl -0x1c(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, 0x6952(%rip)      # 0xb190
movl -0x14(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x4, %rdi
callq 0x11e0
movq %rax, -0x48(%rbp)
movl $0x20, %edi
callq 0x11e0
movq %rax, -0x50(%rbp)
movl -0x14(%rbp), %esi
leaq -0x48(%rbp), %rdi
callq 0x4340
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jge 0x4927
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x1190
cmpl $-0x1, %eax
jne 0x4917
callq 0x1050
cmpl $0x11, (%rax)
je 0x48c5
leaq 0x3b27(%rip), %rdi      # 0x83dd
callq 0x1260
movl $0x15, %edi
callq 0x12a0
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1060
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x1190
cmpl $-0x1, %eax
jne 0x4913
leaq 0x3ae0(%rip), %rdi      # 0x83e4
callq 0x1260
movl $0x63, %edi
callq 0x12a0
jmp 0x4915
jmp 0x4917
jmp 0x4919
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4874
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x11e0
movq %rax, -0x40(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4b70
callq 0x12f0
movl %eax, %edi
movl %edi, -0x58(%rbp)
leaq 0x3a9d(%rip), %rsi      # 0x83f9
movl $0x16, %edx
callq 0x44b0
movl -0x58(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x58(%rbp)
jle 0x49f6
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1240
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %edx
shll %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1240
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x4b60
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4a21
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1030
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x49fd
movq -0x28(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1110
movq %rax, %rdi
addq $0x1, %rdi
callq 0x11e0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x1080
movq -0x50(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1240
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1110
movq %rax, %rdi
addq $0x1, %rdi
callq 0x11e0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x1080
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1240
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x18(%rax)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jge 0x4b43
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1030
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x4b15
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
jmp 0x4b70
jmp 0x4b62
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x493f
cmpl $0x0, -0x58(%rbp)
jle 0x4b81
movq -0x50(%rbp), %rdi
callq 0x1030
jmp 0x4b8a
movq -0x40(%rbp), %rdi
callq 0x1030
cmpl $0x0, -0x58(%rbp)
jle 0x4c65
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1240
movl %eax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1240
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x1c(%rbp), %ecx
movq -0x40(%rbp), %r8
movl -0x18(%rbp), %r9d
movl -0x68(%rbp), %r10d
movl -0x6c(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x4d90
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4c0c
movq -0x28(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1030
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4be8
movq -0x28(%rbp), %rdi
callq 0x1030
movq -0x40(%rbp), %rdi
callq 0x1030
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jge 0x4c53
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edi
callq 0x1130
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4c25
movl -0x68(%rbp), %edi
callq 0x1130
movl -0x6c(%rbp), %edi
callq 0x1130
jmp 0x4c71
movq -0x50(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x2940
jmp 0x4c73
leaq -0x7c(%rbp), %rdi
callq 0x12d0
movl %eax, -0x78(%rbp)
cmpl $0x0, %eax
jle 0x4c86
jmp 0x4c73
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jge 0x4cd2
movq -0x48(%rbp), %rax
movslq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1060
movq -0x48(%rbp), %rax
movslq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1030
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x4c8d
movq -0x48(%rbp), %rdi
callq 0x1030
leaq 0x3723(%rip), %rdi      # 0x8405
movb $0x0, %al
callq 0x1120
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop