// File: /workspace/github_cpp_makefile_star2_O1/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 $0x278, %rsp            # imm = 0x278
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq 0x50(%rdi), %rcx
movq %rcx, -0x80(%rbp)
movq %rax, -0x30(%rbp)
movq 0x18(%rdi), %rax
movq %rax, -0x78(%rbp)
movq 0x48(%rdi), %r12
testq %r12, %r12
je 0x3b1d
movslq 0xb0c(%r15), %rax
cmpq %rax, 0x10(%r12)
jb 0x3b14
leaq 0x8(%r12), %rbx
movq (%rbx), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq 0x8(%r14)
movq %r14, %rdi
movq %rdx, %rsi
movq %rbx, %rdx
callq 0x4b68
testq %rax, %rax
je 0x3b0c
cmpq $0x0, (%rax)
je 0x3b0c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4a56
movq (%rax), %rax
cmpq 0x10(%r12), %rax
je 0x3b10
incq -0x40(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4a56
movl (%rax), %eax
subl 0x10(%r12), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
addq %rcx, -0x38(%rbp)
jmp 0x3b10
incq -0x48(%rbp)
incq -0x30(%rbp)
movq (%r12), %r12
jmp 0x3a93
movl (%r13), %esi
leaq -0x80(%rbp), %rax
movq (%rax), %rdx
leaq -0x30(%rbp), %rax
movq (%rax), %rcx
leaq -0x78(%rbp), %rax
movq (%rax), %r8
leaq 0x28a3(%rip), %rdi      # 0x63e0
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2050
leaq -0x48(%rbp), %rax
movq (%rax), %rsi
leaq -0x40(%rbp), %rax
movq (%rax), %rcx
leaq -0x38(%rbp), %rax
movq (%rax), %rax
cqto
idivq %rcx
leaq 0x28c6(%rip), %rdi      # 0x642d
movq %rcx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2050
leaq -0x98(%rbp), %rsi
movq %rbx, (%rsi)
movq %r13, -0x90(%rbp)
leaq -0x70(%rbp), %rax
movq %rbx, (%rax)
leaq -0x68(%rbp), %rax
movq %rbx, (%rax)
leaq -0x60(%rbp), %rax
movq %rbx, (%rax)
leaq -0x58(%rbp), %rax
movq %rbx, (%rax)
leaq -0x50(%rbp), %rax
movq %rbx, (%rax)
subq $0x8, %rsp
movq 0x53dd(%rip), %r13      # 0x8f90
movq 0x53ee(%rip), %r12      # 0x8fa8
movq 0x53af(%rip), %rbx      # 0x8f70
leaq -0x48(%rbp), %rdi
movl $0x1, %edx
movq %r12, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x1, %edx
movq %r12, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movl $0x1, %edx
movq %r12, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x1, %edx
movq %r12, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x30(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x1, %edx
movq %r12, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x2340
addq $0x8, %rsp
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x1, %edx
movq %r12, %rcx
movq %r13, %r12
movq -0x90(%rbp), %r13
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2340
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x3cbc
movq -0x98(%rbp), %rsi
cvtsi2sdq -0x68(%rbp), %xmm0
movq -0x70(%rbp), %rdx
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm0
leaq 0x2800(%rip), %rdi      # 0x64b5
movb $0x1, %al
callq 0x2050
cmpl $0x0, (%r13)
jne 0x3cdd
movq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq 0x2809(%rip), %rdi      # 0x64df
xorl %eax, %eax
callq 0x2050
addq $0x9f6, %r15            # imm = 0x9F6
leaq -0x298(%rbp), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x2290
leaq -0x228(%rbp), %rdi
callq 0x2080
testb %al, %al
je 0x3e5e
movq %rsp, -0x88(%rbp)
movq %rsp, %rbx
addq $-0x13880, %rbx         # imm = 0xFFFEC780
movq %rbx, %rsp
movl $0x13880, %edx          # imm = 0x13880
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x3d4e
movq 0x10(%rax), %rcx
cmpq $0x270f, %rcx           # imm = 0x270F
ja 0x3d49
incq (%rbx,%rcx,8)
movq (%rax), %rax
jmp 0x3d33
movq %rsp, %r14
addq $-0x13880, %r14         # imm = 0xFFFEC780
movq %r14, %rsp
subq $0x8, %rsp
movq 0x5242(%rip), %rcx      # 0x8fa8
movq 0x5203(%rip), %r8       # 0x8f70
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %r9d, %r9d
pushq %r12
callq 0x2340
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x3e06
movl $0x1, %ebx
leaq -0x298(%rbp), %r15
leaq 0x278d(%rip), %r12      # 0x652d
cmpq $0x0, (%r14,%rbx,8)
je 0x3dfa
movq %r15, %rdi
movl %ebx, %esi
callq 0x2450
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x22c0
movq (%r14,%rbx,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 %rbx
cmpq $0x2710, %rbx           # imm = 0x2710
jne 0x3da0
leaq -0x298(%rbp), %rdi
callq 0x23a0
movq -0x88(%rbp), %rsp
movq 0x5180(%rip), %rax      # 0x8fa0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x290(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x298(%rbp,%rcx)
callq 0x21f0
leaq -0x1a0(%rbp), %rdi
callq 0x20a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5153(%rip), %rdi      # 0x8fb8
leaq 0x26ac(%rip), %rsi      # 0x6518
movl $0x12, %edx
callq 0x22c0
movq 0x513b(%rip), %rdi      # 0x8fb8
callq 0x2110
movl $0x1, %edi
callq 0x2300
jmp 0x3e90
jmp 0x3e90
movq %rax, %rbx
leaq -0x298(%rbp), %rdi
callq 0x23e0
movq %rbx, %rdi
callq 0x2460
nop