// File: /workspace/github_cpp_makefile_star2_O1/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 0x2aee
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 0x25ee(%rip), %r14      # 0x50e7
movq %r14, %rsi
callq 0x2190
movq %rax, 0x48(%rsp)
movq 0x10(%rbx), %rdi
callq 0x2200
movl %eax, 0xc(%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, 0x8(%rsp)
leaq 0x25a6(%rip), %rsi      # 0x50e9
movq %r15, 0x58(%rsp)
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
je 0x2b6d
leaq 0x2592(%rip), %rsi      # 0x50ed
movq 0x58(%rsp), %rdi
callq 0x21d0
testl %eax, %eax
jne 0x2e4c
movq 0x48(%rsp), %rdi
callq 0x30d0
movq %rax, %r15
movslq (%rax), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, %rbx
movq %r15, 0x50(%rsp)
movslq (%r15), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20c0
cmpl $0x0, 0xc(%rsp)
jle 0x2bd4
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
movabsq $0x3fc3333333333334, %rdx # imm = 0x3FC3333333333334
movq %rdx, (%rbx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x2bc8
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
leaq 0x2569(%rip), %r12      # 0x5158
leaq 0x70(%rsp), %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2280
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x2c3c
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x2c2c
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x2c37
movq %r15, %rdi
movq %r13, %rdx
callq 0x2f2a
cmpl $-0x1, %ebp
jne 0x2bf4
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 0x2e6b
movq %rsi, %r15
subq %rdx, %r15
shrq %r15
addq %rdx, %r15
shrq $0x2, %r15
movq 0x5340(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
leaq 0x24f7(%rip), %rsi      # 0x5189
movq %r15, %rdx
xorl %eax, %eax
callq 0x21e0
movq %r15, 0x18(%rsp)
shlq $0x20, %r15
movq %r15, %r12
sarq $0x1e, %r12
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %r12, %rdi
callq 0x2030
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r13
jle 0x2ce9
movq 0x18(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movl %ecx, (%r13,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x2cdc
btrq $0x20, %r15
testq %r15, %r15
je 0x2d3c
movl $0x4, %r15d
callq 0x2060
cltq
movq %r15, %rcx
sarq $0x2, %rcx
incq %rcx
cqto
idivq %rcx
leaq (,%rdx,4), %rax
cmpq %rax, %r15
je 0x2d33
leaq (%r15,%r13), %rax
leaq (,%rdx,4), %rcx
addq %r13, %rcx
movl (%rax), %edx
movl (%rcx), %esi
movl %esi, (%rax)
movl %edx, (%rcx)
addq $0x4, %r15
cmpq %r15, %r12
jne 0x2cf9
movslq 0x8(%rsp), %r12
movl $0x48, %ecx
movq %r12, %rax
mulq %rcx
movq $-0x1, %r15
cmovoq %r15, %rax
movq %rax, %rdi
callq 0x2030
movq %rax, 0x40(%rsp)
shlq $0x3, %r12
cmpl $0x0, 0x8(%rsp)
cmovsq %r15, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movl 0x8(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x60(%rsp)
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rbp
xorl %r15d, %r15d
cmpq %r15, 0x60(%rsp)
je 0x2e8a
movl %r15d, (%r13)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
movl %r12d, %eax
cltd
movl 0x8(%rsp), %ecx
idivl %ecx
movl %eax, 0x10(%r13)
incq %r15
addl 0x18(%rsp), %r12d
movl %r12d, %eax
cltd
idivl %ecx
movl %eax, 0x14(%r13)
movl 0xc(%rsp), %eax
movl %eax, 0x18(%r13)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%r13)
movq %rbx, 0x28(%r13)
movq 0x50(%rsp), %rax
movq %rax, 0x40(%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%r13)
movq 0x58(%rsp), %rax
movq %rax, 0x38(%r13)
movq %rbp, %rdi
xorl %esi, %esi
leaq -0x4c9(%rip), %rdx      # 0x294c
movq %r13, %rcx
callq 0x2270
addq $0x8, %rbp
addq $0x48, %r13
testl %eax, %eax
je 0x2da1
leaq 0x236d(%rip), %rdi      # 0x51a1
leaq 0x22f5(%rip), %rsi      # 0x5130
leaq 0x22ff(%rip), %rcx      # 0x5141
movl $0x72, %edx
callq 0x20d0
leaq 0x229e(%rip), %rdi      # 0x50f1
leaq 0x22d6(%rip), %rsi      # 0x5130
leaq 0x22e0(%rip), %rcx      # 0x5141
movl $0x48, %edx
callq 0x20d0
leaq 0x22ea(%rip), %rdi      # 0x515c
leaq 0x22b7(%rip), %rsi      # 0x5130
leaq 0x22c1(%rip), %rcx      # 0x5141
movl $0x5a, %edx
callq 0x20d0
cmpl $0x0, 0x8(%rsp)
movq 0x38(%rsp), %r12
jle 0x2eb0
movl 0x8(%rsp), %ebx
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
incq %r15
cmpq %r15, %rbx
jne 0x2e9d
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x362c
movl $0x18, %esi
movq %rbx, %rdi
callq 0x2150
movq 0x40(%rsp), %rdi
callq 0x21c0
movq %r12, %rdi
callq 0x21c0
movq 0x10(%rsp), %rdi
callq 0x21c0
movq 0x48(%rsp), %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2130
xorl %edi, %edi
callq 0x2250
jmp 0x2f07
jmp 0x2f07
jmp 0x2f07
jmp 0x2f07
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2f21
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq %rbx, %rdi
callq 0x2240
nop