// File: nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/common/io_utils.cc
// Function: parse_input(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
vector<vector<double>> parse_input(string filename) {
	// open the dataset
	ifstream input(filename, ios::binary);

	// read the magic number of the image
	int magic_number = 0;
	input.read((char*)&magic_number, sizeof(int));
	magic_number = our_math::big_to_litte_endian(magic_number);
	// find out how many images we are going to parse
	int n_of_images;
	input.read((char*)&n_of_images, sizeof(int));
	n_of_images = our_math::big_to_litte_endian(n_of_images);

	// create a list of vectors to return
	vector<vector<double>> list_of_vectors;
	// read the dimensions
	int rows = 0;
	input.read((char*)&rows, sizeof(int));
	rows = our_math::big_to_litte_endian(rows);
	int cols;
	input.read((char*)&cols, sizeof(int));
	cols = our_math::big_to_litte_endian(cols);

	double x;
	// for each image start filling the vectors
	for (int i = 0; i < n_of_images; i++) {
		// create a vector to store our image data
		vector<double> vec;
		// store each byte of the image in the vector, by parsing boh of the image's dimensions
		for (int j = 0; j < rows; j++) {
			for (int k = 0; k < cols; k++) {
				// the pixels are from 0-255, so an unsinged char type is enough
				unsigned char pixel = 0;
				input.read((char*)(&pixel), sizeof(pixel));
				// change its value to double
				x = (double)pixel;
				// push the byte into the vector
				vec.push_back(x);
			}
		}
		// push the vector in our list
		list_of_vectors.push_back(vec);
	}
	
	input.close();
	// return the list of vectors that we created
	return list_of_vectors;
}

// Assembly:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x2a0(%rsp)
movq %rsi, 0x298(%rsp)
leaq 0x90(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movl $0x4, %edx
callq 0x2320
movq 0x28(%rsp), %rdi
movl $0x0, 0x8c(%rsp)
leaq 0x8c(%rsp), %rsi
movl $0x4, %edx
callq 0x2230
jmp 0x25ff
movl 0x8c(%rsp), %edi
callq 0x33a0
movl %eax, 0x14(%rsp)
jmp 0x2611
movl 0x14(%rsp), %eax
movl %eax, 0x8c(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0x4, %edx
callq 0x2230
jmp 0x2635
movl 0x78(%rsp), %edi
callq 0x33a0
movl %eax, 0x10(%rsp)
jmp 0x2644
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %eax
movl %eax, 0x78(%rsp)
movb $0x0, 0x77(%rsp)
callq 0x3410
movl $0x0, 0x70(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl $0x4, %edx
callq 0x2230
jmp 0x267c
movl 0x70(%rsp), %edi
callq 0x33a0
movl %eax, 0xc(%rsp)
jmp 0x268b
movl 0xc(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x6c(%rsp), %rsi
movl $0x4, %edx
callq 0x2230
jmp 0x26ac
movl 0x6c(%rsp), %edi
callq 0x33a0
movl %eax, 0x8(%rsp)
jmp 0x26bb
movl 0x8(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x78(%rsp), %eax
jge 0x27e2
leaq 0x40(%rsp), %rdi
callq 0x3420
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x27b7
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jge 0x27a5
movb $0x0, 0x37(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x37(%rsp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x272d
movzbl 0x37(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x3430
jmp 0x274d
jmp 0x274f
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x2701
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x282b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x2821
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x3510
jmp 0x2821
jmp 0x27a7
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x26eb
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x34a0
jmp 0x27c8
leaq 0x40(%rsp), %rdi
callq 0x3510
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x26cb
leaq 0x90(%rsp), %rdi
callq 0x2060
jmp 0x27f1
movb $0x1, 0x77(%rsp)
testb $0x1, 0x77(%rsp)
jne 0x2807
movq 0x18(%rsp), %rdi
callq 0x3570
leaq 0x90(%rsp), %rdi
callq 0x2080
movq 0x20(%rsp), %rax
addq $0x2a8, %rsp            # imm = 0x2A8
retq
movq 0x18(%rsp), %rdi
callq 0x3570
leaq 0x90(%rsp), %rdi
callq 0x2080
movq 0x80(%rsp), %rdi
callq 0x23c0
nopw %cs:(%rax,%rax)