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

	ThresholdSolver solver;
	FastHASHSolver fsolver;

	if (argc != 3) {
		cerr << "USAGE: ./testOptimalSolver <Tree File> <Read File>" << endl;
		exit(1);
	}

	cout << "Loading tree " << argv[1] << " into memory.\n";
	solver.loadTree(argv[1]);
	fsolver.loadTree(argv[1]);
	cout << "Finished loading. \n";

	int seedNum;
	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(".gems") ).c_str() );

	int threshold = 0;
	vector<int> thresholds;

	while (seedNum > 0) {
		cin >> threshold;
		thresholds.clear();
		thresholds.resize(seedNum, threshold);
		solver.setThresholdVec(thresholds);

		frequencyCounter.clear();

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

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

		//Solve
		frequency = solver.solve(read);

		if (frequency < 0) {
			assert(frequency == -1);
			frequency = fsolver.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.solve(read);

				if (frequency < 0) {
					assert(frequency == -1);
					frequency = fsolver.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 << " | threshold: " << threshold << endl;
		output << "seedNum: " << seedNum << " | threshold: " << threshold << endl;
		for (map<int, unsigned long long>::iterator iter = frequencyCounter.begin(); iter != frequencyCounter.end(); iter++)
			output << iter->first << ": " << iter->second << endl;
		
		cin >> seedNum;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xdf8, %rsp            # imm = 0xDF8
movq %rsi, %r14
movl %edi, %ebx
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x960(%rsp), %rdi
callq 0x4edc
leaq 0x5e0(%rsp), %rdi
callq 0x46f0
cmpl $0x3, %ebx
jne 0x3cfa
movq 0xd8c2(%rip), %rdi      # 0x10fc8
leaq 0x792a(%rip), %rsi      # 0xb037
callq 0x3250
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0x3250
leaq 0x7920(%rip), %rsi      # 0xb045
movq %rax, %rdi
callq 0x3250
movq 0x8(%r14), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x188(%rsp), %rdx
callq 0x3e48
leaq 0x960(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x5034
leaq 0xe8(%rsp), %rdi
callq 0x3110
movq 0x8(%r14), %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x188(%rsp), %rdx
callq 0x3e48
leaq 0x5e0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x4742
leaq 0xc8(%rsp), %rdi
callq 0x3110
movq 0xd81e(%rip), %rdi      # 0x10fc8
leaq 0x78a3(%rip), %rsi      # 0xb054
callq 0x3250
movq 0xd81b(%rip), %rdi      # 0x10fd8
leaq 0x8(%rsp), %rsi
callq 0x3170
leaq 0x188(%rsp), %rdi
callq 0x32a0
movq 0x10(%r14), %rsi
leaq 0x188(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x3180
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x10(%r14), %rsi
leaq 0x390(%rsp), %r14
movq %r14, %rdi
callq 0x3240
leaq 0x7de5(%rip), %rsi      # 0xb5ff
movq %r14, %rdi
callq 0x33d0
movq %rax, %r14
leaq 0x3e0(%rsp), %rdi
callq 0x3440
leaq 0x68(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x3e48
leaq 0x781d(%rip), %rsi      # 0xb068
leaq 0xa8(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x3e48
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x3e83
movq 0x10(%rsp), %rsi
leaq 0x3e0(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x3040
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3110
leaq 0xa8(%rsp), %rdi
callq 0x3110
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x3110
andl $0x0, (%r15)
andq $0x0, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
leaq 0x88(%rsp), %r12
leaq 0x168(%rsp), %r13
leaq 0x3e0(%rsp), %r15
cmpl $0x0, 0x8(%rsp)
jle 0x3c5b
movq 0xd6ee(%rip), %rdi      # 0x10fd8
leaq 0x68(%rsp), %rsi
callq 0x3170
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x3905
movq %rax, 0x18(%rsp)
movslq 0x8(%rsp), %rsi
movq %r14, %rdi
leaq 0x68(%rsp), %rdx
callq 0x3eea
leaq 0x960(%rsp), %rdi
movq %r14, %rsi
callq 0x514e
leaq 0x38(%rsp), %rdi
callq 0x4420
movq 0x188(%rsp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x3430
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3370
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movl 0x90(%rsp), %esi
movl 0x8(%rsp), %edx
leaq 0x5e0(%rsp), %rdi
pushq $0xe
popq %rcx
callq 0x4790
leaq 0x148(%rsp), %rdi
movq %r12, %rsi
callq 0x3380
leaq 0x960(%rsp), %rdi
leaq 0x148(%rsp), %r14
movq %r14, %rsi
callq 0x5bdc
movl %eax, 0x4(%rsp)
movq %r14, %rdi
callq 0x3110
movl 0x4(%rsp), %eax
testl %eax, %eax
jns 0x39fd
cmpl $-0x1, %eax
leaq 0x128(%rsp), %rdi
jne 0x3ce1
movq %r12, %rsi
callq 0x3380
leaq 0x5e0(%rsp), %rdi
leaq 0x128(%rsp), %r14
movq %r14, %rsi
callq 0x47b0
movl %eax, 0x4(%rsp)
movq %r14, %rdi
callq 0x3110
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x444e
leaq 0x40(%rsp), %rcx
cmpq %rcx, %rax
je 0x3a2a
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x3f1e
incq (%rax)
jmp 0x3a40
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x3f1e
movq $0x1, (%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
leaq 0x960(%rsp), %r14
movq 0x188(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1a8(%rsp,%rax)
jne 0x3b75
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %r12, %rdi
pushq $0x4e
popq %rsi
xorl %edx, %edx
callq 0x3340
cmpq $-0x1, %rax
jne 0x3b4f
movq %r13, %rdi
movq %r12, %rsi
callq 0x3380
movq %r14, %rdi
movq %r13, %rsi
callq 0x5bdc
movl %eax, 0x4(%rsp)
movq %r13, %rdi
callq 0x3110
movl 0x4(%rsp), %eax
testl %eax, %eax
jns 0x3b0c
cmpl $-0x1, %eax
jne 0x3cc4
leaq 0x108(%rsp), %rdi
movq %r12, %rsi
callq 0x3380
leaq 0x5e0(%rsp), %rdi
leaq 0x108(%rsp), %rbp
movq %rbp, %rsi
callq 0x47b0
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
callq 0x3110
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x444e
leaq 0x40(%rsp), %rcx
cmpq %rcx, %rax
je 0x3b39
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x3f1e
incq (%rax)
jmp 0x3b4f
leaq 0x38(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x3f1e
movq $0x1, (%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3080
jmp 0x3a69
movq 0xd44c(%rip), %rdi      # 0x10fc8
leaq 0x752a(%rip), %rsi      # 0xb0ad
callq 0x3250
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x3450
movq %rax, %rdi
leaq 0x7519(%rip), %rsi      # 0xb0b7
callq 0x3250
movl 0x68(%rsp), %esi
movq %rax, %rdi
callq 0x3450
movq %rax, %rdi
callq 0x3120
movq %r15, %rdi
leaq 0x74ec(%rip), %rsi      # 0xb0ad
callq 0x3250
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x3450
movq %rax, %rdi
leaq 0x74db(%rip), %rsi      # 0xb0b7
callq 0x3250
movl 0x68(%rsp), %esi
movq %rax, %rdi
callq 0x3450
movq %rax, %rdi
callq 0x3120
movq 0x50(%rsp), %r14
leaq 0x40(%rsp), %rax
cmpq %rax, %r14
je 0x3c40
movl 0x20(%r14), %esi
movq %r15, %rdi
callq 0x3450
movq %rax, %rdi
leaq 0x7a22(%rip), %rsi      # 0xb63c
callq 0x3250
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0x31a0
movq %rax, %rdi
callq 0x3120
movq %r14, %rdi
callq 0x31d0
movq %rax, %r14
jmp 0x3bfa
movq 0xd391(%rip), %rdi      # 0x10fd8
leaq 0x8(%rsp), %rsi
callq 0x3170
leaq 0x10(%rsp), %r14
jmp 0x38d8
leaq 0x10(%rsp), %rdi
callq 0x40e2
leaq 0x3e0(%rsp), %rdi
callq 0x33b0
leaq 0x88(%rsp), %rdi
callq 0x3110
leaq 0x188(%rsp), %rdi
callq 0x30c0
leaq 0x5e0(%rsp), %rdi
callq 0x472c
leaq 0x960(%rsp), %rdi
callq 0x3f6c
leaq 0x38(%rsp), %rdi
callq 0x408a
xorl %eax, %eax
addq $0xdf8, %rsp            # imm = 0xDF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x73a3(%rip), %rdi      # 0xb06e
leaq 0x73ac(%rip), %rsi      # 0xb07e
leaq 0x73b7(%rip), %rcx      # 0xb090
pushq $0x5f
popq %rdx
callq 0x3190
leaq 0x7386(%rip), %rdi      # 0xb06e
leaq 0x738f(%rip), %rsi      # 0xb07e
leaq 0x739a(%rip), %rcx      # 0xb090
pushq $0x45
jmp 0x3cdb
movq 0xd2ef(%rip), %rdi      # 0x10ff0
leaq 0x72fc(%rip), %rsi      # 0xb004
callq 0x3250
movq %rax, %rdi
callq 0x3120
pushq $0x1
popq %rdi
callq 0x32d0
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3110
jmp 0x3d2f
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3110
jmp 0x3d41
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3110
jmp 0x3df5
movq %rax, %rbx
jmp 0x3df5
movq %rax, %rbx
jmp 0x3e02
movq %rax, %rbx
jmp 0x3e0f
jmp 0x3da2
jmp 0x3da2
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x3d86
jmp 0x3da2
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x3110
jmp 0x3e1c
jmp 0x3da2
movq %rax, %rbx
jmp 0x3e29
movq %rax, %rbx
jmp 0x3e36
movq %rax, %rbx
jmp 0x3e1c
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x3ddb
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x3ddb
jmp 0x3de8
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x3ddb
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x3110
jmp 0x3deb
jmp 0x3de8
jmp 0x3de8
jmp 0x3de8
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x40e2
leaq 0x3e0(%rsp), %rdi
callq 0x33b0
leaq 0x88(%rsp), %rdi
callq 0x3110
leaq 0x188(%rsp), %rdi
callq 0x30c0
leaq 0x5e0(%rsp), %rdi
callq 0x472c
leaq 0x960(%rsp), %rdi
callq 0x3f6c
leaq 0x38(%rsp), %rdi
callq 0x408a
movq %rbx, %rdi
callq 0x3470