// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r15
leaq -0x30(%rbp), %rax
andq $0x0, (%rax)
movl (%rdi), %edi
callq 0x24d2
movq %rax, %rbx
movq %rax, %rdi
callq 0x20f0
movq %rax, %r14
movq 0x8(%r15), %r12
movq %r12, %rdi
callq 0x20f0
leaq (%r14,%rax), %rdi
addq $0x9, %rdi
callq 0x2220
movq %rax, %r14
movabsq $0x2f2f3a70747468, %rax # imm = 0x2F2F3A70747468
movq %rax, (%r14)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2310
movq %r14, %rdi
callq 0x20f0
movw $0x3a, (%r14,%rax)
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2310
movq %rbx, %rdi
callq 0x2030
movq %r15, -0x50(%rbp)
leaq 0x3e2f(%rip), %rbx      # 0x7290
movq %rbx, %rdi
callq 0x2390
incl 0x3d89(%rip)            # 0x71f8
movq %rbx, %rdi
callq 0x2210
leaq 0x3e3a(%rip), %rdi      # 0x72b8
callq 0x333d
movq %rax, %r14
leaq 0x3e33(%rip), %rdi      # 0x72c0
callq 0x2160
movq %rbx, %rdi
callq 0x2390
decl 0x3d58(%rip)            # 0x71f8
movq %rbx, %rdi
callq 0x2210
movq %r14, %rdi
leaq 0x1e9e(%rip), %rsi      # 0x5350
callq 0x2180
testl %eax, %eax
je 0x374b
leaq 0x3e2a(%rip), %rbx      # 0x72f0
movq %rbx, %rdi
callq 0x2390
incl 0x3d1c(%rip)            # 0x71f0
movq %rbx, %rdi
callq 0x2210
movq 0x8(%r15), %rsi
movl (%r15), %edx
movq %r14, %rdi
leaq -0x30(%rbp), %rcx
callq 0x2527
testl %eax, %eax
js 0x3553
movq -0x30(%rbp), %rdi
movq 0x8(%r15), %rsi
movl (%r15), %edx
callq 0x2610
movq %rax, %r12
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x23a0
movl %eax, -0x34(%rbp)
testl %eax, %eax
js 0x3779
movq 0x8(%r15), %rdi
callq 0x2d79
testl %eax, %eax
je 0x3570
movq 0x8(%r15), %rdi
leaq -0x48(%rbp), %rbx
movq %rbx, %rsi
callq 0x21e0
movq %rbx, %rdi
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
callq 0x2320
testq %rax, %rax
jne 0x3582
jmp 0x3782
leaq 0x1e1e(%rip), %rdi      # 0x5378
callq 0x2090
movq -0x30(%rbp), %rdi
callq 0x2030
movq %r14, %rdi
jmp 0x373c
movq 0x8(%r15), %rdi
callq 0x2190
testq %rax, %rax
je 0x3794
movw $0x2, -0x70(%rbp)
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
leaq -0x6c(%rbp), %rdi
callq 0x21c0
movzwl (%r15), %eax
rolw $0x8, %ax
movw %ax, -0x6e(%rbp)
movl -0x34(%rbp), %ebx
movl %ebx, %edi
leaq -0x70(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x2340
testl %eax, %eax
jns 0x35cb
callq 0x2040
cmpl $0x73, (%rax)
jne 0x378b
movq %r14, -0x60(%rbp)
movq %r12, %rdi
callq 0x20f0
leaq 0x1(%rax), %rdx
movl %ebx, %edi
movq %r12, -0x58(%rbp)
movq %r12, %rsi
callq 0x20b0
movl %ebx, %edi
movl $0x80, %esi
callq 0x26f1
testq %rax, %rax
je 0x35e9
movq %rax, %r12
movq -0x30(%rbp), %rdi
movq 0x10(%r15), %rsi
movq %rax, %rdx
callq 0x29db
testl %eax, %eax
js 0x370a
movl %eax, %r14d
leaq 0x3cf9(%rip), %rbx      # 0x7318
movq %rbx, %rdi
callq 0x2390
movq %r12, %rdi
callq 0x20f0
subl %r14d, %eax
addl %eax, 0x3bbc(%rip)      # 0x71f4
movq %rbx, %rdi
callq 0x2210
andl $0x0, -0x38(%rbp)
movq %r12, %rdi
movl %r14d, %esi
leaq -0x38(%rbp), %rdx
callq 0x2b6e
movq %rax, %r14
xorl %r15d, %r15d
movslq -0x38(%rbp), %rax
cmpq %rax, %r15
jge 0x36fc
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x20f0
movq %rax, %r13
movq (%r14,%r15,8), %rdi
callq 0x20f0
leaq (%rax,%r13), %rdi
incq %rdi
callq 0x2220
movq %rax, %r13
movq %rax, -0x48(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2070
movq (%r14,%r15,8), %rsi
movq %r13, %rdi
callq 0x2310
leaq -0x48(%rbp), %rdi
callq 0x3110
testl %eax, %eax
je 0x36bf
movq %r13, %rdi
callq 0x2030
jmp 0x36eb
leaq 0x3bf2(%rip), %rdi      # 0x72b8
movq %r13, %rsi
callq 0x32a1
leaq 0x1af8(%rip), %rdi      # 0x51cd
movq %r13, %rsi
xorl %eax, %eax
callq 0x2100
leaq 0x3b7a(%rip), %rdi      # 0x7260
callq 0x2160
movq (%r14,%r15,8), %rdi
callq 0x2030
incq %r15
jmp 0x3659
movq %r14, %rdi
callq 0x2030
movq -0x50(%rbp), %r15
jmp 0x3716
leaq 0x1c56(%rip), %rdi      # 0x5367
callq 0x2090
movl -0x34(%rbp), %edi
callq 0x2130
movq -0x30(%rbp), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
andq $0x0, -0x30(%rbp)
jmp 0x345a
movq -0x40(%rbp), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
leaq 0x1bf2(%rip), %rdi      # 0x5355
callq 0x2090
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a22(%rip), %rdi      # 0x51a2
jmp 0x379b
leaq 0x1a20(%rip), %rdi      # 0x51a9
jmp 0x379b
leaq 0x1a33(%rip), %rdi      # 0x51c5
jmp 0x379b
leaq 0x1a1c(%rip), %rdi      # 0x51b7
callq 0x22e0
pushq $0x9
popq %rdi
callq 0x2330