// File: CMU-SAFARI[P]optimal-seed-solver/testOptimalSolver.cc
// Function: main
int main(int argc, const char* argv[]) {
	map <unsigned int, unsigned long long> frequencyCounter;

	OptimalSolver solver;

	if (argc == 3) {
		cout << "Loading tree " << argv[1] << " into memory.\n";
		solver.loadTree(argv[1]);
		cout << "Finished loading. \n";

		int seedNum;
		unsigned int frequency;
		//cout << "Please input number of seeds: ";
		cin >> seedNum;

		ifstream readFile;
		readFile.open(argv[2]);
		string read;

		char benchFileName[80];
		strcpy(benchFileName, argv[2]);
		char* benchName = strtok(benchFileName, ".");

		ofstream output;
		output.open( (string(benchName) + string(".hobbes") ).c_str() );

		while (seedNum > 0) {
			frequencyCounter.clear();

			readFile.clear();
			readFile.seekg(0, ios::beg);

			//Name
			getline(readFile, read);
			//Read
			getline(readFile, read);
			solver.init(read.length(), seedNum);
			//		cout << "DNA: " << read << endl;

			//Solve
			frequency = solver.solveDNA(read);
			if (frequencyCounter.find(frequency) == frequencyCounter.end() )
				frequencyCounter[frequency] = 1;
			else
				frequencyCounter[frequency]++;

			//Trash
			getline(readFile, read);
			getline(readFile, read);

			//Name
			getline(readFile, read);

			while (!readFile.eof()) {
				//Read
				getline(readFile, read);
				//			cout << "DNA: " << read << endl;

				//Solve
				if (read.find('N') == string::npos) {
					frequency = solver.solveDNA(read);
					if (frequencyCounter.find(frequency) == frequencyCounter.end() )
						frequencyCounter[frequency] = 1;
					else
						frequencyCounter[frequency]++;
				}

				//Trash
				getline(readFile, read);
				getline(readFile, read);

				//Name
				getline(readFile, read);
			}

			cout << "seedNum: " << seedNum << endl;
			output << "seedNum: " << seedNum << endl;
			for (map<unsigned int, unsigned long long>::iterator iter = frequencyCounter.begin(); iter != frequencyCounter.end(); iter++)
				output << iter->first << ": " << iter->second << endl;

			cin >> seedNum;
		}


	}
	else if (argc == 1) {
		cout << "input seedNum and readLength: " << endl;
		int seedNum;
		int readLength;
		cin >> seedNum;
		string dummy = "";
		while (seedNum > 0) {
			cin >> readLength;
			solver.init(readLength, seedNum);
			solver.feedL0();
			solver.solveDNA(dummy);
			cin >> seedNum;
		}
	}
	else
	{
		cerr << "USAGE: ./testOptimalSolver <Tree File> <Read File>" << endl;
		exit(1);
	}

//	cout << "Loading tree " << argv[1] << " into memory.\n";
//	solver.loadTree(argv[1]);
//	cout << "Finished loading. \n";
//
//	int seedNum;
//	unsigned int frequency;
//	//cout << "Please input number of seeds: ";
//	cin >> seedNum;
//
//	ifstream readFile;
//	readFile.open(argv[2]);
//	string read;
//
//	while (seedNum > 0) {
//		frequencyCounter.clear();
//
//		readFile.clear();
//		readFile.seekg(0, ios::beg);
//
//		//Name
//		getline(readFile, read);
//		//Read
//		getline(readFile, read);
//		solver.init(read.length(), seedNum);
//		//		cout << "DNA: " << read << endl;
//
//		//Solve
//		frequency = solver.solveDNA(read);
//		if (frequencyCounter.find(frequency) == frequencyCounter.end() )
//			frequencyCounter[frequency] = 1;
//		else
//			frequencyCounter[frequency]++;
//
//		//Trash
//		getline(readFile, read);
//		getline(readFile, read);
//
//		//Name
//		getline(readFile, read);
//
//		while (!readFile.eof()) {
//			//Read
//			getline(readFile, read);
//			//			cout << "DNA: " << read << endl;
//
//			//Solve
//			if (read.find('N') == string::npos) {
//				frequency = solver.solveDNA(read);
//				if (frequencyCounter.find(frequency) == frequencyCounter.end() )
//					frequencyCounter[frequency] = 1;
//				else
//					frequencyCounter[frequency]++;
//			}
//
//			//Trash
//			getline(readFile, read);
//			getline(readFile, read);
//
//			//Name
//			getline(readFile, read);
//		}
//
//		cout << "seedNum: " << seedNum << endl;
//		for (map<unsigned int, unsigned long long>::iterator iter = frequencyCounter.begin(); iter != frequencyCounter.end(); iter++)
//			cout << iter->first << ": " << iter->second << endl;
//
//		cin >> seedNum;
//	}

	return 0;
}

