// File: /workspace/github_cpp_makefile_star2_O2/wenleix[P]EdgePPR/src/PPRCommon.cpp
// Function: KendalTau(double*, double*, int, int)
double KendalTau(double *trueprv, double *estprv, int n, int k) {
	int *idx1 = new int[n];
	int *idx2 = new int[n];
	for (int i = 0; i < n; i++) {
		idx1[i] = i;
		idx2[i] = i;
	}
	sort(idx1, idx1 + n, IndexSortHelper(trueprv));
	sort(idx2, idx2 + n, IndexSortHelper(estprv));

	set<int> both;
	both.insert(idx1, idx1 + k);
	both.insert(idx2, idx2 + k);
	fprintf(stdout, "INFO: Size of union for two top %d: %lu .\n", k, both.size());

	int *r1 = new int[n];
	int *r2 = new int[n];
	memset(r1, 0, sizeof(int) * n);
	memset(r2, 0, sizeof(int) * n);
	int cc1 = 0, cc2 = 0;
	for (int i = 0; i < n; i++) {
		if (both.find(idx1[i]) != both.end()) {
			cc1++;
			r1[idx1[i]] = cc1;
		}
		if (both.find(idx2[i]) != both.end()) {
			cc2++;
			r2[idx2[i]] = cc2;
		}
		if (cc1 == both.size() && cc2 == both.size())
			break;
	}
	assert(cc1 == both.size() && cc2 == both.size());

	int cor = 0;
	for (set<int>::iterator it1 = both.begin(); it1 != both.end(); it1++) {
		set<int>::iterator it2 = it1; it2++;
		for (; it2 != both.end(); it2++) {
			int id1 = *it1;
			int id2 = *it2;
			bool cmp1 = r1[id1] < r1[id2];
			bool cmp2 = r2[id1] < r2[id2];
			if (cmp1 == cmp2)
				cor++;
		}
	}

    delete [] idx1;
    delete [] idx2;
    delete [] r1;
    delete [] r2;

//    fprintf(stderr, "DEBUG: Kendau-Tau finished!.\n");


	double allp = (double)(both.size()) * (both.size() - 1) / 2.0;
	double wrong = allp - cor;
	return wrong / allp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, (%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movslq %edx, %rbx
leaq (,%rbx,4), %rax
xorl %r14d, %r14d
testl %ebx, %ebx
pushq $-0x1
popq %r12
movq %rax, 0x28(%rsp)
cmovnsq %rax, %r12
movl $0x0, %r15d
cmovgl %ebx, %r15d
movq %r12, %rdi
callq 0x2030
movq %rax, %r13
movq %r12, %rdi
callq 0x2030
cmpq %r14, %r15
je 0x3a25
movl %r14d, (%r13,%r14,4)
movl %r14d, (%rax,%r14,4)
incq %r14
jmp 0x3a12
movq %r15, 0x30(%rsp)
leaq (,%rbx,4), %rsi
addq %r13, %rsi
movq %r13, %rdi
movq %rbp, %rdx
movq %rax, %rbp
callq 0x3d51
leaq (,%rbx,4), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
movq 0x10(%rsp), %rdx
callq 0x3d51
leaq 0x40(%rsp), %rbx
andl $0x0, (%rbx)
andq $0x0, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
movl (%rsp), %r15d
movslq %r15d, %r14
leaq (,%r14,4), %rdx
addq %r13, %rdx
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x4222
leaq (,%r14,4), %rdx
addq %rbp, %rdx
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x4222
movq %rbp, 0x18(%rsp)
movq 0x4510(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
movq 0x60(%rsp), %rcx
leaq 0x1aee(%rip), %rsi      # 0x55b5
movl %r15d, %edx
xorl %eax, %eax
callq 0x21f0
movq %r12, %rdi
callq 0x2030
movq %rax, %r15
movq %r13, 0x20(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, %r12
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %r13
movq %r13, %rdx
callq 0x20d0
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20d0
movq 0x30(%rsp), %r13
shlq $0x2, %r13
xorl %edi, %edi
movl $0x0, (%rsp)
cmpq %r14, %r13
je 0x3ba0
movl %edi, 0x10(%rsp)
movq 0x20(%rsp), %rax
leaq (%rax,%r14), %rbp
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x4474
cmpq %rbx, %rax
je 0x3b54
movl (%rsp), %ecx
incl %ecx
movslq (%rbp), %rax
movl %ecx, (%rsp)
movl %ecx, (%r15,%rax,4)
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rbp
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x4474
cmpq %rbx, %rax
movl 0x10(%rsp), %edi
je 0x3b7d
incl %edi
movslq (%rbp), %rax
movl %edi, (%r12,%rax,4)
movslq (%rsp), %rax
movq 0x60(%rsp), %rdx
movq %rdx, %rsi
xorq %rax, %rsi
movslq %edi, %rcx
xorq %rcx, %rdx
addq $0x4, %r14
orq %rsi, %rdx
movq %rax, %rdx
jne 0x3b20
jmp 0x3bac
movq 0x60(%rsp), %rdx
movslq (%rsp), %rax
movslq %edi, %rcx
cmpq %rax, %rdx
jne 0x3cbe
cmpq %rcx, %rdx
jne 0x3cbe
xorl %r14d, %r14d
movq 0x50(%rsp), %r13
cmpq %rbx, %r13
je 0x3c12
movq %r13, %rdi
callq 0x2180
movq %rax, %rbp
cmpq %rbx, %rax
je 0x3c0d
movslq 0x20(%r13), %rcx
movslq 0x20(%rax), %rdx
movl (%r15,%rcx,4), %esi
cmpl (%r15,%rdx,4), %esi
setl %sil
movl (%r12,%rcx,4), %ecx
cmpl (%r12,%rdx,4), %ecx
setge %cl
xorb %sil, %cl
movzbl %cl, %ecx
addl %ecx, %r14d
movq %rax, %rdi
callq 0x2180
jmp 0x3bd6
movq %rbp, %r13
jmp 0x3bc6
movq 0x20(%rsp), %rdi
callq 0x21d0
movq 0x18(%rsp), %rdi
callq 0x21d0
movq %r15, %rdi
callq 0x21d0
movq %r12, %rdi
callq 0x21d0
leaq 0x38(%rsp), %rdi
movq 0x28(%rdi), %rax
movq %rax, %xmm0
movq 0x17b4(%rip), %xmm1     # 0x5400
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x17b8(%rip), %xmm2     # 0x5410
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
decq %rax
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd %xmm3, %xmm1
mulsd 0x1750(%rip), %xmm1     # 0x53e0
cvtsi2sd %r14d, %xmm0
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
divsd %xmm1, %xmm2
movapd %xmm2, (%rsp)
callq 0x3cfa
movaps (%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x191b(%rip), %rdi      # 0x55e0
leaq 0x1828(%rip), %rsi      # 0x54f4
leaq 0x1936(%rip), %rcx      # 0x5609
movl $0x101, %edx            # imm = 0x101
callq 0x20e0
jmp 0x3ce5
jmp 0x3ce5
jmp 0x3ce5
jmp 0x3ce5
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3cfa
movq %rbx, %rdi
callq 0x2250