// File: /workspace/github_cpp_makefile_star2_O3/Mengjintao[P]SWAPCounter/src/kmerGraph.cpp
// Function: kmerGraph::printKmerFreq(parameter*, MPIEnviroment*)
void kmerGraph::printKmerFreq(parameter *parameters, MPIEnviroment *MPIcontrol)
{
	long long int loss = 0;
	long long int missFreq = 0;
	long long int delta = 0;
	
	long long int kmernum=tmp.size(), validkmernum=0, bloomkmer=kmolecules.size();
	for(unordered_map<unsigned long long, unsigned long long>::iterator I=tmp.begin(); I!=tmp.end(); I++)
	{
		if(I->second>=parameters->cutoffThreshold)
		{
			if(kmolecules.find(I->first)==kmolecules.end())	loss++;
			else if(kmolecules[I->first]!=I->second)	missFreq++, delta += abs((int) (kmolecules[I->first]-I->second));
			validkmernum++;
		}	
	}

	printf("Proc %d: tmpkmernum = %lu, validfilterkmernum = %lu, bloomfilterkmernum=%lu\n", MPIcontrol->rank, kmernum, validkmernum, bloomkmer);
	printf("loss(number of validkmer but lost by Bloomfilter)=%lu, miss(the frequence of validkmer wrongly counted by bloom filter)=%lu, delta=%lu\n", loss, missFreq, delta/missFreq);
	long long int totloss=0, totmiss=0, totdelta=0, totkmernum=0, totbloomkmer=0, allkmers=0;	
	MPI_Reduce(&loss, &totloss, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	MPI_Reduce(&missFreq, &totmiss, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	MPI_Reduce(&delta, &totdelta, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	MPI_Reduce(&kmernum, &allkmers, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	MPI_Reduce(&validkmernum, &totkmernum, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	MPI_Reduce(&bloomkmer, &totbloomkmer, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	if(MPIcontrol->rank==0)	printf("totloss=%lu,  totmiss=%lu, totdelta=%.5f\n", totloss, totmiss, totdelta*1.0/totmiss);
	if(MPIcontrol->rank==0)	printf("allkmers=%lu, totfilterkmernum=%lu, totbloomkmernum=%lu\n", allkmers, totkmernum, totbloomkmer);


	ofstream fs(parameters->kmerCount);
      	if(!fs.is_open())  
       	{ 
		cout<<"Error opening file"<<endl; 
		exit(1); 
	}  
	int freqLen = 10000;
	int i=0;
	long long int a[freqLen];
	memset(a, 0, sizeof(long long int)*freqLen);
	for(unordered_map<unsigned long long, unsigned long long>::iterator I=kmolecules.begin(); I!=kmolecules.end(); I++)
	{
/*		unsigned char *p = (unsigned char *) &(I->second);
		int t=0;
		for(int j=0;j<8;j++)	t += p[j];
*/
		unsigned long long t = (unsigned long long) (I->second);
		if(t<freqLen)		a[t]++;	
		i++;	
	}
/*
	long long int sum=0;
        for(int i=1; i<freqLen; i++) sum+=a[i];
        a[1]=sum-a[1];
        for(int i=2;i<freqLen-1;i++) a[i]=a[i-1]-a[i];   //now the real value shift to left 
	for(int i=freqLen-1;i>1;i--) a[i]=a[i-1];
	a[1]=sum;
*/
	long long int b[freqLen];
	MPI_Reduce(&a, &b, freqLen, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD);	
	if(MPIcontrol->rank==0)	{for(int i=1;i<freqLen;i++)	if(b[i])	fs<<i<<" "<<b[i]<<endl;}
	fs.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdx, -0xa0(%rbp)
movq %rdi, %rbx
xorl %r13d, %r13d
movq %r13, -0x80(%rbp)
movq %r13, -0x78(%rbp)
movq %r13, -0x70(%rbp)
movq 0x50(%rdi), %rax
movq %rax, -0x98(%rbp)
movq %rax, -0xc0(%rbp)
movq %r13, -0x68(%rbp)
movq 0x18(%rdi), %rax
movq %rax, -0x90(%rbp)
movq %rax, -0xb8(%rbp)
movq 0x48(%rdi), %r12
movl $0x0, %eax
movq %rax, -0x30(%rbp)
movl $0x0, %eax
movq %rax, -0x38(%rbp)
movl $0x0, %r14d
testq %r12, %r12
movq %rsi, -0x40(%rbp)
je 0x3b84
movq $0x0, -0x30(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
xorl %r14d, %r14d
movslq 0xb0c(%rsi), %rax
cmpq %rax, 0x10(%r12)
jb 0x3b77
leaq 0x8(%r12), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4d82
testq %rax, %rax
je 0x3b5d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4ae0
movq (%rax), %rax
cmpq 0x10(%r12), %rax
je 0x3b6c
movq -0x30(%rbp), %rcx
incq %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, -0x78(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4ae0
movl (%rax), %eax
subl 0x10(%r12), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
addq %rcx, %r13
movq %r13, -0x70(%rbp)
jmp 0x3b6c
movq -0x38(%rbp), %rcx
incq %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, -0x80(%rbp)
incq %r14
movq %r14, -0x68(%rbp)
movq -0x40(%rbp), %rsi
movq (%r12), %r12
testq %r12, %r12
jne 0x3af4
movq %rbx, -0x88(%rbp)
movq -0xa0(%rbp), %r15
movl (%r15), %esi
leaq 0x2844(%rip), %rdi      # 0x63e0
xorl %r12d, %r12d
movq -0x98(%rbp), %rdx
movq %r14, %rcx
movq -0x90(%rbp), %r8
xorl %eax, %eax
callq 0x2050
movq %r13, %rax
cqto
movq -0x30(%rbp), %rcx
idivq %rcx
leaq 0x2863(%rip), %rdi      # 0x642d
movq -0x38(%rbp), %rsi
movq %rcx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2050
leaq -0xb0(%rbp), %rsi
movq %r12, (%rsi)
leaq -0xa8(%rbp), %rbx
movq %r12, (%rbx)
leaq -0x60(%rbp), %rax
movq %r12, (%rax)
leaq -0x58(%rbp), %rax
movq %r12, (%rax)
leaq -0x50(%rbp), %rax
movq %r12, (%rax)
leaq -0x48(%rbp), %rax
movq %r12, (%rax)
subq $0x8, %rsp
movq 0x537a(%rip), %r13      # 0x8f90
movq 0x538b(%rip), %r14      # 0x8fa8
movq 0x534c(%rip), %r12      # 0x8f70
leaq -0x80(%rbp), %rdi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x78(%rbp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0xc0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0xb8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x1, %edx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x10, %rsp
cmpl $0x0, (%r15)
jne 0x3d3c
movq -0xb0(%rbp), %rsi
cvtsi2sdq -0x60(%rbp), %xmm0
movq -0xa8(%rbp), %rdx
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm0
leaq 0x27a0(%rip), %rdi      # 0x64b5
movb $0x1, %al
callq 0x2050
cmpl $0x0, (%r15)
jne 0x3d3c
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
leaq 0x27aa(%rip), %rdi      # 0x64df
xorl %eax, %eax
callq 0x2050
movq -0x40(%rbp), %rsi
addq $0x9f6, %rsi            # imm = 0x9F6
leaq -0x2c0(%rbp), %rdi
movl $0x10, %edx
callq 0x2290
leaq -0x250(%rbp), %rdi
callq 0x2080
testb %al, %al
je 0x3ec0
movq %rsp, -0x40(%rbp)
movq %rsp, %r14
addq $-0x13880, %r14         # imm = 0xFFFEC780
movq %r14, %rsp
movl $0x13880, %edx          # imm = 0x13880
movq %r14, %rdi
xorl %esi, %esi
callq 0x2140
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x3db2
movq 0x10(%rax), %rcx
cmpq $0x270f, %rcx           # imm = 0x270F
ja 0x3dad
incq (%r14,%rcx,8)
movq (%rax), %rax
jmp 0x3d97
movq %rsp, %rbx
addq $-0x13880, %rbx         # imm = 0xFFFEC780
movq %rbx, %rsp
subq $0x8, %rsp
movq 0x51de(%rip), %rcx      # 0x8fa8
movq 0x519f(%rip), %r8       # 0x8f70
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x10, %rsp
cmpl $0x0, (%r15)
jne 0x3e6b
movl $0x1, %r14d
leaq -0x2c0(%rbp), %r15
leaq 0x2729(%rip), %r12      # 0x652d
cmpq $0x0, (%rbx,%r14,8)
je 0x3e5f
movq %r15, %rdi
movl %r14d, %esi
callq 0x2450
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x22c0
movq (%rbx,%r14,8), %rsi
movq %r13, %rdi
callq 0x20d0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2250
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21a0
incq %r14
cmpq $0x2710, %r14           # imm = 0x2710
jne 0x3e04
leaq -0x2c0(%rbp), %rdi
callq 0x23a0
movq -0x40(%rbp), %rsp
movq 0x511e(%rip), %rax      # 0x8fa0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x2b8(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2c0(%rbp,%rcx)
callq 0x21f0
leaq -0x1c8(%rbp), %rdi
callq 0x20a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50f1(%rip), %rdi      # 0x8fb8
leaq 0x264a(%rip), %rsi      # 0x6518
movl $0x12, %edx
callq 0x22c0
movq 0x50d9(%rip), %rdi      # 0x8fb8
callq 0x2110
movl $0x1, %edi
callq 0x2300
jmp 0x3ef2
jmp 0x3ef2
movq %rax, %rbx
leaq -0x2c0(%rbp), %rdi
callq 0x23e0
movq %rbx, %rdi
callq 0x2460
nop