// File: /workspace/github_c_makefile_star2_O1/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 0x154a1
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 0x154d6
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 0x1546d
xorl %ebp, %ebp
leaq 0x20(%rsp), %r14
leaq 0x38(%rsp), %rbx
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 0x158ac
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
leaq 0xe(%rsp), %rcx
callq 0x1592e
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x153db
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
cmpl $0x0, 0x4(%rsp)
jle 0x15447
xorl %r12d, %r12d
cmpl $0x0, (%rsp)
jle 0x1543a
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 0x45b5(%rip), %xmm0     # 0x199d0
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 0x153f2
incq %r12
movslq 0x4(%rsp), %rax
cmpq %rax, %r12
jl 0x153e5
movq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x159c6
movq %rbx, %rdi
callq 0x159f2
incl %ebp
cmpl 0x8(%rsp), %ebp
jl 0x1538e
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 0xbb28(%rip), %rdi      # 0x20fd0
leaq 0x46a3(%rip), %rsi      # 0x19b52
movl $0x16, %edx
callq 0x3340
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0xbb09(%rip), %rdi      # 0x20fd0
callq 0x3340
movq %rax, %rdi
callq 0x3160
jmp 0x1551b
movq 0xbaf3(%rip), %rdi      # 0x20fd0
leaq 0x4685(%rip), %rsi      # 0x19b69
movl $0x22, %edx
callq 0x3340
movq 0xbadb(%rip), %rdi      # 0x20fd0
movl %ebp, %esi
callq 0x3560
movq %rax, %rbx
leaq 0x4686(%rip), %rsi      # 0x19b8c
movl $0xd, %edx
movq %rax, %rdi
callq 0x3340
movq %rbx, %rdi
callq 0x3160
movl $0x1, %edi
callq 0x3370
jmp 0x1552b
jmp 0x1552b
jmp 0x1554c
movq %rax, %rbx
jmp 0x15559
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x15559
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
jmp 0x15559
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x159f2
leaq 0x50(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3570