// File: 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
movq %rsp, %rbp
subq $0x7a0, %rsp            # imm = 0x7A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x85c5(%rip), %rdi      # 0xb1d5
callq 0x21e0
leaq -0x218(%rbp), %rdi
movq %rdi, -0x720(%rbp)
callq 0x2290
movq -0x720(%rbp), %rdi
leaq 0x85ae(%rip), %rsi      # 0xb1e4
movl $0x8, %edx
callq 0x2160
jmp 0x2c42
leaq -0x218(%rbp), %rdi
callq 0x2340
movb %al, -0x721(%rbp)
jmp 0x2c56
movb -0x721(%rbp), %al
testb $0x1, %al
jne 0x2cc0
jmp 0x2c62
movq 0xe387(%rip), %rdi      # 0x10ff0
leaq 0x8578(%rip), %rsi      # 0xb1e8
callq 0x2210
movq %rax, -0x730(%rbp)
jmp 0x2c7e
movq -0x730(%rbp), %rdi
movq 0xe33c(%rip), %rsi      # 0x10fc8
callq 0x2240
jmp 0x2c93
movl $0x1, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0x35ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3617
leaq -0x240(%rbp), %rdi
callq 0x3670
leaq -0x258(%rbp), %rdi
callq 0x3ce0
leaq -0x278(%rbp), %rdi
callq 0x22f0
leaq -0x218(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x2070
movq %rax, -0x738(%rbp)
jmp 0x2d00
movq -0x738(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22d0
movb %al, -0x739(%rbp)
jmp 0x2d1e
movb -0x739(%rbp), %al
testb $0x1, %al
jne 0x2d2d
jmp 0x30df
leaq -0x338(%rbp), %rdi
callq 0x3d00
leaq -0x4b8(%rbp), %rdi
leaq -0x278(%rbp), %rsi
movl $0x8, %edx
callq 0x2370
jmp 0x2d53
leaq -0x2f8(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x9, %edx
callq 0x23d0
jmp 0x2d6d
leaq -0x4b8(%rbp), %rdi
leaq -0x338(%rbp), %rsi
movl $0x9, %edx
callq 0x23d0
jmp 0x2d87
leaq -0x2d8(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x9, %edx
callq 0x23d0
jmp 0x2da1
leaq -0x318(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x9, %edx
callq 0x23d0
jmp 0x2dbb
leaq -0x2b8(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x9, %edx
callq 0x23d0
jmp 0x2dd5
leaq -0x298(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x9, %edx
callq 0x23d0
jmp 0x2def
leaq -0x500(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x20e0
jmp 0x2e04
leaq -0x4e0(%rbp), %rdi
leaq -0x500(%rbp), %rsi
xorl %edx, %edx
callq 0x3d70
jmp 0x2e1b
leaq -0x500(%rbp), %rdi
callq 0x2150
leaq -0x318(%rbp), %rsi
leaq -0x548(%rbp), %rdi
callq 0x20e0
jmp 0x2e3c
leaq -0x528(%rbp), %rdi
leaq -0x548(%rbp), %rsi
xorl %edx, %edx
callq 0x3d70
jmp 0x2e53
leaq -0x548(%rbp), %rdi
callq 0x2150
leaq -0x258(%rbp), %rdi
movq %rdi, -0x750(%rbp)
callq 0x3a90
movq -0x750(%rbp), %rdi
movq %rax, -0x558(%rbp)
callq 0x3b00
movq %rax, -0x560(%rbp)
movq -0x558(%rbp), %rdi
movq -0x560(%rbp), %rsi
leaq -0x4e0(%rbp), %rdx
callq 0x3de0
movq %rax, -0x748(%rbp)
jmp 0x2eaf
movq -0x748(%rbp), %rax
movq %rax, -0x550(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3b00
movq %rax, -0x568(%rbp)
leaq -0x550(%rbp), %rdi
leaq -0x568(%rbp), %rsi
callq 0x3e30
testb $0x1, %al
jne 0x2eec
jmp 0x2fcb
leaq -0x258(%rbp), %rdi
leaq -0x4e0(%rbp), %rsi
callq 0x3e70
jmp 0x2f01
jmp 0x2fcb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x35d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x30ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x30c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x500(%rbp), %rdi
callq 0x2150
jmp 0x30c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x30b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x548(%rbp), %rdi
callq 0x2150
jmp 0x30b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x528(%rbp), %rdi
callq 0x3ee0
jmp 0x30b6
leaq -0x258(%rbp), %rdi
movq %rdi, -0x760(%rbp)
callq 0x3a90
movq -0x760(%rbp), %rdi
movq %rax, -0x578(%rbp)
callq 0x3b00
movq %rax, -0x580(%rbp)
movq -0x578(%rbp), %rdi
movq -0x580(%rbp), %rsi
leaq -0x528(%rbp), %rdx
callq 0x3de0
movq %rax, -0x758(%rbp)
jmp 0x301b
movq -0x758(%rbp), %rax
movq %rax, -0x570(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3b00
movq %rax, -0x588(%rbp)
leaq -0x570(%rbp), %rdi
leaq -0x588(%rbp), %rsi
callq 0x3e30
testb $0x1, %al
jne 0x3055
jmp 0x306c
leaq -0x258(%rbp), %rdi
leaq -0x528(%rbp), %rsi
callq 0x3e70
jmp 0x306a
jmp 0x306c
leaq -0x240(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x3850
jmp 0x3081
leaq -0x528(%rbp), %rdi
callq 0x3ee0
leaq -0x4e0(%rbp), %rdi
callq 0x3ee0
leaq -0x4b8(%rbp), %rdi
callq 0x2110
leaq -0x338(%rbp), %rdi
callq 0x3f00
jmp 0x2ce4
leaq -0x4e0(%rbp), %rdi
callq 0x3ee0
leaq -0x4b8(%rbp), %rdi
callq 0x2110
leaq -0x338(%rbp), %rdi
callq 0x3f00
jmp 0x35d9
leaq -0x218(%rbp), %rdi
callq 0x2080
jmp 0x30ed
leaq -0x5d8(%rbp), %rdi
callq 0x3f70
jmp 0x30fb
leaq -0x5f0(%rbp), %rdi
callq 0x3fa0
leaq -0x608(%rbp), %rdi
callq 0x3ce0
leaq -0x258(%rbp), %rdi
callq 0x3a90
movq %rax, -0x610(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3b00
movq %rax, -0x618(%rbp)
leaq -0x610(%rbp), %rdi
leaq -0x618(%rbp), %rsi
callq 0x3ac0
testb $0x1, %al
jne 0x3155
jmp 0x34c2
leaq -0x610(%rbp), %rdi
callq 0x3c40
testb $0x1, 0x20(%rax)
je 0x316c
jmp 0x34a8
leaq -0x610(%rbp), %rdi
movq %rdi, -0x768(%rbp)
callq 0x3c40
movq -0x768(%rbp), %rdi
movb $0x1, 0x20(%rax)
callq 0x3c20
movq %rax, %rsi
leaq -0x5d8(%rbp), %rdi
callq 0x3fc0
jmp 0x31a0
leaq -0x608(%rbp), %rdi
callq 0x3ff0
leaq -0x5d8(%rbp), %rdi
callq 0x4010
movb %al, -0x769(%rbp)
jmp 0x31c0
movb -0x769(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x31d1
jmp 0x3491
leaq -0x5d8(%rbp), %rdi
callq 0x4030
movq %rax, -0x778(%rbp)
jmp 0x31e6
movq -0x778(%rbp), %rsi
leaq -0x640(%rbp), %rdi
callq 0x4050
jmp 0x31fb
leaq -0x5d8(%rbp), %rdi
callq 0x4090
jmp 0x3209
leaq -0x608(%rbp), %rdi
leaq -0x640(%rbp), %rsi
callq 0x3e70
jmp 0x321e
leaq -0x240(%rbp), %rdi
callq 0x3690
movq %rax, -0x648(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x3700
movq %rax, -0x650(%rbp)
leaq -0x648(%rbp), %rdi
leaq -0x650(%rbp), %rsi
callq 0x36c0
testb $0x1, %al
jne 0x3260
jmp 0x346f
leaq -0x648(%rbp), %rdi
callq 0x3760
movq %rax, %rsi
leaq -0x670(%rbp), %rdi
leaq -0x640(%rbp), %rdx
callq 0x3780
jmp 0x3284
leaq 0x7f32(%rip), %rsi      # 0xb1bd
leaq -0x670(%rbp), %rdi
callq 0x3730
movb %al, -0x779(%rbp)
jmp 0x329f
movb -0x779(%rbp), %al
testb $0x1, %al
jne 0x32ae
jmp 0x343b
leaq -0x678(%rbp), %rdi
callq 0x40b0
leaq -0x6c0(%rbp), %rdi
leaq -0x670(%rbp), %rsi
callq 0x20e0
jmp 0x32cf
leaq -0x6a0(%rbp), %rdi
leaq -0x6c0(%rbp), %rsi
xorl %edx, %edx
callq 0x3d70
jmp 0x32e6
leaq -0x6c0(%rbp), %rdi
callq 0x2150
leaq -0x258(%rbp), %rdi
movq %rdi, -0x790(%rbp)
callq 0x3a90
movq -0x790(%rbp), %rdi
movq %rax, -0x6d0(%rbp)
callq 0x3b00
movq %rax, -0x6d8(%rbp)
movq -0x6d0(%rbp), %rdi
movq -0x6d8(%rbp), %rsi
leaq -0x6a0(%rbp), %rdx
callq 0x3de0
movq %rax, -0x788(%rbp)
jmp 0x3342
movq -0x788(%rbp), %rax
movq %rax, -0x6c8(%rbp)
movq -0x6c8(%rbp), %rax
movq %rax, -0x678(%rbp)
leaq -0x678(%rbp), %rdi
callq 0x3c40
testb $0x1, 0x20(%rax)
jne 0x342f
leaq -0x678(%rbp), %rdi
movq %rdi, -0x798(%rbp)
callq 0x3c40
movq -0x798(%rbp), %rdi
movb $0x1, 0x20(%rax)
callq 0x3c20
movq %rax, %rsi
leaq -0x5d8(%rbp), %rdi
callq 0x3fc0
jmp 0x33a8
jmp 0x342f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x35b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3480
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x3461
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x6c0(%rbp), %rdi
callq 0x2150
jmp 0x3461
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x6a0(%rbp), %rdi
callq 0x3ee0
jmp 0x3461
leaq -0x6a0(%rbp), %rdi
callq 0x3ee0
leaq -0x670(%rbp), %rdi
callq 0x2150
leaq -0x648(%rbp), %rdi
xorl %esi, %esi
callq 0x38e0
movq %rax, -0x6e0(%rbp)
jmp 0x3231
leaq -0x670(%rbp), %rdi
callq 0x2150
jmp 0x3480
leaq -0x640(%rbp), %rdi
callq 0x3ee0
jmp 0x31ac
leaq -0x640(%rbp), %rdi
callq 0x3ee0
jmp 0x35b5
leaq -0x5f0(%rbp), %rdi
leaq -0x608(%rbp), %rsi
callq 0x40d0
jmp 0x34a6
jmp 0x34a8
leaq -0x610(%rbp), %rdi
xorl %esi, %esi
callq 0x3c60
movq %rax, -0x6e8(%rbp)
jmp 0x3126
leaq -0x700(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x3b30
jmp 0x34d7
leaq -0x718(%rbp), %rdi
leaq -0x5f0(%rbp), %rsi
callq 0x4140
jmp 0x34ec
leaq -0x700(%rbp), %rdi
leaq -0x718(%rbp), %rsi
callq 0x26e0
jmp 0x3501
leaq -0x718(%rbp), %rdi
callq 0x4230
leaq -0x700(%rbp), %rdi
callq 0x3970
movl $0x0, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x4290
leaq -0x5f0(%rbp), %rdi
callq 0x4230
leaq -0x5d8(%rbp), %rdi
callq 0x42f0
leaq -0x278(%rbp), %rdi
callq 0x2150
leaq -0x258(%rbp), %rdi
callq 0x4290
leaq -0x240(%rbp), %rdi
callq 0x3970
jmp 0x35ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x35a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x718(%rbp), %rdi
callq 0x4230
leaq -0x700(%rbp), %rdi
callq 0x3970
leaq -0x608(%rbp), %rdi
callq 0x4290
leaq -0x5f0(%rbp), %rdi
callq 0x4230
leaq -0x5d8(%rbp), %rdi
callq 0x42f0
leaq -0x278(%rbp), %rdi
callq 0x2150
leaq -0x258(%rbp), %rdi
callq 0x4290
leaq -0x240(%rbp), %rdi
callq 0x3970
jmp 0x3617
leaq -0x218(%rbp), %rdi
callq 0x2090
movl -0x4(%rbp), %eax
addq $0x7a0, %rsp            # imm = 0x7A0
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x2090
movq -0x220(%rbp), %rdi
callq 0x23a0
nop