// File: 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
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x33b0
movq -0x2a0(%rbp), %rdx
leaq 0x3610(%rip), %rsi      # 0xe125
leaq -0x38(%rbp), %rdi
callq 0x58f0
jmp 0xab20
leaq -0x39(%rbp), %rdi
callq 0x3230
movl $0x0, -0x50(%rbp)
testb $0x1, -0x11(%rbp)
je 0xab7c
leaq 0x365f(%rip), %rsi      # 0xe19c
leaq -0x38(%rbp), %rdi
callq 0x3400
jmp 0xab48
movl $0xea60, -0x50(%rbp)    # imm = 0xEA60
jmp 0xab95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x3230
jmp 0xae63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xae5a
leaq 0x3631(%rip), %rsi      # 0xe1b4
leaq -0x38(%rbp), %rdi
callq 0x3400
jmp 0xab8e
movl $0x2710, -0x50(%rbp)    # imm = 0x2710
leaq -0x258(%rbp), %rdi
callq 0x3260
jmp 0xaba3
leaq -0x38(%rbp), %rdi
callq 0x30c0
movq %rax, %rsi
leaq -0x258(%rbp), %rdi
movl $0x8, %edx
callq 0x3110
jmp 0xabc2
leaq -0x258(%rbp), %rdi
callq 0x3330
movb %al, -0x2a1(%rbp)
jmp 0xabd6
movb -0x2a1(%rbp), %al
testb $0x1, %al
jne 0xac3a
jmp 0xabe2
movq 0xa3ef(%rip), %rdi      # 0x14fd8
leaq 0x3571(%rip), %rsi      # 0xe161
callq 0x31e0
movq %rax, -0x2b0(%rbp)
jmp 0xabfe
movq -0x2b0(%rbp), %rdi
movq 0xa3b4(%rip), %rsi      # 0x14fc0
callq 0x3210
jmp 0xac13
movl $0x1, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0xae2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xae4e
leaq -0x258(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xac54
leaq -0x258(%rbp), %rdi
leaq -0x264(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xac6e
leaq -0x258(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xac88
leaq -0x258(%rbp), %rdi
leaq -0x26c(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xaca2
movl -0x260(%rbp), %edi
callq 0xa7f0
cmpl $0x803, %eax            # imm = 0x803
jne 0xacc4
movl -0x264(%rbp), %edi
callq 0xa7f0
cmpl -0x50(%rbp), %eax
je 0xad0b
movq 0xa30d(%rip), %rdi      # 0x14fd8
leaq 0x34af(%rip), %rsi      # 0xe181
callq 0x31e0
movq %rax, -0x2b8(%rbp)
jmp 0xace0
movq -0x2b8(%rbp), %rdi
movq 0xa2d2(%rip), %rsi      # 0x14fc0
callq 0x3210
jmp 0xacf5
movl $0x2, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0xae2d
leaq -0x288(%rbp), %rdi
callq 0xafe0
movl -0x268(%rbp), %edi
callq 0xa7f0
movl %eax, -0x2bc(%rbp)
movl -0x26c(%rbp), %edi
callq 0xa7f0
movl %eax, %ecx
movl -0x2bc(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x28c(%rbp)
movl $0x0, -0x290(%rbp)
movl -0x290(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0xae10
movl $0x0, -0x294(%rbp)
movl -0x294(%rbp), %eax
cmpl -0x28c(%rbp), %eax
jge 0xadde
leaq -0x258(%rbp), %rdi
leaq -0x26d(%rbp), %rsi
movl $0x1, %edx
callq 0x3280
jmp 0xad8f
movzbl -0x26d(%rbp), %eax
movl %eax, -0x298(%rbp)
leaq -0x288(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0xafb0
jmp 0xadb1
jmp 0xadb3
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0xad67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0xb090
jmp 0xae4e
movq -0x10(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0xb000
jmp 0xadf0
leaq -0x288(%rbp), %rdi
callq 0xb070
movl -0x290(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x290(%rbp)
jmp 0xad4e
movl $0x0, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0xb090
leaq -0x258(%rbp), %rdi
callq 0x3060
leaq -0x38(%rbp), %rdi
callq 0x3100
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
leaq -0x258(%rbp), %rdi
callq 0x3060
leaq -0x38(%rbp), %rdi
callq 0x3100
movq -0x48(%rbp), %rdi
callq 0x33a0
nopl (%rax)