// Assembly:
subq $0xaa8, %rsp            # imm = 0xAA8
movl $0x0, 0xaa4(%rsp)
movl %edi, 0xaa0(%rsp)
movq %rsi, 0xa98(%rsp)
leaq 0xa68(%rsp), %rdi
callq 0x44d0
leaq 0x6d0(%rsp), %rdi
callq 0x5fd0
jmp 0x38dd
cmpl $0x3, 0xaa0(%rsp)
jne 0x4282
movq 0x1e6d6(%rip), %rdi     # 0x21fc8
leaq 0x1670b(%rip), %rsi     # 0x1a004
callq 0x32d0
movq %rax, 0xc8(%rsp)
jmp 0x3908
movq 0xc8(%rsp), %rdi
movq 0xa98(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x32d0
movq %rax, 0xc0(%rsp)
jmp 0x392b
movq 0xc0(%rsp), %rdi
leaq 0x166d8(%rip), %rsi     # 0x1a012
callq 0x32d0
jmp 0x3941
movq 0xa98(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xb0(%rsp)
leaq 0x69f(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0x35d0
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
leaq 0x6a0(%rsp), %rdi
callq 0x44e0
jmp 0x3989
leaq 0x6d0(%rsp), %rdi
leaq 0x6a0(%rsp), %rsi
callq 0x6130
jmp 0x39a0
leaq 0x6a0(%rsp), %rdi
callq 0x31c0
leaq 0x69f(%rsp), %rdi
callq 0x3340
movq 0x1e607(%rip), %rdi     # 0x21fc8
leaq 0x16659(%rip), %rsi     # 0x1a021
callq 0x32d0
jmp 0x39cf
movq 0x1e602(%rip), %rdi     # 0x21fd8
leaq 0x698(%rsp), %rsi
callq 0x31b0
jmp 0x39e5
leaq 0x488(%rsp), %rdi
callq 0x3380
jmp 0x39f4
movq 0xa98(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x488(%rsp), %rdi
movl $0x8, %edx
callq 0x31d0
jmp 0x3a14
leaq 0x468(%rsp), %rdi
callq 0x3480
movq 0xa98(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x410(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x32c0
movq 0xa8(%rsp), %rdi
leaq 0x16e3c(%rip), %rsi     # 0x1a88d
callq 0x34f0
movq %rax, 0x408(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x3570
jmp 0x3a6d
movq 0x408(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x1c7(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x35d0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x1c8(%rsp), %rdi
callq 0x44e0
jmp 0x3ab1
leaq 0x19f(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x35d0
movq 0x90(%rsp), %rdx
leaq 0x16560(%rip), %rsi     # 0x1a035
leaq 0x1a0(%rsp), %rdi
callq 0x44e0
jmp 0x3ae4
leaq 0x1e8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
leaq 0x1a0(%rsp), %rdx
callq 0x45a0
jmp 0x3b03
leaq 0x1e8(%rsp), %rdi
callq 0x3140
movq %rax, %rsi
leaq 0x208(%rsp), %rdi
movl $0x10, %edx
callq 0x3050
jmp 0x3b27
leaq 0x1e8(%rsp), %rdi
callq 0x31c0
leaq 0x1a0(%rsp), %rdi
callq 0x31c0
leaq 0x19f(%rsp), %rdi
callq 0x3340
leaq 0x1c8(%rsp), %rdi
callq 0x31c0
leaq 0x1c7(%rsp), %rdi
callq 0x3340
cmpl $0x0, 0x698(%rsp)
jle 0x422a
leaq 0xa68(%rsp), %rdi
callq 0x4690
movq 0x488(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x488(%rsp,%rax), %rdi
xorl %esi, %esi
callq 0x3550
jmp 0x3ba0
xorl %edx, %edx
movl %edx, %esi
leaq 0x488(%rsp), %rdi
callq 0x3470
jmp 0x3bb3
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3bca
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3be1
leaq 0x468(%rsp), %rdi
callq 0x34d0
movl %eax, %esi
movl 0x698(%rsp), %edx
leaq 0x6d0(%rsp), %rdi
callq 0x6210
jmp 0x3c06
leaq 0x178(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x3150
jmp 0x3c1d
leaq 0x6d0(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x6b90
movl %eax, 0x8c(%rsp)
jmp 0x3c3b
movl 0x8c(%rsp), %eax
movl %eax, 0x694(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x31c0
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x46c0
movq %rax, 0x80(%rsp)
jmp 0x3c75
movq 0x80(%rsp), %rax
movq %rax, 0x170(%rsp)
leaq 0xa68(%rsp), %rdi
callq 0x46f0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x46a0
testb $0x1, %al
jne 0x3cb8
jmp 0x3e6d
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x4720
movq %rax, 0x78(%rsp)
jmp 0x3cd4
movq 0x78(%rsp), %rax
movq $0x1, (%rax)
jmp 0x3e98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x44a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x449b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x3d4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x6a0(%rsp), %rdi
callq 0x31c0
leaq 0x69f(%rsp), %rdi
callq 0x3340
jmp 0x449b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x4270
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x4263
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x3e1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x3e02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x3df5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x31c0
leaq 0x1a0(%rsp), %rdi
callq 0x31c0
leaq 0x19f(%rsp), %rdi
callq 0x3340
leaq 0x1c8(%rsp), %rdi
callq 0x31c0
leaq 0x1c7(%rsp), %rdi
callq 0x3340
jmp 0x4256
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x4256
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x31c0
jmp 0x4256
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x4720
movq %rax, 0x70(%rsp)
jmp 0x3e89
movq 0x70(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3eaf
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3ec6
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3edd
jmp 0x3edf
movq 0x488(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x488(%rsp,%rax), %rdi
callq 0x3600
movb %al, 0x6f(%rsp)
jmp 0x3efe
movb 0x6f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3f0d
jmp 0x40ae
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x3f24
leaq 0x468(%rsp), %rdi
movl $0x4e, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3420
cmpq $-0x1, %rax
jne 0x4064
leaq 0x148(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x3150
jmp 0x3f5b
leaq 0x6d0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6b90
movl %eax, 0x68(%rsp)
jmp 0x3f76
movl 0x68(%rsp), %eax
movl %eax, 0x694(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x31c0
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x46c0
movq %rax, 0x60(%rsp)
jmp 0x3faa
movq 0x60(%rsp), %rax
movq %rax, 0x140(%rsp)
leaq 0xa68(%rsp), %rdi
callq 0x46f0
movq %rax, 0x138(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x46a0
testb $0x1, %al
jne 0x3fe7
jmp 0x4037
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x4720
movq %rax, 0x58(%rsp)
jmp 0x4003
movq 0x58(%rsp), %rax
movq $0x1, (%rax)
jmp 0x4062
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x31c0
jmp 0x4256
leaq 0xa68(%rsp), %rdi
leaq 0x694(%rsp), %rsi
callq 0x4720
movq %rax, 0x50(%rsp)
jmp 0x4053
movq 0x50(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x4064
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x407b
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x4092
leaq 0x488(%rsp), %rdi
leaq 0x468(%rsp), %rsi
callq 0x30b0
jmp 0x40a9
jmp 0x3edf
movq 0x1df13(%rip), %rdi     # 0x21fc8
leaq 0x15f81(%rip), %rsi     # 0x1a03d
callq 0x32d0
movq %rax, 0x48(%rsp)
jmp 0x40c8
movq 0x48(%rsp), %rdi
movl 0x698(%rsp), %esi
callq 0x35a0
movq %rax, 0x40(%rsp)
jmp 0x40e0
movq 0x40(%rsp), %rdi
movq 0x1decc(%rip), %rsi     # 0x21fb8
callq 0x3320
jmp 0x40f3
leaq 0x15f43(%rip), %rsi     # 0x1a03d
leaq 0x208(%rsp), %rdi
callq 0x32d0
movq %rax, 0x38(%rsp)
jmp 0x410e
movq 0x38(%rsp), %rdi
movl 0x698(%rsp), %esi
callq 0x35a0
movq %rax, 0x30(%rsp)
jmp 0x4126
movq 0x30(%rsp), %rdi
movq 0x1de86(%rip), %rsi     # 0x21fb8
callq 0x3320
jmp 0x4139
leaq 0xa68(%rsp), %rdi
callq 0x4810
movq %rax, 0x130(%rsp)
leaq 0xa68(%rsp), %rdi
callq 0x46f0
movq %rax, 0x128(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x4840
testb $0x1, %al
jne 0x4181
jmp 0x420f
leaq 0x130(%rsp), %rdi
callq 0x4860
movl (%rax), %esi
leaq 0x208(%rsp), %rdi
callq 0x3450
movq %rax, 0x28(%rsp)
jmp 0x41a4
movq 0x28(%rsp), %rdi
leaq 0x16737(%rip), %rsi     # 0x1a8e7
callq 0x32d0
movq %rax, 0x20(%rsp)
jmp 0x41bc
leaq 0x130(%rsp), %rdi
callq 0x4860
movq 0x20(%rsp), %rdi
movq 0x8(%rax), %rsi
callq 0x32b0
movq %rax, 0x18(%rsp)
jmp 0x41de
movq 0x18(%rsp), %rdi
movq 0x1ddce(%rip), %rsi     # 0x21fb8
callq 0x3320
jmp 0x41f1
jmp 0x41f3
leaq 0x130(%rsp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, 0x120(%rsp)
jmp 0x414e
movq 0x1ddc2(%rip), %rdi     # 0x21fd8
leaq 0x698(%rsp), %rsi
callq 0x31b0
jmp 0x4225
jmp 0x3b68
leaq 0x208(%rsp), %rdi
callq 0x34c0
leaq 0x468(%rsp), %rdi
callq 0x31c0
leaq 0x488(%rsp), %rdi
callq 0x30f0
jmp 0x4467
leaq 0x208(%rsp), %rdi
callq 0x34c0
leaq 0x468(%rsp), %rdi
callq 0x31c0
leaq 0x488(%rsp), %rdi
callq 0x30f0
jmp 0x449b
cmpl $0x1, 0xaa0(%rsp)
jne 0x4430
movq 0x1dd31(%rip), %rdi     # 0x21fc8
leaq 0x15da9(%rip), %rsi     # 0x1a047
callq 0x32d0
movq %rax, 0x10(%rsp)
jmp 0x42aa
movq 0x10(%rsp), %rdi
movq 0x1dd02(%rip), %rsi     # 0x21fb8
callq 0x3320
jmp 0x42bd
movq 0x1dd14(%rip), %rdi     # 0x21fd8
leaq 0x11c(%rsp), %rsi
callq 0x31b0
jmp 0x42d3
leaq 0xf7(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x35d0
movq 0x8(%rsp), %rdx
leaq 0x1650a(%rip), %rsi     # 0x1a7fb
leaq 0xf8(%rsp), %rdi
callq 0x44e0
jmp 0x4300
leaq 0xf7(%rsp), %rdi
callq 0x3340
cmpl $0x0, 0x11c(%rsp)
jle 0x4412
movq 0x1dcb6(%rip), %rdi     # 0x21fd8
leaq 0x118(%rsp), %rsi
callq 0x31b0
jmp 0x4331
movl 0x118(%rsp), %esi
movl 0x11c(%rsp), %edx
leaq 0x6d0(%rsp), %rdi
callq 0x6210
jmp 0x434e
leaq 0x6d0(%rsp), %rdi
callq 0x69e0
jmp 0x435d
leaq 0xd0(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x3150
jmp 0x4374
leaq 0x6d0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x6b90
jmp 0x438b
leaq 0xd0(%rsp), %rdi
callq 0x31c0
movq 0x1dc39(%rip), %rdi     # 0x21fd8
leaq 0x11c(%rsp), %rsi
callq 0x31b0
jmp 0x43ae
jmp 0x430d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0xf7(%rsp), %rdi
callq 0x3340
jmp 0x449b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x4421
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x31c0
jmp 0x4421
leaq 0xf8(%rsp), %rdi
callq 0x31c0
jmp 0x4465
leaq 0xf8(%rsp), %rdi
callq 0x31c0
jmp 0x449b
movq 0x1dbb9(%rip), %rdi     # 0x21ff0
leaq 0x15c28(%rip), %rsi     # 0x1a066
callq 0x32d0
movq %rax, (%rsp)
jmp 0x4449
movq (%rsp), %rdi
movq 0x1db64(%rip), %rsi     # 0x21fb8
callq 0x3320
jmp 0x445b
movl $0x1, %edi
callq 0x33b0
jmp 0x4467
movl $0x0, 0xaa4(%rsp)
leaq 0x6d0(%rsp), %rdi
callq 0x6050
leaq 0xa68(%rsp), %rdi
callq 0x48c0
movl 0xaa4(%rsp), %eax
addq $0xaa8, %rsp            # imm = 0xAA8
retq
leaq 0x6d0(%rsp), %rdi
callq 0x6050
leaq 0xa68(%rsp), %rdi
callq 0x48c0
movq 0x6c8(%rsp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)
nopl (%rax)