// File: arinaschwartz[P]KNN-MNIST/MNISTDataset.cpp
// Function: MNISTDataset::MNISTDataset(std::istream&, std::istream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
MNISTDataset::MNISTDataset(istream &imageFile, istream &labelFile, string type)
{
	uint32 magic, numLabels;

	magic = readUInt32(imageFile);

	if(magic!=2051)
		throw MNISTFileException("Magic number in image file is not 2051");

	this->numImages = readUInt32(imageFile);
	this->numRows = readUInt32(imageFile);
	this->numCols = readUInt32(imageFile);

	magic = readUInt32(labelFile);
	if(magic!=2049)
		throw MNISTFileException("Magic number in label file is not 2049");

	numLabels = readUInt32(labelFile);

	if (numLabels != this->numImages)
		throw MNISTFileException("Number of images doesn't match the number of labels");

	imgs = new MNISTImage*[this->numImages];

	for(uint32 i=0; i<this->numImages; i++)
	{
		uint8 img[this->numCols*this->numRows];
		uint8 label;

		label = readUInt8(labelFile);
		readUInt8Array(imageFile, img, numCols*numRows);

		imgs[i] = createImage(img, label, type);
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rcx, -0x88(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x80(%rbp)
movq -0x10(%rbp), %rsi
callq 0x3910
movl %eax, -0x24(%rbp)
cmpl $0x803, -0x24(%rbp)     # imm = 0x803
je 0x36e9
movl $0x10, %edi
callq 0x3100
movq %rax, %rdi
movq %rdi, -0x90(%rbp)
leaq 0x393a(%rip), %rsi      # 0x7004
callq 0x3610
movq -0x90(%rbp), %rdi
leaq 0x74e3(%rip), %rsi      # 0xabc0
leaq 0x82c(%rip), %rdx       # 0x3f10
callq 0x32f0
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3910
movq -0x80(%rbp), %rdi
movl %eax, 0x8(%rdi)
movq -0x10(%rbp), %rsi
callq 0x3910
movq -0x80(%rbp), %rdi
movl %eax, 0xc(%rdi)
movq -0x10(%rbp), %rsi
callq 0x3910
movq -0x80(%rbp), %rdi
movl %eax, 0x10(%rdi)
movq -0x18(%rbp), %rsi
callq 0x3910
movl %eax, -0x24(%rbp)
cmpl $0x801, -0x24(%rbp)     # imm = 0x801
je 0x376c
movl $0x10, %edi
callq 0x3100
movq %rax, %rdi
movq %rdi, -0x98(%rbp)
leaq 0x38de(%rip), %rsi      # 0x702b
callq 0x3610
movq -0x98(%rbp), %rdi
leaq 0x7460(%rip), %rsi      # 0xabc0
leaq 0x7a9(%rip), %rdx       # 0x3f10
callq 0x32f0
movq -0x80(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3910
movq -0x80(%rbp), %rcx
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl 0x8(%rcx), %eax
je 0x37c2
movl $0x10, %edi
callq 0x3100
movq %rax, %rdi
movq %rdi, -0xa0(%rbp)
leaq 0x38af(%rip), %rsi      # 0x7052
callq 0x3610
movq -0xa0(%rbp), %rdi
leaq 0x740a(%rip), %rsi      # 0xabc0
leaq 0x753(%rip), %rdx       # 0x3f10
callq 0x32f0
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x2c(%rbp)
movq -0x80(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x38f8
movq -0x80(%rbp), %rdi
movl 0xc(%rdi), %ecx
movl 0x10(%rdi), %eax
imull %ecx, %eax
movq %rsp, %rcx
movq %rcx, -0x38(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rsi
callq 0x3980
movq -0x80(%rbp), %rdi
movq -0xc0(%rbp), %rdx
movb %al, -0x41(%rbp)
movq -0x10(%rbp), %rsi
movl 0xc(%rdi), %eax
movl 0x10(%rdi), %ecx
imull %eax, %ecx
callq 0x39b0
movq -0x88(%rbp), %rsi
movzbl -0x41(%rbp), %eax
movl %eax, -0xb4(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x30e0
movq -0x80(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl -0xb4(%rbp), %edx
movq -0xb0(%rbp), %rcx
callq 0x39e0
movq %rax, -0xa8(%rbp)
jmp 0x38ae
movq -0xa8(%rbp), %rdx
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
leaq -0x68(%rbp), %rdi
callq 0x3398
movq -0x38(%rbp), %rax
movq %rax, %rsp
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x37fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3398
jmp 0x38fd
movq %rbp, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x3310
nopw %cs:(%rax,%rax)