// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x40(%rbp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
leaq -0x2c(%rbp), %r14
movl $0x4, %edx
movq %rsi, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %eax
bswapl %eax
cmpl $0x803, %eax            # imm = 0x803
jne 0x26ca
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %eax
bswapl %eax
movl %eax, 0x8(%r12)
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %eax
bswapl %eax
movl %eax, 0xc(%r12)
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %eax
bswapl %eax
movl %eax, 0x10(%r12)
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %eax
bswapl %eax
cmpl $0x801, %eax            # imm = 0x801
jne 0x26e7
movq %rbx, -0x38(%rbp)
movl $0x4, %edx
movq %r15, %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x21f0
movl (%r14), %ebx
bswapl %ebx
cmpl 0x8(%r12), %ebx
jne 0x2704
movl %ebx, %edi
shlq $0x3, %rdi
callq 0x2030
movq %rax, (%r12)
testl %ebx, %ebx
je 0x26bb
movq %r13, %rdi
xorl %r13d, %r13d
movq %rsp, -0x48(%rbp)
movl 0xc(%r12), %eax
imull 0x10(%r12), %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movl $0x1, %edx
movq %rdi, %r15
leaq -0x2c(%rbp), %rsi
callq 0x21f0
movzbl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl 0xc(%r12), %edx
imull 0x10(%r12), %edx
movq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0x21f0
leaq -0x58(%rbp), %rbx
movq %rbx, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x2ab4
movq %r12, %rdi
movq %r14, %rsi
movl -0x30(%rbp), %edx
leaq -0x68(%rbp), %rcx
callq 0x27b0
movq (%r12), %rcx
movq %rax, (%rcx,%r13,8)
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x26a3
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x21b0
movq -0x48(%rbp), %rsp
incq %r13
movl 0x8(%r12), %eax
cmpq %rax, %r13
movq %r15, %rdi
jb 0x2606
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20f0
leaq 0x44d5(%rip), %rcx      # 0x6bb0
movq %rcx, (%rax)
leaq 0x191f(%rip), %rcx      # 0x4004
jmp 0x271f
movl $0x10, %edi
callq 0x20f0
leaq 0x44b8(%rip), %rcx      # 0x6bb0
movq %rcx, (%rax)
leaq 0x1929(%rip), %rcx      # 0x402b
jmp 0x271f
movl $0x10, %edi
callq 0x20f0
leaq 0x449b(%rip), %rcx      # 0x6bb0
movq %rcx, (%rax)
leaq 0x1933(%rip), %rcx      # 0x4052
movq %rcx, 0x8(%rax)
leaq 0x449e(%rip), %rsi      # 0x6bc8
movq 0x48a7(%rip), %rdx      # 0x6fd8
movq %rax, %rdi
callq 0x2260
movq %rax, %r14
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2751
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x2280
nop