// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/include/utils/ModelLoader.h
// Function: bool abcdl::utils::ModelLoader::write<float>(std::vector<abcdl::algebra::Matrix<float>*, std::allocator<abcdl::algebra::Matrix<float>*>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
bool ModelLoader::write(std::vector<abcdl::algebra::Matrix<T>*> models,
                        const std::string& path,
                        const std::string& signature,
                        bool is_append){
    std::vector<abcdl::algebra::Matrix<T>*> old_models;
    if(is_append){
        read<T>(path, &old_models, signature);
    }
    
    size_t num_models = models.size() + old_models.size();
    std::vector<ModelInfo> infos;

    if(!generate_header(old_models, &infos) || !generate_header(models, &infos)){
        return false;
    }
     
    /**
    std::ofstream out_file(path, std::ios::binary);
    std::ofstream out_file(path, std::ios::out);

    out_file.write(signature.c_str(), sizeof(char)*signature.size());
    out_file.write((char*)&num_models, sizeof(size_t));
    for(auto&& info : infos){
        out_file.write(&info.type, sizeof(char));
        out_file.write((char*)&info.rows, sizeof(size_t));
        out_file.write((char*)&info.cols, sizeof(size_t));
    }

    size_t num_old_models = old_models.size();
    for(size_t i = 0; i != num_old_models; i++){
        out_file.write((char*)old_models[i]->data(), sizeof(T) * infos[i].rows * infos[i].cols);
    }

    num_models = models.size();
    for(size_t i = 0; i != num_models; i++){
        ModelInfo info = infos[i+num_old_models];
        out_file.write((char*)models[i]->data(), sizeof(T) * info.rows * info.cols);
    }
    **/

    std::ofstream out_file(path, std::ios::out);
    out_file << signature << std::endl;
    out_file << num_models << std::endl;
    for(auto&& info : infos){
        out_file << info.type << std::endl;
        out_file << info.rows << std::endl;
        out_file << info.cols << std::endl;
    }
    
    size_t num_old_models = old_models.size();
    for(size_t i = 0; i != num_old_models; i++){
        size_t model_size = infos[i].rows * infos[i].cols;
        for(size_t j = 0; j < model_size; j++){
            out_file << old_models[i]->get_data(j) << std::endl;
        }
    }

    num_models = models.size();
    for(size_t i = 0; i < num_models; i++){
        size_t model_size = infos[i + num_old_models].rows * infos[i + num_old_models].cols;
        for(size_t j = 0; j < model_size; j++){
            out_file << models[i]->get_data(j) << std::endl;
        }
    }
    out_file.close();

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
testl %r8d, %r8d
je 0x17748
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x170aa
movq %r14, 0x8(%rsp)
movq 0x8(%rbx), %r12
subq (%rbx), %r12
leaq 0x30(%rsp), %rsi
movq 0x8(%rsi), %r14
subq (%rsi), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x17c90
leaq 0x68(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
callq 0x183fe
movl %eax, %ebp
movb $0x1, 0x7(%rsp)
testb %al, %al
je 0x177bd
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x17c90
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
callq 0x183fe
xorb $0x1, %al
movb %al, 0x7(%rsp)
testb %bpl, %bpl
je 0x177d1
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x177d1
callq 0x9350
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
movb 0x7(%rsp), %bpl
je 0x177e5
callq 0x9350
testb %bpl, %bpl
jne 0x17bd3
leaq 0x208(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x10, %edx
callq 0x9650
movq (%r15), %rsi
movq 0x8(%r15), %rdx
leaq 0x208(%rsp), %rdi
callq 0x93b0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %r15, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
sarq $0x3, %r12
sarq $0x3, %r14
addq %r12, %r14
leaq 0x208(%rsp), %rdi
movq %r14, %rsi
callq 0x9200
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %r14, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
cmpq %rbp, %r13
je 0x17969
leaq 0x208(%rsp), %r14
leaq 0x80(%rsp), %r15
movb (%r13), %al
movb %al, 0x80(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x93b0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %r12, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
movq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x9200
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %r12, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
movq 0x10(%r13), %rsi
movq %r14, %rdi
callq 0x9200
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %r12, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
addq $0x18, %r13
cmpq %rbp, %r13
jne 0x178ab
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq %rdx, 0x8(%rsp)
cmpq %rcx, %rax
je 0x17a74
xorl %r13d, %r13d
leaq 0x208(%rsp), %r14
movq 0x10(%rsp), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq 0x10(%rax,%rcx,8), %r12
imulq 0x8(%rax,%rcx,8), %r12
testq %r12, %r12
je 0x17a66
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
movq (%rax,%r13,8), %rbp
movq 0x8(%rbp), %rax
imulq (%rbp), %rax
cmpq %r15, %rax
jbe 0x17a1d
movq 0x10(%rbp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r15,4), %xmm0
movq %r14, %rdi
callq 0x9540
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %rbp, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
incq %r15
cmpq %r15, %r12
jne 0x179bd
jmp 0x17a66
leaq 0x80(%rsp), %rdi
leaq 0x24a21(%rip), %rsi     # 0x3c44d
movl $0x3f, %edx
movl $0x3, %ecx
callq 0x3894e
movl $0x1e, %edx
leaq 0x80(%rsp), %rdi
leaq 0x24a17(%rip), %rsi     # 0x3c466
callq 0x93b0
leaq 0x80(%rsp), %rdi
callq 0x38b76
jmp 0x179d4
incq %r13
cmpq 0x8(%rsp), %r13
jne 0x17996
movq 0x8(%rbx), %rax
subq (%rbx), %rax
je 0x17b89
sarq $0x3, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x8(%rsp)
leaq 0x208(%rsp), %r14
leaq (%r12,%r12,2), %rax
shlq $0x3, %rax
addq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %r13
imulq 0x8(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x17b7b
xorl %r15d, %r15d
movq (%rbx), %rax
movq (%rax,%r12,8), %rbp
movq 0x8(%rbp), %rax
imulq (%rbp), %rax
cmpq %r15, %rax
jbe 0x17b32
movq 0x10(%rbp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r15,4), %xmm0
movq %r14, %rdi
callq 0x9540
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x9360
movsbl %al, %esi
movq %rbp, %rdi
callq 0x9040
movq %rax, %rdi
callq 0x9210
incq %r15
cmpq %r15, %r13
jne 0x17ad4
jmp 0x17b7b
leaq 0x80(%rsp), %rdi
leaq 0x2490c(%rip), %rsi     # 0x3c44d
movl $0x3f, %edx
movl $0x3, %ecx
callq 0x3894e
movl $0x1e, %edx
leaq 0x80(%rsp), %rdi
leaq 0x24902(%rip), %rsi     # 0x3c466
callq 0x93b0
leaq 0x80(%rsp), %rdi
callq 0x38b76
jmp 0x17ae9
incq %r12
cmpq 0x48(%rsp), %r12
jne 0x17aab
leaq 0x208(%rsp), %rdi
callq 0x94a0
movq 0x40423(%rip), %rax     # 0x57fc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x210(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x208(%rsp,%rcx)
callq 0x92d0
leaq 0x300(%rsp), %rdi
callq 0x9100
movb 0x7(%rsp), %bpl
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17be2
callq 0x9350
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17bf1
callq 0x9350
xorb $0x1, %bpl
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17c0b
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x38b76
jmp 0x17c5c
jmp 0x17c59
jmp 0x17c3a
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x17c42
callq 0x9350
jmp 0x17c42
movq %rax, %rbx
jmp 0x17c78
movq %rax, %rbx
jmp 0x17c69
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x17c69
callq 0x9350
jmp 0x17c69
jmp 0x17c59
jmp 0x17c59
jmp 0x17c59
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x94e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17c78
callq 0x9350
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17c87
callq 0x9350
movq %rbx, %rdi
callq 0x95d0
nop