// File: /workspace/github_c_makefile_star2_O3/stefpant[P]TCPserver/webcrawler/webcrawler.c
// Function: main
int main(int argc,char* argv[]){
	int s_port,c_port;
	int n_threads = 5;//by default 5 threads
	char *savedir = NULL;
	char *hostid = NULL;
	pid_t pid;//for calling jobExec and in the end wait termination
	char *starting_url = NULL;
	int flagSHUTDOWN=0;//when set 1 exit
	int flagSEARCH=1;//first time(if crawler ready) call jobExecutor
	int FIFOfd,FIFOfd2;
	printf("Parent %ld: just started!\n", pthread_self());
//~~~~~~~~~~~~~~~~~~~~~~~~~~PARSE ARGUMENTS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if(input_parse(argc,argv,&s_port,&c_port,&n_threads,&savedir,&hostid,&starting_url) < 0){
		free(savedir);
		free(hostid);
		free(starting_url);
		exit(9);
	}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~INIT: pool,mutexes,conditions~~~~~~~~~~~~~~~~~~~~~~~
	urlpool_init(&up);

	pthread_mutex_init(&mtx, 0);
	pthread_mutex_init(&mtxwt, 0);
	pthread_mutex_init(&pumtx, 0);
	pthread_mutex_init(&mtxcounter, 0);
	pthread_mutex_init(&mtxbytes, 0);
	pthread_cond_init(&cv_notempty, 0);
	pthread_cond_init(&cv_notfull, 0);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAKING THREADS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	pthread_t *tids = NULL;
	tids = malloc(n_threads*sizeof(pthread_t));

	tdata *dat;//struct to send to threads
	dat = malloc(sizeof(tdata));
	dat->port = s_port;
	dat->savedir = malloc((strlen(savedir)+1)*sizeof(char));
	strcpy(dat->savedir,savedir);
	dat->hostname = malloc((strlen(hostid)+1)*sizeof(char));
	strcpy(dat->hostname,hostid);

	for(int i=0; i<n_threads; i++)//create threads to execute threadExec
		pthread_create(tids+i,NULL,threadExec, (void *)dat);

	printf("Parent %ld: just created new threads!\n", pthread_self());
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	pthread_mutex_lock(&pumtx);
	placedurls=malloc(sizeof(char*));
	pucounter++;
	placedurls[0]=starting_url;
	pthread_mutex_unlock(&pumtx);
	//place starting url in pool
	place(&up, starting_url);
	pthread_cond_signal(&cv_notempty);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	time_t start,localend;
	int hh,mm,ss;
	time(&start);//crawler started
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAKING SOCKETS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	int c_sock,newcsock;
	struct sockaddr_in server;
	struct sockaddr * serverptr = (struct sockaddr *)&server;
	if( (c_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) //socket for command port
		{perror("socket");exit(9);}
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(c_port);

	if( setsockopt(c_sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 },sizeof(int)) < 0 )
		{perror("setsockopt");exit(9);}
	if( bind(c_sock,serverptr,sizeof(server)) < 0 ){perror("bind");exit(9);}
	if( listen(c_sock,100) < 0 ){perror("listen");exit(9);}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~INIT POLL~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	struct pollfd pfd[1];
	pfd[0].fd = c_sock;
	pfd[0].events = POLLIN;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~LOOP:ACCEPTING CONNECTIONS~~~~~~~~~~~~~~~~~~~~~~~
	while( 1 ){//only on command port

		poll(pfd,1,-1);//wait a connection
		if(pfd[0].revents & POLLIN){//client on command port
			if( (newcsock = accept(c_sock,NULL,NULL)) < 0)
				{perror("accept");exit(9);}
			while(1){//1
				char *buf = malloc(9*sizeof(char));//should not be resized
				read_command(newcsock,&buf,9);
				if( !strcmp(buf,"STATS") ){
					time(&localend);
					localend -= start;
					hh = localend/3600;//creating time format
					localend -= hh*3600;
					mm = localend/60;
					ss = localend - mm*60;
					char* statrep=NULL;
					if(asprintf(&statrep,"Crawler up for %02d:%02d:%02d, downloaded %d pages, %d bytes\n",hh,mm,ss,totalrc,totalBytes) != -1){
						int len=strlen(statrep);
						write(newcsock,statrep,len+1);
						free(statrep);
					}
					//printf("Crawler up for %02d:%02d:%02d, downloaded %d pages, %d bytes\n",hh,mm,ss,totalrc,totalBytes);
				}
				else if( !strcmp(buf,"SHUTDOWN") ){
					close(newcsock);
					free(buf);
					flagSHUTDOWN=1;//2
					break;
				}
				else if( !strcmp(buf,"STOP") ){//3
					close(newcsock);//4
					free(buf);//5
					break;//6
				}//7
				else{
					char* ms=NULL;//maybe search command
					//printf("%s\n",buf);
					nextWord(buf,&ms,0);
					if( !strcmp(ms,"SEARCH") ){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~check if crawling is over~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
						if( waitingThreads!=n_threads || up->count!=0 ){//pool not empty or some threads not waiting
							//printf("Crawling in progress,wait!\n");
							write(newcsock,"Crawling in progress,wait!\n",28);
							free(ms);free(buf);
							continue;
						}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
						
						if(flagSEARCH){//first time got SEARCH command,init some structs
							char* sdoc="docfile.txt";
							createDocfile(savedir,sdoc);
//~~~~~~~~~~~~~~~~~~~~fifo for crawler-jobExecutor communication~~~~~~~~~~~~~~~~~~~~~~
							if(mkfifo("../JECfifo",0666) == -1){
								if(errno != EEXIST){
									perror("mkfifo");
									exit(21);
								}
								else{//if fifo exists unlink it and make it again :-)
									unlink("../JECfifo");
									if(mkfifo("../JECfifo",0666) == -1){
										perror("mkfifo isn't working");
										exit(99);
									}
								}
							}
							if(mkfifo("../CJEfifo",0666) == -1){
								if(errno != EEXIST){
									perror("mkfifo");
									exit(21);
								}
								else{//if fifo exists unlink it and make it again :-)
									unlink("../CJEfifo");
									if(mkfifo("../CJEfifo",0666) == -1){
										perror("mkfifo isn't working");
										exit(99);
									}
								}
							}
							pid = fork();
							if(pid < 0){perror("fork");exit(87);}
							else if(pid == 0){//child call jobExecutor...
								close(newcsock);
								char *const args[]={//arguments for jobExecutor
												"../PantoulasStefanosProject2/jobExecutor",//executable
												"-d",//flag for docfile
												"docfile.txt",//docfile
												"-w",//flag for workers
												"5",//workers
												"-n",//flag for fifo names for C-jE communication
												"../JECfifo",//jE reads from C in this fifo
												"../CJEfifo",//C reads from jE in this fifo
												NULL};
								execv("../PantoulasStefanosProject2/jobExecutor",args);
								exit(54);//unexpected return here
							}
							//parent continue from here
							FIFOfd = open("../JECfifo",O_WRONLY);
							FIFOfd2 = open("../CJEfifo",O_RDONLY);
							flagSEARCH=0;//never again here
						}
						char* msg=malloc((strlen(buf)+9)*sizeof(char));
						strcpy(msg,buf);
						strcat(msg," -d 1000");//add the deadline

						write(FIFOfd,msg,strlen(msg)+1);
						free(msg);

						while( 1 ){
							char *ans = malloc(9*sizeof(char));//should not be resized
							read_command(FIFOfd2,&ans,9);//read jobExec answers
							if(!strcmp(ans,"_STOP_")){free(ans);break;}//no more results
							char *realans=malloc((strlen(ans)+2)*sizeof(char));
							strcpy(realans,ans);
							strcat(realans,"\n");
							write(newcsock,realans,strlen(ans)+2);//write ans to socket
							free(realans);
							free(ans);
						}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
					}
					else
						write(newcsock,"Unknown command!\n",18);
					free(ms);
				}
				free(buf);
				//close(newcsock);//--8
			}
		}//9
		if(flagSHUTDOWN) break;//10
	}

	//clear pool and then place in pool exit msg
	urlpool_clear(&up);
	//here if i got SHUTDOWN command to send exit msg(sending "exit" in pool)
	for(int i=0; i<n_threads; i++){
		place(&up,"exit");
		pthread_cond_signal(&cv_notempty);
		printf("Parent: send stop!\n");
	}

	for(int i=0; i<n_threads; i++)
		 pthread_join(*(tids+i),NULL);//join threads

	if( !flagSEARCH ){//free struct from search
		write(FIFOfd,"/exit",6);
		close(FIFOfd);
		close(FIFOfd2);
	}

	//free structs
	pthread_mutex_destroy(&mtx);
	pthread_mutex_destroy(&mtxwt);
	pthread_mutex_destroy(&pumtx);
	pthread_mutex_destroy(&mtxcounter);
	pthread_mutex_destroy(&mtxbytes);
	pthread_cond_destroy(&cv_notempty);
	pthread_cond_destroy(&cv_notfull);
	urlpool_destroy(&up);
	destroyPU();
	free(dat->savedir);
	free(dat->hostname);
	free(savedir);
	free(hostid);
	free(tids);
	free(dat);
	if( !flagSEARCH ){//free other structs first to give jE some
		waitpid(pid,0,0);//time to exit and then
		unlink("../JECfifo");//unlink fifos
		unlink("../CJEfifo");
	}
	printf("Crawler about to exit!\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r15
movl %edi, %r12d
leaq -0x2c(%rbp), %rax
movl $0x5, (%rax)
xorl %eax, %eax
leaq -0x60(%rbp), %r13
movq %rax, (%r13)
leaq -0x78(%rbp), %rbx
movq %rax, (%rbx)
leaq -0x70(%rbp), %r14
movq %rax, (%r14)
callq 0x2290
leaq 0x174f(%rip), %rdi      # 0x51e1
movq %rax, -0x40(%rbp)
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
leaq -0x88(%rbp), %rdx
leaq -0x84(%rbp), %rcx
movl %r12d, %edi
movq %r15, %rsi
leaq -0x2c(%rbp), %r8
movq %r13, %r9
pushq %r14
pushq %rbx
callq 0x303f
addq $0x10, %rsp
testl %eax, %eax
js 0x43ba
movl $0x18, %edi
callq 0x2220
movq %rax, %r14
movq %rax, 0x37d5(%rip)      # 0x72b8
movl $0x50, %edi
callq 0x2220
movq %rax, (%r14)
movq $0xa, 0x8(%r14)
movl $0xffffffff, 0x10(%r14) # imm = 0xFFFFFFFF
leaq 0x3731(%rip), %rdi      # 0x7238
xorl %esi, %esi
callq 0x2360
leaq 0x377b(%rip), %rdi      # 0x7290
xorl %esi, %esi
callq 0x2360
leaq 0x36ed(%rip), %rdi      # 0x7210
xorl %esi, %esi
callq 0x2360
leaq 0x37bf(%rip), %rdi      # 0x72f0
xorl %esi, %esi
callq 0x2360
leaq 0x37d9(%rip), %rdi      # 0x7318
xorl %esi, %esi
callq 0x2360
leaq 0x3713(%rip), %rdi      # 0x7260
xorl %esi, %esi
callq 0x21f0
leaq 0x3765(%rip), %rdi      # 0x72c0
xorl %esi, %esi
callq 0x21f0
movslq -0x2c(%rbp), %r15
leaq (,%r15,8), %rdi
callq 0x2220
movq %rax, -0x68(%rbp)
movl $0x18, %edi
callq 0x2220
movq %rax, %rbx
movl -0x88(%rbp), %eax
movl %eax, (%rbx)
movq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
leaq 0x1(%rax), %rdi
callq 0x2220
movq %rax, 0x10(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2070
movq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
leaq 0x1(%rax), %rdi
callq 0x2220
movq %rax, 0x8(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2070
testq %r15, %r15
jle 0x3c07
xorl %r12d, %r12d
leaq -0x591(%rip), %r14      # 0x3652
movq -0x68(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2280
incq %r12
movslq -0x2c(%rbp), %rax
addq $0x8, %r15
cmpq %rax, %r12
jl 0x3be7
movq %rbx, -0xa0(%rbp)
leaq 0x15e7(%rip), %rdi      # 0x51fc
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x2100
leaq 0x35e9(%rip), %rbx      # 0x7210
movq %rbx, %rdi
callq 0x2390
movl $0x8, %edi
callq 0x2220
incl 0x35c9(%rip)            # 0x7208
movq %rax, 0x35ba(%rip)      # 0x7200
movq -0x70(%rbp), %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
callq 0x2210
movq -0x70(%rbp), %rsi
leaq 0x3658(%rip), %rdi      # 0x72b8
callq 0x351d
leaq 0x35f4(%rip), %rdi      # 0x7260
callq 0x2160
leaq -0xa8(%rbp), %rdi
callq 0x21d0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23a0
testl %eax, %eax
js 0x43df
movl %eax, %r12d
movw $0x2, -0xbc(%rbp)
movzwl -0x84(%rbp), %eax
rolw $0x8, %ax
movl $0x0, -0xb8(%rbp)
movw %ax, -0xba(%rbp)
leaq -0xac(%rbp), %rcx
movl $0x1, (%rcx)
movl %r12d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x20a0
testl %eax, %eax
js 0x43e4
leaq -0xbc(%rbp), %rsi
movl %r12d, %edi
movl $0x10, %edx
callq 0x2270
testl %eax, %eax
js 0x43e9
movl %r12d, %edi
movl $0x64, %esi
callq 0x2230
testl %eax, %eax
js 0x43ee
leaq -0x98(%rbp), %r15
movl %r12d, (%r15)
movw $0x1, 0x4(%r15)
movl $0x1, -0x40(%rbp)
leaq -0x38(%rbp), %r13
leaq 0x1502(%rip), %r14      # 0x5241
leaq -0x110(%rbp), %rbx
movl %r12d, -0x80(%rbp)
movl $0x1, %esi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2260
testb $0x1, -0x92(%rbp)
je 0x3d4a
movl %r12d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22f0
movl %eax, %r15d
testl %eax, %eax
movl %eax, -0x48(%rbp)
js 0x4376
movl $0x9, %edi
callq 0x2220
movq %rax, -0x38(%rbp)
movl %r15d, %edi
movq %r13, %rsi
movl $0x9, %edx
callq 0x296d
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2180
testl %eax, %eax
je 0x3e20
movq %r12, %rdi
leaq 0x14cb(%rip), %rsi      # 0x5285
callq 0x2180
testl %eax, %eax
je 0x41c3
movq %r12, %rdi
leaq 0x14bd(%rip), %rsi      # 0x528e
callq 0x2180
testl %eax, %eax
je 0x41a2
movq $0x0, -0x58(%rbp)
movq %r12, %rdi
leaq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x2a24
movq -0x58(%rbp), %rdi
leaq 0x1494(%rip), %rsi      # 0x5293
callq 0x2180
testl %eax, %eax
je 0x3f1a
movl $0x12, %edx
movl %r15d, %edi
leaq 0x1522(%rip), %rsi      # 0x533d
jmp 0x3f41
leaq -0x90(%rbp), %rdi
callq 0x21d0
movq -0x90(%rbp), %r8
subq -0xa8(%rbp), %r8
movq %r8, %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %r10
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %r10
addq %rax, %r10
movq %r10, %rax
movabsq $0xe1000000000, %rcx    # imm = 0xE1000000000
imulq %rcx, %rax
sarq $0x20, %rax
subq %rax, %r8
movq %r8, -0x90(%rbp)
movq %r8, %rax
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
imulq %rcx
movq %rdx, %rcx
addq %r8, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x5, %rcx
addq %rax, %rcx
movq %rcx, %rax
movabsq $0x3c00000000, %rdx     # imm = 0x3C00000000
imulq %rdx, %rax
shrq $0x20, %rax
subl %eax, %r8d
movq $0x0, -0x110(%rbp)
movl 0x3328(%rip), %r9d      # 0x71f0
movl 0x3325(%rip), %r11d     # 0x71f4
subq $0x8, %rsp
movq %rbx, %rdi
leaq 0x136a(%rip), %rsi      # 0x5247
movl %r10d, %edx
xorl %eax, %eax
pushq %r11
callq 0x2250
addq $0x10, %rsp
cmpl $-0x1, %eax
je 0x3f4f
movq -0x110(%rbp), %r12
movq %r12, %rdi
callq 0x20f0
incl %eax
movslq %eax, %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x20b0
movq -0x110(%rbp), %rdi
jmp 0x3f4a
movl 0x32d8(%rip), %eax      # 0x71f8
cmpl -0x2c(%rbp), %eax
jne 0x3f32
movq 0x338c(%rip), %rax      # 0x72b8
cmpl $0x0, 0xc(%rax)
je 0x3f5d
movl $0x1c, %edx
movl %r15d, %edi
leaq 0x1359(%rip), %rsi      # 0x529a
callq 0x20b0
movq -0x58(%rbp), %rdi
callq 0x2030
movq -0x38(%rbp), %rdi
callq 0x2030
jmp 0x3d7f
movq %rbx, %r15
cmpl $0x0, -0x40(%rbp)
movl -0x4c(%rbp), %ebx
je 0x403d
movq -0x60(%rbp), %rdi
leaq 0x133e(%rip), %rsi      # 0x52b6
callq 0x2e82
leaq 0x133e(%rip), %r14      # 0x52c2
movq %r14, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
leaq 0x134e(%rip), %r12      # 0x52e9
jne 0x3fc9
callq 0x2040
cmpl $0x11, (%rax)
jne 0x43f3
movq %r14, %rdi
callq 0x2050
movq %r14, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
je 0x4402
movq %r12, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
jne 0x4007
callq 0x2040
cmpl $0x11, (%rax)
jne 0x43f8
movq %r12, %rdi
callq 0x2050
movq %r12, %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x21b0
cmpl $-0x1, %eax
je 0x43fd
callq 0x2380
testl %eax, %eax
js 0x43b5
movl %eax, -0x7c(%rbp)
je 0x437b
movq %r14, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22c0
movl %eax, -0x44(%rbp)
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x22c0
movl %eax, %ebx
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x20f0
leaq 0x9(%rax), %rdi
callq 0x2220
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0x2070
movq %r13, %rdi
callq 0x20f0
movabsq $0x3030303120642d20, %rcx # imm = 0x3030303120642D20
movq %rcx, (%r13,%rax)
movb $0x0, 0x8(%r13,%rax)
movq %r13, %rdi
callq 0x20f0
leaq 0x1(%rax), %rdx
movl -0x44(%rbp), %edi
movq %r13, %rsi
callq 0x20b0
movq %r13, %rdi
callq 0x2030
movl $0x9, %edi
callq 0x2220
movq %rax, -0x110(%rbp)
movl %ebx, %edi
movq %r15, %rsi
movl $0x9, %edx
callq 0x296d
movq -0x110(%rbp), %r12
movq %r12, %rdi
leaq 0x1269(%rip), %r14      # 0x5336
movq %r14, %rsi
callq 0x2180
testl %eax, %eax
movl -0x48(%rbp), %r15d
je 0x4179
movq %r12, %rdi
callq 0x20f0
leaq 0x2(%rax), %rdi
callq 0x2220
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0x2070
movq %r13, %rdi
callq 0x20f0
movw $0xa, (%r13,%rax)
movq %r12, %rdi
callq 0x20f0
leaq 0x2(%rax), %rdx
movl %r15d, %edi
movq %r13, %rsi
callq 0x20b0
movq %r13, %rdi
callq 0x2030
movq -0x110(%rbp), %rdi
callq 0x2030
movl $0x9, %edi
callq 0x2220
movq %rax, -0x110(%rbp)
movl %ebx, %edi
leaq -0x110(%rbp), %rsi
movl $0x9, %edx
callq 0x296d
movq -0x110(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2180
testl %eax, %eax
jne 0x40e1
movl %ebx, -0x4c(%rbp)
movq %r12, %rdi
callq 0x2030
movl $0x0, -0x40(%rbp)
leaq -0x38(%rbp), %r13
leaq 0x10ab(%rip), %r14      # 0x5241
leaq -0x110(%rbp), %rbx
jmp 0x3f46
movl %r15d, %edi
callq 0x2130
movq -0x38(%rbp), %rdi
callq 0x2030
leaq -0x98(%rbp), %r15
movl -0x80(%rbp), %r12d
jmp 0x3d4a
movl %r15d, %edi
callq 0x2130
movq -0x38(%rbp), %rdi
callq 0x2030
leaq 0x30dd(%rip), %rdi      # 0x72b8
callq 0x3493
cmpl $0x0, -0x2c(%rbp)
movq -0x68(%rbp), %r14
jle 0x4250
xorl %r14d, %r14d
leaq 0x30c4(%rip), %rbx      # 0x72b8
leaq 0x1155(%rip), %r15      # 0x5350
leaq 0x305e(%rip), %r12      # 0x7260
leaq 0x1197(%rip), %r13      # 0x53a0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x351d
movq %r12, %rdi
callq 0x2160
movq %r13, %rdi
callq 0x2090
incl %r14d
movl -0x2c(%rbp), %eax
cmpl %eax, %r14d
jl 0x4209
testl %eax, %eax
movq -0x68(%rbp), %r14
jle 0x4250
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2350
incq %rbx
movslq -0x2c(%rbp), %rax
cmpq %rax, %rbx
jl 0x4239
movl -0x40(%rbp), %r15d
testl %r15d, %r15d
jne 0x427e
leaq 0x10ef(%rip), %rsi      # 0x534f
movl $0x6, %edx
movl -0x44(%rbp), %ebx
movl %ebx, %edi
callq 0x20b0
movl %ebx, %edi
callq 0x2130
movl -0x4c(%rbp), %edi
callq 0x2130
leaq 0x2fb3(%rip), %rdi      # 0x7238
callq 0x2110
leaq 0x2fff(%rip), %rdi      # 0x7290
callq 0x2110
leaq 0x2f73(%rip), %rdi      # 0x7210
callq 0x2110
leaq 0x3047(%rip), %rdi      # 0x72f0
callq 0x2110
leaq 0x3063(%rip), %rdi      # 0x7318
callq 0x2110
leaq 0x2f9f(%rip), %rdi      # 0x7260
callq 0x22b0
leaq 0x2ff3(%rip), %rdi      # 0x72c0
callq 0x22b0
movq 0x2fdf(%rip), %rax      # 0x72b8
movq (%rax), %rdi
callq 0x2030
movq 0x2fd0(%rip), %rdi      # 0x72b8
callq 0x2030
callq 0x3415
movq -0xa0(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2030
movq 0x8(%rbx), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x78(%rbp), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
testl %r15d, %r15d
jne 0x4356
movl -0x7c(%rbp), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a0
leaq 0xf7d(%rip), %rdi       # 0x52c2
callq 0x2050
leaq 0xf98(%rip), %rdi       # 0x52e9
callq 0x2050
leaq 0x102c(%rip), %rdi      # 0x5389
callq 0x2090
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2498
movl -0x48(%rbp), %edi
callq 0x2130
leaq 0x2a26(%rip), %rsi      # 0x6db0
leaq -0x110(%rbp), %rax
movl $0x9, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0xf56(%rip), %rdi       # 0x52f9
movq %rax, %rsi
callq 0x2370
movl $0x36, %edi
callq 0x2330
callq 0x2450
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x78(%rbp), %rdi
callq 0x2030
movq -0x70(%rbp), %rdi
callq 0x2030
movl $0x9, %edi
callq 0x2330
callq 0x24f8
callq 0x24e0
callq 0x24c8
callq 0x24b0
callq 0x2420
callq 0x2438
callq 0x2468
callq 0x2480