// File: /workspace/github_c_makefile_star2_O1/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
movl $0x1, %esi
movl $0x2, %edi
callq 0x21a0
movl $0x1, %esi
movl $0xf, %edi
callq 0x21a0
movl $0x1, %esi
movl $0x3, %edi
callq 0x21a0
leaq -0x2c(%rbp), %rcx
movl $0x3, (%rcx)
xorl %eax, %eax
leaq -0x4c(%rbp), %r8
movl %eax, (%r8)
leaq -0x3c(%rbp), %r9
movl %eax, (%r9)
leaq -0x48(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x60(%rbp), %r10
movq %rax, (%r10)
leaq -0x58(%rbp), %r11
movq %rax, (%r11)
movl %r14d, %edi
movq %rbx, %rsi
pushq %r11
pushq %r10
callq 0x25ec
addq $0x10, %rsp
testl %eax, %eax
js 0x4b4d
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x47dd
movl %eax, -0x2c(%rbp)
movslq -0x2c(%rbp), %r14
movl %r14d, 0x59c8(%rip)     # 0xa1b0
movq %r14, %rdi
shlq $0x5, %rdi
callq 0x2210
leaq -0x38(%rbp), %r15
movq %rax, (%r15)
movl $0x20, %edi
callq 0x2210
movq %rax, %r12
movq %r15, %rdi
movl %r14d, %esi
callq 0x4450
movl -0x2c(%rbp), %eax
testl %eax, %eax
jle 0x4881
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x38(%rbp), %rax
movq (%rax,%r14), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21c0
cmpl $-0x1, %eax
jne 0x486d
callq 0x2050
cmpl $0x11, (%rax)
jne 0x4b48
movq -0x38(%rbp), %rax
movq (%rax,%r14), %rdi
callq 0x2060
movq -0x38(%rbp), %rax
movq (%rax,%r14), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21c0
cmpl $-0x1, %eax
je 0x4b43
incq %r15
movslq -0x2c(%rbp), %rax
leaq (%rax,%rax), %rcx
addq $0x10, %r14
cmpq %rcx, %r15
jl 0x4820
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x2210
movq %rax, %r14
cmpl $0x0, -0x2c(%rbp)
jle 0x4ac2
movq %r12, -0x68(%rbp)
leaq 0x2b1c(%rip), %r15      # 0x73c1
xorl %r13d, %r13d
xorl %ebx, %ebx
callq 0x2330
movl %eax, %r12d
movl %eax, %edi
movq %r15, %rsi
movl $0x16, %edx
callq 0x4568
movl %r12d, (%r14,%r13)
testl %r12d, %r12d
jle 0x49d4
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2270
movq -0x38(%rbp), %rcx
movl %eax, 0x8(%rcx,%r13,8)
movq 0x10(%rcx,%r13,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2270
movq -0x38(%rbp), %rcx
movl %eax, 0x18(%rcx,%r13,8)
incq %rbx
movslq -0x2c(%rbp), %rax
addq $0x4, %r13
cmpq %rax, %rbx
jl 0x48aa
movq -0x68(%rbp), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2270
movl %eax, %ebx
movq -0x58(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2270
movl %eax, %r15d
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl -0x4c(%rbp), %r9d
movq %r14, %r8
pushq %r15
pushq %rbx
callq 0x4bcc
addq $0x10, %rsp
cmpl $0x0, -0x3c(%rbp)
jle 0x4980
xorl %r12d, %r12d
movq -0x48(%rbp), %rax
movq (%rax,%r12,8), %rdi
callq 0x2030
incq %r12
movslq -0x3c(%rbp), %rax
cmpq %rax, %r12
jl 0x4967
movq -0x48(%rbp), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
cmpl $0x0, -0x2c(%rbp)
jle 0x49c0
movl $0x8, %r14d
xorl %r12d, %r12d
movq -0x38(%rbp), %rax
movl (%rax,%r14), %edi
callq 0x2150
incq %r12
movslq -0x2c(%rbp), %rax
addq %rax, %rax
addq $0x10, %r14
cmpq %rax, %r12
jl 0x49a0
movl %ebx, %edi
callq 0x2150
movl %r15d, %edi
callq 0x2150
jmp 0x4ad5
cmpl $0x0, -0x3c(%rbp)
jle 0x49f5
xorl %ebx, %ebx
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x2030
incq %rbx
movslq -0x3c(%rbp), %rax
cmpq %rax, %rbx
jl 0x49dc
movq -0x48(%rbp), %rdi
callq 0x2030
movq -0x38(%rbp), %rbx
movq (%rbx,%r13,8), %rdi
callq 0x2120
leaq 0x1(%rax), %rdi
callq 0x2210
movq %rax, %r15
movq -0x68(%rbp), %r12
movq %rax, (%r12)
movq (%rbx,%r13,8), %rsi
movq %rax, %rdi
callq 0x2080
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2270
movl %eax, 0x8(%r12)
movq -0x38(%rbp), %rbx
movq 0x10(%rbx,%r13,8), %rdi
callq 0x2120
leaq 0x1(%rax), %rdi
callq 0x2210
movq %rax, %r15
movq %rax, 0x10(%r12)
movq 0x10(%rbx,%r13,8), %rsi
movq %rax, %rdi
callq 0x2080
movq %r15, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2270
movl %eax, 0x18(%r12)
cmpl $0x0, -0x2c(%rbp)
jle 0x4aa7
xorl %ebx, %ebx
xorl %r15d, %r15d
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
callq 0x2030
incq %r15
movslq -0x2c(%rbp), %rax
addq %rax, %rax
addq $0x10, %rbx
cmpq %rax, %r15
jl 0x4a87
movq -0x38(%rbp), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movl -0x4c(%rbp), %esi
movq %r12, %rdi
callq 0x331c
leaq -0x6c(%rbp), %rbx
movq %rbx, %rdi
callq 0x2310
testl %eax, %eax
jg 0x4ad9
cmpl $0x0, -0x2c(%rbp)
jle 0x4b1d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
callq 0x2060
movq -0x38(%rbp), %rax
movq (%rax,%rbx), %rdi
callq 0x2030
incq %r14
movslq -0x2c(%rbp), %rax
addq %rax, %rax
addq $0x10, %rbx
cmpq %rax, %r14
jl 0x4af0
movq -0x38(%rbp), %rdi
callq 0x2030
leaq 0x28a0(%rip), %rdi      # 0x73cd
callq 0x20a0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2368
callq 0x2350
leaq 0x2895(%rip), %rdi      # 0x73e9
callq 0x20a0
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2030
movl $0x7, %edi
callq 0x22d0
nopl (%rax)