// File: /workspace/github_c_makefile_star2_O2/stonezwr[P]SpiKL-IP/util.C
// Function: ReadMnistSample(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>, std::allocator<std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>>>&, int)
int ReadMnistSample(std::string filename, Matrices& vec, int num)
{
    std::ifstream file(filename.c_str(), std::ios::binary);
    if(!file.is_open()){
        std::cout<<"Cannot open the file: "<<filename<<std::endl;
        exit(EXIT_FAILURE);
    }
    int magic_number = 0;
    int number_of_images = 0;
    int n_rows = 0;
    int n_cols = 0;
    file.read((char*) &magic_number, sizeof(magic_number));
    magic_number = ReverseInt(magic_number);
    file.read((char*) &number_of_images,sizeof(number_of_images));
    number_of_images = ReverseInt(number_of_images);
    if(number_of_images >= num){
        number_of_images = num;
    }
    else{
        std::cout<<"ReadMnistSample::number of images "<<num<<" is overflow!"<<std::endl;
        exit(EXIT_FAILURE);
    }
    file.read((char*) &n_rows, sizeof(n_rows));
    n_rows = ReverseInt(n_rows);
    file.read((char*) &n_cols, sizeof(n_cols));
    n_cols = ReverseInt(n_cols);
    for(int i = 0; i < number_of_images; ++i){
        Matrix tpmat = Matrix(n_rows, std::vector<float>(n_cols, 1));
                    
        for(int r = 0; r < n_rows; ++r){
            for(int c = 0; c < n_cols; ++c){
                unsigned char temp = 0;
                file.read((char*) &temp, sizeof(temp));        
                tpmat[r][c] = (float)temp / (5.5*255.0f);
                //if(i == 0)  std::cout<<tpmat[r][c]<<"\t";
            }
            //if(i == 0)  std::cout<<std::endl;
        }
        vec.push_back(tpmat);
    }
    return vec.size();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movl %edx, %ebp
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
movq (%rdi), %rsi
leaq 0x60(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x35a0
leaq 0xd8(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x11fc2
leaq 0x18(%rsp), %rsi
andl $0x0, (%rsi)
andl $0x0, 0x10(%rsp)
andl $0x0, 0xc(%rsp)
andl $0x0, 0x8(%rsp)
leaq 0x60(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x3370
movl 0x18(%rsp), %eax
bswapl %eax
movl %eax, 0x18(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x3370
movl 0x10(%rsp), %eax
bswapl %eax
movl %eax, 0x10(%rsp)
cmpl %ebp, %eax
jl 0x11fea
movl %ebp, 0x10(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x3370
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0xc(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x3370
movl 0x8(%rsp), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
leaq 0x30(%rsp), %r14
leaq 0x60(%rsp), %r15
pushq $0x1
popq %r12
cmpl 0x10(%rsp), %eax
jge 0x11f8d
movq %rax, 0x28(%rsp)
movslq 0xc(%rsp), %rbp
movslq 0x8(%rsp), %rsi
movl $0x3f800000, 0x1c(%rsp) # imm = 0x3F800000
movq %r14, %rdi
leaq 0x1c(%rsp), %rdx
leaq 0x17(%rsp), %rcx
callq 0x12306
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
leaq 0x16(%rsp), %rcx
callq 0x1235c
movq %r14, %rdi
callq 0x12560
xorl %ebp, %ebp
movslq 0xc(%rsp), %rax
cmpq %rax, %rbp
jge 0x11f67
imulq $0x18, %rbp, %r13
xorl %ebx, %ebx
movslq 0x8(%rsp), %rax
cmpq %rax, %rbx
jge 0x11f62
movb $0x0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3370
movzbl 0x30(%rsp), %eax
cvtsi2sd %eax, %xmm0
divsd 0x4b05(%rip), %xmm0     # 0x16a50
cvtsd2ss %xmm0, %xmm0
movq 0x48(%rsp), %rax
movq (%rax,%r13), %rax
movss %xmm0, (%rax,%rbx,4)
incq %rbx
jmp 0x11f1d
incq %rbp
jmp 0x11f0d
movq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rbx
movq %rbx, %rsi
callq 0x123b2
movq %rbx, %rdi
callq 0x123de
movq 0x28(%rsp), %rax
incl %eax
jmp 0x11ebb
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x30d0
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdfff(%rip), %rdi      # 0x1ffc8
leaq 0x4c02(%rip), %rsi      # 0x16bd2
callq 0x32a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, %rdi
callq 0x3160
jmp 0x1201e
movq 0xdfd7(%rip), %rdi      # 0x1ffc8
leaq 0x4bf1(%rip), %rsi      # 0x16be9
callq 0x32a0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3530
leaq 0x4bfe(%rip), %rsi      # 0x16c0c
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3160
pushq $0x1
popq %rdi
callq 0x3330
jmp 0x1203b
jmp 0x1203b
jmp 0x12040
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x12560
jmp 0x1204d
movq %rax, %rbx
jmp 0x1204d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x123de
leaq 0x60(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3540