// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
movq %rsi, %r15
movq %rdi, %r12
movslq %edx, %r13
leaq (,%r13,4), %rax
testl %r13d, %r13d
movq $-0x1, %r14
movq %rax, 0x78(%rsp)
cmovnsq %rax, %r14
movq %r14, %rdi
callq 0x2030
movq %rax, %rbx
movq %r14, 0x80(%rsp)
movq %r14, %rdi
callq 0x2030
movl %ebp, 0x1c(%rsp)
movl %ebp, %edx
testl %r13d, %r13d
jle 0x3b1b
xorl %ecx, %ecx
movl %ecx, (%rbx,%rcx,4)
movl %ecx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x3b0d
movq %rdx, 0x10(%rsp)
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x3b84
leaq (%rbx,%r13,4), %r14
bsrq %r13, %rbp
xorl $0x3f, %ebp
addl %ebp, %ebp
xorq $0x7e, %rbp
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x3f6b
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x406f
movq 0x20(%rsp), %r12
leaq (%r12,%r13,4), %r14
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x3f6b
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x406f
movq %rbx, 0x38(%rsp)
leaq 0x50(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movslq 0xc(%rsp), %r13
leaq 0x48(%rsp), %rbx
movq %rbx, 0x40(%rsp)
testl %r13d, %r13d
je 0x3bee
leaq (,%r13,4), %r15
leaq 0x48(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq 0x38(%rsp), %r14
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x4352
addq $0x4, %r14
addq $-0x4, %r15
jne 0x3bd1
movq %rbx, 0x40(%rsp)
cmpl $0x0, 0xc(%rsp)
movq 0x38(%rsp), %rbx
leaq 0x50(%rsp), %r15
je 0x3c32
shlq $0x2, %r13
leaq 0x48(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq 0x20(%rsp), %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x4352
addq $0x4, %r14
addq $-0x4, %r13
jne 0x3c17
movq 0x438f(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
movq 0x70(%rsp), %rcx
leaq 0x196d(%rip), %rsi      # 0x55b5
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x21e0
movq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
movq %rax, %r13
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
movq 0x78(%rsp), %rbp
movq %rbp, %rdx
callq 0x20c0
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20c0
cmpl $0x0, 0x1c(%rsp)
jle 0x3d86
movq 0x58(%rsp), %rax
movq 0x70(%rsp), %rcx
xorl %edx, %edx
xorl %esi, %esi
movq %r15, %rdi
testq %rax, %rax
je 0x3cd7
movl (%rbx,%rdx,4), %r8d
movq %rax, %r9
movq %r15, %rdi
xorl %r10d, %r10d
cmpl %r8d, 0x20(%r9)
setl %r10b
cmovgeq %r9, %rdi
movq 0x10(%r9,%r10,8), %r9
testq %r9, %r9
jne 0x3cbe
movq %r15, %r8
cmpq %r15, %rdi
je 0x3cee
movl (%rbx,%rdx,4), %r8d
cmpl 0x20(%rdi), %r8d
cmovlq %r15, %rdi
movq %rdi, %r8
cmpq %r15, %r8
je 0x3cfe
incl %r14d
movslq (%rbx,%rdx,4), %rdi
movl %r14d, (%r12,%rdi,4)
movq %r15, %rdi
testq %rax, %rax
je 0x3d2e
movq 0x20(%rsp), %rdi
movl (%rdi,%rdx,4), %r8d
movq %rax, %r9
movq %r15, %rdi
xorl %r10d, %r10d
cmpl %r8d, 0x20(%r9)
setl %r10b
cmovgeq %r9, %rdi
movq 0x10(%r9,%r10,8), %r9
testq %r9, %r9
jne 0x3d15
movq %r15, %r8
cmpq %r15, %rdi
je 0x3d4a
movq 0x20(%rsp), %r8
movl (%r8,%rdx,4), %r8d
cmpl 0x20(%rdi), %r8d
cmovlq %r15, %rdi
movq %rdi, %r8
cmpq %r15, %r8
je 0x3d5f
incl %esi
movq 0x20(%rsp), %rdi
movslq (%rdi,%rdx,4), %rdi
movl %esi, (%r13,%rdi,4)
movslq %r14d, %r8
movq %rcx, %r9
xorq %r8, %r9
movslq %esi, %rdi
movq %rcx, %r10
xorq %rdi, %r10
orq %r9, %r10
je 0x3d8b
incq %rdx
cmpq 0x10(%rsp), %rdx
jne 0x3cac
jmp 0x3d8b
xorl %edi, %edi
xorl %r8d, %r8d
movq 0x70(%rsp), %rax
cmpq %r8, %rax
movq 0x20(%rsp), %rbx
jne 0x3ed6
cmpq %rdi, %rax
jne 0x3ed6
movq 0x60(%rsp), %r14
cmpq %r15, %r14
je 0x3e20
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2170
cmpq %r15, %rax
movq %rax, 0x10(%rsp)
je 0x3e00
movslq 0x20(%r14), %rcx
movl (%r12,%rcx,4), %r14d
movl (%r13,%rcx,4), %ebp
leaq 0x50(%rsp), %r15
movslq 0x20(%rax), %rcx
cmpl (%r12,%rcx,4), %r14d
setl %dl
cmpl (%r13,%rcx,4), %ebp
setge %cl
xorb %dl, %cl
movzbl %cl, %ecx
addl %ecx, %ebx
movq %rax, %rdi
callq 0x2170
cmpq %r15, %rax
jne 0x3dd7
jmp 0x3e05
leaq 0x50(%rsp), %r15
movq 0x10(%rsp), %r14
cmpq %r15, %r14
jne 0x3db3
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x20(%rsp), %rbx
jmp 0x3e2a
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rdi
callq 0x21c0
movq %rbx, %rdi
callq 0x21c0
movq %r12, %rdi
callq 0x21c0
movq %r13, %rdi
callq 0x21c0
leaq 0x48(%rsp), %rdi
movq 0x28(%rdi), %rax
movq %rax, %xmm0
movq 0x159e(%rip), %xmm1     # 0x5400
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x15a2(%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 0x1542(%rip), %xmm1     # 0x53e8
movapd %xmm1, %xmm0
subsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
callq 0x3f1c
movaps 0x20(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1703(%rip), %rdi      # 0x55e0
leaq 0x1610(%rip), %rsi      # 0x54f4
leaq 0x171e(%rip), %rcx      # 0x5609
movl $0x101, %edx            # imm = 0x101
callq 0x20d0
jmp 0x3efb
jmp 0x3efb
jmp 0x3efb
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3f1c
movq %rbx, %rdi
callq 0x2240