// File: 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
subq $0x360, %rsp            # imm = 0x360
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x300(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
addq $0x38, %rdi
callq 0xd500
movq -0x300(%rbp), %rdi
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
callq 0xd500
movq -0x300(%rbp), %rdi
movq %rax, -0x48(%rbp)
addq $0x38, %rdi
callq 0xd550
movq %rax, -0x50(%rbp)
movq -0x300(%rbp), %rdi
addq $0x38, %rdi
callq 0xd2d0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xd280
testb $0x1, %al
jne 0xbdcd
jmp 0xbef3
leaq -0x50(%rbp), %rdi
callq 0xd580
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0xb0c(%rcx), %rcx
cmpq %rcx, %rax
jb 0xbedd
leaq -0x50(%rbp), %rdi
callq 0xd580
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0xd2a0
movq -0x300(%rbp), %rdi
movq %rax, -0x60(%rbp)
callq 0xd2d0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xd5a0
testb $0x1, %al
jne 0xbe2d
jmp 0xbe3e
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xbed1
leaq -0x50(%rbp), %rdi
callq 0xd580
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0xd250
movq (%rax), %rax
movq %rax, -0x308(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xd580
movq %rax, %rcx
movq -0x308(%rbp), %rax
cmpq 0x8(%rcx), %rax
je 0xbecf
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xd580
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0xd250
movq (%rax), %rax
movq %rax, -0x310(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xd580
movq %rax, %rcx
movq -0x310(%rbp), %rax
subq 0x8(%rcx), %rax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cltq
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xbed1
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xbedf
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xd5c0
movq %rax, -0x70(%rbp)
jmp 0xbda3
movq -0x18(%rbp), %rax
movl (%rax), %esi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
leaq 0xa615(%rip), %rdi      # 0x16521
movb $0x0, %al
callq 0x8040
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq %rax, -0x318(%rbp)
movq -0x30(%rbp), %rax
cqto
idivq -0x28(%rbp)
movq -0x318(%rbp), %rdx
movq %rax, %rcx
leaq 0xa631(%rip), %rdi      # 0x1656e
movb $0x0, %al
callq 0x8040
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movl $0x1, %edx
movq 0x1100c(%rip), %rcx     # 0x1cfa0
movq 0x10fcd(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10feb(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
leaq -0x28(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl $0x1, %edx
movq 0x10fd6(%rip), %rcx     # 0x1cfa0
movq 0x10f97(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10fb5(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
leaq -0x30(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movl $0x1, %edx
movq 0x10f9d(%rip), %rcx     # 0x1cfa0
movq 0x10f5e(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10f7c(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
leaq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x1, %edx
movq 0x10f64(%rip), %rcx     # 0x1cfa0
movq 0x10f25(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10f43(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x1, %edx
movq 0x10f2b(%rip), %rcx     # 0x1cfa0
movq 0x10eec(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10f0a(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
leaq -0x48(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x1, %edx
movq 0x10ef2(%rip), %rcx     # 0x1cfa0
movq 0x10eb3(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
movq 0x10ed1(%rip), %rax     # 0x1cf90
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x88c0
addq $0x10, %rsp
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xc10e
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdx
cvtsi2sdq -0x88(%rbp), %xmm0
movsd 0x9f1e(%rip), %xmm1     # 0x16010
mulsd %xmm1, %xmm0
cvtsi2sdq -0x80(%rbp), %xmm1
divsd %xmm1, %xmm0
leaq 0xa4ef(%rip), %rdi      # 0x165f6
movb $0x1, %al
callq 0x8040
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xc13a
movq -0xa0(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
leaq 0xa4ed(%rip), %rdi      # 0x16620
movb $0x0, %al
callq 0x8040
movq -0x10(%rbp), %rsi
addq $0x9f6, %rsi            # imm = 0x9F6
leaq -0x2a0(%rbp), %rdi
movq %rdi, -0x328(%rbp)
movl $0x10, %edx
callq 0x8730
movq -0x328(%rbp), %rdi
callq 0x84b0
movb %al, -0x319(%rbp)
jmp 0xc171
movb -0x319(%rbp), %al
testb $0x1, %al
jne 0xc1db
jmp 0xc17d
movq 0x10e34(%rip), %rdi     # 0x1cfb8
leaq 0xa4ce(%rip), %rsi      # 0x16659
callq 0x8680
movq %rax, -0x330(%rbp)
jmp 0xc199
movq -0x330(%rbp), %rdi
movq 0x10de1(%rip), %rsi     # 0x1cf88
callq 0x86c0
jmp 0xc1ae
movl $0x1, %edi
callq 0x8800
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2a8(%rbp)
movl %eax, -0x2ac(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x8a40
jmp 0xc437
movl $0x2710, -0x2b0(%rbp)   # imm = 0x2710
movl $0x0, -0x2b4(%rbp)
movl -0x2b0(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x2c0(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x338(%rbp)
movq %rdi, %rsp
movq %rax, -0x2c8(%rbp)
movslq -0x2b0(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x82e0
movq -0x300(%rbp), %rdi
callq 0xd550
movq %rax, -0x2d0(%rbp)
movq -0x300(%rbp), %rdi
callq 0xd2d0
movq %rax, -0x2d8(%rbp)
leaq -0x2d0(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0xd280
testb $0x1, %al
jne 0xc273
jmp 0xc2e0
leaq -0x2d0(%rbp), %rdi
callq 0xd580
movq 0x8(%rax), %rax
movq %rax, -0x2e0(%rbp)
movq -0x2e0(%rbp), %rax
movslq -0x2b0(%rbp), %rcx
cmpq %rcx, %rax
jae 0xc2b7
movq -0x338(%rbp), %rax
movq -0x2e0(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x2b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b4(%rbp)
leaq -0x2d0(%rbp), %rdi
xorl %esi, %esi
callq 0xd5c0
movq %rax, -0x2e8(%rbp)
jmp 0xc247
movq -0x338(%rbp), %rdi
movl -0x2b0(%rbp), %eax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, -0x340(%rbp)
movq %rsi, %rsp
movq %rax, -0x2f0(%rbp)
movl -0x2b0(%rbp), %edx
subq $0x10, %rsp
movq 0x10c6f(%rip), %rcx     # 0x1cf90
movq %rsp, %rax
movq %rcx, (%rax)
movq 0x10c72(%rip), %rcx     # 0x1cfa0
movq 0x10c33(%rip), %r8      # 0x1cf68
xorl %r9d, %r9d
callq 0x88c0
addq $0x10, %rsp
jmp 0xc343
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0xc40e
movl $0x1, -0x2f4(%rbp)
movl -0x2f4(%rbp), %eax
cmpl -0x2b0(%rbp), %eax
jge 0xc40c
movq -0x340(%rbp), %rax
movslq -0x2f4(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xc3f6
movl -0x2f4(%rbp), %esi
leaq -0x2a0(%rbp), %rdi
callq 0x8bf0
movq %rax, -0x348(%rbp)
jmp 0xc39c
movq -0x348(%rbp), %rdi
leaq 0xa2c4(%rip), %rsi      # 0x1666e
callq 0x8680
movq %rax, -0x350(%rbp)
jmp 0xc3b8
movq -0x350(%rbp), %rdi
movq -0x340(%rbp), %rax
movslq -0x2f4(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x8290
movq %rax, -0x358(%rbp)
jmp 0xc3df
movq -0x358(%rbp), %rdi
movq 0x10b9b(%rip), %rsi     # 0x1cf88
callq 0x86c0
jmp 0xc3f4
jmp 0xc3f6
jmp 0xc3f8
movl -0x2f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2f4(%rbp)
jmp 0xc35a
jmp 0xc40e
leaq -0x2a0(%rbp), %rdi
callq 0x89c0
jmp 0xc41c
movq -0x2c0(%rbp), %rax
movq %rax, %rsp
leaq -0x2a0(%rbp), %rdi
callq 0x8a40
movq %rbp, %rsp
popq %rbp
retq
movq -0x2a8(%rbp), %rdi
callq 0x8c20
nopw %cs:(%rax,%rax)