// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movl %edi, %r14d
pushq $0x2
popq %rdi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x2190
pushq $0xf
popq %rdi
movq %r15, %rsi
callq 0x2190
pushq $0x3
popq %rdi
movq %r15, %rsi
callq 0x2190
leaq -0x2c(%rbp), %rcx
movl $0x3, (%rcx)
leaq -0x40(%rbp), %r8
andl $0x0, (%r8)
leaq -0x3c(%rbp), %r9
andl $0x0, (%r9)
leaq -0x50(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x60(%rbp), %rax
andq $0x0, (%rax)
leaq -0x58(%rbp), %r10
andq $0x0, (%r10)
movl %r14d, %edi
movq %rbx, %rsi
pushq %r10
pushq %rax
callq 0x2573
popq %rcx
popq %rdx
testl %eax, %eax
js 0x486f
movl -0x3c(%rbp), %eax
movl -0x2c(%rbp), %r14d
cmpl %r14d, %eax
jge 0x4514
movl %eax, -0x2c(%rbp)
movl %eax, %r14d
movl %r14d, 0x5c8d(%rip)     # 0xa1a8
leal (%r14,%r14), %eax
movslq %eax, %rdi
shlq $0x4, %rdi
callq 0x2200
leaq -0x38(%rbp), %r15
movq %rax, (%r15)
pushq $0x20
popq %rdi
callq 0x2200
movq %rax, %r12
movq %r15, %rdi
movl %r14d, %esi
callq 0x41ec
xorl %ebx, %ebx
xorl %r14d, %r14d
movl -0x2c(%rbp), %r15d
movslq %r15d, %rdi
leaq (%rdi,%rdi), %rax
cmpq %rax, %r14
jge 0x45b3
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
jne 0x45aa
callq 0x2050
cmpl $0x11, (%rax)
jne 0x484b
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
callq 0x2060
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
je 0x485b
incq %r14
addq $0x10, %rbx
jmp 0x454d
movq %r12, -0x68(%rbp)
shlq $0x2, %rdi
callq 0x2200
movq %rax, -0x48(%rbp)
pushq $0x16
popq %r12
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq %r15d, %rax
cmpq %rax, %r13
jge 0x463d
callq 0x2320
movl %eax, %ebx
movl %eax, %edi
leaq 0x2dd2(%rip), %rsi      # 0x73b8
movl %r12d, %edx
callq 0x4300
movq -0x48(%rbp), %rax
movl %ebx, (%rax,%r14)
testl %ebx, %ebx
jle 0x46f1
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x2260
movq -0x38(%rbp), %rcx
movl %eax, 0x8(%rcx,%r14,8)
movq 0x10(%rcx,%r14,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2260
movq -0x38(%rbp), %rcx
movl %eax, 0x18(%rcx,%r14,8)
incq %r13
movl -0x2c(%rbp), %r15d
addq $0x4, %r14
jmp 0x45ce
movq -0x68(%rbp), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x2260
movl %eax, %ebx
movq -0x58(%rbp), %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x2260
movl %eax, %r15d
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl -0x40(%rbp), %r9d
movq -0x48(%rbp), %r8
pushq %r15
pushq %rbx
callq 0x48f0
addq $0x10, %rsp
movslq -0x3c(%rbp), %rax
movq -0x50(%rbp), %rdi
cmpq %rax, %r14
jge 0x46a6
movq (%rdi,%r14,8), %rdi
callq 0x2030
incq %r14
jmp 0x468b
callq 0x2030
movq -0x48(%rbp), %rdi
callq 0x2030
pushq $0x8
popq %r14
xorl %r12d, %r12d
movslq -0x2c(%rbp), %rax
addq %rax, %rax
cmpq %rax, %r12
jge 0x46dd
movq -0x38(%rbp), %rax
movl (%rax,%r14), %edi
callq 0x2140
incq %r12
addq $0x10, %r14
jmp 0x46bb
movl %ebx, %edi
callq 0x2140
movl %r15d, %edi
callq 0x2140
jmp 0x47e5
xorl %ebx, %ebx
movq -0x68(%rbp), %r13
movslq -0x3c(%rbp), %rax
cmpq %rax, %rbx
jge 0x4712
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x2030
incq %rbx
jmp 0x46f7
movq -0x50(%rbp), %rdi
callq 0x2030
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rbx
movq %rbx, %rdi
callq 0x2120
leaq 0x1(%rax), %rdi
callq 0x2200
movq %rax, %r15
movq %rax, (%r13)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2080
xorl %r12d, %r12d
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2260
movl %eax, 0x8(%r13)
movq -0x38(%rbp), %rax
movq 0x10(%rax,%r14,8), %rbx
movq %rbx, %rdi
callq 0x2120
leaq 0x1(%rax), %rdi
callq 0x2200
movq %rax, %r15
movq %rax, 0x10(%r13)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2080
pushq $0x1
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x2260
movl %eax, 0x18(%r13)
xorl %ebx, %ebx
movslq -0x2c(%rbp), %rax
addq %rax, %rax
movq -0x38(%rbp), %rdi
cmpq %rax, %rbx
jge 0x47ba
movq (%rdi,%r12), %rdi
callq 0x2030
incq %rbx
addq $0x10, %r12
jmp 0x4798
callq 0x2030
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2030
movq -0x48(%rbp), %rdi
callq 0x2030
movl -0x40(%rbp), %esi
movq %r13, %rdi
callq 0x3178
leaq -0x6c(%rbp), %rbx
movq %rbx, %rdi
callq 0x2300
testl %eax, %eax
jg 0x47e9
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq -0x2c(%rbp), %rax
addq %rax, %rax
movq -0x38(%rbp), %rdi
cmpq %rax, %r14
jge 0x4829
movq (%rdi,%rbx), %rdi
callq 0x2060
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
callq 0x2030
incq %r14
addq $0x10, %rbx
jmp 0x47fa
callq 0x2030
leaq 0x2b8f(%rip), %rdi      # 0x73c4
callq 0x20a0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b4a(%rip), %rdi      # 0x739c
callq 0x2280
pushq $0x15
jmp 0x4869
leaq 0x2b41(%rip), %rdi      # 0x73a3
callq 0x2280
pushq $0x63
popq %rdi
callq 0x22c0
leaq 0x2b6a(%rip), %rdi      # 0x73e0
callq 0x20a0
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2030
pushq $0x7
jmp 0x4869
nopl (%rax)