// File: ghulette[P]neural-net-openmp/idx.cpp
// Function: IdxData::IdxData(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
IdxData::IdxData(std::string file) {
	using namespace std;
	
	cout << "Reading IDX file: " << file << endl;
	
	ifstream ifs(file.c_str(), ios::in | ios::binary);
	if(ifs.is_open()) {
		int magic = read_int(ifs);
		int num_dims = (magic - 0x800) - 1;
		n_records = read_int(ifs);
		record_len = 1;
		for(int i=0; i < num_dims; i++) {
			record_len *= read_int(ifs);
		}
		//cout << "  found " << n_records << " records of size " << record_len << endl;
		data = new unsigned char[n_records * record_len];
		for(int i=0; i < n_records; i++) {
			ifs.read((char *)&data[i * record_len], record_len);
			if(ifs.bad()) {
				cerr << "Error reading data" << endl;
			}
		}
		ifs.close();
	}
	else cerr << "Could not open file " << file << endl;
}

// Assembly:
subq $0x298, %rsp            # imm = 0x298
movq %rsi, 0x48(%rsp)
movq %rdi, 0x290(%rsp)
movq %rsi, 0x288(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x48b0(%rip), %rdi      # 0x7fd0
leaq 0x29c2(%rip), %rsi      # 0x60e9
callq 0x2150
movq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
movq 0x4875(%rip), %rsi      # 0x7fb8
callq 0x2180
movq 0x48(%rsp), %rdi
callq 0x20a0
movq %rax, 0x50(%rsp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x3a30
movq 0x50(%rsp), %rsi
movl %eax, %edx
leaq 0x80(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x22c0
movq 0x58(%rsp), %rdi
callq 0x2240
movb %al, 0x63(%rsp)
jmp 0x378f
movb 0x63(%rsp), %al
testb $0x1, %al
jne 0x379c
jmp 0x3934
leaq 0x80(%rsp), %rdi
callq 0x3a50
movl %eax, 0x3c(%rsp)
jmp 0x37af
movl 0x3c(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %eax
addl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, 0x6c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x3a50
movl %eax, 0x38(%rsp)
jmp 0x37d7
movq 0x40(%rsp), %rax
movl 0x38(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl $0x1, 0xc(%rax)
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jge 0x384c
leaq 0x80(%rsp), %rdi
callq 0x3a50
movl %eax, 0x34(%rsp)
jmp 0x380f
movq 0x40(%rsp), %rax
movl 0x34(%rsp), %ecx
imull 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x37f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x2060
jmp 0x398e
movq 0x40(%rsp), %rcx
movl 0x8(%rcx), %eax
movl 0xc(%rcx), %ecx
imull %ecx, %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, 0x28(%rsp)
jmp 0x3869
movq 0x40(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x64(%rsp)
movq 0x40(%rsp), %rcx
movl 0x64(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x3923
movq 0x40(%rsp), %rcx
movq (%rcx), %rsi
movl 0x64(%rsp), %eax
movslq 0xc(%rcx), %rdx
movl %edx, %ecx
imull %ecx, %eax
cltq
addq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x21f0
jmp 0x38b9
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x80(%rsp,%rax), %rdi
callq 0x22b0
movb %al, 0x27(%rsp)
jmp 0x38d8
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x38e2
jmp 0x3911
movq 0x4707(%rip), %rdi      # 0x7ff0
leaq 0x280c(%rip), %rsi      # 0x60fc
callq 0x2150
movq %rax, 0x18(%rsp)
jmp 0x38fc
movq 0x18(%rsp), %rdi
movq 0x46b0(%rip), %rsi      # 0x7fb8
callq 0x2180
jmp 0x390f
jmp 0x3911
jmp 0x3913
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x387e
leaq 0x80(%rsp), %rdi
callq 0x2050
jmp 0x3932
jmp 0x3979
movq 0x46b5(%rip), %rdi      # 0x7ff0
leaq 0x27cd(%rip), %rsi      # 0x610f
callq 0x2150
movq %rax, 0x10(%rsp)
jmp 0x394e
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
jmp 0x3964
movq 0x8(%rsp), %rdi
movq 0x4648(%rip), %rsi      # 0x7fb8
callq 0x2180
jmp 0x3977
jmp 0x3979
leaq 0x80(%rsp), %rdi
callq 0x2060
addq $0x298, %rsp            # imm = 0x298
retq
movq 0x78(%rsp), %rdi
callq 0x2270
nopl (%rax,%rax)