// File: /workspace/github_cpp_makefile_star2_O3/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 0xd67(%rip), %rdx       # 0x4226
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
callq 0x3dba
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0xbaa(%rip), %rsi       # 0x4086
leaq 0xba7(%rip), %rdx       # 0x408a
leaq 0x28(%rsp), %rdi
callq 0x3dba
movl $0x1, 0x4(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0xd0f(%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 0x352f
cmpl $0x64, %ebp
jne 0x3564
leaq 0x8(%rsp), %r13
jmp 0x3539
leaq 0x28(%rsp), %r13
cmpl $0x74, %ebp
jne 0x3587
movq 0x3a78(%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 0x350e
cmpl $-0x1, %ebp
je 0x35ba
cmpl $0x6b, %ebp
jne 0x3a77
movq 0x3a3f(%rip), %rax      # 0x6fb8
movq (%rax), %rdi
callq 0x2230
movl %eax, 0x4(%rsp)
jmp 0x350e
cmpl $0x6e, %ebp
je 0x35a1
cmpl $0x76, %ebp
jne 0x3a77
movb $0x1, %al
movq %rax, 0x48(%rsp)
jmp 0x350e
movq 0x3a10(%rip), %rax      # 0x6fb8
movq (%rax), %rdi
callq 0x2230
movq %rax, 0x50(%rsp)
jmp 0x350e
leaq 0xc65(%rip), %rsi       # 0x4226
leaq 0x8(%rsp), %rdi
callq 0x20a0
testl %eax, %eax
je 0x3b19
leaq 0x4d8(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x4c8(%rsp), %rdi
callq 0x2ab4
movq 0x50(%rsp), %rbx
leaq 0xc62(%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 0x3649
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 0x2ab4
leaq 0xc0a(%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 0x36ba
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 0x2ab4
leaq 0xbb2(%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 0x372b
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 0x2ab4
leaq 0xb5f(%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 0x378a
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 0x2ab4
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 0x37fa
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 0x2ab4
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 0x3861
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21b0
testl %ebx, %ebx
jne 0x386f
movq %r15, %rdi
callq 0x2a8e
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 0x2f8c
movl %eax, %ebp
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x3728(%rip), %rdi      # 0x6fc8
callq 0x21d0
movq %rax, %rbx
leaq 0x976(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x2270
movq %rax, %rbx
leaq 0x953(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x2270
movq %rax, %rbx
leaq 0x931(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl %ebp, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x910(%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 0x8e8(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl %r12d, %eax
cvtsi2ss %rax, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x7da(%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 0x2a80
movl $0x18, %esi
movq %r15, %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x2a80
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 0x3a47
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3a63
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 0x3572(%rip), %rdi      # 0x6ff0
leaq 0x79a(%rip), %rsi       # 0x421f
movl $0x7, %edx
callq 0x21d0
movq (%r14), %rsi
movq 0x3557(%rip), %rdi      # 0x6ff0
callq 0x2190
movq %rax, %rbx
leaq 0x77d(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq 0x36d4(%rip), %rsi      # 0x7190
movq 0x36d5(%rip), %rdx      # 0x7198
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0x3516(%rip), %rdi      # 0x6ff0
leaq 0x746(%rip), %rsi       # 0x4227
movl $0x17, %edx
callq 0x21d0
leaq 0x6d0(%rsp), %rsi
movb %bpl, (%rsi)
movq 0x34f3(%rip), %rdi      # 0x6ff0
movl $0x1, %edx
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21e0
movq 0x34d0(%rip), %rdi      # 0x6ff0
leaq 0x6f8(%rip), %rsi       # 0x421f
movl $0x7, %edx
callq 0x21d0
movq (%r14), %rsi
movq 0x34b5(%rip), %rdi      # 0x6ff0
callq 0x2190
movq %rax, %rbx
leaq 0x6db(%rip), %rsi       # 0x4225
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movq 0x3632(%rip), %rsi      # 0x7190
movq 0x3633(%rip), %rdx      # 0x7198
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0x3474(%rip), %rdi      # 0x6ff0
leaq 0x6bc(%rip), %rsi       # 0x423f
movl $0x27, %edx
callq 0x21d0
movq 0x345c(%rip), %rdi      # 0x6ff0
callq 0x20e0
jmp 0x3b0f
movq %rdx, %r12
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x3bc6
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3bc6
movq %rdx, %r12
movq %rax, %rbx
movl $0x18, %esi
movq %r15, %rdi
jmp 0x3c03
movq %rdx, %r12
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x3bfb
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3bfb
movq %rdx, %r12
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x21b0
jmp 0x3ce6
jmp 0x3c0f
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d40
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d40
movq %rax, %rbx
jmp 0x3d40
jmp 0x3c3c
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d4d
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d4d
movq %rax, %rbx
jmp 0x3d4d
jmp 0x3c6f
movq %rax, %rbx
movq 0x2c0(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d5a
movq 0x2d0(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d5a
movq %rax, %rbx
jmp 0x3d5a
jmp 0x3ca2
movq %rax, %rbx
movq 0x4c8(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d6e
movq 0x4d8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3d6e
jmp 0x3d6b
movq %rax, %rbx
jmp 0x3d8a
movq %rax, %rbx
jmp 0x3da6
movq %rdx, %r12
movq %rax, %rbx
cmpl $0x1, %r12d
jne 0x3d33
movq %rbx, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x32ec(%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 0x3d33
movq %rax, %rdi
callq 0x3dae
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 0x3d6e
jmp 0x3d6b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3d8a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x3da6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280