// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: filterMatrix(Matrix&, Filter2D&)
static Matrix filterMatrix(Matrix &A, Filter2D &F)
{
    // this function takes out all active elements from A according to F
    //       and return them in another matrix
    //       the filter info is given in matrix F
    auto dimA = A.Dimension();
    auto dimF = F.Dimension();
    assert(dimA == dimF);

    std::vector<std::vector<double>> R;
    for(size_t i=0;i<dimA.first;i++)
    {
        std::vector<double> _tmp_row;
        for(size_t j=0;j<dimA.second;j++)
        {
            if(F[i][j]==1)
            {
                _tmp_row.push_back(A[i][j]);
            }
            else if(F[i][j] != 0)
            {
                std::cout<<"Error: filter matrix element value must be 0 or 1"<<std::endl;
                exit(0);
            }
        }
        if(_tmp_row.size() > 0)
            R.push_back(_tmp_row);
    }
    Matrix Ret(R);
    return Ret;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x48(%rsp)
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq 0x118(%rsp), %rdi
callq 0x3bae0
movq %rax, 0x100(%rsp)
movq %rdx, 0x108(%rsp)
movq 0x110(%rsp), %rdi
callq 0x200b0
movq %rax, 0xf0(%rsp)
movq %rdx, 0xf8(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
callq 0x3b180
testb $0x1, %al
jne 0x10a21
jmp 0x10a23
jmp 0x10a42
leaq 0x4367b(%rip), %rdi     # 0x540a5
leaq 0x417a1(%rip), %rsi     # 0x521d2
movl $0x622, %edx            # imm = 0x622
leaq 0x43675(%rip), %rcx     # 0x540b2
callq 0x4170
leaq 0xb8(%rsp), %rdi
callq 0x21e70
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0x100(%rsp), %rax
jae 0x10c75
leaq 0x98(%rsp), %rdi
callq 0x1f090
movq $0x0, 0x90(%rsp)
movq 0x90(%rsp), %rax
cmpq 0x108(%rsp), %rax
jae 0x10c23
movq 0x110(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1fff0
movq %rax, 0x40(%rsp)
jmp 0x10abc
movq 0x40(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x20020
movq %rdx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x10ada
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x80(%rsp)
movq %rax, 0x88(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x20080
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x10b70
movq 0x118(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x3be50
movq %rax, 0x28(%rsp)
jmp 0x10b27
movq 0x28(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x1fd60
movq %rax, %rsi
leaq 0x98(%rsp), %rdi
callq 0x21ce0
jmp 0x10b4b
jmp 0x10c08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f190
jmp 0x10cd0
movq 0x110(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1fff0
movq %rax, 0x20(%rsp)
jmp 0x10b8c
movq 0x20(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x20020
movq %rdx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x10baa
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x20080
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x10c06
movq 0x5a3ff(%rip), %rdi     # 0x6afd8
leaq 0x434fc(%rip), %rsi     # 0x540dc
callq 0x4230
movq %rax, 0x8(%rsp)
jmp 0x10bec
movq 0x8(%rsp), %rdi
movq 0x5a3b8(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x10bff
xorl %edi, %edi
callq 0x4290
jmp 0x10c08
jmp 0x10c0a
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x10a8a
leaq 0x98(%rsp), %rdi
callq 0x21e20
cmpq $0x0, %rax
jbe 0x10c4f
leaq 0xb8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x21f00
jmp 0x10c4d
jmp 0x10c4f
leaq 0x98(%rsp), %rdi
callq 0x1f190
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x10a5b
movq 0x48(%rsp), %rdi
movb $0x0, 0x5f(%rsp)
leaq 0xb8(%rsp), %rsi
callq 0x3b770
jmp 0x10c8e
movb $0x1, 0x5f(%rsp)
testb $0x1, 0x5f(%rsp)
jne 0x10cb6
jmp 0x10cac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
jmp 0x10cd0
movq 0x48(%rsp), %rdi
callq 0x3bb80
leaq 0xb8(%rsp), %rdi
callq 0x21f70
movq 0x50(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xb8(%rsp), %rdi
callq 0x21f70
movq 0x78(%rsp), %rdi
callq 0x43f0
nopw (%rax,%rax)