// File: CominLab[P]QCluster/freqs.cc
// Function: fill_overlap_count_vector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, double*, double*, int, double, double*, double**, double*, bool)
void fill_overlap_count_vector(string seq, string seq_qual, int K,
						double* freq, double *quality_vector, int normalize,
						double pc, double *avg_quality_1, double **avg_quality, 
						double *freq_1, bool redistribute)
{
	int L = seq.length();
	int *kmer = new int[K];
	int *qual = new int[K];
	double *prob = new double[K];
	double *freq_bases = new double[NUM_NT];
	
	//If K==1, freq_1 is NULL and freq_bases is initialized to 1/4 for each base
	if (K==1){
		for (int i=0; i<NUM_NT; i++) freq_bases[i] = 1.0/4.0;
	}//Otherwise, freq_bases is the normalization of freq_1
	else{
		double somma =0;
		for (int i=0; i<NUM_NT; i++) somma += freq_1[i];
		for (int i=0; i<NUM_NT; i++) freq_bases[i] = freq_1[i]/somma;
	}
	
	double readqual=1;
	for(int i=0; i<L; ++i){
		seq[i] = toupper(seq[i]);
	}
	int N = 1;  // number of K-mers
	for(int k=0; k<K; ++k){
		N *= NUM_NT;
	}
	for(int i=0; i<N; ++i){
		freq[i] = pc;  // initialize with the pseudocount
		quality_vector[i] = pc;
	}
	bool valid_kmer = true;
	int index_kmer = 0;
	
	for (int i=0; i<L-K+1; i++){
		valid_kmer = true;
		index_kmer = 0;
		readqual = 1;
		//Fill the kmer, qual and prob vectors
		for (int j=0; j<K; j++){
			if (!is_valid_nt(seq[i+j])){
					valid_kmer = false;
					break;
				}
			kmer[j] = nt2int(seq[i+j]);
			qual[j] = int(seq_qual[i+j])-base;
			prob[j] = (1.0 - pow(10.0,-(qual[j])/10.0));
			readqual *= prob[j];
			//Calculate vector index of the kmer
			index_kmer = NUM_NT*index_kmer + kmer[j];
		}

		if (!valid_kmer) continue;

		freq[index_kmer] += 1;
		quality_vector[index_kmer] += readqual;
		
		if (avg_quality != NULL)
			for (int j=0; j<K; j++) avg_quality[index_kmer][kmer[j]] += qual[j];
			
		if (avg_quality_1 != NULL) 
			avg_quality_1[kmer[0]] += qual[0];
		
		if (!redistribute) continue;
		
		//Redistributing quality: for each letter in the kmer...
		for (int j=0; j<K; j++){
			int original_letter = kmer[j];
			double original_prob = prob[j];
			//...we try to replace it with another
			for (int letter=0; letter<NUM_NT; letter++){
				//we must redistribute also to the same letter
				//if (letter == original_letter) continue;
				kmer[j] = letter;
				//Equally subdivided probability
				//prob[j] = (1.0-original_prob) / double(NUM_NT-1.0);
				//Probability subidivided basing on the letter frequency
				prob[j] = (1.0-original_prob) * double(freq_bases[letter]);
				
				//Recalculation of the new kmer probability
				readqual = 1;
				index_kmer = 0;
				for (int f=0; f<K; f++){
					readqual *= prob[j];
					index_kmer = NUM_NT*index_kmer + kmer[j];
				}
				//Add a little probability to that kmer
				quality_vector[index_kmer] += readqual;
			}
			kmer[j] = original_letter;
			prob[j] = original_prob;
		}
	}
	
	
	switch(normalize){
		case 1:	
			normalize_row(freq, N);
			normalize_row(quality_vector, N);
			break;
		case 2:
			normalize_row_against(freq, quality_vector, N);
			normalize_row(freq, N);
			break;
		case 3:
			normalize_row_against(freq, quality_vector, N);
			normalize_row(freq, N);
			normalize_row_projection(freq, quality_vector, N);
			break;
		default:
			break;
	}
	
	delete[] kmer;
	delete[] qual;
	delete[] prob;
	if (K==1) delete[] freq_bases;
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rsi, -0xd0(%rbp)
movq %rdi, -0xc8(%rbp)
movb 0x28(%rbp), %al
movq 0x20(%rbp), %r10
movq 0x18(%rbp), %r10
movq 0x10(%rbp), %r10
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movsd %xmm0, -0x38(%rbp)
andb $0x1, %al
movb %al, -0x39(%rbp)
callq 0x33a0
movl %eax, -0x40(%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 0x3030
movq %rax, -0x48(%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 0x3030
movq %rax, -0x50(%rbp)
movslq -0x14(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x58(%rbp)
movl $0x20, %edi
callq 0x3030
movq %rax, -0x60(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x7fef
movl $0x0, -0x64(%rbp)
cmpl $0x4, -0x64(%rbp)
jge 0x7fed
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsd 0x3d13(%rip), %xmm0     # 0xbcf0
movsd %xmm0, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x7fc7
jmp 0x805f
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x4, -0x74(%rbp)
jge 0x8026
movq 0x20(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x7ffe
movl $0x0, -0x78(%rbp)
cmpl $0x4, -0x78(%rbp)
jge 0x805d
movq 0x20(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd -0x70(%rbp), %xmm0
movq -0x60(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x802d
jmp 0x805f
movsd 0x2fa1(%rip), %xmm0     # 0xb008
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x80ce
movq -0xc8(%rbp), %rdi
movslq -0x84(%rbp), %rsi
callq 0x34b0
movsbl (%rax), %edi
callq 0x3470
movq -0xc8(%rbp), %rdi
movb %al, -0xd1(%rbp)
movslq -0x84(%rbp), %rsi
callq 0x34b0
movb -0xd1(%rbp), %cl
movb %cl, (%rax)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x8076
movl $0x1, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x810d
movl -0x88(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x88(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x80e2
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x8160
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0x90(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x38(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x90(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x8117
movb $0x1, -0x91(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl -0x40(%rbp), %ecx
subl -0x14(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x8568
movb $0x1, -0x91(%rbp)
movl $0x0, -0x98(%rbp)
movsd 0x2e5d(%rip), %xmm0     # 0xb008
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x82f5
movq -0xc8(%rbp), %rdi
movl -0x9c(%rbp), %eax
addl -0xa0(%rbp), %eax
movslq %eax, %rsi
callq 0x34b0
movsbl (%rax), %edi
callq 0x86a0
testb $0x1, %al
jne 0x81fc
movb $0x0, -0x91(%rbp)
jmp 0x82f5
movq -0xc8(%rbp), %rdi
movl -0x9c(%rbp), %eax
addl -0xa0(%rbp), %eax
movslq %eax, %rsi
callq 0x34b0
movsbl (%rax), %edi
callq 0x8710
movq -0xd0(%rbp), %rdi
movl %eax, %edx
movq -0x48(%rbp), %rax
movslq -0xa0(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x9c(%rbp), %eax
addl -0xa0(%rbp), %eax
movslq %eax, %rsi
callq 0x34b0
movsbl (%rax), %edx
subl $0x21, %edx
movq -0x50(%rbp), %rax
movslq -0xa0(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rcx
movslq -0xa0(%rbp), %rdx
xorl %eax, %eax
subl (%rcx,%rdx,4), %eax
cvtsi2sd %eax, %xmm1
movsd 0x3906(%rip), %xmm0     # 0xbb80
divsd %xmm0, %xmm1
movsd 0x38fa(%rip), %xmm0     # 0xbb80
callq 0x3410
movaps %xmm0, %xmm1
movsd 0x2d72(%rip), %xmm0     # 0xb008
subsd %xmm1, %xmm0
movq -0x58(%rbp), %rax
movslq -0xa0(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movslq -0xa0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x98(%rbp), %eax
shll $0x2, %eax
movq -0x48(%rbp), %rcx
movslq -0xa0(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0x98(%rbp)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x81ba
testb $0x1, -0x91(%rbp)
jne 0x8303
jmp 0x8554
movq -0x20(%rbp), %rax
movslq -0x98(%rbp), %rcx
movsd 0x2cf2(%rip), %xmm0     # 0xb008
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x98(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x18(%rbp)
je 0x83a1
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x839f
movq -0x50(%rbp), %rax
movslq -0xa4(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq 0x18(%rbp), %rax
movslq -0x98(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movslq -0xa4(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x834b
jmp 0x83a1
cmpq $0x0, 0x10(%rbp)
je 0x83c5
movq -0x50(%rbp), %rax
cvtsi2sdl (%rax), %xmm0
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq (%rcx), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
testb $0x1, -0x39(%rbp)
jne 0x83d0
jmp 0x8554
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8552
movq -0x48(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xac(%rbp)
movq -0x58(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
cmpl $0x4, -0xbc(%rbp)
jge 0x8512
movl -0xbc(%rbp), %edx
movq -0x48(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movsd 0x2bc0(%rip), %xmm0     # 0xb008
subsd -0xb8(%rbp), %xmm0
movq -0x60(%rbp), %rax
movslq -0xbc(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq -0x58(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x2b90(%rip), %xmm0     # 0xb008
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x84e4
movq -0x58(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x98(%rbp), %eax
shll $0x2, %eax
movq -0x48(%rbp), %rcx
movslq -0xa8(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0x98(%rbp)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x8491
movsd -0x80(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x98(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x841f
movl -0xac(%rbp), %edx
movq -0x48(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movsd -0xb8(%rbp), %xmm0
movq -0x58(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x83da
jmp 0x8554
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x817b
movl -0x2c(%rbp), %eax
movl %eax, -0xd8(%rbp)
subl $0x1, %eax
je 0x8592
jmp 0x8578
movl -0xd8(%rbp), %eax
subl $0x2, %eax
je 0x85b2
jmp 0x8585
movl -0xd8(%rbp), %eax
subl $0x3, %eax
je 0x85d6
jmp 0x860d
movq -0x20(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x7d20
movq -0x28(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x7d20
jmp 0x860f
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x88(%rbp), %edx
callq 0x7da0
movq -0x20(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x7d20
jmp 0x860f
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x88(%rbp), %edx
callq 0x7da0
movq -0x20(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x7d20
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x88(%rbp), %edx
callq 0x7e20
jmp 0x860f
jmp 0x860f
movq -0x48(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x862c
movq -0xe0(%rbp), %rdi
callq 0x3350
movq -0x50(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0x8649
movq -0xe8(%rbp), %rdi
callq 0x3350
movq -0x58(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x8666
movq -0xf0(%rbp), %rdi
callq 0x3350
cmpl $0x1, -0x14(%rbp)
jne 0x868b
movq -0x60(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, %rax
je 0x8689
movq -0xf8(%rbp), %rdi
callq 0x3350
jmp 0x868b
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)