// File: /workspace/github_cpp_makefile_star2_O2/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, %r13d
shrl $0x8, %r13d
movl %r14d, %r10d
shrl $0x10, %r10d
movl %r14d, %r9d
shrl $0x18, %r9d
movq %rax, %r12
shrq $0x20, %r12
imulq $0x6, %rdx, %rax
leaq (%rsi,%rax), %rbp
movzbl 0x4(%rsi,%rax), %r15d
cmpl $0xf, %r15d
ja 0x4b39
movb %r14b, (%rbp)
movb %r13b, 0x1(%rbp)
movb %r10b, 0x2(%rbp)
movb %r9b, 0x3(%rbp)
andl $0x1, %r12d
leal (%r12,%rbx,2), %ebx
movb %bl, 0x4(%rbp)
movsbl %r8b, %edi
callq 0x2430
cmpl $0x41, %eax
je 0x4c6f
cmpl $0x43, %eax
je 0x4c67
cmpl $0x47, %eax
je 0x4c6b
cmpl $0x54, %eax
jne 0x4c76
movb $0x40, %al
jmp 0x4c71
movq %rdx, %rcx
movl %r15d, %eax
shrl %eax
andl $0x7, %eax
cmpl %ebx, %eax
jne 0x4ba4
cmpb %r14b, (%rbp)
jne 0x4ba4
cmpb %r13b, 0x1(%rbp)
jne 0x4ba4
cmpb %r10b, 0x2(%rbp)
jne 0x4ba4
cmpb %r9b, 0x3(%rbp)
jne 0x4ba4
movl %r15d, %eax
andl $0x1, %eax
movl %r12d, %edx
andl $0x1, %edx
cmpl %eax, %edx
jne 0x4ba4
movsbl %r8b, %edi
callq 0x2430
cmpl $0x41, %eax
je 0x4d56
cmpl $0x43, %eax
je 0x4d4e
cmpl $0x47, %eax
je 0x4d52
cmpl $0x54, %eax
jne 0x4d5f
movb $0x40, %al
jmp 0x4d58
leaq 0x66e9(%rip), %rax      # 0xb294
cmpl %ebx, (%rax)
jne 0x4bfa
leaq 0x670a(%rip), %rsi      # 0xb2c0
movq %rcx, %rax
xorl %edx, %edx
divq (%rsi)
movq 0x66bb(%rip), %rsi      # 0xb280
movslq %edx, %rax
imulq $0x18, %rax, %rdx
leaq (%rsi,%rdx), %rdi
movq (%rsi,%rdx), %rax
movq %rdx, 0x10(%rsp)
movq 0x8(%rsi,%rdx), %rdx
pushq $0x8
popq %rbp
movl %r9d, 0x8(%rsp)
cmpq %rdx, %rax
je 0x4c03
cmpq %rcx, (%rax)
je 0x4bfe
addq $0x20, %rax
addq $0x20, %rbp
jmp 0x4be6
pushq $0x3
jmp 0x4c7c
cmpq %rdx, %rax
jne 0x4c3b
movl %r8d, 0xc(%rsp)
movl %r10d, %r15d
leaq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rcx, -0x8(%rax)
leaq 0x18(%rsp), %rsi
callq 0x6a7e
leaq 0x20(%rsp), %rdi
callq 0x6ace
movl %r15d, %r10d
movl 0xc(%rsp), %r8d
movl %r10d, %r15d
movsbl %r8b, %edi
callq 0x2430
cmpl $0x54, %eax
je 0x4c8a
cmpl $0x43, %eax
movl %r15d, %r10d
movl 0x8(%rsp), %r11d
je 0x4c82
cmpl $0x47, %eax
je 0x4c86
cmpl $0x41, %eax
jne 0x4c96
movb $-0x80, %al
jmp 0x4c98
movb $0x20, %al
jmp 0x4c71
movb $0x10, %al
jmp 0x4c71
movb $-0x80, %al
orb %bl, %al
movb %al, 0x4(%rbp)
movb $0x1, 0x5(%rbp)
pushq $0x1
popq %rax
jmp 0x4d27
movb $0x20, %al
jmp 0x4c98
movb $0x10, %al
jmp 0x4c98
movb $0x40, %al
movl %r15d, %r10d
movl 0x8(%rsp), %r11d
jmp 0x4c98
xorl %eax, %eax
movq 0x65e1(%rip), %rcx      # 0xb280
movq 0x10(%rsp), %rdx
movq (%rcx,%rdx), %rdx
leaq (%rdx,%rbp), %rdi
movq (%rdx,%rbp), %rcx
movq 0x8(%rdx,%rbp), %rdx
movl %r12d, %esi
andl $0x1, %esi
cmpq %rdx, %rcx
je 0x4cfd
movzbl 0x4(%rcx), %r8d
movl %r8d, %r9d
shrl %r9d
andl $0x7, %r9d
cmpl %ebx, %r9d
jne 0x4cf7
cmpb %r14b, (%rcx)
jne 0x4cf7
cmpb %r13b, 0x1(%rcx)
jne 0x4cf7
cmpb %r10b, 0x2(%rcx)
jne 0x4cf7
cmpb %r11b, 0x3(%rcx)
jne 0x4cf7
movl %r8d, %r9d
andl $0x1, %r9d
cmpl %r9d, %esi
je 0x4d36
addq $0x6, %rcx
jmp 0x4cbb
leaq 0x18(%rsp), %rsi
movb %r14b, (%rsi)
movb %r13b, 0x1(%rsi)
movb %r10b, 0x2(%rsi)
movb %r11b, 0x3(%rsi)
andl $0x1, %r12d
leal (%r12,%rbx,2), %eax
movb %al, 0x4(%rsi)
movb $0x1, 0x5(%rsi)
callq 0x6aaa
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb %r8b, %al
movb %al, 0x4(%rcx)
movb 0x5(%rcx), %dl
pushq $0x6
popq %rax
cmpb $-0x1, %dl
je 0x4d27
incb %dl
movb %dl, 0x5(%rcx)
jmp 0x4d27
movb $0x20, %al
jmp 0x4d58
movb $0x10, %al
jmp 0x4d58
movb $-0x80, %al
orb %al, %r15b
movb %r15b, 0x4(%rbp)
movb 0x5(%rbp), %cl
pushq $0x2
popq %rax
cmpb $-0x1, %cl
je 0x4d27
incb %cl
movb %cl, 0x5(%rbp)
jmp 0x4d27
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x6ace
movq %rbx, %rdi
callq 0x2400