// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movl %ecx, 0xc(%rsp)
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movslq %edx, %r15
leaq (,%r15,4), %rax
testl %r15d, %r15d
movq $-0x1, %r12
movq %rax, 0x48(%rsp)
cmovnsq %rax, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, %r14
movq %r12, 0x50(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, %r13
movl %ebx, 0x1c(%rsp)
movl %ebx, %ecx
testl %r15d, %r15d
jle 0x3cf8
xorl %eax, %eax
movl %eax, (%r14,%rax,4)
movl %eax, (%r13,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x3ce7
movq %rcx, 0x20(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x3d65
leaq (%r14,%r15,4), %rbx
bsrq %r15, %r12
xorl $0x3f, %r12d
addl %r12d, %r12d
xorq $0x7e, %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x412f
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4233
leaq (,%r15,4), %rbx
addq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %r15
movq %r15, %rcx
callq 0x412f
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x4233
movq %r13, 0x38(%rsp)
leaq 0x60(%rsp), %r15
movl $0x0, (%r15)
xorl %eax, %eax
movq %rax, 0x8(%r15)
movq %r15, 0x10(%r15)
movq %r15, 0x18(%r15)
movq %rax, 0x20(%r15)
leaq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0xc(%rsp), %eax
testl %eax, %eax
movq %r14, 0x10(%rsp)
je 0x3e11
movslq %eax, %r13
movq %r14, %rbx
leaq (,%r13,4), %r14
leaq 0x58(%rsp), %rbp
leaq 0x40(%rsp), %r12
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x4500
addq $0x4, %rbx
addq $-0x4, %r14
jne 0x3db7
movq %rbp, 0x40(%rsp)
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
je 0x3e11
shlq $0x2, %r13
leaq 0x58(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq 0x38(%rsp), %rbx
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x4500
addq $0x4, %rbx
addq $-0x4, %r13
jne 0x3df6
movq 0x41b0(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
movq 0x80(%rsp), %rcx
leaq 0x17eb(%rip), %rsi      # 0x5615
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x21e0
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r12
movq %rbx, %rdi
callq 0x2030
movq %rax, %r13
xorl %ebx, %ebx
movq %r12, %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rbp
movq %rbp, %rdx
callq 0x20c0
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20c0
movq 0x80(%rsp), %rax
cmpl $0x0, 0x1c(%rsp)
jle 0x3f5d
movq 0x68(%rsp), %rcx
xorl %edx, %edx
xorl %esi, %esi
testq %rcx, %rcx
je 0x3f36
movslq (%r14,%rdx,4), %rdi
movq %rcx, %r14
movq %r15, %r11
movq %r11, %r8
movq %r14, %r9
movl 0x20(%r14), %r10d
xorl %r14d, %r14d
cmpl %edi, %r10d
setl %r14b
movq %r9, %r11
cmovlq %r8, %r11
movq 0x10(%r9,%r14,8), %r14
testq %r14, %r14
jne 0x3ea0
cmpq %r15, %r11
je 0x3edd
cmpl %edi, %r10d
cmovlq %r8, %r9
cmpl 0x20(%r9), %edi
jl 0x3edd
incl %ebx
movl %ebx, (%r12,%rdi,4)
movq 0x38(%rsp), %rdi
movslq (%rdi,%rdx,4), %rdi
movq %rcx, %r14
movq %r15, %r11
movq %r11, %r8
movq %r14, %r9
movl 0x20(%r14), %r10d
xorl %r14d, %r14d
cmpl %edi, %r10d
setl %r14b
movq %r9, %r11
cmovlq %r8, %r11
movq 0x10(%r9,%r14,8), %r14
testq %r14, %r14
jne 0x3eec
cmpq %r15, %r11
je 0x3f31
cmpl %edi, %r10d
cmovlq %r8, %r9
cmpl 0x20(%r9), %edi
movq 0x10(%rsp), %r14
jl 0x3f36
incl %esi
movl %esi, (%r13,%rdi,4)
jmp 0x3f36
movq 0x10(%rsp), %r14
movslq %ebx, %r8
movq %rax, %r9
xorq %r8, %r9
movslq %esi, %rdi
movq %rax, %r10
xorq %rdi, %r10
orq %r9, %r10
je 0x3f62
incq %rdx
cmpq 0x20(%rsp), %rdx
jne 0x3e8d
jmp 0x3f62
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %r8, %rax
jne 0x409a
cmpq %rdi, %rax
jne 0x409a
movq 0x70(%rsp), %rbx
cmpq %r15, %rbx
je 0x3fe2
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x2170
cmpq %r15, %rax
je 0x3fdb
movslq 0x20(%rbx), %rcx
movl (%r12,%rcx,4), %ebx
movq %r15, %rbp
movl (%r13,%rcx,4), %r15d
movq %rax, 0x20(%rsp)
movslq 0x20(%rax), %rcx
cmpl (%r12,%rcx,4), %ebx
setl %dl
cmpl (%r13,%rcx,4), %r15d
setge %cl
xorb %dl, %cl
movzbl %cl, %ecx
addl %ecx, %r14d
movq %rax, %rdi
callq 0x2170
cmpq %rbp, %rax
jne 0x3fa3
movq 0x20(%rsp), %rax
movq %rax, %rbx
cmpq %rbp, %rax
movq %rbp, %r15
jne 0x3f81
cvtsi2sd %r14d, %xmm0
jmp 0x3fe6
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
callq 0x21c0
movq 0x38(%rsp), %rdi
callq 0x21c0
movq %r12, %rdi
callq 0x21c0
movq %r13, %rdi
callq 0x21c0
leaq 0x58(%rsp), %rdi
movq 0x28(%rdi), %rax
movq %rax, %xmm0
movq 0x143a(%rip), %xmm1     # 0x5460
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x143e(%rip), %xmm2     # 0x5470
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 0x13d6(%rip), %xmm1     # 0x5440
movapd %xmm1, %xmm0
subsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
callq 0x40e0
movaps 0x20(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x159f(%rip), %rdi      # 0x5640
leaq 0x14ac(%rip), %rsi      # 0x5554
leaq 0x15ba(%rip), %rcx      # 0x5669
movl $0x101, %edx            # imm = 0x101
callq 0x20d0
jmp 0x40bf
jmp 0x40bf
jmp 0x40bf
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x40e0
movq %rbx, %rdi
callq 0x2240