// File: /workspace/github_cpp_makefile_star2_O3/wenleix[P]EdgePPR/src/ParamPPR.cpp
// Function: main
int main(int argc, char** argv) {
	if (argc != 7) {
		fprintf(stderr, "Usage: %s [ParamGraph.txt] [NumUseVtx] [ParameterFile] "
				"[OutputPrefix] [OutFormat: bin/txt] [NumThread]\n",
			    argv[0]);
		return 1;
	}

	FILE *gfile = fopen(argv[1], "r");
	int numUseVtx = atoi(argv[2]);	//	# Useful Vertices
	FILE *paramFile = fopen(argv[3], "r");
	char* outputPrefix = argv[4];
	char *outFormat = argv[5];
	int numThread = atoi(argv[6]);

	assert(strcmp(outFormat, "bin") == 0 || strcmp(outFormat, "txt") == 0);
	ParamGraph *g = ParamGraph::ReadTextFile(gfile);

	int paramId = 0;
	double *pref = new double[g->numVertex];
	memset(pref, 0, sizeof(double) * g->numVertex);
	for (int i = 0; i < numUseVtx; i++)
		pref[i] = 1.0 - DAMP_FACTOR;

	vector<double> params;
	//	Read all the parameters
	while (true) {
		double p;
		if (fscanf(paramFile, "%lf", &p) == EOF)
			break;
		params.push_back(p);
	}

	assert(params.size() % ParamGraph_NumEdgeTypes == 0);
	int numParam = params.size() / ParamGraph_NumEdgeTypes;
	fprintf(stdout, "INFO: # Params = %lu. \n", params.size() / ParamGraph_NumEdgeTypes);
	int *paramIdx = new int[numParam];		//	Random shuffle the parameter indices assigned to each thread, to get better workload balance.
	for (int i = 0; i < numParam; i++)
		paramIdx[i] = i;
	random_shuffle(paramIdx, paramIdx + numParam);

	ThreadConfig *conf = new ThreadConfig[numThread];
	pthread_t *threads = new pthread_t[numThread];
	for (int i = 0; i < numThread; i++) {
		conf[i].threadId = i;
		conf[i].params = &params[0];
		conf[i].start = i * numParam / numThread;
		conf[i].end = (i + 1) * numParam / numThread;
		conf[i].numUseVtx = numUseVtx;
		conf[i].outputPrefix = outputPrefix;
		conf[i].pref = pref;
		conf[i].g = g;
		conf[i].paramIdx = paramIdx;
		conf[i].outputFormat = outFormat;

		//	Start thread
		int rc = pthread_create(&threads[i], NULL /* attr */, WorkerThread, (void*)&conf[i]);
		assert(rc == 0);
	}

	for (int i = 0; i < numThread; i++)
		pthread_join(threads[i], NULL /* retval */);



	delete g;
	delete [] conf;
	delete [] threads;
	delete [] paramIdx;
	fclose(gfile);
	fclose(paramFile);

	//	Finish
	pthread_exit(NULL);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
cmpl $0x7, %edi
je 0x2abe
movq %rbx, %rdi
callq 0x22c0
movl $0x1, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x264e(%rip), %r14      # 0x5117
movq %r14, %rsi
callq 0x2190
movq %rax, 0x48(%rsp)
movq 0x10(%rbx), %rdi
callq 0x2200
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0x2190
movq %rax, %r14
movq 0x20(%rbx), %rax
movq %rax, 0x68(%rsp)
movq 0x28(%rbx), %r15
movq 0x30(%rbx), %rdi
callq 0x2200
movl %eax, 0x4(%rsp)
leaq 0x2605(%rip), %rsi      # 0x5119
movq %r15, 0x58(%rsp)
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
je 0x2b3e
leaq 0x25f1(%rip), %rsi      # 0x511d
movq 0x58(%rsp), %rdi
callq 0x21d0
testl %eax, %eax
jne 0x2f1b
movq 0x48(%rsp), %rdi
callq 0x319c
movq %rax, 0x50(%rsp)
movslq (%rax), %rax
leaq (,%rax,8), %r15
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %r15, %rdi
callq 0x2030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
cmpl $0x0, 0x18(%rsp)
jle 0x2c25
movq 0x18(%rsp), %rax
movl %eax, %ecx
incl %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x4, %rax
movdqa 0x2466(%rip), %xmm1     # 0x5010
xorl %ecx, %ecx
movdqa 0x246c(%rip), %xmm2     # 0x5020
pxor %xmm2, %xmm0
movabsq $0x3fc3333333333334, %rdx # imm = 0x3FC3333333333334
pcmpeqd %xmm3, %xmm3
movdqa 0x2462(%rip), %xmm4     # 0x5030
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x2c04
movq %rdx, (%rbx,%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x2c18
movq %rdx, 0x8(%rbx,%rcx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x2bce
pxor %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movdqa %xmm0, (%r15)
movq $0x0, 0x10(%r15)
leaq 0x2546(%rip), %r12      # 0x5188
leaq 0x70(%rsp), %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2280
cmpl $-0x1, %eax
je 0x2c8a
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x2c7d
movq 0x70(%rsp), %xmm0
movq %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x2c47
movq %r15, %rdi
movq %r13, %rdx
callq 0x2ff8
jmp 0x2c47
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x3, %rsi
movabsq $0x2492492492492493, %rcx # imm = 0x2492492492492493
movq %rsi, %rax
mulq %rcx
movabsq $0x6db6db6db6db6db7, %rax # imm = 0x6DB6DB6DB6DB6DB7
imulq %rsi, %rax
cmpq %rcx, %rax
jae 0x2f3a
movq %rsi, %r13
subq %rdx, %r13
shrq %r13
addq %rdx, %r13
shrq $0x2, %r13
movq 0x52f2(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
leaq 0x24d9(%rip), %rsi      # 0x51b9
movq %r13, %rdx
xorl %eax, %eax
callq 0x21e0
movq %r13, 0x10(%rsp)
shlq $0x20, %r13
movq %r13, %r15
sarq $0x1e, %r15
testq %r13, %r13
movq $-0x1, %rdi
cmovnsq %r15, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rbp
jle 0x2dba
movq 0x10(%rsp), %rax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
incl %eax
andl $-0x2, %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x22c7(%rip), %xmm1     # 0x5010
xorl %ecx, %ecx
movdqa 0x22cd(%rip), %xmm2     # 0x5020
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x22cd(%rip), %xmm4     # 0x5030
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x2d98
movl %ecx, (%rbp,%rcx,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x2dad
leal 0x1(%rcx), %edx
movl %edx, 0x4(%rbp,%rcx,4)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x2d63
btrq $0x20, %r13
testq %r13, %r13
je 0x2e0d
movl $0x4, %r12d
callq 0x2060
cltq
movq %r12, %rcx
sarq $0x2, %rcx
incq %rcx
cqto
idivq %rcx
leaq (,%rdx,4), %rax
cmpq %rax, %r12
je 0x2e04
leaq (%r12,%rbp), %rax
leaq (,%rdx,4), %rcx
addq %rbp, %rcx
movl (%rax), %edx
movl (%rcx), %esi
movl %esi, (%rax)
movl %edx, (%rcx)
addq $0x4, %r12
cmpq %r12, %r15
jne 0x2dca
movslq 0x4(%rsp), %r15
movl $0x48, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %r12
cmovoq %r12, %rax
movq %rax, %rdi
callq 0x2030
movq %rax, 0x40(%rsp)
shlq $0x3, %r15
cmpl $0x0, 0x4(%rsp)
cmovsq %r12, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x60(%rsp)
movq 0x40(%rsp), %rbp
movq 0x38(%rsp), %r15
xorl %r12d, %r12d
cmpq %r12, 0x60(%rsp)
je 0x2f59
movl %r12d, (%rbp)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rbp)
movl %r13d, %eax
cltd
movl 0x4(%rsp), %ecx
idivl %ecx
movl %eax, 0x10(%rbp)
incq %r12
addl 0x10(%rsp), %r13d
movl %r13d, %eax
cltd
idivl %ecx
movl %eax, 0x14(%rbp)
movq 0x18(%rsp), %rax
movl %eax, 0x18(%rbp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rbp)
movq %rbx, 0x28(%rbp)
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rbp)
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rbp)
movq 0x58(%rsp), %rax
movq %rax, 0x38(%rbp)
movq %r15, %rdi
xorl %esi, %esi
leaq -0x5c8(%rip), %rdx      # 0x291c
movq %rbp, %rcx
callq 0x2270
addq $0x8, %r15
addq $0x48, %rbp
testl %eax, %eax
je 0x2e72
leaq 0x22ce(%rip), %rdi      # 0x51d1
leaq 0x2256(%rip), %rsi      # 0x5160
leaq 0x2260(%rip), %rcx      # 0x5171
movl $0x72, %edx
callq 0x20d0
leaq 0x21ff(%rip), %rdi      # 0x5121
leaq 0x2237(%rip), %rsi      # 0x5160
leaq 0x2241(%rip), %rcx      # 0x5171
movl $0x48, %edx
callq 0x20d0
leaq 0x224b(%rip), %rdi      # 0x518c
leaq 0x2218(%rip), %rsi      # 0x5160
leaq 0x2222(%rip), %rcx      # 0x5171
movl $0x5a, %edx
callq 0x20d0
cmpl $0x0, 0x4(%rsp)
movq 0x38(%rsp), %r12
jle 0x2f7f
movl 0x4(%rsp), %ebx
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
incq %r15
cmpq %r15, %rbx
jne 0x2f6c
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3802
movl $0x18, %esi
movq %rbx, %rdi
callq 0x2150
movq 0x40(%rsp), %rdi
callq 0x21c0
movq %r12, %rdi
callq 0x21c0
movq 0x8(%rsp), %rdi
callq 0x21c0
movq 0x48(%rsp), %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2130
xorl %edi, %edi
callq 0x2250
jmp 0x2fd6
jmp 0x2fd6
jmp 0x2fd6
jmp 0x2fd6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2ff0
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq %rbx, %rdi
callq 0x2240