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

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

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

	// Read the header
	uint32_t magic;
	uint32_t nLabels;
	uint8_t label;
	labelFile.read(reinterpret_cast<char *>(&magic), sizeof(magic));
	labelFile.read(reinterpret_cast<char *>(&nLabels), sizeof(nLabels));

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

	for (int i = 0; i < samples; i++)
	{
		labelFile.read(reinterpret_cast<char *>(&label), sizeof(label));
		vLabels.push_back(int(label));
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x33b0
movq -0x278(%rbp), %rdx
leaq 0x38c0(%rip), %rsi      # 0xe125
leaq -0x38(%rbp), %rdi
callq 0x58f0
jmp 0xa870
leaq -0x39(%rbp), %rdi
callq 0x3230
movl $0x0, -0x50(%rbp)
testb $0x1, -0x11(%rbp)
je 0xa8cc
leaq 0x38a5(%rip), %rsi      # 0xe132
leaq -0x38(%rbp), %rdi
callq 0x3400
jmp 0xa898
movl $0xea60, -0x50(%rbp)    # imm = 0xEA60
jmp 0xa8e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x3230
jmp 0xaacc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xaac3
leaq 0x3877(%rip), %rsi      # 0xe14a
leaq -0x38(%rbp), %rdi
callq 0x3400
jmp 0xa8de
movl $0x2710, -0x50(%rbp)    # imm = 0x2710
leaq -0x258(%rbp), %rdi
callq 0x3260
jmp 0xa8f3
leaq -0x38(%rbp), %rdi
callq 0x30c0
movq %rax, %rsi
leaq -0x258(%rbp), %rdi
movl $0x8, %edx
callq 0x3110
jmp 0xa912
leaq -0x258(%rbp), %rdi
callq 0x3330
movb %al, -0x279(%rbp)
jmp 0xa926
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0xa996
jmp 0xa932
movq 0xa69f(%rip), %rdi      # 0x14fd8
leaq 0x3821(%rip), %rsi      # 0xe161
callq 0x31e0
movq %rax, -0x288(%rbp)
jmp 0xa94e
movq -0x288(%rbp), %rdi
movq 0xa664(%rip), %rsi      # 0x14fc0
callq 0x3210
jmp 0xa963
movl $0x1, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0xaaa2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3060
jmp 0xaac3
leaq -0x258(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xa9b0
leaq -0x258(%rbp), %rdi
leaq -0x264(%rbp), %rsi
movl $0x4, %edx
callq 0x3280
jmp 0xa9ca
movl -0x260(%rbp), %edi
callq 0xa7f0
cmpl $0x801, %eax            # imm = 0x801
jne 0xa9ec
movl -0x264(%rbp), %edi
callq 0xa7f0
cmpl -0x50(%rbp), %eax
je 0xaa30
movq 0xa5e5(%rip), %rdi      # 0x14fd8
leaq 0x3787(%rip), %rsi      # 0xe181
callq 0x31e0
movq %rax, -0x290(%rbp)
jmp 0xaa08
movq -0x290(%rbp), %rdi
movq 0xa5aa(%rip), %rsi      # 0x14fc0
callq 0x3210
jmp 0xaa1d
movl $0x2, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0xaaa2
movl $0x0, -0x26c(%rbp)
movl -0x26c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0xaa91
leaq -0x258(%rbp), %rdi
leaq -0x265(%rbp), %rsi
movl $0x1, %edx
callq 0x3280
jmp 0xaa5f
movq -0x10(%rbp), %rdi
movzbl -0x265(%rbp), %eax
movl %eax, -0x270(%rbp)
leaq -0x270(%rbp), %rsi
callq 0xafb0
jmp 0xaa7e
jmp 0xaa80
movl -0x26c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x26c(%rbp)
jmp 0xaa3a
movl $0x0, -0x4(%rbp)
movl $0x1, -0x25c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3060
leaq -0x38(%rbp), %rdi
callq 0x3100
movl -0x4(%rbp), %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x3100
movq -0x48(%rbp), %rdi
callq 0x33a0
nopw %cs:(%rax,%rax)