// File: /workspace/github_cpp_makefile_star2_O1/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 0x8b72
movq %r14, 0x18(%rsp)
movslq (%r14), %r14
cmpl 0x8(%rbx), %r14d
jge 0x52c3
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 0x526b
movq %rbp, 0x40(%rsp)
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
testb %al, %al
je 0x4e4e
movq %r12, 0x38(%rsp)
movl $0x38, %edi
callq 0x3270
movq %rax, %rbp
leaq 0x7fb3(%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 0x53ae
movq (%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq 0x30(%rsp), %rdi
callq 0x86b6
cmpl %eax, %r12d
jae 0x53cd
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 0x8896
movzbl %al, %eax
cmpl $0x4c, %eax
jg 0x4e56
cmpl $0x41, %eax
je 0x4ec9
cmpl $0x43, %eax
je 0x502a
cmpl $0x47, %eax
movq 0x8(%rsp), %rbx
jne 0x4fc3
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7f0f(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x28(%rbp)
jmp 0x507d
movq %r15, %rbp
jmp 0x51a0
cmpl $0x4d, %eax
je 0x4f1b
cmpl $0x52, %eax
je 0x508a
cmpl $0x54, %eax
movq 0x8(%rsp), %rbx
jne 0x4fc3
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7e94(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x30(%rbp)
jmp 0x507d
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7e41(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
jmp 0x512d
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7def(%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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7d9a(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%rbp)
jmp 0x512d
movl $0x18, %edx
movq 0x8021(%rip), %r12      # 0xcff0
movq %r12, %rdi
leaq 0x420d(%rip), %rsi      # 0x91e6
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 0x513a
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7cdb(%rip), %rcx      # 0xcd48
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%rbp)
movl $0x1, 0x8(%r13)
jmp 0x513a
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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7c80(%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)
movl $0x0, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x28(%r13)
leaq 0x7c2b(%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 0x5167
movq (%rbx), %rax
movzbl -0x1(%rax,%r14), %eax
cmpl $0x46, %eax
jg 0x517a
cmpl $0x41, %eax
je 0x518a
cmpl $0x43, %eax
jne 0x5197
addq $0x20, %rcx
jmp 0x5194
movq 0x18(%rsp), %rax
movq 0x320(%rax), %rcx
addq 0x28(%rsp), %rcx
jmp 0x5194
cmpl $0x47, %eax
je 0x5190
cmpl $0x54, %eax
jne 0x5197
addq $0x30, %rcx
jmp 0x5194
addq $0x18, %rcx
jmp 0x5194
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 0x51c0
cmpl $0x41, %eax
je 0x5234
cmpl $0x43, %eax
jne 0x51d0
movq 0x20(%rbp), %r12
jmp 0x523e
cmpl $0x47, %eax
je 0x523a
cmpl $0x54, %eax
jne 0x51d0
movq 0x30(%rbp), %r12
jmp 0x523e
movl $0x18, %edx
movq 0x7e14(%rip), %r15      # 0xcff0
movq %r15, %rdi
leaq 0x4000(%rip), %rsi      # 0x91e6
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 0x523e
movq 0x18(%rbp), %r12
jmp 0x523e
movq 0x28(%rbp), %r12
testq %r12, %r12
je 0x52d2
movq 0x8(%rbx), %rax
leal -0x1(%rax), %ecx
cmpl %ecx, %r14d
jne 0x5258
incl 0x8(%r12)
incq %r14
cltq
movq %r12, %r15
cmpq %rax, %r14
jl 0x4d1d
jmp 0x52c3
movl $0x40, %edi
callq 0x3270
movl $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
leaq 0x7aba(%rip), %rcx      # 0xcd48
movq %rcx, (%rax)
movq $0x0, 0x38(%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 0x7a5a(%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 0x5327
cmpl $0x41, %ecx
je 0x539c
cmpl $0x43, %ecx
jne 0x5337
movq %rax, 0x20(%rbp)
jmp 0x52c3
cmpl $0x47, %ecx
je 0x53a5
cmpl $0x54, %ecx
jne 0x5337
movq %rax, 0x30(%rbp)
jmp 0x52c3
movq 0x7cb2(%rip), %r15      # 0xcff0
leaq 0x3ea1(%rip), %rsi      # 0x91e6
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 0x52c3
movq %rax, 0x18(%rbp)
jmp 0x52c3
movq %rax, 0x28(%rbp)
jmp 0x52c3
leaq 0x3d9b(%rip), %rdi      # 0x9150
leaq 0x3dad(%rip), %rsi      # 0x9169
leaq 0x3db2(%rip), %rcx      # 0x9175
movl $0xe5, %edx
callq 0x3190
leaq 0x3de5(%rip), %rdi      # 0x91b9
leaq 0x3d8e(%rip), %rsi      # 0x9169
leaq 0x3d93(%rip), %rcx      # 0x9175
movl $0xe7, %edx
callq 0x3190
jmp 0x53fa
jmp 0x53fa
jmp 0x53fa
jmp 0x53fa
jmp 0x53fa
jmp 0x53fa
jmp 0x53fa
movq %rax, %rbx
movl $0x40, %esi
movq %r13, %rdi
callq 0x3280
movq %rbx, %rdi
callq 0x3450