// File: /workspace/github_cpp_makefile_star2_O2/Mengjintao[P]SWAPCounter/src/kmerGraph.cpp
// Function: kmerGraph::addKmers(char const*, char const*, unsigned long long*, unsigned char*, parameter*)
long long kmerGraph::addKmers(const char *s, const char *end, unsigned long long *kmers, unsigned char *arcs, parameter *parameters)
{
//	if(s>end || end-s <= parameters->hashLength)	return 0;
	unsigned long long curNodeID=0,     primeNodeID=0,   	twinNodeID=0;
	unsigned long long preNodeID=0,     prePrimeNodeID=0; 
	unsigned long long kmer_index=0;
//	for(int i=0; i<end-s; i++)	printf("%c", *(s+i));
//	printf("\n");
	
	curNodeID = this->stringToLongLong(s, 0, parameters->hashLength-1, parameters);
        for(int j=parameters->hashLength-1; s+j<end; j++) 
	{
		if(!(s[j]=='A'||s[j]=='T'||s[j]=='C'||s[j]=='G'))	continue;	
		assert(s[j]=='A'||s[j]=='T'||s[j]=='C'||s[j]=='G');
	       	curNodeID <<= 2;
               	curNodeID |= (parameters->nucleotideValue[(int) s[j]]&(3ull));
               	curNodeID &= parameters->MASK;
		
               	twinNodeID  = this->reverseComplement(curNodeID, parameters);
               	primeNodeID = max(curNodeID, twinNodeID);
//		cout<<longLongToString(curNodeID, parameters)<<" "<<longLongToString(primeNodeID, parameters)<<endl;
	
		if(j-parameters->hashLength+1==0)	{
			preNodeID = curNodeID;
			prePrimeNodeID = primeNodeID; 
			kmers[kmer_index] = primeNodeID;	arcs[kmer_index] = 0;
			kmer_index ++;
			continue;
		}
		kmers[kmer_index]  = primeNodeID;	arcs[kmer_index]  = 0;

		int pos_ret;
		//preNode -> curNode  add(A, B, +, +, last(B+))
		if( preNodeID == prePrimeNodeID && curNodeID == primeNodeID)
		{
			pos_ret = arcPos(prePrimeNodeID, primeNodeID, '+', '+', parameters->hashLength);	
			assert(kmer_index-1>=0);
			arcs[kmer_index-1] |= ((unsigned char) 1<<pos_ret);
				
			pos_ret = arcPos(primeNodeID, prePrimeNodeID, '-', '-', parameters->hashLength); 
			arcs[kmer_index] |=  ((unsigned char) 1<<pos_ret);
		}
		//preNode -> ~curNode
		else if(preNodeID == prePrimeNodeID && curNodeID != primeNodeID)
		{
			pos_ret = arcPos(prePrimeNodeID, primeNodeID, '+', '-', parameters->hashLength);	
			assert(kmer_index-1>=0);
			arcs[kmer_index-1] |= ((unsigned char) 1<<pos_ret);
				
			pos_ret = arcPos(primeNodeID, prePrimeNodeID, '+', '-', parameters->hashLength); 
			arcs[kmer_index] |= ((unsigned char) 1<<pos_ret);
		}
		//~preNode -> curNode
		else if(preNodeID != prePrimeNodeID && curNodeID == primeNodeID)
		{
			pos_ret = arcPos(prePrimeNodeID, primeNodeID, '-', '+', parameters->hashLength);	
			assert(kmer_index-1>=0);
			arcs[kmer_index-1] |= ((unsigned char) 1<<pos_ret);
				
			pos_ret = arcPos(primeNodeID, prePrimeNodeID, '-', '+', parameters->hashLength); 
			arcs[kmer_index] |= ((unsigned char) 1<<pos_ret);
		}			
		//~preNode -> ~curNode
		else if(preNodeID != prePrimeNodeID && curNodeID != primeNodeID)
		{
			pos_ret = arcPos(prePrimeNodeID, primeNodeID, '-', '-', parameters->hashLength);	
			assert(kmer_index-1>=0);
			arcs[kmer_index-1] |= ((unsigned char) 1<<pos_ret);
				
			pos_ret = arcPos(primeNodeID, prePrimeNodeID, '+', '+', parameters->hashLength); 
			arcs[kmer_index] |=  ((unsigned char) 1<<pos_ret);
		}
		preNodeID = curNodeID;
		prePrimeNodeID = primeNodeID;
		kmer_index++;
	}
	return (long long int) kmer_index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movl 0xaf8(%r9), %edx
decl %edx
xorl %r14d, %r14d
movq %rsi, %rdi
xorl %esi, %esi
movq %r9, %rcx
callq 0x25d2
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
movl 0xaf8(%rbx), %edx
leal -0x1(%rdx), %eax
movslq %eax, %r8
addq %r12, %r8
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq 0x40(%rsp), %r8
jae 0x29b4
movzbl (%r8), %eax
leal -0x41(%rax), %ecx
cmpl $0x13, %ecx
ja 0x29aa
movl $0x80045, %esi          # imm = 0x80045
btl %ecx, %esi
jae 0x29aa
movq %r8, 0x20(%rsp)
movq %r14, %r13
movq 0x18(%rsp), %r15
movzbl 0xb38(%r15,%rax), %eax
andl $0x3, %eax
leaq (%rax,%rbp,4), %rbp
andq 0xb00(%r15), %rbp
movq %rbp, %rsi
movq %rdx, 0x38(%rsp)
movq %r15, %rdx
callq 0x25ac
movq 0x38(%rsp), %rdx
cmpq %rax, %rbp
movq %rax, %r14
cmovaq %rbp, %r14
movq %r14, 0x28(%rsp)
cmpl %edx, 0xaf8(%r15)
jne 0x2863
movq %r14, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq %r14, (%rax,%r12,8)
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%r12)
incq %r12
movq %rbp, %rbx
movq 0x20(%rsp), %r8
jmp 0x29aa
movq 0x30(%rsp), %rcx
movq %r14, (%rcx,%r12,8)
movq 0x8(%rsp), %rcx
movb $0x0, (%rcx,%r12)
movl 0xaf8(%r15), %r9d
cmpq %r13, %rbx
jne 0x28d5
cmpq %rax, %rbp
pushq $0x1
popq %r15
leaq 0x28(%rsp), %rdx
jae 0x2917
pushq $0x2b
popq %r13
movl %r13d, %ecx
pushq $0x2d
popq %r8
callq 0x26ac
movl %r15d, %edx
movl %eax, %ecx
shll %cl, %edx
movl %r15d, %ebx
movq 0x8(%rsp), %r15
orb %dl, -0x1(%r15,%r12)
movq 0x18(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
pushq $0x2d
popq %r8
jmp 0x2986
cmpq %rax, %rbp
pushq $0x1
popq %r15
leaq 0x28(%rsp), %rdx
pushq $0x2d
popq %rcx
jae 0x294f
movl %ecx, %r8d
callq 0x26ac
movl %r15d, %edx
movl %eax, %ecx
shll %cl, %edx
movl %r15d, %ebx
movq 0x8(%rsp), %r15
orb %dl, -0x1(%r15,%r12)
movq 0x18(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x10(%rsp), %rdx
pushq $0x2b
jmp 0x2949
pushq $0x2b
popq %rcx
movl %ecx, %r8d
callq 0x26ac
movl %r15d, %edx
movl %eax, %ecx
shll %cl, %edx
movl %r15d, %ebx
movq 0x8(%rsp), %r15
orb %dl, -0x1(%r15,%r12)
movq 0x18(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x10(%rsp), %rdx
pushq $0x2d
popq %rcx
movl %ecx, %r8d
jmp 0x2986
pushq $0x2b
popq %r13
movl %r13d, %r8d
callq 0x26ac
movl %r15d, %edx
movl %eax, %ecx
shll %cl, %edx
movl %r15d, %ebx
movq 0x8(%rsp), %r15
orb %dl, -0x1(%r15,%r12)
movq 0x18(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x10(%rsp), %rdx
pushq $0x2d
popq %rcx
movl %r13d, %r8d
callq 0x26ac
movq 0x20(%rsp), %r8
movl %ebx, %edx
movl %eax, %ecx
shll %cl, %edx
orb %dl, (%r15,%r12)
movq %r14, 0x10(%rsp)
incq %r12
movq %rbp, %rbx
movq 0x38(%rsp), %rdx
incl %edx
incq %r8
jmp 0x27c1
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq