// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rsi, %rbx
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testl %r8d, %r8d
je 0x174f8
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x16e5e
movq 0x18(%rsp), %r14
subq 0x10(%rsp), %r14
sarq $0x3, %r14
jmp 0x174fb
xorl %r14d, %r14d
movq 0x8(%rbx), %rbp
subq (%rbx), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x17a24
leaq 0x68(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x181a4
movb $0x1, 0x7(%rsp)
testb %al, %al
je 0x17571
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x17a24
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x181a4
xorb $0x1, %al
movb %al, 0x7(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x17571
callq 0x9350
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x17580
callq 0x9350
movb 0x7(%rsp), %r13b
testb %r13b, %r13b
jne 0x17967
leaq 0x208(%rsp), %rdi
movq %r12, %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, %rbp
addq %rbp, %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 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
cmpq %rbp, %r13
je 0x17703
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 0x17645
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq %rdx, 0x8(%rsp)
cmpq %rcx, %rax
je 0x1780b
xorl %r13d, %r13d
leaq 0x208(%rsp), %r14
movq 0x30(%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 0x177fd
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %r15
movq 0x8(%r15), %rax
imulq (%r15), %rax
cmpq %rbp, %rax
jbe 0x177b4
movq 0x10(%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbp,4), %xmm0
movq %r14, %rdi
callq 0x9540
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
incq %rbp
cmpq %rbp, %r12
jne 0x17756
jmp 0x177fd
leaq 0x80(%rsp), %rdi
leaq 0x24c8a(%rip), %rsi     # 0x3c44d
movl $0x3f, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1e, %edx
leaq 0x80(%rsp), %rdi
leaq 0x24c80(%rip), %rsi     # 0x3c466
callq 0x93b0
leaq 0x80(%rsp), %rdi
callq 0x389de
jmp 0x1776c
incq %r13
cmpq 0x8(%rsp), %r13
jne 0x17730
movq 0x8(%rbx), %rax
subq (%rbx), %rax
je 0x1791d
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 0x30(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %r13
imulq 0x8(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x1790f
xorl %ebp, %ebp
movq (%rbx), %rax
movq (%rax,%r12,8), %r15
movq 0x8(%r15), %rax
imulq (%r15), %rax
cmpq %rbp, %rax
jbe 0x178c6
movq 0x10(%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbp,4), %xmm0
movq %r14, %rdi
callq 0x9540
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
incq %rbp
cmpq %rbp, %r13
jne 0x1786a
jmp 0x1790f
leaq 0x80(%rsp), %rdi
leaq 0x24b78(%rip), %rsi     # 0x3c44d
movl $0x3f, %edx
movl $0x3, %ecx
callq 0x387b6
movl $0x1e, %edx
leaq 0x80(%rsp), %rdi
leaq 0x24b6e(%rip), %rsi     # 0x3c466
callq 0x93b0
leaq 0x80(%rsp), %rdi
callq 0x389de
jmp 0x1787e
incq %r12
cmpq 0x48(%rsp), %r12
jne 0x17842
leaq 0x208(%rsp), %rdi
callq 0x94a0
movq 0x4068f(%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), %r13b
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17976
callq 0x9350
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17985
callq 0x9350
xorb $0x1, %r13b
movl %r13d, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x179a0
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x389de
jmp 0x179f1
jmp 0x179ee
jmp 0x179cf
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x179d7
callq 0x9350
jmp 0x179d7
movq %rax, %rbx
jmp 0x17a0d
movq %rax, %rbx
jmp 0x179fe
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x179fe
callq 0x9350
jmp 0x179fe
jmp 0x179ee
jmp 0x179ee
jmp 0x179ee
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x94e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x17a0d
callq 0x9350
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17a1c
callq 0x9350
movq %rbx, %rdi
callq 0x95d0