// File: /workspace/github_c_makefile_star2_O3/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), %r13
movq $0x0, (%r13)
movl (%rdi), %edi
callq 0x262b
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, -0x50(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2310
movq %rbx, %rdi
callq 0x2030
leaq 0x3ba8(%rip), %r14      # 0x7290
movq %r15, -0x60(%rbp)
movq %r14, %rdi
callq 0x2390
incl 0x3afe(%rip)            # 0x71f8
movq %r14, %rdi
callq 0x2210
leaq 0x3baf(%rip), %rdi      # 0x72b8
callq 0x35b9
movq %rax, %r12
leaq 0x3ba8(%rip), %rdi      # 0x72c0
callq 0x2160
movq %r14, %rdi
callq 0x2390
decl 0x3acd(%rip)            # 0x71f8
movq %r14, %rdi
callq 0x2210
movq %r12, %rdi
leaq 0x1c13(%rip), %rsi      # 0x5350
callq 0x2180
testl %eax, %eax
je 0x3a08
leaq 0x3b9f(%rip), %rbx      # 0x72f0
movq %rbx, %rdi
callq 0x2390
incl 0x3a91(%rip)            # 0x71f0
movq %rbx, %rdi
callq 0x2210
movq 0x8(%r15), %rsi
movl (%r15), %edx
movq %r12, %rdi
movq %r13, %rcx
callq 0x26bf
testl %eax, %eax
js 0x37e6
movq -0x30(%rbp), %rdi
movq 0x8(%r15), %rsi
movl (%r15), %edx
callq 0x27aa
movq %rax, -0x48(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23a0
movl %eax, -0x38(%rbp)
testl %eax, %eax
js 0x3a36
movq 0x8(%r15), %rdi
callq 0x2fa0
testl %eax, %eax
je 0x3803
movq 0x8(%r15), %rdi
leaq -0x58(%rbp), %rbx
movq %rbx, %rsi
callq 0x21e0
movq %rbx, %rdi
movl $0x4, %esi
movl $0x2, %edx
callq 0x2320
testq %rax, %rax
jne 0x3815
jmp 0x3a3b
leaq 0x1b8b(%rip), %rdi      # 0x5378
callq 0x2090
movq -0x30(%rbp), %rdi
callq 0x2030
movq %r12, %rdi
jmp 0x39f6
movq 0x8(%r15), %rdi
callq 0x2190
testq %rax, %rax
je 0x3a45
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 -0x38(%rbp), %r13d
movl %r13d, %edi
leaq -0x70(%rbp), %rsi
movl $0x10, %edx
callq 0x2340
testl %eax, %eax
jns 0x3862
callq 0x2040
cmpl $0x73, (%rax)
jne 0x3a40
movq %r12, -0x40(%rbp)
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x20f0
leaq 0x1(%rax), %rdx
movl %r13d, %edi
movq %rbx, %rsi
callq 0x20b0
movl %r13d, %edi
movl $0x80, %esi
callq 0x288b
testq %rax, %rax
je 0x3881
movq %rax, %rbx
movq -0x30(%rbp), %rdi
movq 0x10(%r15), %rsi
movq %rax, %rdx
callq 0x2bc4
testl %eax, %eax
js 0x39bd
movl %eax, %r12d
leaq 0x3a60(%rip), %r14      # 0x7318
movq %r14, %rdi
callq 0x2390
movq %rbx, %rdi
callq 0x20f0
subl %r12d, %eax
addl %eax, 0x3923(%rip)      # 0x71f4
movq %r14, %rdi
callq 0x2210
movl $0x0, -0x34(%rbp)
movq %rbx, %rdi
movl %r12d, %esi
leaq -0x34(%rbp), %rdx
callq 0x2d6d
movq %rax, %r13
cmpl $0x0, -0x34(%rbp)
jle 0x39a0
xorl %r15d, %r15d
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq 0x20f0
movq %rax, %r12
movq (%r13,%r15,8), %rdi
callq 0x20f0
leaq (%r12,%rax), %rdi
incq %rdi
callq 0x2220
movq %rax, %r12
movq %rax, -0x58(%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2070
movq (%r13,%r15,8), %rsi
movq %r12, %rdi
callq 0x2310
leaq -0x58(%rbp), %rdi
callq 0x3370
testl %eax, %eax
je 0x395a
movq %r12, %rdi
callq 0x2030
jmp 0x3986
leaq 0x3957(%rip), %rdi      # 0x72b8
movq %r12, %rsi
callq 0x351d
leaq 0x185d(%rip), %rdi      # 0x51cd
movq %r12, %rsi
xorl %eax, %eax
callq 0x2100
leaq 0x38df(%rip), %rdi      # 0x7260
callq 0x2160
movq (%r13,%r15,8), %rdi
callq 0x2030
incq %r15
movslq -0x34(%rbp), %rax
cmpq %rax, %r15
jl 0x38ff
movq %r13, %rdi
callq 0x2030
movq -0x60(%rbp), %r15
leaq -0x30(%rbp), %r13
leaq 0x38d9(%rip), %r14      # 0x7290
movq -0x40(%rbp), %r12
jmp 0x39d1
leaq 0x19a3(%rip), %rdi      # 0x5367
callq 0x2090
movq -0x40(%rbp), %r12
leaq -0x30(%rbp), %r13
movl -0x38(%rbp), %edi
callq 0x2130
movq -0x30(%rbp), %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movq -0x48(%rbp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movq $0x0, -0x30(%rbp)
jmp 0x36ec
movq -0x50(%rbp), %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
leaq 0x1935(%rip), %rdi      # 0x5355
callq 0x2090
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2408
callq 0x23c0
callq 0x23d8
callq 0x23f0