// File: 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
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x20(%rbp)
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x54f1
movl -0x2c(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x54c2
movq -0x20(%rbp), %rax
movq %rax, -0x148(%rbp)
movslq -0x14(%rbp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x140(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x5c10
movq -0x148(%rbp), %rdi
movq -0x140(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x5bc0
movq -0x28(%rbp), %rax
movq %rax, -0x138(%rbp)
movslq -0x14(%rbp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x130(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x5c10
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x5bc0
leaq -0x70(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x5c30
movq -0x128(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x18(%rbp), %rax
leaq (%rsi,%rax,4), %rdx
callq 0x5c50
jmp 0x5597
movq -0x28(%rbp), %rsi
movslq -0x18(%rbp), %rax
leaq (%rsi,%rax,4), %rdx
leaq -0x70(%rbp), %rdi
callq 0x5c50
jmp 0x55ae
movq 0x6a13(%rip), %rax      # 0xbfc8
movq (%rax), %rax
movq %rax, -0x160(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x154(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c80
movq -0x160(%rbp), %rdi
movl -0x154(%rbp), %edx
movq %rax, %rcx
leaq 0x2fcd(%rip), %rsi      # 0x85b5
xorl %eax, %eax
callq 0x2280
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x150(%rbp)
jmp 0x5617
movq -0x150(%rbp), %rax
movq %rax, -0x88(%rbp)
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x168(%rbp)
jmp 0x564d
movq -0x168(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movslq -0x14(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x20d0
movq -0x90(%rbp), %rdi
movslq -0x14(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x20d0
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5840
movq -0x20(%rbp), %rax
movslq -0x9c(%rbp), %rcx
leaq (%rax,%rcx,4), %rsi
leaq -0x70(%rbp), %rdi
callq 0x5cd0
movq %rax, -0x170(%rbp)
jmp 0x56d5
movq -0x170(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5d10
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5ca0
testb $0x1, %al
jne 0x570c
jmp 0x5756
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %edx
movq -0x88(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x9c(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x5756
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5dd0
jmp 0x5bab
movq -0x28(%rbp), %rax
movslq -0x9c(%rbp), %rcx
leaq (%rax,%rcx,4), %rsi
leaq -0x70(%rbp), %rdi
callq 0x5cd0
movq %rax, -0x178(%rbp)
jmp 0x5777
movq -0x178(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5d10
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x5ca0
testb $0x1, %al
jne 0x57ae
jmp 0x57dc
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %edx
movq -0x90(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x9c(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movslq -0x94(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c80
movq %rax, %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
jne 0x582a
movslq -0x98(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c80
movq %rax, %rcx
movq -0x188(%rbp), %rax
cmpq %rcx, %rax
jne 0x582a
jmp 0x5840
jmp 0x582c
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x56a5
movslq -0x94(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c80
movq -0x198(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x189(%rbp)
jne 0x589b
movslq -0x98(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c80
movq %rax, %rcx
movq -0x1a0(%rbp), %rax
cmpq %rcx, %rax
sete %al
movb %al, -0x189(%rbp)
movb -0x189(%rbp), %al
testb $0x1, %al
jne 0x58a7
jmp 0x58a9
jmp 0x58c8
leaq 0x2d30(%rip), %rdi      # 0x85e0
leaq 0x2c3d(%rip), %rsi      # 0x84f4
movl $0x101, %edx            # imm = 0x101
leaq 0x2d46(%rip), %rcx      # 0x8609
callq 0x20f0
movl $0x0, -0xc4(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5d40
movq %rax, -0xd0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5d10
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x5ca0
testb $0x1, %al
jne 0x590e
jmp 0x5a41
movq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
callq 0x5d70
movq %rax, -0xe8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5d10
movq %rax, -0xf0(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x5ca0
testb $0x1, %al
jne 0x595d
jmp 0x5a25
leaq -0xd0(%rbp), %rdi
callq 0x5db0
movl (%rax), %eax
movl %eax, -0xf4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x5db0
movl (%rax), %eax
movl %eax, -0xf8(%rbp)
movq -0x88(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x88(%rbp), %rcx
movslq -0xf8(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
setl %al
andb $0x1, %al
movb %al, -0xf9(%rbp)
movq -0x90(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x90(%rbp), %rcx
movslq -0xf8(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
setl %al
andb $0x1, %al
movb %al, -0xfa(%rbp)
movb -0xf9(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb -0xfa(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x5a09
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x5a0b
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
callq 0x5d70
movq %rax, -0x108(%rbp)
jmp 0x5931
jmp 0x5a27
leaq -0xd0(%rbp), %rdi
xorl %esi, %esi
callq 0x5d70
movq %rax, -0x110(%rbp)
jmp 0x58e2
movq -0x20(%rbp), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, %rax
je 0x5a5e
movq -0x1a8(%rbp), %rdi
callq 0x2250
movq -0x28(%rbp), %rax
movq %rax, -0x1b0(%rbp)
cmpq $0x0, %rax
je 0x5a7b
movq -0x1b0(%rbp), %rdi
callq 0x2250
movq -0x88(%rbp), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0x5a9b
movq -0x1b8(%rbp), %rdi
callq 0x2250
movq -0x90(%rbp), %rax
movq %rax, -0x1c0(%rbp)
cmpq $0x0, %rax
je 0x5abb
movq -0x1c0(%rbp), %rdi
callq 0x2250
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x5c80
movq -0x1f8(%rbp), %rdi
movq %rax, %xmm0
movaps 0x2922(%rip), %xmm1     # 0x8400
movaps %xmm1, -0x1f0(%rbp)
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x291f(%rip), %xmm1     # 0x8410
movaps %xmm1, -0x1e0(%rbp)
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x1d0(%rbp)
callq 0x5c80
movaps -0x1f0(%rbp), %xmm3
movaps -0x1e0(%rbp), %xmm2
movsd -0x1d0(%rbp), %xmm0
decq %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd 0x2897(%rip), %xmm1     # 0x83e8
divsd %xmm1, %xmm0
movsd %xmm0, -0x118(%rbp)
movsd -0x118(%rbp), %xmm0
cvtsi2sdl -0xc4(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x120(%rbp)
movsd -0x120(%rbp), %xmm0
divsd -0x118(%rbp), %xmm0
movsd %xmm0, -0x1c8(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5dd0
movsd -0x1c8(%rbp), %xmm0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)
nop