// File: /workspace/github_cpp_makefile_star2_O3/cannam[P]mini-cpp-convnet/flower.cpp
// Function: convolve(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>>>>>> const&, std::vector<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>>>>>>, std::allocator<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>>>>>>>> const&, std::vector<float, std::allocator<float>> const&)
vector<vector<vector<float>>>
convolve(const vector<vector<vector<float>>> &in,
         const vector<vector<vector<vector<float>>>> &weights,
         const vector<float> &biases)
{
    // Weights tensor is indexed HWCK, i.e. height - width - number of
    // input channels (e.g. image depth) - number of output channels
    // (i.e. number of learned kernels)
    
    size_t kernel_height = weights.size();
    size_t kernel_width = weights[0].size();
    size_t nkernels = weights[0][0][0].size();
    
    size_t out_height = in.size();
    if (out_height < kernel_height - 1) {
        throw runtime_error("Input too small in convolve");
    }

    size_t out_width = in[0].size();
    if (out_width < kernel_width - 1) {
        throw runtime_error("Input too small in convolve");
    }

    size_t depth = in[0][0].size();
    if (depth != weights[0][0].size()) {
        cerr << "convolve: input depth is " << depth << " but we expected "
             << weights[0][0].size() << endl;
        throw runtime_error("Depth mismatch in convolve");
    }
        
    out_height -= kernel_height - 1;
    out_width -= kernel_width - 1;

#ifdef VERBOSE
    cerr << "convolve: " << nkernels << " kernels of size "
         << kernel_width << "x" << kernel_height << "; output size "
         << out_width << "x" << out_height << "; input depth " << depth << endl;
#endif

    auto out =
        vector<vector<vector<float>>>
        (out_height,
         vector<vector<float>>
         (out_width,
          vector<float>
          (nkernels,
           0.f)));

    for (size_t k = 0; k < nkernels; ++k) {
        for (size_t y = 0; y < out_height; ++y) {
            for (size_t x = 0; x < out_width; ++x) {
                for (size_t c = 0; c < depth; ++c) {
                    for (size_t ky = 0; ky < kernel_height; ++ky) {
                        for (size_t kx = 0; kx < kernel_width; ++kx) {
                            out[y][x][k] +=
                                weights[ky][kx][c][k] * in[y + ky][x + kx][c];
                        }
                    }
                }
                out[y][x][k] += biases[k];
            }
        }
    }

    return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rcx, 0x50(%rsp)
movq %rdi, 0x20(%rsp)
movq (%rdx), %rbx
movq %rdx, 0x40(%rsp)
movq 0x8(%rdx), %rax
movq %rax, 0xc0(%rsp)
movq %rax, %rcx
subq %rbx, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdi, %rcx
movq (%rsi), %rax
movq %rsi, 0xa8(%rsp)
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rdi, %rdx
movq %rcx, 0x10(%rsp)
decq %rcx
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
subq %rcx, %rdx
movq %rdx, 0x18(%rsp)
jb 0x1c1ed
movq (%rbx), %r12
movq 0x8(%rbx), %rcx
movq %rcx, 0xc8(%rsp)
movq %rcx, %rdx
subq %r12, %rdx
sarq $0x3, %rdx
imulq %rdi, %rdx
movq (%rax), %rcx
movq 0x8(%rax), %r15
subq %rcx, %r15
sarq $0x3, %r15
imulq %rdi, %r15
movq %rdx, 0x38(%rsp)
decq %rdx
movq %r15, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
subq %rdx, %r15
jb 0x1c20e
movq (%r12), %rax
movq (%rcx), %rsi
movq 0x8(%rcx), %rdx
movq %rdx, 0x88(%rsp)
movq %rsi, 0x90(%rsp)
subq %rsi, %rdx
sarq $0x2, %rdx
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %rdi, %rcx
movq %rdx, %rbp
cmpq %rcx, %rdx
jne 0x1c22f
movq (%rax), %rcx
movq 0x8(%rax), %rsi
movq %rsi, %r13
movq %rcx, %r14
subq %rcx, %rsi
sarq $0x2, %rsi
leaq 0xec(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0xd0(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movq %rsi, 0x30(%rsp)
callq 0x1e4b6
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %r15, %rsi
callq 0x1e640
leaq 0xf0(%rsp), %rdx
leaq 0xd(%rsp), %rcx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1e6d8
leaq 0xf0(%rsp), %rdi
callq 0x1e770
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x1bf91
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
cmpq %r14, %r13
je 0x1c1d6
movq 0x38(%rsp), %r13
cmpq $0x1, %r13
adcq $0x0, %r13
movq %r13, 0x38(%rsp)
movq 0x10(%rsp), %r8
cmpq $0x1, %r8
adcq $0x0, %r8
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq %rbp, 0xb0(%rsp)
cmpq $0x1, %r15
adcq $0x0, %r15
movq %r15, 0x80(%rsp)
movq 0x18(%rsp), %r15
cmpq $0x1, %r15
adcq $0x0, %r15
movq %r15, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq $0x1, %rax
adcq $0x0, %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movq %rbx, 0xb8(%rsp)
movq 0x58(%rsp), %rcx
cmpq %rcx, 0x60(%rsp)
je 0x1c1c8
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x98(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x68(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x70(%rsp), %rcx
cmpq %rcx, 0x78(%rsp)
je 0x1c1b0
movq 0x28(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x68(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0xa0(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x88(%rsp), %rcx
cmpq 0x90(%rsp), %rcx
je 0x1c168
xorl %r10d, %r10d
cmpq %rbx, 0xc0(%rsp)
je 0x1c14f
movq 0x40(%rsp), %rcx
movq (%rcx), %r11
movq 0x28(%rsp), %rcx
leaq (,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %rdx
movq 0xa8(%rsp), %rcx
movq (%rcx), %rcx
addq %rdx, %rcx
movq 0x20(%rsp), %rsi
addq (%rsi), %rdx
xorl %edi, %edi
movq %r12, %r9
cmpq %r12, 0xc8(%rsp)
je 0x1c144
leaq (,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%r11,%rsi), %rbx
movq (%rdx), %r14
movq 0x10(%rsp), %r15
leaq (%r15,%r15,2), %r13
movq (%r14,%r13,8), %rbp
movss (%rbp,%rax,4), %xmm0
movq (%rcx,%rsi), %rsi
addq 0x48(%rsp), %rsi
xorl %r13d, %r13d
movq 0x38(%rsp), %r14
movq (%rbx,%r13), %r15
leaq (%r10,%r10,2), %r12
movq (%r15,%r12,8), %r15
movss (%r15,%rax,4), %xmm1
movq (%rsi,%r13), %r15
mulss (%r15,%r10,4), %xmm1
addss %xmm1, %xmm0
movss %xmm0, (%rbp,%rax,4)
addq $0x18, %r13
decq %r14
jne 0x1c115
incq %rdi
cmpq %r8, %rdi
movq %r9, %r12
jne 0x1c0d1
incq %r10
cmpq 0xb0(%rsp), %r10
movq 0xb8(%rsp), %rbx
jne 0x1c092
movq 0x98(%rsp), %rcx
movss (%rcx,%rax,4), %xmm0
movq 0x10(%rsp), %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0xa0(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
addss (%rcx,%rax,4), %xmm0
movss %xmm0, (%rcx,%rax,4)
incq %rsi
addq $0x18, 0x48(%rsp)
movq %rsi, 0x10(%rsp)
cmpq 0x80(%rsp), %rsi
jne 0x1c079
movq 0x28(%rsp), %rdx
incq %rdx
movq %rdx, 0x28(%rsp)
cmpq 0x18(%rsp), %rdx
jne 0x1c03d
incq %rax
cmpq 0x30(%rsp), %rax
jne 0x1c007
movq 0x20(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x3e5f(%rip), %rsi      # 0x20060
movq %rax, %rdi
callq 0x20b0
jmp 0x1c2b5
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x3e3e(%rip), %rsi      # 0x20060
movq %rax, %rdi
callq 0x20b0
jmp 0x1c2b5
movq 0x94db2(%rip), %r14     # 0xb0fe8
leaq 0x3e3f(%rip), %rsi      # 0x2007c
movl $0x19, %edx
movq %rdi, %rbx
movq %r14, %rdi
callq 0x2200
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2140
movq %rax, %r14
leaq 0x3e34(%rip), %rsi      # 0x20096
movl $0x11, %edx
movq %rax, %rdi
callq 0x2200
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
imulq %rbx, %rsi
movq %r14, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20e0
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x3dfb(%rip), %rsi      # 0x200a8
movq %rax, %rdi
callq 0x20b0
movq 0x94d34(%rip), %rsi     # 0xb0ff0
movq 0x94ced(%rip), %rdx     # 0xb0fb0
movq %r14, %rdi
callq 0x22c0
jmp 0x1c2cf
jmp 0x1c2cf
movq %rax, %rbx
movq %r14, %rdi
callq 0x2180
jmp 0x1c313
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x1e770
jmp 0x1c2f1
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x1c313
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
jmp 0x1c313
movq %rax, %rbx
movq %rbx, %rdi
callq 0x22d0