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

	OptimalSolverLN 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(".optimalLN") ).c_str() );
#ifdef FREQ
		ofstream freqOutput;
		freqOutput.open( (string(benchName) + string(".optimalLN_freq") ).c_str() );
#endif

		while (seedNum > 0) {
			cout << "seedNum: " << seedNum << endl;
			output << "seedNum: " << seedNum << endl;
#ifdef FREQ
			freqOutput << "seedNum: " << seedNum << endl;
#endif

			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);
#ifdef FREQ
					solver.backtrack();
					solver.sortOfFreq();
					solver.printFreqs(freqOutput);
					solver.printLength(freqOutput);
#endif
					if (frequencyCounter.find(frequency) == frequencyCounter.end() )
						frequencyCounter[frequency] = 1;
					else
						frequencyCounter[frequency]++;
				}

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

				//Name
				getline(readFile, read);
			}

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

			cin >> seedNum;

			solver.printStats(cout);
			solver.printStats(output);
		}
		output.close();
#ifdef FREQ
		freqOutput.close();
#endif

	}
	else if (argc == 1) {
		cout << "input seedNum and readLength: " << endl;
		int seedNum;
		int readLength;
		cin >> seedNum;
		string dummy = "";
		while (seedNum > 0) {
			cin >> readLength;
			solver.setMinLength(10);
			solver.init(readLength, seedNum);
			solver.feedL0();
			solver.solveDNA(dummy);
			solver.backtrack();
			solver.sortOfFreq();
			solver.printFreqs(cout);
			solver.printStats(cout);
			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 $0xab8, %rsp            # imm = 0xAB8
movl $0x0, 0xab4(%rsp)
movl %edi, 0xab0(%rsp)
movq %rsi, 0xaa8(%rsp)
leaq 0xa78(%rsp), %rdi
callq 0x44a0
leaq 0x670(%rsp), %rdi
callq 0x5fa0
jmp 0x38fd
cmpl $0x3, 0xab0(%rsp)
jne 0x424a
movq 0x206b6(%rip), %rdi     # 0x23fc8
leaq 0x176eb(%rip), %rsi     # 0x1b004
callq 0x32e0
movq %rax, 0xc8(%rsp)
jmp 0x3928
movq 0xc8(%rsp), %rdi
movq 0xaa8(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x32e0
movq %rax, 0xc0(%rsp)
jmp 0x394b
movq 0xc0(%rsp), %rdi
leaq 0x176b8(%rip), %rsi     # 0x1b012
callq 0x32e0
jmp 0x3961
movq 0xaa8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xb0(%rsp)
leaq 0x63f(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0x35e0
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
leaq 0x640(%rsp), %rdi
callq 0x44b0
jmp 0x39a9
leaq 0x670(%rsp), %rdi
leaq 0x640(%rsp), %rsi
callq 0x6260
jmp 0x39c0
leaq 0x640(%rsp), %rdi
callq 0x31c0
leaq 0x63f(%rsp), %rdi
callq 0x3350
movq 0x205e7(%rip), %rdi     # 0x23fc8
leaq 0x17639(%rip), %rsi     # 0x1b021
callq 0x32e0
jmp 0x39ef
movq 0x205e2(%rip), %rdi     # 0x23fd8
leaq 0x638(%rsp), %rsi
callq 0x31b0
jmp 0x3a05
leaq 0x428(%rsp), %rdi
callq 0x3390
jmp 0x3a14
movq 0xaa8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x428(%rsp), %rdi
movl $0x8, %edx
callq 0x31d0
jmp 0x3a34
leaq 0x408(%rsp), %rdi
callq 0x3490
movq 0xaa8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x3b0(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x32d0
movq 0xa8(%rsp), %rdi
leaq 0x17d04(%rip), %rsi     # 0x1b775
callq 0x3500
movq %rax, 0x3a8(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x3580
jmp 0x3a8d
movq 0x3a8(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x167(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x35e0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x168(%rsp), %rdi
callq 0x44b0
jmp 0x3ad1
leaq 0x13f(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x35e0
movq 0x90(%rsp), %rdx
leaq 0x17540(%rip), %rsi     # 0x1b035
leaq 0x140(%rsp), %rdi
callq 0x44b0
jmp 0x3b04
leaq 0x188(%rsp), %rdi
leaq 0x168(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x4570
jmp 0x3b23
leaq 0x188(%rsp), %rdi
callq 0x3140
movq %rax, %rsi
leaq 0x1a8(%rsp), %rdi
movl $0x10, %edx
callq 0x3050
jmp 0x3b47
leaq 0x188(%rsp), %rdi
callq 0x31c0
leaq 0x140(%rsp), %rdi
callq 0x31c0
leaq 0x13f(%rsp), %rdi
callq 0x3350
leaq 0x168(%rsp), %rdi
callq 0x31c0
leaq 0x167(%rsp), %rdi
callq 0x3350
cmpl $0x0, 0x638(%rsp)
jle 0x41e3
movq 0x2042b(%rip), %rdi     # 0x23fc8
leaq 0x1749c(%rip), %rsi     # 0x1b040
callq 0x32e0
movq %rax, 0x88(%rsp)
jmp 0x3bb3
movq 0x88(%rsp), %rdi
movl 0x638(%rsp), %esi
callq 0x35b0
movq %rax, 0x80(%rsp)
jmp 0x3bd1
movq 0x80(%rsp), %rdi
movq 0x203d8(%rip), %rsi     # 0x23fb8
callq 0x3330
jmp 0x3be7
leaq 0x17452(%rip), %rsi     # 0x1b040
leaq 0x1a8(%rsp), %rdi
callq 0x32e0
movq %rax, 0x78(%rsp)
jmp 0x3c02
movq 0x78(%rsp), %rdi
movl 0x638(%rsp), %esi
callq 0x35b0
movq %rax, 0x70(%rsp)
jmp 0x3c1a
movq 0x70(%rsp), %rdi
movq 0x20392(%rip), %rsi     # 0x23fb8
callq 0x3330
jmp 0x3c2d
leaq 0xa78(%rsp), %rdi
callq 0x4660
movq 0x428(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x428(%rsp,%rax), %rdi
xorl %esi, %esi
callq 0x3560
jmp 0x3c57
xorl %edx, %edx
movl %edx, %esi
leaq 0x428(%rsp), %rdi
callq 0x3480
jmp 0x3c6a
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3c81
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3c98
leaq 0x408(%rsp), %rdi
callq 0x34e0
movl %eax, %esi
movl 0x638(%rsp), %edx
leaq 0x670(%rsp), %rdi
callq 0x6360
jmp 0x3cbd
leaq 0x670(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x9ac0
movl %eax, 0x6c(%rsp)
jmp 0x3cd8
movl 0x6c(%rsp), %eax
movl %eax, 0x634(%rsp)
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x4690
movq %rax, 0x60(%rsp)
jmp 0x3cff
movq 0x60(%rsp), %rax
movq %rax, 0x130(%rsp)
leaq 0xa78(%rsp), %rdi
callq 0x46c0
movq %rax, 0x128(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x4670
testb $0x1, %al
jne 0x3d3f
jmp 0x3ece
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x46f0
movq %rax, 0x58(%rsp)
jmp 0x3d5b
movq 0x58(%rsp), %rax
movq $0x1, (%rax)
jmp 0x3ef9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x4485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x4478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x3dd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
leaq 0x640(%rsp), %rdi
callq 0x31c0
leaq 0x63f(%rsp), %rdi
callq 0x3350
jmp 0x4478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x4238
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x422b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x3ea3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x3e89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x3e7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x31c0
leaq 0x140(%rsp), %rdi
callq 0x31c0
leaq 0x13f(%rsp), %rdi
callq 0x3350
leaq 0x168(%rsp), %rdi
callq 0x31c0
leaq 0x167(%rsp), %rdi
callq 0x3350
jmp 0x421e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
jmp 0x421e
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x46f0
movq %rax, 0x50(%rsp)
jmp 0x3eea
movq 0x50(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3f10
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3f27
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3f3e
jmp 0x3f40
movq 0x428(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x428(%rsp,%rax), %rdi
callq 0x3610
movb %al, 0x4f(%rsp)
jmp 0x3f5f
movb 0x4f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3f6e
jmp 0x40c5
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x3f85
leaq 0x408(%rsp), %rdi
movl $0x4e, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3430
cmpq $-0x1, %rax
jne 0x407b
leaq 0x670(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x9ac0
movl %eax, 0x48(%rsp)
jmp 0x3fc0
movl 0x48(%rsp), %eax
movl %eax, 0x634(%rsp)
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x4690
movq %rax, 0x40(%rsp)
jmp 0x3fe7
movq 0x40(%rsp), %rax
movq %rax, 0x120(%rsp)
leaq 0xa78(%rsp), %rdi
callq 0x46c0
movq %rax, 0x118(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x4670
testb $0x1, %al
jne 0x4024
jmp 0x404e
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x46f0
movq %rax, 0x38(%rsp)
jmp 0x4040
movq 0x38(%rsp), %rax
movq $0x1, (%rax)
jmp 0x4079
leaq 0xa78(%rsp), %rdi
leaq 0x634(%rsp), %rsi
callq 0x46f0
movq %rax, 0x30(%rsp)
jmp 0x406a
movq 0x30(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x407b
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x4092
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x40a9
leaq 0x428(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x30b0
jmp 0x40c0
jmp 0x3f40
leaq 0xa78(%rsp), %rdi
callq 0x47e0
movq %rax, 0x110(%rsp)
leaq 0xa78(%rsp), %rdi
callq 0x46c0
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x4810
testb $0x1, %al
jne 0x410d
jmp 0x419b
leaq 0x110(%rsp), %rdi
callq 0x4830
movl (%rax), %esi
leaq 0x1a8(%rsp), %rdi
callq 0x3460
movq %rax, 0x28(%rsp)
jmp 0x4130
movq 0x28(%rsp), %rdi
leaq 0x17693(%rip), %rsi     # 0x1b7cf
callq 0x32e0
movq %rax, 0x20(%rsp)
jmp 0x4148
leaq 0x110(%rsp), %rdi
callq 0x4830
movq 0x20(%rsp), %rdi
movq 0x8(%rax), %rsi
callq 0x32c0
movq %rax, 0x18(%rsp)
jmp 0x416a
movq 0x18(%rsp), %rdi
movq 0x1fe42(%rip), %rsi     # 0x23fb8
callq 0x3330
jmp 0x417d
jmp 0x417f
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x4850
movq %rax, 0x100(%rsp)
jmp 0x40da
movq 0x1fe36(%rip), %rdi     # 0x23fd8
leaq 0x638(%rsp), %rsi
callq 0x31b0
jmp 0x41b1
movq 0x1fe10(%rip), %rsi     # 0x23fc8
leaq 0x670(%rsp), %rdi
callq 0xa190
jmp 0x41c7
leaq 0x670(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xa190
jmp 0x41de
jmp 0x3b88
leaq 0x1a8(%rsp), %rdi
callq 0x34b0
jmp 0x41f2
leaq 0x1a8(%rsp), %rdi
callq 0x34d0
leaq 0x408(%rsp), %rdi
callq 0x31c0
leaq 0x428(%rsp), %rdi
callq 0x30f0
jmp 0x4444
leaq 0x1a8(%rsp), %rdi
callq 0x34d0
leaq 0x408(%rsp), %rdi
callq 0x31c0
leaq 0x428(%rsp), %rdi
callq 0x30f0
jmp 0x4478
cmpl $0x1, 0xab0(%rsp)
jne 0x440d
movq 0x1fd69(%rip), %rdi     # 0x23fc8
leaq 0x16de4(%rip), %rsi     # 0x1b04a
callq 0x32e0
movq %rax, 0x10(%rsp)
jmp 0x4272
movq 0x10(%rsp), %rdi
movq 0x1fd3a(%rip), %rsi     # 0x23fb8
callq 0x3330
jmp 0x4285
movq 0x1fd4c(%rip), %rdi     # 0x23fd8
leaq 0xfc(%rsp), %rsi
callq 0x31b0
jmp 0x429b
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x35e0
movq 0x8(%rsp), %rdx
leaq 0x1742a(%rip), %rsi     # 0x1b6e3
leaq 0xd8(%rsp), %rdi
callq 0x44b0
jmp 0x42c8
leaq 0xd7(%rsp), %rdi
callq 0x3350
cmpl $0x0, 0xfc(%rsp)
jle 0x43fe
movq 0x1fcee(%rip), %rdi     # 0x23fd8
leaq 0xf8(%rsp), %rsi
callq 0x31b0
jmp 0x42f9
leaq 0x670(%rsp), %rdi
movl $0xa, %esi
callq 0x6e40
jmp 0x430d
movl 0xf8(%rsp), %esi
movl 0xfc(%rsp), %edx
leaq 0x670(%rsp), %rdi
callq 0x6360
jmp 0x432a
leaq 0x670(%rsp), %rdi
callq 0x6cc0
jmp 0x4339
leaq 0x670(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x9ac0
jmp 0x4350
leaq 0x670(%rsp), %rdi
callq 0x9b10
jmp 0x435f
leaq 0x670(%rsp), %rdi
callq 0x9ee0
jmp 0x436e
movq 0x1fc53(%rip), %rsi     # 0x23fc8
leaq 0x670(%rsp), %rdi
callq 0xa040
jmp 0x4384
movq 0x1fc3d(%rip), %rsi     # 0x23fc8
leaq 0x670(%rsp), %rdi
callq 0xa190
jmp 0x439a
movq 0x1fc37(%rip), %rdi     # 0x23fd8
leaq 0xfc(%rsp), %rsi
callq 0x31b0
jmp 0x43b0
jmp 0x42d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
leaq 0xd7(%rsp), %rdi
callq 0x3350
jmp 0x4478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x668(%rsp)
movl %eax, 0x664(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x31c0
jmp 0x4478
leaq 0xd8(%rsp), %rdi
callq 0x31c0
jmp 0x4442
movq 0x1fbdc(%rip), %rdi     # 0x23ff0
leaq 0x16c4e(%rip), %rsi     # 0x1b069
callq 0x32e0
movq %rax, (%rsp)
jmp 0x4426
movq (%rsp), %rdi
movq 0x1fb87(%rip), %rsi     # 0x23fb8
callq 0x3330
jmp 0x4438
movl $0x1, %edi
callq 0x33c0
jmp 0x4444
movl $0x0, 0xab4(%rsp)
leaq 0x670(%rsp), %rdi
callq 0x6080
leaq 0xa78(%rsp), %rdi
callq 0x4890
movl 0xab4(%rsp), %eax
addq $0xab8, %rsp            # imm = 0xAB8
retq
leaq 0x670(%rsp), %rdi
callq 0x6080
leaq 0xa78(%rsp), %rdi
callq 0x4890
movq 0x668(%rsp), %rdi
callq 0x35d0
nop