// File: /workspace/github_cpp_makefile_star2_O2/wkohlenberg[P]simple_NN/src/mnist.cpp
// Function: readImages(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, bool)
int readImages(vector<vector<int> >&vImages, bool training=true)
{
	string filePath = "../database/";
	int samples = 0;

	if (training)
	{
		filePath.append("train-images-idx3-ubyte");
		samples = 60000;
	}
	else
	{
		filePath.append("t10k-images-idx3-ubyte");
		samples = 10000;
	}

	// Open the image files, training or test set
	ifstream imageFile;
	imageFile.open(filePath.c_str());
	if (!imageFile.is_open())
	{
		cout << "Couldn't open or find the file." << endl;
		return 1;
	}

	// Read the header
	uint32_t magic;
	uint32_t nImages;
	uint32_t rows;
	uint32_t columns;
	uint8_t pixel;
	imageFile.read(reinterpret_cast<char *>(&magic), sizeof(magic));
	imageFile.read(reinterpret_cast<char *>(&nImages), sizeof(nImages));
	imageFile.read(reinterpret_cast<char *>(&rows), sizeof(rows));
	imageFile.read(reinterpret_cast<char *>(&columns), sizeof(columns));

	if(!((endianSwap(magic) == 2051) && (endianSwap(nImages) == samples)))
	{
		cout << "No corresponding header..." << endl;
		return 2;
	}


	vector<int> temp;
	int pixelsImg = endianSwap(rows) * endianSwap(columns);

	for (int y = 0; y < samples; y++)
	{
		for (int x = 0; x < pixelsImg; x++)
		{
			imageFile.read(reinterpret_cast<char *>(&pixel), sizeof(pixel));
			temp.push_back(int(pixel));
		}
		vImages.push_back(temp);
		temp.clear();
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movl %esi, %ebp
movq %rdi, 0x48(%rsp)
leaq 0xa85(%rip), %rsi       # 0x5141
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x34ae
testl %ebp, %ebp
je 0x46ea
movl $0xea60, 0x10(%rsp)     # imm = 0xEA60
leaq 0xada(%rip), %rsi       # 0x51b8
leaq 0x50(%rsp), %rdi
callq 0x2300
jmp 0x4703
movl $0x2710, 0x10(%rsp)     # imm = 0x2710
leaq 0xad7(%rip), %rsi       # 0x51d0
leaq 0x50(%rsp), %rdi
callq 0x2300
leaq 0x70(%rsp), %rdi
callq 0x21d0
movq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2100
leaq 0xe8(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x4839
leaq 0x70(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x21e0
leaq 0x70(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x21e0
leaq 0x70(%rsp), %rdi
leaq 0x24(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x21e0
leaq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x21e0
cmpl $0x3080000, 0x2c(%rsp)  # imm = 0x3080000
jne 0x4858
movl 0x28(%rsp), %eax
bswapl %eax
cmpl 0x10(%rsp), %eax
jne 0x4858
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movl 0x24(%rsp), %eax
bswapl %eax
movl 0x20(%rsp), %ecx
bswapl %ecx
imull %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
movl %ecx, 0x14(%rsp)
leaq 0x70(%rsp), %r15
leaq 0xf(%rsp), %r12
pushq $0x1
popq %r13
leaq 0x1c(%rsp), %rbp
movl 0x14(%rsp), %ebx
movl %eax, 0x18(%rsp)
cmpl 0x10(%rsp), %eax
je 0x489e
subl $0x1, %ebx
jb 0x4813
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x21e0
movzbl 0xf(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4a28
jmp 0x47ea
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x49bc
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x4831
movq %rax, 0x38(%rsp)
movl 0x18(%rsp), %eax
incl %eax
jmp 0x47d8
movq 0x3798(%rip), %rdi      # 0x7fd8
leaq 0x936(%rip), %rsi       # 0x517d
callq 0x21a0
movq %rax, %rdi
callq 0x20d0
pushq $0x1
jmp 0x4875
movq 0x3779(%rip), %rdi      # 0x7fd8
leaq 0x937(%rip), %rsi       # 0x519d
callq 0x21a0
movq %rax, %rdi
callq 0x20d0
pushq $0x2
popq %rbx
leaq 0x70(%rsp), %rdi
callq 0x2060
leaq 0x50(%rsp), %rdi
callq 0x20c0
movl %ebx, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rdi
callq 0x4bb4
xorl %ebx, %ebx
jmp 0x4876
jmp 0x48ae
movq %rax, %rbx
jmp 0x48d5
jmp 0x48b5
movq %rax, %rbx
jmp 0x48cb
jmp 0x48be
jmp 0x48be
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4bb4
leaq 0x70(%rsp), %rdi
callq 0x2060
leaq 0x50(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x22d0