// File: 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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x68(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %edx
subl $0x1, %edx
movq 0x48(%rsp), %rcx
xorl %esi, %esi
callq 0x8fa0
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %eax
subl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
addq %rcx, %rax
cmpq 0x60(%rsp), %rax
jae 0x9a37
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x41, %eax
je 0x94b1
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x54, %eax
je 0x94b1
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x43, %eax
je 0x94b1
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x47, %eax
je 0x94b1
jmp 0x9a27
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x41, %ecx
movb %al, 0x7(%rsp)
je 0x9514
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x54, %ecx
movb %al, 0x7(%rsp)
je 0x9514
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x43, %ecx
movb %al, 0x7(%rsp)
je 0x9514
movq 0x68(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x47, %eax
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x951e
jmp 0x9520
jmp 0x953f
leaq 0xcbfe(%rip), %rdi      # 0x16125
leaq 0xcb25(%rip), %rsi      # 0x16053
movl $0x87, %edx
leaq 0xcc16(%rip), %rcx      # 0x16150
callq 0x83d0
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rax
shlq $0x2, %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq 0x68(%rsp), %rcx
movslq 0x14(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
movslq %ecx, %rcx
movsbq 0xb38(%rax,%rcx), %rax
andq $0x3, %rax
orq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq 0xb00(%rax), %rax
andq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x8f20
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xd210
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movl 0x14(%rsp), %eax
movq 0x48(%rsp), %rcx
subl 0xaf8(%rcx), %eax
addl $0x1, %eax
cmpl $0x0, %eax
jne 0x961f
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x9a27
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
jne 0x972f
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x972f
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movl $0x2b, %r8d
movl %r8d, %ecx
callq 0x91c0
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rax
subq $0x1, %rax
cmpq $0x0, %rax
jb 0x969e
jmp 0x96bd
leaq 0xcb19(%rip), %rdi      # 0x161be
leaq 0xc9a7(%rip), %rsi      # 0x16053
movl $0x9e, %edx
leaq 0xca98(%rip), %rcx      # 0x16150
callq 0x83d0
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x2d, %r8d
movl %r8d, %ecx
callq 0x91c0
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x9a05
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
jne 0x9822
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x9822
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movl $0x2b, %ecx
movl $0x2d, %r8d
callq 0x91c0
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rax
subq $0x1, %rax
cmpq $0x0, %rax
jb 0x978f
jmp 0x97ae
leaq 0xca28(%rip), %rdi      # 0x161be
leaq 0xc8b6(%rip), %rsi      # 0x16053
movl $0xa8, %edx
leaq 0xc9a7(%rip), %rcx      # 0x16150
callq 0x83d0
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x2b, %ecx
movl $0x2d, %r8d
callq 0x91c0
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x9a03
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x9915
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x9915
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movl $0x2d, %ecx
movl $0x2b, %r8d
callq 0x91c0
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rax
subq $0x1, %rax
cmpq $0x0, %rax
jb 0x9882
jmp 0x98a1
leaq 0xc935(%rip), %rdi      # 0x161be
leaq 0xc7c3(%rip), %rsi      # 0x16053
movl $0xb2, %edx
leaq 0xc8b4(%rip), %rcx      # 0x16150
callq 0x83d0
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x2d, %ecx
movl $0x2b, %r8d
callq 0x91c0
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x9a01
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x99ff
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x99ff
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movl $0x2d, %r8d
movl %r8d, %ecx
callq 0x91c0
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rax
subq $0x1, %rax
cmpq $0x0, %rax
jb 0x9973
jmp 0x9992
leaq 0xc844(%rip), %rdi      # 0x161be
leaq 0xc6d2(%rip), %rsi      # 0x16053
movl $0xbc, %edx
leaq 0xc7c3(%rip), %rcx      # 0x16150
callq 0x83d0
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rax
movl 0xaf8(%rax), %r9d
leaq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x2b, %r8d
movl %r8d, %ecx
callq 0x91c0
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x9a01
jmp 0x9a03
jmp 0x9a05
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x9448
movq 0x18(%rsp), %rax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)