// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]optimal-seed-solver/HashTree.cc
// Function: HashTree::addEntry(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int, unsigned int)
void HashTree::addEntry(const string& DNA, unsigned int chrNum, unsigned int location) {
	unsigned int hashVal = hashgen.calculateHash(DNA, hashLength);

	//cout << "DNA: " << DNA << endl;
	//cout << "hashVal: " << hashVal << endl;

	TreeNode* cursor = root[hashVal];
	TreeNode* parentCursor = NULL;
	TreeNode* nextCursor = NULL;

	//cout << "***location: " << location << endl;

	/*
	   if (DNA == "CCTGGTGCTCCCACAAAGGAGAAGGGCTGA") {
	   cout << "found something" << endl;
	   cout << "hashVal: " << hashVal << endl;
	   }
	 */

	//The depth depends on the length of the string: DNA
	for (int i = hashLength; i < static_cast<int>(DNA.length() ); i++) {
		//cout << i << ' ' << DNA[i] << ' ';
		//Create a root node if there isn't one
		if (cursor == NULL) {
			cursor = new LeafNode();
			root[hashVal] = cursor;
			cursor->setContent(chrNum, location);
			//Initialize the counter
			cursor->addLoc();
			return;
		}

		//cout << "i: " << i << " of DNA.length() - 1 = " << DNA.length() - 1 << endl;

		//This is a leaf node but it should no longer be one
		if (cursor->isLeaf() ) {
			//cout << "i: " << i << " Replacing!" << endl;
			// Creating a trunk
			TreeNode* newTrunk = new TreeNode(1);
			//cout << "cursor->getLocNum: " << cursor->getLocNum() << endl;
			//cout.flush();
			assert(cursor->getLocNum() == 1);

			assert(cursor->getChrNum() < refdb.getNumOfChromo() );

			//Push the LeafNode 1 level lower
			switch (checkdb[cursor->getChrNum()].getRefBase(cursor->getLocation(), i) ) {
				case 'A':
					newTrunk->setA(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getA()->addLoc();
					break;
				case 'C':
					newTrunk->setC(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getC()->addLoc();
					break;
				case 'G':
					newTrunk->setG(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getG()->addLoc();
					break;
				case 'T':
					newTrunk->setT(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getT()->addLoc();
					break;
				case 'M':
					newTrunk->setA(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getA()->addLoc();
					newTrunk->setC(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getC()->addLoc();
					break;
				case 'R':
					newTrunk->setA(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getA()->addLoc();
					newTrunk->setG(new LeafNode(cursor->getChrNum(), cursor->getLocation() ) );
					newTrunk->getG()->addLoc();
					break;
				default:
					cerr << "unrecognized character: " << DNA[i] << endl;
					break;
			}

			//Check if we need to update root or a trunk
			if (parentCursor == NULL)
				root[hashVal] = newTrunk;
			else {
				switch (DNA[i-1]) {
					case 'A':
						parentCursor->setA(newTrunk);
						break;
					case 'C':
						parentCursor->setC(newTrunk);
						break;
					case 'G':
						parentCursor->setG(newTrunk);
						break;
					case 'T':
						parentCursor->setT(newTrunk);
						break;
				}
			}

			delete cursor;
			cursor = newTrunk;
			newTrunk = NULL;
		}

		//Incrementing the counter along the path
		cursor->addLoc();

		switch (DNA[i]) {
			case 'A':
				nextCursor = cursor->getA();
				break;
			case 'C':
				nextCursor = cursor->getC();
				break;
			case 'G':
				nextCursor = cursor->getG();
				break;
			case 'T':
				nextCursor = cursor->getT();
				break;
			default:
				cerr << "unrecognized character: " << DNA[i] << endl;
				break;
		}

		if (nextCursor == NULL) {
			//cout << "WTF!!!!!" << "DNA[" << i << "]: " << DNA[i] << endl;

			nextCursor = new LeafNode(chrNum, location);
			//Incrementing the counter of the new node
			nextCursor->addLoc();

			switch (DNA[i]) {
				case 'A':
					cursor->setA(nextCursor);
					break;
				case 'C':
					cursor->setC(nextCursor);
					break;
				case 'G':
					cursor->setG(nextCursor);
					break;
				case 'T':
					cursor->setT(nextCursor);
					break;
				default:
					cerr << "unrecognized character: " << DNA[i] << endl;
					break;
			}

			return;
		}

		//if this is the last node, we have to increment the counter
		if (i == static_cast<int>(DNA.length() ) - 1)
			nextCursor->addLoc();

		parentCursor = cursor;
		cursor = nextCursor;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, 0x14(%rsp)
movl %edx, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
addq $0x318, %rdi            # imm = 0x318
movl (%r14), %edx
callq 0x8b32
movq %r14, 0x18(%rsp)
movslq (%r14), %r14
cmpl 0x8(%rbx), %r14d
jge 0x527e
movl %eax, %edx
movq 0x18(%rsp), %rcx
movq 0x320(%rcx), %rax
movq (%rax,%rdx,8), %r15
leaq 0xa8(%rcx), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movq %rdx, 0x20(%rsp)
leaq (,%rdx,8), %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x8(%rsp)
testq %r15, %r15
je 0x5235
movq %rbp, 0x40(%rsp)
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
testb %al, %al
je 0x4e54
movq %r12, 0x38(%rsp)
movl $0x38, %edi
callq 0x3270
movq %rax, %rbp
leaq 0x7fa5(%rip), %rax      # 0xcd08
movq %rax, (%rbp)
movl $0x1, 0x8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbp)
movups %xmm0, 0x28(%rbp)
cmpl $0x1, 0x8(%r15)
jne 0x5369
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq 0x30(%rsp), %rdi
callq 0x8676
cmpl %eax, %r12d
jae 0x5388
movq %rbp, %rbx
addq $0x18, %rbx
movq 0x18(%rsp), %rax
movq 0x310(%rax), %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %eax
imulq $0x268, %rax, %r12      # imm = 0x268
addq %r13, %r12
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
movq %r12, %rdi
movl %eax, %esi
movl %r14d, %edx
callq 0x8856
movzbl %al, %eax
cmpl $0x4c, %eax
jg 0x4e5c
cmpl $0x41, %eax
je 0x4ec7
cmpl $0x43, %eax
je 0x5010
cmpl $0x47, %eax
movq 0x8(%rsp), %rbx
jne 0x4fa9
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7f09(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x28(%rbp)
jmp 0x505b
movq %r15, %rbp
jmp 0x516e
cmpl $0x4d, %eax
je 0x4f11
cmpl $0x52, %eax
je 0x5068
cmpl $0x54, %eax
movq 0x8(%rsp), %rbx
jne 0x4fa9
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7e96(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x30(%rbp)
jmp 0x505b
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7e4b(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
jmp 0x50fb
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7e01(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
movl $0x1, 0x8(%r13)
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7db4(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%rbp)
jmp 0x50fb
movl $0x18, %edx
movq 0x803b(%rip), %r12      # 0xcff0
movq %r12, %rdi
leaq 0x4217(%rip), %rsi      # 0x91d6
callq 0x32d0
movq (%rbx), %rax
movb (%rax,%r14), %al
movb %al, 0x5(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0x5(%rsp), %rsi
callq 0x32d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
jmp 0x5108
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movq 0x8(%rsp), %rbx
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7cfd(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%rbp)
movl $0x1, 0x8(%r13)
jmp 0x5108
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7caa(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
movl $0x1, 0x8(%r13)
movl $0x40, %edi
callq 0x3270
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x7c5d(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x28(%rbp)
movl $0x1, 0x8(%r13)
movq 0x8(%rsp), %rbx
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
movq 0x38(%rsp), %r12
je 0x5135
movq (%rbx), %rax
movzbl -0x1(%rax,%r14), %eax
cmpl $0x46, %eax
jg 0x5148
cmpl $0x41, %eax
je 0x5158
cmpl $0x43, %eax
jne 0x5165
addq $0x20, %rcx
jmp 0x5162
movq 0x18(%rsp), %rax
movq 0x320(%rax), %rcx
addq 0x28(%rsp), %rcx
jmp 0x5162
cmpl $0x47, %eax
je 0x515e
cmpl $0x54, %eax
jne 0x5165
addq $0x30, %rcx
jmp 0x5162
addq $0x18, %rcx
jmp 0x5162
addq $0x28, %rcx
movq %rbp, (%rcx)
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
incl 0x8(%rbp)
movq (%rbx), %rax
movzbl (%rax,%r14), %eax
cmpl $0x46, %eax
jg 0x518e
cmpl $0x41, %eax
je 0x5202
cmpl $0x43, %eax
jne 0x519e
movq 0x20(%rbp), %r12
jmp 0x520c
cmpl $0x47, %eax
je 0x5208
cmpl $0x54, %eax
jne 0x519e
movq 0x30(%rbp), %r12
jmp 0x520c
movl $0x18, %edx
movq 0x7e46(%rip), %r15      # 0xcff0
movq %r15, %rdi
leaq 0x4022(%rip), %rsi      # 0x91d6
callq 0x32d0
movq (%rbx), %rax
movb (%rax,%r14), %al
movb %al, 0x6(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x6(%rsp), %rsi
callq 0x32d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
jmp 0x520c
movq 0x18(%rbp), %r12
jmp 0x520c
movq 0x28(%rbp), %r12
testq %r12, %r12
je 0x528d
movq 0x8(%rbx), %rax
leal -0x1(%rax), %ecx
cmpl %ecx, %r14d
jne 0x5222
incl 0x8(%r12)
incq %r14
cltq
movq %r12, %r15
cmpq %rax, %r14
jl 0x4d2b
jmp 0x527e
movl $0x40, %edi
callq 0x3270
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
leaq 0x7af7(%rip), %rcx      # 0xcd48
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
movq 0x320(%rcx), %rcx
movq 0x20(%rsp), %rdx
movq %rax, (%rcx,%rdx,8)
movl 0x10(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x14(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movl $0x1, 0x8(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x40, %edi
callq 0x3270
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
leaq 0x7a9f(%rip), %rcx      # 0xcd48
movq %rcx, (%rax)
movl 0x10(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x14(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movl $0x1, 0x8(%rax)
movq (%rbx), %rcx
movzbl (%rcx,%r14), %ecx
cmpl $0x46, %ecx
jg 0x52e2
cmpl $0x41, %ecx
je 0x5357
cmpl $0x43, %ecx
jne 0x52f2
movq %rax, 0x20(%rbp)
jmp 0x527e
cmpl $0x47, %ecx
je 0x5360
cmpl $0x54, %ecx
jne 0x52f2
movq %rax, 0x30(%rbp)
jmp 0x527e
movq 0x7cf7(%rip), %r15      # 0xcff0
leaq 0x3ed6(%rip), %rsi      # 0x91d6
movl $0x18, %edx
movq %r15, %rdi
callq 0x32d0
movq (%rbx), %rax
movb (%rax,%r14), %al
leaq 0x7(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %r15, %rdi
callq 0x32d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
jmp 0x527e
movq %rax, 0x18(%rbp)
jmp 0x527e
movq %rax, 0x28(%rbp)
jmp 0x527e
leaq 0x3dd0(%rip), %rdi      # 0x9140
leaq 0x3de2(%rip), %rsi      # 0x9159
leaq 0x3de7(%rip), %rcx      # 0x9165
movl $0xe5, %edx
callq 0x3190
leaq 0x3e1a(%rip), %rdi      # 0x91a9
leaq 0x3dc3(%rip), %rsi      # 0x9159
leaq 0x3dc8(%rip), %rcx      # 0x9165
movl $0xe7, %edx
callq 0x3190
jmp 0x53b5
jmp 0x53b5
jmp 0x53b5
jmp 0x53b5
jmp 0x53b5
jmp 0x53b5
jmp 0x53b5
movq %rax, %rbx
movl $0x40, %esi
movq %r13, %rdi
callq 0x3280
movq %rbx, %rdi
callq 0x3450
nop