// File: /workspace/github_cpp_makefile_star2_O1/ratulSharker[P]Havec/src/vector_with_rehash_8_hash_with_N_detection_6_bytes.cpp
// Function: storeKhmerInMemBlock6Byte(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, khmerInfo6Byte*, unsigned long long, unsigned long long, char, unsigned int)
int storeKhmerInMemBlock6Byte(string sample,struct khmerInfo6Byte * memBlock,unsigned long long blockSize, unsigned long long khmer_h, char next_neucleotide, unsigned int whichHashFunc){

	unsigned long long index;
	unsigned long long qutioned; //this value must be under 12bit
	char neighbour_musk = 0xF0;
	char *p;

	//find the index value in the memBlock
	index = khmer_h % blockSize;
	
	qutioned = khmer_h / blockSize;//this value must be under 10bit

	//std::cout<<"data :: "<<(int)memBlock[index].data[1]<<std::endl;
	//cout<<"sample khmer "<<sample<<" kmer_h "<<khmer_h<<" hash func no "<<whichHashFunc<<endl;

	//now check the specified index is neighbourless or not (which is equivalent to -> is the space occupied)
	if((memBlock[index].data[4] & neighbour_musk) == 0){
		//no one is occupuying here put khmer_h here

		//putting the qutioned
		p = (char*) &qutioned;
		memBlock[index].data[0] = *p;
		p++;
		memBlock[index].data[1] = *p;
		p++;
		memBlock[index].data[2] = *p;
		p++;
		memBlock[index].data[3] = *p;
		p++;
		memBlock[index].data[4] = 0x01 & (*p);

		//putting "which hash value is stored"
		memBlock[index].data[4] = memBlock[index].data[4] | (whichHashFunc << 1);
		
		/*memBlock[index].data[0] = qutioned & 0xFF;
		memBlock[index].data[1] = (qutioned >> 8) & 0x0F;
		*/

		//std::cout<<"putting in the block array"<<std::endl;

		switch(toupper(next_neucleotide)){//putting the neighbour
		case 'A'://1000
			memBlock[index].data[4] = memBlock[index].data[4] | 0x80;
			break;
		case 'T'://0100
			memBlock[index].data[4] = memBlock[index].data[4] | 0x40;
			break;
		case 'C'://0010
			memBlock[index].data[4] = memBlock[index].data[4] | 0x20;
			break;
		case 'G'://0001
			memBlock[index].data[4] = memBlock[index].data[4] | 0x10;
			break;
		}
		//std::cout<<"index "<<index<<" quotioned :: "<< qutioned<< " data[0]=" <<(int)memBlock[index].data[0]<<" data[1]="<<(int)memBlock[index].data[1]<<std::endl;

		//increment the count value
		memBlock[index].count = 1; //--6 byte final update
		
		return MEM_BLOCK; //khmer putted in the mem_block so return mem_block
	}
	else if(((memBlock[index].data[4] >> 1) & 0x07) == whichHashFunc && //hash value matched
			(memBlock[index].data[0] == (qutioned & 0xFF)) &&
			(memBlock[index].data[1] == (qutioned & 0xFF00)>>8) &&
			(memBlock[index].data[2] == (qutioned & 0xFF0000)>>16) &&
			(memBlock[index].data[3] == (qutioned & 0xFF000000)>>24) &&
			((memBlock[index].data[4] & 0x01) == ((qutioned & 0x0100000000) >> 32))){
		//someone is sitting on the memBlock index & who is same as the khmer

		//now just update its neighbour
		//std::cout<<"now just update its neighbour"<<std::endl;
		switch(toupper(next_neucleotide)){//putting the neighbour
		case 'A'://1000
			memBlock[index].data[4] = memBlock[index].data[4] | 0x80;
			break;
		case 'T'://0100
			memBlock[index].data[4] = memBlock[index].data[4] | 0x40;
			break;
		case 'C'://0010
			memBlock[index].data[4] = memBlock[index].data[4] | 0x20;
			break;
		case 'G'://0001
			memBlock[index].data[4] = memBlock[index].data[4] | 0x10;
			break;
		}
		
		//counting upto 255 -- 6byte final
		if(memBlock[index].count<255){
			memBlock[index].count++;
		}
		/*//helps to compute how many khmer is not being listed
		else{
			number_of_khmer_not_listed_in_memblock_count_variable++;
		}
		*/
			
			
		return MEM_BLOCK_FOUND; //khmer putted in the mem_block so return mem_block -- simply update the neighbour table
	}
	else if(whichHashFunc == LAST_HASH_FUNC){
		//only for the last time put in the alternative mem
		//neighbour field is not blank -- someone already sitting there
		//and who sitting there is not same as the given khmer
		//and the set pointer is null so it must be initialized
		//std::cout<<"else reached index "<<index<<" quotioned::"<<qutioned<< " data[0]=" <<(int)memBlock[index].data[0]<<" data[1]="<<(int)memBlock[index].data[1]<<std::endl;
		//expand the set pointer & place the khmer in there
		vector<struct vector_trial_6_byte>::iterator it;
		int index_vect=0;
		int index_v=index%num_of_vector;

		for(it=mapPointer6Byte[index_v].begin();it!=mapPointer6Byte[index_v].end();it++)
		{
			if(it->index_val==index)
			{
				//cout<<"existing value found for index "<<index<<" for khmer "<<khmer_h<<endl;
				break;
			}
			index_vect++;
		}


		if(it == mapPointer6Byte[index_v].end()){
			struct vector_trial_6_byte temp_vect;
			temp_vect.index_val=index;
			//temp_vect.vect_pnt=pp;
			mapPointer6Byte[index_v].push_back(temp_vect);
			//cout<<"new value inserted for index "<<index<<" for khmer "<<khmer_h<<" stored in index "<<index_v<<endl;
		}

		struct khmerInfo6Byte kin;//making a demo
		kin.data[0] = kin.data[1] = kin.data[2] = kin.data[3] = kin.data[4] = 0; //cleaning the demo info
		
		//cout<<"New value inserted in list for memblock index "<<index<<" for khmer "<<khmer_h<<" stored in list in index "<<index_v<<" hash func no "<<whichHashFunc<<endl;
		
		switch(toupper(next_neucleotide)){//putting the neighbour
		case 'A'://1000
			kin.data[4] = kin.data[4] | 0x80;
			break;
		case 'T'://0100
			kin.data[4] = kin.data[4] | 0x40;
			break;
		case 'C'://0010
			kin.data[4] = kin.data[4] | 0x20;
			break;
		case 'G'://s
			kin.data[4] = kin.data[4] | 0x10;
			break;
		}

		//putting the hash function data in the newly made khmerInfo6Byte obj
		
		/*****************************************************/
		//problem occur -- limon
		/*****************************************************/
		bool isFound = false;
		for(std::vector<khmerInfo6Byte>::iterator pi = mapPointer6Byte[index_v][index_vect].vect.begin();
				pi != mapPointer6Byte[index_v][index_vect].vect.end() ; pi++)
		{
			if(
				((pi->data[4] >> 1) & 0x07) == whichHashFunc && //hash value matched
				(pi->data[0] == (qutioned & 0xFF)) &&
				(pi->data[1] == (qutioned & 0xFF00)>>8) &&
				(pi->data[2] == (qutioned & 0xFF0000)>>16) &&
				(pi->data[3] == (qutioned & 0xFF000000)>>24) &&
				((pi->data[4] & 0x01) == ((qutioned & 0x0100000000) >> 32))){
			//(pi->data[0] == (qutioned & 0xFF)) && ((pi->data[1] & 0x0F) == ((qutioned & 0x0F00) >> 8))){
				pi->data[4] = pi->data[4] | kin.data[4];
				
				/*
				 * searched khmer found in the list
				 * so simply increment the count variable
				*/
				if(pi->count < 255){
					pi->count++;
				}
				/*//helps to compute how many khmer is not being listed
				else{
					number_of_khmer_not_listed_in_vector_count_variable++;
				}
				*/
				
				
				isFound = true;
				//cout<<"isFound true"<<endl;
				break;
			}
		}
		if(isFound == false){
		
			//putting the qutioned
			p = (char*) &qutioned;
			kin.data[0] = *p;
			p++;
			kin.data[1] = *p;
			p++;
			kin.data[2] = *p;
			p++;
			kin.data[3] = *p;
			p++;
			kin.data[4] = 0x01 & (*p);
			
			//putting "which hash value is stored"
			kin.data[4] = kin.data[4] | (whichHashFunc << 1); //--optimization		

			/*
			 *  searched khmer not found in the vector
			 *  so set count to one
			 */
			kin.count = 1;
			
			
			(mapPointer6Byte[index_v])[index_vect].vect.push_back(kin);
			
			return LIST;//new entry in the list
		}

		return LIST_FOUND; //khmer found in the list so return LIST_FOUND
	}

	//none of the if-else block is reached so it caused error
	//this means that particular khmer couldn't be placed inside the memblock
	//it will basically return for hash func 0,1,2 -> for hash func 3 if it fails
	//it will be stored in the list/vector/sqlite
	return ERROR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebx
movq %rcx, %rax
movq %rdx, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r14
movl %r14d, %r12d
shrl $0x8, %r12d
movl %r14d, %edi
shrl $0x10, %edi
movl %r14d, %r9d
shrl $0x18, %r9d
movq %rax, %r15
shrq $0x20, %r15
leaq (%rdx,%rdx,2), %rax
leaq (%rsi,%rax,2), %rbp
movzbl 0x4(%rbp), %r13d
cmpl $0xf, %r13d
ja 0x6805
movb %r14b, (%rbp)
movb %r12b, 0x1(%rbp)
movb %dil, 0x2(%rbp)
movb %r9b, 0x3(%rbp)
andl $0x1, %r15d
leal (%r15,%rbx,2), %ebx
movb %bl, 0x4(%rbp)
movsbl %r8b, %edi
callq 0x3490
cmpl $0x46, %eax
jg 0x693e
cmpl $0x41, %eax
je 0x6964
cmpl $0x43, %eax
jne 0x696f
movb $0x20, %al
jmp 0x696a
movq %rdx, %rcx
movl %r13d, %eax
shrl %eax
andl $0x7, %eax
cmpl %ebx, %eax
jne 0x6867
cmpb %r14b, (%rbp)
jne 0x6867
cmpb %r12b, 0x1(%rbp)
jne 0x6867
cmpb %dil, 0x2(%rbp)
jne 0x6867
cmpb %r9b, 0x3(%rbp)
jne 0x6867
movl %r13d, %eax
andl $0x1, %eax
movl %r15d, %edx
andl $0x1, %edx
cmpl %eax, %edx
jne 0x6867
movsbl %r8b, %edi
callq 0x3490
cmpl $0x46, %eax
jg 0x6a73
cmpl $0x41, %eax
je 0x6a81
cmpl $0x43, %eax
jne 0x6a8e
movb $0x20, %al
jmp 0x6a87
leaq 0x7a56(%rip), %rdx      # 0xe2c4
movl $0x3, %eax
cmpl %ebx, (%rdx)
jne 0x6a47
leaq 0x7a6e(%rip), %rsi      # 0xe2f0
movq %rcx, %rax
xorl %edx, %edx
divq (%rsi)
xorl %ebp, %ebp
movq 0x7a1d(%rip), %rax      # 0xe2b0
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %r13
movq (%rax,%r13,8), %rdx
movq 0x8(%rax,%r13,8), %rsi
cmpq %rsi, %rdx
je 0x68ba
cmpq %rcx, (%rdx)
je 0x68b8
incl %ebp
addq $0x20, %rdx
cmpq %rsi, %rdx
jne 0x68a8
movl %ebp, %ebp
movl %r9d, 0xc(%rsp)
movl %edi, 0x8(%rsp)
cmpq %rsi, %rdx
jne 0x6909
movl %r8d, 0x34(%rsp)
leaq (%rax,%r13,8), %rdi
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movups %xmm0, 0x8(%rsi)
movq $0x0, 0x18(%rsi)
movq %rcx, (%rsi)
callq 0x8d7e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x6904
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x3270
movl 0x34(%rsp), %r8d
movsbl %r8b, %edi
movw $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x3490
cmpl $0x46, %eax
jg 0x694c
cmpl $0x41, %eax
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
je 0x697d
cmpl $0x43, %eax
jne 0x6987
movb $0x20, %al
jmp 0x6983
cmpl $0x47, %eax
je 0x6968
cmpl $0x54, %eax
jne 0x696f
movb $0x40, %al
jmp 0x696a
cmpl $0x47, %eax
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
je 0x6981
cmpl $0x54, %eax
jne 0x6987
movb $0x40, %al
jmp 0x6983
movb $-0x80, %al
jmp 0x696a
movb $0x10, %al
orb %bl, %al
movb %al, 0x4(%rbp)
movb $0x1, 0x5(%rbp)
movl $0x1, %eax
jmp 0x6a47
movb $-0x80, %al
jmp 0x6983
movb $0x10, %al
movb %al, 0x14(%rsp)
movq 0x7922(%rip), %rax      # 0xe2b0
movq (%rax,%r13,8), %rax
shlq $0x5, %rbp
movq 0x8(%rax,%rbp), %rcx
movq 0x10(%rax,%rbp), %rax
cmpq %rax, %rcx
je 0x69e3
movl %r15d, %edx
andl $0x1, %edx
movzbl 0x4(%rcx), %esi
movl %esi, %edi
shrl %edi
andl $0x7, %edi
cmpl %ebx, %edi
jne 0x69da
cmpb %r14b, (%rcx)
jne 0x69da
cmpb %r12b, 0x1(%rcx)
jne 0x69da
cmpb %r8b, 0x2(%rcx)
jne 0x69da
cmpb %r9b, 0x3(%rcx)
jne 0x69da
movl %esi, %edi
andl $0x1, %edi
cmpl %edi, %edx
je 0x6a56
addq $0x6, %rcx
cmpq %rax, %rcx
jne 0x69ab
movb %r14b, 0x10(%rsp)
movb %r12b, 0x11(%rsp)
movb %r8b, 0x12(%rsp)
movb %r9b, 0x13(%rsp)
andl $0x1, %r15d
leal (%r15,%rbx,2), %eax
movb %al, 0x14(%rsp)
movb $0x1, 0x15(%rsp)
movq 0x78a1(%rip), %rax      # 0xe2b0
movq (%rax,%r13,8), %rax
leaq (%rax,%rbp), %rdi
addq $0x8, %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x6a3b
movzwl 0x14(%rsp), %eax
movw %ax, 0x4(%rsi)
movl 0x10(%rsp), %eax
movl %eax, (%rsi)
addq $0x6, 0x8(%rdi)
jmp 0x6a45
leaq 0x10(%rsp), %rdx
callq 0x9072
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb 0x14(%rsp), %sil
movb %sil, 0x4(%rcx)
movb 0x5(%rcx), %dl
movl $0x6, %eax
cmpb $-0x1, %dl
je 0x6a47
incb %dl
movb %dl, 0x5(%rcx)
jmp 0x6a47
cmpl $0x47, %eax
je 0x6a85
cmpl $0x54, %eax
jne 0x6a8e
movb $0x40, %al
jmp 0x6a87
movb $-0x80, %al
jmp 0x6a87
movb $0x10, %al
orb %al, %r13b
movb %r13b, 0x4(%rbp)
movb 0x5(%rbp), %cl
movl $0x2, %eax
cmpb $-0x1, %cl
je 0x6a47
incb %cl
movb %cl, 0x5(%rbp)
jmp 0x6a47
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x6abc
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x3270
movq %rbx, %rdi
callq 0x3450