// File: /workspace/github_cpp_makefile_star2_O2/codeniko[P]mossel_tov/mossel_tov.cpp
// Function: main
int main(int argc, char **argv)
{
	//parse through moss results, delimit info by tabs
	system("./parseMoss.sh"); 
	ifstream mossresults;
	mossresults.open(MOSSRESULTS);
	if (!mossresults.is_open()) {
		cerr << "ERROR: Unable to read moss results file that was generated by parseMoss.sh" << endl;
		return 1;
	}

	vector<Match> matches; //matches collection
	vector<Student> students; //collection of all students
	string line;
	while (getline(mossresults, line)) {
		Match match;
		istringstream iss(line);
		getline(iss, match.url, '\t');
		getline(iss, match.student1, '\t');
		getline(iss, match.percent1, '\t');
		getline(iss, match.student2, '\t');
		getline(iss, match.percent2, '\t');
		getline(iss, match.numIdentical, '\t');
		Student s1(match.student1, false);
		Student s2(match.student2, false);

		//insert into student vector if new student
		vector<Student>::iterator s_it1 = find(students.begin(), students.end(), s1);
		if (s_it1 == students.end())
			students.push_back(s1);

		//insert into student vector if new student
		vector<Student>::iterator s_it2 = find(students.begin(), students.end(), s2);
		if (s_it2 == students.end())
			students.push_back(s2);

		matches.push_back(match);
	}

	mossresults.close();
	
	queue<Student> q; // Queue of students that are placed into a group
	vector< vector <Student> > groups;
	vector<Student> curGroup;

	/*Process:
	 * Start with a new (unqueued) student. This will be the first student
	 * in the current group. Find all students that have a match pair with
	 * this student based on MOSS results. For each student paired, add that 
	 * student to the current group and enqueue the student if not 
	 * already enqueued. Once the queue is empty, all the students 
	 * in the current group are related based on Moss results.*/

	for (vector<Student>::iterator student_it = students.begin(); student_it != students.end(); student_it++) {
		if (student_it->queued) //student already in queue or grouped 
			continue;

		student_it->queued = true;
		q.push(*student_it);
		curGroup.clear();

		while (!q.empty()) {
			Student studentObj = q.front();
			q.pop();
			curGroup.push_back(studentObj);
			for (vector<Match>::iterator match_it = matches.begin(); match_it != matches.end(); match_it++) {
				string student_matched = match_it->hasStudent(studentObj);
				if (student_matched != "") {
					//If 2nd student is new (not queued), add student to queue and to group
					vector<Student>::iterator s2_it;
					Student s(student_matched, false);
					s2_it = find(students.begin(), students.end(), s);
					if (!s2_it->queued) {
						s2_it->queued = true;
						q.push(*s2_it);
					}
				}
			}
		}

		groups.push_back(curGroup);
	}

	genPage(matches, groups);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
leaq 0x2a1c(%rip), %rdi      # 0x51c5
callq 0x2180
leaq 0x3c0(%rsp), %rbx
movq %rbx, %rdi
callq 0x21e0
leaq 0x2a0f(%rip), %rsi      # 0x51d4
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x2140
leaq 0x438(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x2a84
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rsp, %rcx
andq $0x0, 0x10(%rcx)
leaq 0x1a8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, (%rcx)
movb $0x0, (%rax)
xorl %r13d, %r13d
leaq 0x240(%rsp), %rbx
pushq $0x9
popq %r14
leaq 0x80(%rsp), %rbp
leaq 0x1b8(%rsp), %r12
leaq 0x3c0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2aa7
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
andq $0x0, 0x88(%rsp)
movb %r13b, 0x90(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0xa0(%rsp)
andq $0x0, 0xa8(%rsp)
movb %r13b, 0xb0(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, 0xc0(%rsp)
andq $0x0, 0xc8(%rsp)
movb %r13b, 0xd0(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, 0xe0(%rsp)
andq $0x0, 0xe8(%rsp)
movb %r13b, 0xf0(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0x100(%rsp)
andq $0x0, 0x108(%rsp)
movb %r13b, 0x110(%rsp)
leaq 0x130(%rsp), %rax
movq %rax, 0x120(%rsp)
andq $0x0, 0x128(%rsp)
movb %r13b, 0x130(%rsp)
movq %rbx, %rdi
leaq 0x198(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x2290
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
movl %r14d, %edx
callq 0x22e0
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
callq 0x22e0
movq %rbx, %rdi
leaq 0xe0(%rsp), %rsi
movl %r14d, %edx
callq 0x22e0
movq %rbx, %rdi
leaq 0xa0(%rsp), %rsi
movl %r14d, %edx
callq 0x22e0
movq %rbx, %rdi
leaq 0x100(%rsp), %rsi
movl %r14d, %edx
callq 0x22e0
movq %rbx, %rdi
leaq 0x120(%rsp), %rsi
movl %r14d, %edx
callq 0x22e0
leaq 0x220(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2220
leaq 0x140(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2f6a
movq %r15, %rdi
callq 0x20f0
leaq 0x200(%rsp), %r15
movq %r15, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2220
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2f6a
movq %r15, %rdi
callq 0x20f0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
callq 0x42bf
movq 0x8(%rsp), %rsi
cmpq %rsi, %rax
jne 0x2a2f
movq %rsp, %rdi
leaq 0x140(%rsp), %rsi
callq 0x2fa4
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
movq %r12, %rdx
callq 0x42bf
cmpq 0x8(%rsp), %rax
jne 0x2a4d
movq %rsp, %rdi
movq %r12, %rsi
callq 0x2fa4
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x2ebc
movq %r12, %rdi
callq 0x20f0
leaq 0x140(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2100
movq %rbp, %rdi
callq 0x2fd0
jmp 0x2833
movq 0x6565(%rip), %rdi      # 0x8ff0
leaq 0x2746(%rip), %rsi      # 0x51d8
callq 0x21b0
movq %rax, %rdi
callq 0x2110
pushq $0x1
popq %rbx
jmp 0x2cd0
leaq 0x3c0(%rsp), %rdi
callq 0x2060
leaq 0x240(%rsp), %rdi
callq 0x3010
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
movq (%rsp), %r12
leaq 0x80(%rsp), %r13
leaq 0x1b8(%rsp), %rbp
leaq 0x26b4(%rip), %rbx      # 0x51ad
leaq 0x1e0(%rsp), %r15
leaq 0x140(%rsp), %r14
cmpq 0x8(%rsp), %r12
je 0x2c3b
cmpb $0x0, 0x20(%r12)
jne 0x2c32
movb $0x1, 0x20(%r12)
leaq 0x240(%rsp), %rdi
movq %r12, %rsi
callq 0x47f6
movq %r12, 0x58(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x302c
movq 0x250(%rsp), %rsi
cmpq %rsi, 0x270(%rsp)
je 0x2c1e
movq %r13, %rdi
callq 0x3034
leaq 0x240(%rsp), %rdi
callq 0x4aa4
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x2fa4
movq 0x20(%rsp), %r12
cmpq 0x28(%rsp), %r12
je 0x2c11
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2e54
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2e4a
testb %al, %al
je 0x2bfd
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2220
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2f6a
movq %r15, %rdi
callq 0x20f0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x42bf
cmpb $0x0, 0x20(%rax)
jne 0x2bf5
movb $0x1, 0x20(%rax)
leaq 0x240(%rsp), %rdi
movq %rax, %rsi
callq 0x47f6
movq %r14, %rdi
callq 0x20f0
movq %rbp, %rdi
callq 0x20f0
addq $0xc0, %r12
jmp 0x2b82
movq %r13, %rdi
callq 0x20f0
jmp 0x2b45
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3052
movq 0x58(%rsp), %r12
addq $0x28, %r12
jmp 0x2b09
leaq 0x180(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2f0e
leaq 0x168(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x307e
leaq 0x180(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x24f0
leaq 0x168(%rsp), %rdi
callq 0x30d8
leaq 0x180(%rsp), %rdi
callq 0x2eec
leaq 0x40(%rsp), %rdi
callq 0x30fa
leaq 0x60(%rsp), %rdi
callq 0x30d8
leaq 0x240(%rsp), %rdi
callq 0x322e
leaq 0x198(%rsp), %rdi
callq 0x20f0
movq %rsp, %rdi
callq 0x30fa
leaq 0x20(%rsp), %rdi
callq 0x2eec
xorl %ebx, %ebx
leaq 0x3c0(%rsp), %rdi
callq 0x2080
movl %ebx, %eax
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x30d8
jmp 0x2d06
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x2eec
jmp 0x2de2
jmp 0x2d96
jmp 0x2d4e
jmp 0x2d4e
movq %rax, %rbx
jmp 0x2e22
jmp 0x2d96
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
jmp 0x2d63
movq %rax, %rbx
jmp 0x2d68
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
jmp 0x2d70
jmp 0x2d77
movq %rax, %rbx
jmp 0x2d87
movq %rax, %rbx
jmp 0x2e03
jmp 0x2d58
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x20f0
leaq 0x140(%rsp), %rdi
callq 0x20f0
jmp 0x2d7a
movq %rax, %rbx
leaq 0x240(%rsp), %rdi
callq 0x2100
leaq 0x80(%rsp), %rdi
callq 0x2fd0
jmp 0x2e03
movq %rax, %rbx
jmp 0x2de2
jmp 0x2dc0
jmp 0x2da1
jmp 0x2dc5
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
jmp 0x2db9
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0x20f0
jmp 0x2dc8
movq %rax, %rbx
jmp 0x2dd5
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x20f0
leaq 0x80(%rsp), %rdi
callq 0x20f0
leaq 0x40(%rsp), %rdi
callq 0x30fa
leaq 0x60(%rsp), %rdi
callq 0x30d8
leaq 0x240(%rsp), %rdi
callq 0x322e
leaq 0x198(%rsp), %rdi
callq 0x20f0
movq %rsp, %rdi
callq 0x30fa
leaq 0x20(%rsp), %rdi
callq 0x2eec
leaq 0x3c0(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x22c0