// File: /workspace/github_cpp_makefile_star2_O1/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 $0x268, %rsp            # imm = 0x268
movl %esi, %ebp
movq %rdi, 0x58(%rsp)
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1472(%rip), %rsi      # 0x6149
leaq 0x1477(%rip), %rdx      # 0x6155
leaq 0x38(%rsp), %rdi
callq 0x3e42
testl %ebp, %ebp
je 0x4d07
movl $0xea60, 0x4(%rsp)      # imm = 0xEA60
leaq 0x14c5(%rip), %rsi      # 0x61c0
leaq 0x38(%rsp), %rdi
callq 0x2340
jmp 0x4d20
movl $0x2710, 0x4(%rsp)      # imm = 0x2710
leaq 0x14c2(%rip), %rsi      # 0x61d8
leaq 0x38(%rsp), %rdi
callq 0x2340
leaq 0x60(%rsp), %rdi
callq 0x2200
movq 0x38(%rsp), %rsi
leaq 0x60(%rsp), %rdi
movl $0x8, %edx
callq 0x2120
leaq 0xd8(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x4e8d
leaq 0x60(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl $0x4, %edx
callq 0x2220
leaq 0x60(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl $0x4, %edx
callq 0x2220
leaq 0x60(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movl $0x4, %edx
callq 0x2220
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x2220
cmpl $0x3080000, 0x1c(%rsp)  # imm = 0x3080000
jne 0x4eda
movl 0x18(%rsp), %eax
bswapl %eax
cmpl 0x4(%rsp), %eax
jne 0x4eda
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movl 0x14(%rsp), %eax
bswapl %eax
movl 0x10(%rsp), %ecx
bswapl %ecx
imull %eax, %ecx
movl %ecx, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x60(%rsp), %r15
leaq 0x3(%rsp), %r12
leaq 0xc(%rsp), %r13
movl 0x8(%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
jle 0x4e44
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2220
movzbl 0x3(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x4e35
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x4e40
movq %r14, %rdi
movq %r13, %rdx
callq 0x5130
decl %ebx
jne 0x4e03
movq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0x5104
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x4e62
movq %rax, 0x28(%rsp)
incl %ebp
cmpl 0x4(%rsp), %ebp
jne 0x4df9
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
leaq 0x48(%rsp), %r14
je 0x4e86
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
xorl %ebx, %ebx
jmp 0x4f25
movq 0x4144(%rip), %rbx      # 0x8fd8
leaq 0x12ea(%rip), %rsi      # 0x6185
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x4112(%rip), %rdi      # 0x8fd8
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2150
jmp 0x4f25
movq 0x40f7(%rip), %rbx      # 0x8fd8
leaq 0x12bd(%rip), %rsi      # 0x61a5
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x40c5(%rip), %rdi      # 0x8fd8
callq 0x2030
movl $0x2, %ebx
movq %rax, %rdi
callq 0x2150
leaq 0x60(%rsp), %rdi
callq 0x2070
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x4f46
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4f5c
movq %rax, %rbx
jmp 0x4f90
jmp 0x4f63
movq %rax, %rbx
jmp 0x4f86
jmp 0x4f6c
jmp 0x4f6c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4f86
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
leaq 0x60(%rsp), %rdi
callq 0x2070
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x4fac
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2310