// File: /workspace/github_cpp_makefile_star2_O2/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, 0x1c(%rsp)
movl %edx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
addq $0x318, %rdi            # imm = 0x318
movl (%rbx), %edx
callq 0x7f8a
movq 0x320(%rbx), %rcx
movl %eax, %edx
movq (%rcx,%rdx,8), %rbp
movslq (%rbx), %r14
movq %rbx, 0x20(%rsp)
leaq 0xa8(%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%r13), %rax
xorl %r15d, %r15d
movq %rdx, 0x28(%rsp)
leaq (,%rdx,8), %rcx
movq %rcx, 0x30(%rsp)
xorl %ebx, %ebx
movq %r13, 0x10(%rsp)
cltq
cmpq %rax, %r14
jge 0x4c5c
testq %rbp, %rbp
je 0x4c15
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x28(%rax)
testb %al, %al
je 0x48f1
movq %r15, 0x40(%rsp)
pushq $0x38
popq %rdi
callq 0x3230
movq %rax, %r15
leaq 0x9520(%rip), %rax      # 0xdd10
movq %rax, (%r15)
movl $0x1, 0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x28(%r15)
cmpl $0x1, 0x8(%rbp)
jne 0x4d0c
movq %rbx, 0x8(%rsp)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq 0x38(%rsp), %rdi
callq 0x7b36
cmpl %eax, %r12d
jae 0x4d2b
movq 0x20(%rsp), %rax
movq 0x310(%rax), %rbx
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %eax
imulq $0x268, %rax, %r12      # imm = 0x268
addq %rbx, %r12
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
movq %r12, %rdi
movl %eax, %esi
movl %r14d, %edx
callq 0x7c2a
cmpb $0x54, %al
je 0x4ad8
movzbl %al, %eax
cmpl $0x43, %eax
je 0x4a7e
cmpl $0x47, %eax
je 0x48f9
movq %r15, %rbx
addq $0x18, %rbx
cmpl $0x4d, %eax
je 0x49b5
cmpl $0x52, %eax
je 0x491d
cmpl $0x41, %eax
jne 0x4a4d
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x9473(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
jmp 0x4b1e
movq %rbp, %r15
jmp 0x4b8e
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
jmp 0x498c
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x93fd(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
movl $0x1, 0x8(%r13)
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x93b0(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x28(%r15)
jmp 0x4b1e
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x9365(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, (%rbx)
movl $0x1, 0x8(%r13)
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x9318(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%r15)
jmp 0x4b1e
movq 0x959c(%rip), %rdi      # 0xdff0
leaq 0x477b(%rip), %rsi      # 0x91d6
callq 0x3220
movq (%r13), %rcx
movsbl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x3290
movq %rax, %rdi
callq 0x3120
jmp 0x4b2b
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movq 0x8(%rsp), %rbx
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x9297(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x20(%r15)
movl $0x1, 0x8(%r13)
movq 0x10(%rsp), %r13
jmp 0x4b30
pushq $0x40
popq %rdi
callq 0x3230
movq %rax, %r13
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x18(%rax)
movl %eax, %r12d
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
leaq 0x9242(%rip), %rcx      # 0xdd50
movq %rcx, (%r13)
movl %r12d, 0x38(%r13)
movl %eax, 0x3c(%r13)
movq %r13, 0x30(%r15)
movl $0x1, 0x8(%r13)
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbx
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
je 0x4b5e
movq (%r13), %rax
movzbl -0x1(%rax,%r14), %eax
cmpl $0x54, %eax
je 0x4b7d
cmpl $0x43, %eax
je 0x4b71
cmpl $0x47, %eax
je 0x4b77
cmpl $0x41, %eax
jne 0x4b84
addq $0x18, %rcx
jmp 0x4b81
movq 0x20(%rsp), %rax
movq 0x320(%rax), %rcx
addq 0x30(%rsp), %rcx
jmp 0x4b81
addq $0x20, %rcx
jmp 0x4b81
addq $0x28, %rcx
jmp 0x4b81
addq $0x30, %rcx
movq %r15, (%rcx)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
incl 0x8(%r15)
movq (%r13), %rax
movzbl (%rax,%r14), %eax
cmpl $0x54, %eax
je 0x4bc1
cmpl $0x43, %eax
je 0x4bb5
cmpl $0x47, %eax
je 0x4bbb
cmpl $0x41, %eax
jne 0x4bc7
movq 0x18(%r15), %rbp
jmp 0x4bf6
movq 0x20(%r15), %rbp
jmp 0x4bf6
movq 0x28(%r15), %rbp
jmp 0x4bf6
movq 0x30(%r15), %rbp
jmp 0x4bf6
movq 0x9422(%rip), %rdi      # 0xdff0
leaq 0x4601(%rip), %rsi      # 0x91d6
callq 0x3220
movq (%r13), %rcx
movsbl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x3290
movq %rax, %rdi
callq 0x3120
movq %rbx, %rbp
testq %rbp, %rbp
je 0x4c6b
movq 0x8(%r13), %rax
leal -0x1(%rax), %ecx
cmpl %ecx, %r14d
jne 0x4c0a
incl 0x8(%rbp)
incq %r14
movq %rbp, %rbx
jmp 0x47b3
pushq $0x40
popq %rdi
callq 0x3230
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
leaq 0x9121(%rip), %rcx      # 0xdd50
movq %rcx, (%rax)
movq 0x20(%rsp), %rcx
movq 0x320(%rcx), %rcx
movq 0x28(%rsp), %rdx
movq %rax, (%rcx,%rdx,8)
movl 0x18(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x1c(%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
pushq $0x40
popq %rdi
callq 0x3230
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
leaq 0x90cb(%rip), %rcx      # 0xdd50
movq %rcx, (%rax)
movl 0x18(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movl $0x1, 0x8(%rax)
movq (%r13), %rcx
movzbl (%rcx,%r14), %ecx
cmpl $0x54, %ecx
je 0x4ccc
cmpl $0x43, %ecx
je 0x4cc0
cmpl $0x47, %ecx
je 0x4cc6
cmpl $0x41, %ecx
jne 0x4cd2
movq %rax, 0x18(%r15)
jmp 0x4c5c
movq %rax, 0x20(%r15)
jmp 0x4c5c
movq %rax, 0x28(%r15)
jmp 0x4c5c
movq %rax, 0x30(%r15)
jmp 0x4c5c
movq 0x9317(%rip), %rdi      # 0xdff0
leaq 0x44f6(%rip), %rsi      # 0x91d6
callq 0x3220
movq (%r13), %rcx
movsbl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x3290
movq %rax, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3120
leaq 0x442d(%rip), %rdi      # 0x9140
leaq 0x443f(%rip), %rsi      # 0x9159
leaq 0x4444(%rip), %rcx      # 0x9165
movl $0xe5, %edx
callq 0x3170
leaq 0x4477(%rip), %rdi      # 0x91a9
leaq 0x4420(%rip), %rsi      # 0x9159
leaq 0x4425(%rip), %rcx      # 0x9165
movl $0xe7, %edx
callq 0x3170
jmp 0x4d58
jmp 0x4d58
jmp 0x4d58
jmp 0x4d58
jmp 0x4d58
jmp 0x4d58
jmp 0x4d58
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r13, %rdi
callq 0x3240
movq %rbx, %rdi
callq 0x3430