// File: /workspace/github_cpp_makefile_star2_O2/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, -0x58(%rbp)
andq $0x0, -0xa0(%rbp)
movq %rsi, %r15
andq $0x0, -0x98(%rbp)
movq %rdi, %r14
andq $0x0, -0x90(%rbp)
movq 0x50(%rdi), %rdx
andq $0x0, -0x88(%rbp)
movq %rdx, -0xb8(%rbp)
movq 0x18(%rdi), %r8
movq %r8, -0xb0(%rbp)
leaq 0x48(%rdi), %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq (%r13), %r13
testq %r13, %r13
je 0x3a40
movslq 0xb0c(%r15), %rax
cmpq %rax, 0x10(%r13)
jb 0x398b
movq %r8, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
leaq 0x8(%r13), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4aae
testq %rax, %rax
je 0x3a1b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x473a
movq (%rax), %rax
cmpq 0x10(%r13), %rax
je 0x3a15
movq -0x50(%rbp), %rcx
incq %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, -0x98(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x473a
movl (%rax), %eax
subl 0x10(%r13), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq -0x48(%rbp), %rdx
addq %rcx, %rdx
movq %rdx, -0x48(%rbp)
movq %rdx, -0x90(%rbp)
movq -0x40(%rbp), %rbx
jmp 0x3a29
movq -0x40(%rbp), %rbx
incq %rbx
movq %rbx, -0xa0(%rbp)
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %r8
incq %r12
movq %r12, -0x88(%rbp)
jmp 0x398b
movq -0x58(%rbp), %rax
movl (%rax), %esi
leaq 0x2993(%rip), %rdi      # 0x63e0
movq %r12, %rcx
xorl %eax, %eax
callq 0x2040
movq -0x48(%rbp), %rax
cqto
movq -0x50(%rbp), %rcx
idivq %rcx
leaq 0x29c2(%rip), %rdi      # 0x642d
movq %rbx, %rsi
movq %rcx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2040
leaq -0xa8(%rbp), %rsi
andq $0x0, (%rsi)
leaq -0x80(%rbp), %rax
andq $0x0, (%rax)
leaq -0x78(%rbp), %rax
andq $0x0, (%rax)
leaq -0x70(%rbp), %rax
andq $0x0, (%rax)
leaq -0x68(%rbp), %rax
andq $0x0, (%rax)
leaq -0x60(%rbp), %rax
andq $0x0, (%rax)
subq $0x8, %rsp
movq 0x54df(%rip), %r12      # 0x8f98
movq 0x54e8(%rip), %r13      # 0x8fa8
movq 0x54b1(%rip), %rbx      # 0x8f78
leaq -0xa0(%rbp), %rdi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2300
popq %rcx
popq %rdx
subq $0x8, %rsp
leaq -0x98(%rbp), %rdi
leaq -0x80(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2300
popq %rcx
popq %rdx
subq $0x8, %rsp
leaq -0x90(%rbp), %rdi
leaq -0x78(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2300
popq %rcx
popq %rdx
subq $0x8, %rsp
leaq -0xb8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2300
popq %rcx
popq %rdx
subq $0x8, %rsp
leaq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
callq 0x2300
popq %rcx
popq %rdx
subq $0x8, %rsp
leaq -0xb0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
pushq %r12
movq -0x58(%rbp), %r12
callq 0x2300
popq %rcx
popq %rdx
cmpl $0x0, (%r12)
jne 0x3beb
movq -0xa8(%rbp), %rsi
cvtsi2sdq -0x78(%rbp), %xmm0
movq -0x80(%rbp), %rdx
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm0
leaq 0x28f2(%rip), %rdi      # 0x64b5
movb $0x1, %al
callq 0x2040
cmpl $0x0, (%r12)
jne 0x3beb
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
leaq 0x28fb(%rip), %rdi      # 0x64df
xorl %eax, %eax
callq 0x2040
addq $0x9f6, %r15            # imm = 0x9F6
leaq -0x2b8(%rbp), %rdi
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x2260
leaq -0x248(%rbp), %rdi
callq 0x2070
testb %al, %al
je 0x3d11
movq %rsp, %r13
movq %rsp, %r15
addq $-0x13880, %r15         # imm = 0xFFFEC780
movq %r15, %rsp
movl $0x13880, %edx          # imm = 0x13880
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
addq $0x10, %r14
movq (%r14), %r14
testq %r14, %r14
je 0x3c55
movq 0x10(%r14), %rax
cmpq $0x270f, %rax           # imm = 0x270F
ja 0x3c3b
incq (%r15,%rax,8)
jmp 0x3c3b
movq %rsp, %rbx
addq $-0x13880, %rbx         # imm = 0xFFFEC780
movq %rbx, %rsp
subq $0x8, %rsp
movq 0x533b(%rip), %rcx      # 0x8fa8
movq 0x5304(%rip), %r8       # 0x8f78
movq %r15, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %r9d, %r9d
pushq 0x5310(%rip)            # 0x8f98
callq 0x2300
popq %rcx
popq %rdx
cmpl $0x0, (%r12)
je 0x3cc0
leaq -0x2b8(%rbp), %rdi
callq 0x2360
movq %r13, %rsp
leaq -0x2b8(%rbp), %rdi
callq 0x23a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r14
leaq -0x2b8(%rbp), %r15
leaq 0x285b(%rip), %r12      # 0x652d
cmpq $0x2710, %r14           # imm = 0x2710
je 0x3c96
cmpq $0x0, (%rbx,%r14,8)
je 0x3d0c
movq %r15, %rdi
movl %r14d, %esi
callq 0x2410
movq %rax, %rdi
movq %r12, %rsi
callq 0x2230
movq (%rbx,%r14,8), %rsi
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2100
incq %r14
jmp 0x3cd2
movq 0x52a0(%rip), %rdi      # 0x8fb8
leaq 0x27f9(%rip), %rsi      # 0x6518
callq 0x2230
movq %rax, %rdi
callq 0x2100
pushq $0x1
popq %rdi
callq 0x22c0
jmp 0x3d38
jmp 0x3d38
movq %rax, %rbx
leaq -0x2b8(%rbp), %rdi
callq 0x23a0
movq %rbx, %rdi
callq 0x2420
nop