// File: /workspace/github_c_makefile_star2_O0/stefpant[P]TCPserver/webcrawler/webcrawler.c
// Function: threadExec
void *threadExec(void * data){
	tdata *mydata = (tdata *)data;
	char *url=NULL;
	char *url_page=NULL;
	char *req=NULL;
	char *buf=NULL;
	char* prt=myitoa(mydata->port);
	char *sturl=malloc((strlen(prt)+strlen(mydata->hostname)+9)*sizeof(char));
	strcpy(sturl,"http://");
	strcat(sturl,mydata->hostname);
	strcat(sturl,":");
	strcat(sturl,prt);
	free(prt);
	while( 1 ){//try to obtain a url and exit if u obtain "exit"
//~~~~~~~~~~~~~~~~thread trying to obtain url~~~~~~~~~~~~~~~~~~~~
		pthread_mutex_lock(&mtxwt);//maybe wait in pool
		waitingThreads++;
		pthread_mutex_unlock(&mtxwt);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		url = obtain(&up);
		pthread_cond_signal(&cv_notfull);
//~~~~~~~~~~~~~~let parent know i got the url~~~~~~~~~~~~~~~~~~~~
		pthread_mutex_lock(&mtxwt);
		waitingThreads--;
		pthread_mutex_unlock(&mtxwt);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		if(!strcmp(url,"exit")) break;

		pthread_mutex_lock(&mtxcounter);
		totalrc++;
		pthread_mutex_unlock(&mtxcounter);

		//printf("%ld:received %s\n",pthread_self(),url);
		if( analyze_url(url,mydata->hostname,mydata->port,&url_page) < 0){
			printf("Wrong url found!\n");
			free(url_page);
			free(url);
			url_page=NULL;
			url = NULL;
			continue;
		}
//!!!never saving in pool urls from pages that already exists in savedir!!!

		req = create_request(url_page,mydata->hostname,mydata->port);
		//printf("%s\n",req);
//~~~~~~~~~~~create socket,connect and send request~~~~~~~~~~~
		int sock;
		struct sockaddr_in server;
		struct sockaddr * serverptr = (struct sockaddr *)&server;
		struct hostent *rem;
		if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
			perror("socket");
			exit(9);
		}

		if(isIP(mydata->hostname)){
			struct in_addr myaddress;
			inet_aton(mydata->hostname,&myaddress);
			rem=gethostbyaddr((const char*)&myaddress,sizeof(myaddress),AF_INET);
			if(rem == NULL){perror("gethostbyaddr");exit(9);}
		}
		else{		
			if( (rem = gethostbyname(mydata->hostname) ) == NULL ) //socket for service port
				{perror("gethostbyname");exit(9);}
		}
		server.sin_family = AF_INET;
		memcpy(&server.sin_addr,rem->h_addr,rem->h_length);
		server.sin_port = htons(mydata->port);

		if( (connect(sock,serverptr,sizeof(server))) <0){
			if(errno != EINPROGRESS){//need to check this for non blocking connection
				perror("connect");
				exit(9);
			}
		}

		write(sock,req,strlen(req)+1);

		while( (buf = read_request(sock,BUFFERSIZE)) == NULL ){}
		//printf("%s\n",buf);

		//save page in savedir(content of page in buf)
		int bufp=save_page(url_page,mydata->savedir,buf);
		if(bufp<0) printf("Can't find page!\n");
		else{
			pthread_mutex_lock(&mtxbytes);
			totalBytes += (strlen(buf) - bufp);//page bytes:requestBytes-headerBytes
			pthread_mutex_unlock(&mtxbytes);
			char **newpages=NULL;
			int newpagescounter=0;
			newpages = search_new_pages(buf,bufp,&newpagescounter);
			//char *testpage=NULL;
			for(int i=0;i<newpagescounter;i++){
				//testpage=malloc((strlen(mydata->savedir)+strlen(newpages[i])+1)*sizeof(char));
				//strcpy(testpage,mydata->savedir);
				//strcat(testpage,newpages[i]);
				//if(0 != access(testpage,F_OK)){//page doesnot exist in savedir,place in pool
				char* newurl=malloc((strlen(sturl)+strlen(newpages[i])+1)*sizeof(char));
				strcpy(newurl,sturl);
				strcat(newurl,newpages[i]);
				if( checkinPU(&newurl)==0 ){
					place(&up, newurl);
					printf("Placing in pool:%s\n",newurl);
					pthread_cond_signal(&cv_notempty);
				}
				else free(newurl);//free only if url exists else save it in placedurls
				//}
				//free(testpage);
				free(newpages[i]);
			}
			free(newpages);
		}

		close(sock);
		free(url_page);
		free(url);
		free(req);
		free(buf);
		url_page=NULL;
		url = NULL;
		req = NULL;
		buf = NULL;
	}
	free(sturl);
	free(url);
	printf("thread exiting...\n");
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
callq 0x24f0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x20e0
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x20e0
movq -0xa0(%rbp), %rdi
addq %rax, %rdi
addq $0x9, %rdi
shlq $0x0, %rdi
callq 0x2220
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x14b0(%rip), %rsi      # 0x51c9
callq 0x2070
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x2310
movq -0x40(%rbp), %rdi
leaq 0x12e8(%rip), %rsi      # 0x5022
callq 0x2310
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2310
movq -0x38(%rbp), %rdi
callq 0x2030
leaq 0x3534(%rip), %rdi      # 0x7290
callq 0x2390
movl 0x3491(%rip), %eax      # 0x71f8
addl $0x1, %eax
movl %eax, 0x3488(%rip)      # 0x71f8
leaq 0x3519(%rip), %rdi      # 0x7290
callq 0x2210
leaq 0x3535(%rip), %rdi      # 0x72b8
callq 0x3ba0
movq %rax, -0x18(%rbp)
leaq 0x352d(%rip), %rdi      # 0x72c0
callq 0x2160
leaq 0x34f1(%rip), %rdi      # 0x7290
callq 0x2390
movl 0x344e(%rip), %eax      # 0x71f8
addl $-0x1, %eax
movl %eax, 0x3445(%rip)      # 0x71f8
leaq 0x34d6(%rip), %rdi      # 0x7290
callq 0x2210
movq -0x18(%rbp), %rdi
leaq 0x15fd(%rip), %rsi      # 0x53c7
callq 0x2180
cmpl $0x0, %eax
jne 0x3dd9
jmp 0x41fb
leaq 0x3510(%rip), %rdi      # 0x72f0
callq 0x2390
movl 0x3405(%rip), %eax      # 0x71f0
addl $0x1, %eax
movl %eax, 0x33fc(%rip)      # 0x71f0
leaq 0x34f5(%rip), %rdi      # 0x72f0
callq 0x2210
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq -0x20(%rbp), %rcx
callq 0x2590
cmpl $0x0, %eax
jge 0x3e55
leaq 0x13aa(%rip), %rdi      # 0x51d1
movb $0x0, %al
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x2030
movq -0x18(%rbp), %rdi
callq 0x2030
movq $0x0, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
jmp 0x3d55
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x26e0
movq %rax, -0x28(%rbp)
leaq -0x54(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23a0
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jge 0x3ea7
leaq 0x134b(%rip), %rdi      # 0x51e3
callq 0x22e0
movl $0x9, %edi
callq 0x2330
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3210
cmpl $0x0, %eax
je 0x3f00
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x21e0
leaq -0x6c(%rbp), %rdi
movl $0x4, %esi
movl $0x2, %edx
callq 0x2320
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x3efe
leaq 0x12fb(%rip), %rdi      # 0x51ea
callq 0x22e0
movl $0x9, %edi
callq 0x2330
jmp 0x3f2f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2190
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
jne 0x3f2d
leaq 0x12da(%rip), %rdi      # 0x51f8
callq 0x22e0
movl $0x9, %edi
callq 0x2330
jmp 0x3f2f
movw $0x2, -0x54(%rbp)
leaq -0x54(%rbp), %rdi
addq $0x4, %rdi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x68(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x21c0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movzwl %ax, %edi
callq 0x20f0
movw %ax, -0x52(%rbp)
movl -0x44(%rbp), %edi
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rsi
movl $0x10, %edx
callq 0x2340
cmpl $0x0, %eax
jge 0x3fa7
callq 0x2040
cmpl $0x73, (%rax)
je 0x3fa5
leaq 0x1270(%rip), %rdi      # 0x5206
callq 0x22e0
movl $0x9, %edi
callq 0x2330
jmp 0x3fa7
movl -0x44(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x20e0
movl -0xac(%rbp), %edi
movq -0xa8(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x20a0
movl -0x44(%rbp), %edi
movl $0x80, %esi
callq 0x27d0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x3ff6
jmp 0x3fdd
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x2d20
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jge 0x4027
leaq 0x11f3(%rip), %rdi      # 0x520e
movb $0x0, %al
callq 0x2100
jmp 0x41aa
leaq 0x32ea(%rip), %rdi      # 0x7318
callq 0x2390
movq -0x30(%rbp), %rdi
callq 0x20e0
movq %rax, %rcx
movslq -0x7c(%rbp), %rax
subq %rax, %rcx
movslq 0x31a7(%rip), %rax      # 0x71f4
addq %rcx, %rax
movl %eax, 0x319e(%rip)      # 0x71f4
leaq 0x32bb(%rip), %rdi      # 0x7318
callq 0x2210
movq $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x7c(%rbp), %esi
leaq -0x8c(%rbp), %rdx
callq 0x2f90
movq %rax, -0x88(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0x419e
movq -0x40(%rbp), %rdi
callq 0x20e0
movq %rax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x20e0
movq -0xb8(%rbp), %rdi
addq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x2220
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2070
movq -0x98(%rbp), %rdi
movq -0x88(%rbp), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x2310
leaq -0x98(%rbp), %rdi
callq 0x3830
cmpl $0x0, %eax
jne 0x4167
movq -0x98(%rbp), %rsi
leaq 0x3179(%rip), %rdi      # 0x72b8
callq 0x3aa0
movq -0x98(%rbp), %rsi
leaq 0x10ce(%rip), %rdi      # 0x5220
movb $0x0, %al
callq 0x2100
leaq 0x3100(%rip), %rdi      # 0x7260
callq 0x2160
jmp 0x4173
movq -0x98(%rbp), %rdi
callq 0x2030
movq -0x88(%rbp), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2030
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x409b
movq -0x88(%rbp), %rdi
callq 0x2030
movl -0x44(%rbp), %edi
callq 0x2130
movq -0x20(%rbp), %rdi
callq 0x2030
movq -0x18(%rbp), %rdi
callq 0x2030
movq -0x28(%rbp), %rdi
callq 0x2030
movq -0x30(%rbp), %rdi
callq 0x2030
movq $0x0, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x3d55
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x18(%rbp), %rdi
callq 0x2030
leaq 0x1020(%rip), %rdi      # 0x5234
movb $0x0, %al
callq 0x2100
xorl %eax, %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)