// File: /workspace/github_c_makefile_star2_O3/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 $0x258, %rsp            # imm = 0x258
movl %edx, %ebp
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
movq (%rdi), %rsi
leaq 0x50(%rsp), %rdi
movl $0x4, %edx
callq 0x35d0
leaq 0xc8(%rsp), %rdi
callq 0x30b0
testb %al, %al
je 0x15171
xorl %eax, %eax
leaq 0x10(%rsp), %rsi
movl %eax, (%rsi)
movl %eax, 0x8(%rsp)
movl %eax, 0x4(%rsp)
movl %eax, (%rsp)
leaq 0x50(%rsp), %rdi
movl $0x4, %edx
callq 0x33b0
movl 0x10(%rsp), %eax
bswapl %eax
movl %eax, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x33b0
movl 0x8(%rsp), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
cmpl %ebp, %eax
jl 0x151a6
movl %ebp, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x33b0
movl 0x4(%rsp), %eax
bswapl %eax
movl %eax, 0x4(%rsp)
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
movl $0x4, %edx
callq 0x33b0
movl (%rsp), %eax
bswapl %eax
movl %eax, (%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x1513d
xorl %ebp, %ebp
leaq 0x20(%rsp), %r14
leaq 0x50(%rsp), %r15
movslq 0x4(%rsp), %r12
movslq (%rsp), %rsi
movl $0x3f800000, 0x14(%rsp) # imm = 0x3F800000
movq %r14, %rdi
leaq 0x14(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x1567a
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
leaq 0xe(%rsp), %rcx
callq 0x15804
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x150a7
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
jle 0x15117
movl (%rsp), %eax
xorl %r12d, %r12d
testl %eax, %eax
jle 0x1510c
leaq (%r12,%r12,2), %rbx
xorl %r13d, %r13d
movb $0x0, 0x20(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x33b0
movzbl 0x20(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x4987(%rip), %xmm0     # 0x19a70
cvtsd2ss %xmm0, %xmm0
movq 0x38(%rsp), %rax
movq (%rax,%rbx,8), %rax
movss %xmm0, (%rax,%r13,4)
incq %r13
movslq (%rsp), %rax
cmpq %rax, %r13
jl 0x150c0
movl 0x4(%rsp), %ecx
incq %r12
movslq %ecx, %rdx
cmpq %rdx, %r12
jl 0x150b5
movq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x1589c
movq %rbx, %rdi
callq 0x158c8
incl %ebp
cmpl 0x8(%rsp), %ebp
jl 0x15058
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %ebx # imm = 0xAAAAAAAB
leaq 0x50(%rsp), %rdi
callq 0x30d0
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbe58(%rip), %rdi      # 0x20fd0
leaq 0x4a8b(%rip), %rsi      # 0x19c0a
movl $0x16, %edx
callq 0x3340
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0xbe39(%rip), %rdi      # 0x20fd0
callq 0x3340
movq %rax, %rdi
callq 0x3160
jmp 0x151eb
movq 0xbe23(%rip), %rdi      # 0x20fd0
leaq 0x4a6d(%rip), %rsi      # 0x19c21
movl $0x22, %edx
callq 0x3340
movq 0xbe0b(%rip), %rdi      # 0x20fd0
movl %ebp, %esi
callq 0x3560
movq %rax, %rbx
leaq 0x4a6e(%rip), %rsi      # 0x19c44
movl $0xd, %edx
movq %rax, %rdi
callq 0x3340
movq %rbx, %rdi
callq 0x3160
movl $0x1, %edi
callq 0x3370
jmp 0x151fb
jmp 0x151fb
jmp 0x1521c
movq %rax, %rbx
jmp 0x15229
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x15229
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
jmp 0x15229
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x158c8
leaq 0x50(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3570