// File: /workspace/github_cpp_makefile_star2_O3/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 0x149a(%rip), %rsi      # 0x6179
leaq 0x149f(%rip), %rdx      # 0x6185
leaq 0x38(%rsp), %rdi
callq 0x3e4e
testl %ebp, %ebp
je 0x4d0f
movl $0xea60, 0x4(%rsp)      # imm = 0xEA60
leaq 0x14ed(%rip), %rsi      # 0x61f0
leaq 0x38(%rsp), %rdi
callq 0x2340
jmp 0x4d28
movl $0x2710, 0x4(%rsp)      # imm = 0x2710
leaq 0x14ea(%rip), %rsi      # 0x6208
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 0x4e90
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 0x4edd
movl 0x18(%rsp), %eax
bswapl %eax
cmpl 0x4(%rsp), %eax
jne 0x4edd
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 0x4e4c
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 0x4e3d
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x4e48
movq %r14, %rdi
movq %r13, %rdx
callq 0x5136
decl %ebx
jne 0x4e0b
movq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0x510a
movq 0x20(%rsp), %rdi
cmpq %rdi, 0x28(%rsp)
je 0x4e6a
movq %rdi, 0x28(%rsp)
incl %ebp
cmpl 0x4(%rsp), %ebp
jne 0x4e01
testq %rdi, %rdi
leaq 0x48(%rsp), %r14
je 0x4e89
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
xorl %ebx, %ebx
jmp 0x4f28
movq 0x4141(%rip), %rbx      # 0x8fd8
leaq 0x1317(%rip), %rsi      # 0x61b5
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 0x410f(%rip), %rdi      # 0x8fd8
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2150
jmp 0x4f28
movq 0x40f4(%rip), %rbx      # 0x8fd8
leaq 0x12ea(%rip), %rsi      # 0x61d5
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 0x40c2(%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 0x4f49
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 0x4f5f
movq %rax, %rbx
jmp 0x4f93
jmp 0x4f66
movq %rax, %rbx
jmp 0x4f89
jmp 0x4f6f
jmp 0x4f6f
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4f89
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 0x4faf
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2310