// File: /workspace/github_cpp_makefile_star2_O1/arinaschwartz[P]KNN-MNIST/mnist-knn.cpp
// Function: main
int main(int argc, char** argv)
{
	// For parsing command-line options
	int opt;
	// Should we be verbose?
	bool verbose = false;
	// Directory containing MNIST files
	string directory = "";
	// Distance strategy
	string type = "full";
	// K
	int k = 1;
	// Process only these many images in the testing set (0 = process all)
	int max = 0;
	// Number of images labelled correctly
	uint32 correct;

	// Parse command-line options
	while ((opt = getopt(argc, argv, "d:t:n:k:vh")) != -1)
		switch (opt)
		{
			case 'd':
				directory = optarg;
				break;
			case 't':
				type = optarg;
				break;
			case 'v':
				verbose = true;
				break;
			case 'k':
				k = atoi(optarg);
				break;
			case 'n':
				max = atoi(optarg);
				break;
			default:
				cerr << "Usage: " << argv[0] << " " << USAGE << endl;
				cerr << "ERROR: Unknown option -" << char(opt) << endl;
				exit(-1);
		}

	if(directory == "")
	{
		cerr << "Usage: " << argv[0] << " " << USAGE << endl;
		cerr << "ERROR: Must specify a directory with -d" << endl;
		exit(-1);
	}

	// Load files
	ifstream trainImageFile((directory + "/train-images-idx3-ubyte").c_str(), ios::in|ios::binary);
	ifstream trainLabelFile((directory + "/train-labels-idx1-ubyte").c_str(), ios::in|ios::binary);
	ifstream testImageFile((directory + "/t10k-images-idx3-ubyte").c_str(), ios::in|ios::binary);
	ifstream testLabelFile((directory + "/t10k-labels-idx1-ubyte").c_str(), ios::in|ios::binary);

	MNISTDataset *trainSet, *testSet;

	try
	{
		trainSet = new MNISTDataset(trainImageFile, trainLabelFile, type);
		testSet = new MNISTDataset(testImageFile, testLabelFile, type);
	} catch (MNISTFileException &e)
	{
		cerr << e.what() << endl;
		exit(-2);
	}

	if(max==0)
		max = testSet->getNumImages();

	// Run KNN
	correct = KNN(*trainSet, *testSet, k, max, verbose);

	// Print summary
	cout << type << " " << k << " " << max << " " << correct << " " << (max - correct) << " " << (float(max-correct)*100.0/max) << endl;

	// Clean up
	delete testSet;
	delete trainSet;

	trainImageFile.close();
	trainLabelFile.close();
	testImageFile.close();
	testLabelFile.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8d8, %rsp            # imm = 0x8D8
movq %rsi, %r14
movl %edi, %r15d
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0xd6f(%rip), %rdx       # 0x4226
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
callq 0x3db2
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0xbb2(%rip), %rsi       # 0x4086
leaq 0xbaf(%rip), %rdx       # 0x408a
leaq 0x28(%rsp), %rdi
callq 0x3db2
movl $0x1, 0x4(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0xd17(%rip), %rbx       # 0x4214
movq $0x0, 0x48(%rsp)
movl %r15d, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2060
movl %eax, %ebp
cmpl $0x6d, %eax
jg 0x3527
cmpl $0x64, %ebp
jne 0x355c
leaq 0x8(%rsp), %r13
jmp 0x3531
leaq 0x28(%rsp), %r13
cmpl $0x74, %ebp
jne 0x357f
movq 0x3a80(%rip), %rax      # 0x6fb8
movq (%rax), %rbp
movq 0x8(%r13), %r12
movq %rbp, %rdi
callq 0x20d0
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbp, %rcx
movq %rax, %r8
callq 0x2220
jmp 0x3506
cmpl $-0x1, %ebp
je 0x35b2
cmpl $0x6b, %ebp
jne 0x3a6f
movq 0x3a47(%rip), %rax      # 0x6fb8
movq (%rax), %rdi
callq 0x2230
movl %eax, 0x4(%rsp)
jmp 0x3506
cmpl $0x6e, %ebp
je 0x3599
cmpl $0x76, %ebp
jne 0x3a6f
movb $0x1, %al
movq %rax, 0x48(%rsp)
jmp 0x3506
movq 0x3a18(%rip), %rax      # 0x6fb8
movq (%rax), %rdi
callq 0x2230
movq %rax, 0x50(%rsp)
jmp 0x3506
leaq 0xc6d(%rip), %rsi       # 0x4226
leaq 0x8(%rsp), %rdi
callq 0x20a0
testl %eax, %eax
je 0x3b11
leaq 0x4d8(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x4c8(%rsp), %rdi
callq 0x2ab8
movq 0x50(%rsp), %rbx
leaq 0xc6a(%rip), %rsi       # 0x4267
leaq 0x4c8(%rsp), %rdi
callq 0x22b0
movq 0x4c8(%rsp), %rsi
leaq 0x6d0(%rsp), %rdi
movl $0xc, %edx
callq 0x22a0
movq 0x4c8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3641
movq 0x4d8(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x2d0(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x2c0(%rsp), %rdi
callq 0x2ab8
leaq 0xc12(%rip), %rsi       # 0x4280
leaq 0x2c0(%rsp), %rdi
callq 0x22b0
movq 0x2c0(%rsp), %rsi
leaq 0x4c8(%rsp), %rdi
movl $0xc, %edx
callq 0x22a0
movq 0x2c0(%rsp), %rdi
cmpq %r14, %rdi
je 0x36b2
movq 0x2d0(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0xc8(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x2ab8
leaq 0xbba(%rip), %rsi       # 0x4299
leaq 0xb8(%rsp), %rdi
callq 0x22b0
movq 0xb8(%rsp), %rsi
leaq 0x2c0(%rsp), %rdi
movl $0xc, %edx
callq 0x22a0
movq 0xb8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3723
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x68(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x2ab8
leaq 0xb67(%rip), %rsi       # 0x42b1
leaq 0x58(%rsp), %rdi
callq 0x22b0
movq 0x58(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movl $0xc, %edx
callq 0x22a0
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x3782
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21b0
movl $0x18, %edi
callq 0x21a0
movq %rax, %r14
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0x2ab8
leaq 0x6d0(%rsp), %rsi
leaq 0x4c8(%rsp), %rdx
leaq 0x98(%rsp), %rcx
movq %r14, %rdi
callq 0x2512
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x37f2
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x21b0
movl $0x18, %edi
callq 0x21a0
movq %rax, %r15
leaq 0x88(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x2ab8
leaq 0x2c0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq %r15, %rdi
callq 0x2512
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x3859
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21b0
testl %ebx, %ebx
jne 0x3867
movq %r15, %rdi
callq 0x2a92
movl %eax, %ebx
movzbl 0x48(%rsp), %r8d
andl $0x1, %r8d
movq %r14, %rdi
movq %r15, %rsi
movl 0x4(%rsp), %edx
movq %rbx, %r13
movl %ebx, %ecx
callq 0x2f9c
movl %eax, %ebp
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x3730(%rip), %rdi      # 0x6fc8
callq 0x21d0
movq %rax, %rbx
leaq 0x97e(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x2270
movq %rax, %rbx
leaq 0x95b(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x2270
movq %rax, %rbx
leaq 0x939(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl %ebp, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x918(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl %r13d, %r12d
subl %ebp, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2110
movq %rax, %rbx
leaq 0x8f0(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl %r12d, %eax
cvtsi2ss %rax, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x7e2(%rip), %xmm0      # 0x4138
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2240
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %r15, %rdi
callq 0x2a84
movl $0x18, %esi
movq %r15, %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x2a84
movl $0x18, %esi
movq %r14, %rdi
callq 0x21b0
leaq 0x6d0(%rsp), %rdi
callq 0x2050
leaq 0x4c8(%rsp), %rdi
callq 0x2050
leaq 0x2c0(%rsp), %rdi
callq 0x2050
leaq 0xb8(%rsp), %rdi
callq 0x2050
leaq 0xb8(%rsp), %rdi
callq 0x2070
leaq 0x2c0(%rsp), %rdi
callq 0x2070
leaq 0x4c8(%rsp), %rdi
callq 0x2070
leaq 0x6d0(%rsp), %rdi
callq 0x2070
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3a3f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3a5b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21b0
xorl %eax, %eax
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x357a(%rip), %rdi      # 0x6ff0
leaq 0x7a2(%rip), %rsi       # 0x421f
movl $0x7, %edx
callq 0x21d0
movq (%r14), %rsi
movq 0x355f(%rip), %rdi      # 0x6ff0
callq 0x2190
movq %rax, %rbx
leaq 0x785(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq 0x36dc(%rip), %rsi      # 0x7190
movq 0x36dd(%rip), %rdx      # 0x7198
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0x351e(%rip), %rdi      # 0x6ff0
leaq 0x74e(%rip), %rsi       # 0x4227
movl $0x17, %edx
callq 0x21d0
leaq 0x6d0(%rsp), %rsi
movb %bpl, (%rsi)
movq 0x34fb(%rip), %rdi      # 0x6ff0
movl $0x1, %edx
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21e0
movq 0x34d8(%rip), %rdi      # 0x6ff0
leaq 0x700(%rip), %rsi       # 0x421f
movl $0x7, %edx
callq 0x21d0
movq (%r14), %rsi
movq 0x34bd(%rip), %rdi      # 0x6ff0
callq 0x2190
movq %rax, %rbx
leaq 0x6e3(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq 0x363a(%rip), %rsi      # 0x7190
movq 0x363b(%rip), %rdx      # 0x7198
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0x347c(%rip), %rdi      # 0x6ff0
leaq 0x6c4(%rip), %rsi       # 0x423f
movl $0x27, %edx
callq 0x21d0
movq 0x3464(%rip), %rdi      # 0x6ff0
callq 0x20e0
jmp 0x3b07
movq %rdx, %r12
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x3bbe
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3bbe
movq %rdx, %r12
movq %rax, %rbx
movl $0x18, %esi
movq %r15, %rdi
jmp 0x3bfb
movq %rdx, %r12
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x3bf3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3bf3
movq %rdx, %r12
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x21b0
jmp 0x3cde
jmp 0x3c07
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d38
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d38
movq %rax, %rbx
jmp 0x3d38
jmp 0x3c34
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d45
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d45
movq %rax, %rbx
jmp 0x3d45
jmp 0x3c67
movq %rax, %rbx
movq 0x2c0(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d52
movq 0x2d0(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d52
movq %rax, %rbx
jmp 0x3d52
jmp 0x3c9a
movq %rax, %rbx
movq 0x4c8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d66
movq 0x4d8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d66
jmp 0x3d63
movq %rax, %rbx
jmp 0x3d82
movq %rax, %rbx
jmp 0x3d9e
movq %rdx, %r12
movq %rax, %rbx
cmpl $0x1, %r12d
jne 0x3d2b
movq %rbx, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x32f4(%rip), %rdi      # 0x6ff0
movq %rax, %rsi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x21e0
movq %rax, %rbx
callq 0x2250
jmp 0x3d2b
movq %rax, %rdi
callq 0x3da6
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2070
leaq 0x2c0(%rsp), %rdi
callq 0x2070
leaq 0x4c8(%rsp), %rdi
callq 0x2070
leaq 0x6d0(%rsp), %rdi
callq 0x2070
jmp 0x3d66
jmp 0x3d63
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3d82
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3d9e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280