// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %rcx, 0x40(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdx), %rdi
movq %rdx, 0x30(%rsp)
movq 0x8(%rdx), %rax
subq %rdi, %rax
pushq $0x18
popq %rbx
cqto
idivq %rbx
movq %rax, %r9
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
subq %rcx, %rax
cqto
idivq %rbx
movq %rax, %rbp
movq (%rsi), %r8
movq %rsi, 0x60(%rsp)
movq 0x8(%rsi), %rax
subq %r8, %rax
cqto
idivq %rbx
movq %r9, 0x68(%rsp)
leaq -0x1(%r9), %rdx
subq %rdx, %rax
movq %rax, 0x18(%rsp)
jb 0x1b26f
movq (%r8), %rdi
movq 0x8(%r8), %rax
subq %rdi, %rax
cqto
idivq %rbx
leaq -0x1(%rbp), %rdx
subq %rdx, %rax
movq %rax, 0x20(%rsp)
jb 0x1b28e
movq (%rcx), %r9
movq 0x8(%rdi), %r8
subq (%rdi), %r8
sarq $0x2, %r8
movq 0x8(%rcx), %rax
subq %r9, %rax
cqto
idivq %rbx
movq %r8, 0x28(%rsp)
cmpq %rax, %r8
jne 0x1b2aa
movq 0x8(%r9), %rsi
subq (%r9), %rsi
sarq $0x2, %rsi
leaq 0x74(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x78(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movq %rsi, 0x38(%rsp)
callq 0x1ca80
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq 0x20(%rsp), %rsi
callq 0x1cad6
leaq 0x90(%rsp), %rdx
leaq 0xd(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1cb2c
leaq 0x90(%rsp), %rdi
callq 0x1cb82
leaq 0x78(%rsp), %rdi
callq 0x1cef2
xorl %eax, %eax
cmpq 0x38(%rsp), %rax
je 0x1b258
xorl %edx, %edx
cmpq 0x18(%rsp), %rdx
je 0x1b250
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x58(%rsp)
movq %rdx, 0x48(%rsp)
imulq $0x18, %rdx, %rsi
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
addq %rsi, %rcx
movq %rcx, 0x50(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq 0x20(%rsp), %r9
je 0x1b243
xorl %r10d, %r10d
cmpq 0x28(%rsp), %r10
je 0x1b213
xorl %r11d, %r11d
cmpq 0x68(%rsp), %r11
je 0x1b20b
imulq $0x18, %r11, %rdi
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
addq %rdi, %rcx
movq 0x60(%rsp), %rdx
movq (%rdx), %rdx
addq %rsi, %rdx
addq %rdi, %rdx
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
addq %rsi, %rdi
xorl %r14d, %r14d
movq %rbp, %rbx
subq $0x1, %rbp
jb 0x1b200
movq (%rcx), %r15
movq (%r15,%r14), %r15
imulq $0x18, %r10, %r12
movq (%r15,%r12), %r15
movss (%r15,%rax,4), %xmm0
movq (%rdx), %r15
addq %r8, %r15
movq (%r14,%r15), %r15
movq (%rdi), %r12
imulq $0x18, %r9, %r13
movq (%r12,%r13), %r12
mulss (%r15,%r10,4), %xmm0
addss (%r12,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
addq $0x18, %r14
jmp 0x1b1b8
incq %r11
movq %rbx, %rbp
jmp 0x1b17f
incq %r10
jmp 0x1b171
movq 0x58(%rsp), %rcx
movss (%rcx,%rax,4), %xmm0
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
imulq $0x18, %r9, %rdx
movq (%rcx,%rdx), %rcx
addss (%rcx,%rax,4), %xmm0
movss %xmm0, (%rcx,%rax,4)
incq %r9
addq $0x18, %r8
jmp 0x1b163
movq 0x48(%rsp), %rdx
incq %rdx
jmp 0x1b12c
incq %rax
jmp 0x1b11f
movq 0x10(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r14
leaq 0x3db7(%rip), %rsi      # 0x1f038
movq %rax, %rdi
callq 0x2090
jmp 0x1b31d
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r14
leaq 0x3d98(%rip), %rsi      # 0x1f038
movq %rax, %rdi
callq 0x2090
jmp 0x1b31d
movq 0x96d37(%rip), %rdi     # 0xb1fe8
leaq 0x3d9c(%rip), %rsi      # 0x1f054
callq 0x21c0
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x2130
leaq 0x3d9d(%rip), %rsi      # 0x1f06e
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
idivq %rbx
movq %rax, %rsi
callq 0x2130
movq %rax, %rdi
callq 0x20d0
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r14
leaq 0x3d6b(%rip), %rsi      # 0x1f080
movq %rax, %rdi
callq 0x2090
movq 0x96ccc(%rip), %rsi     # 0xb1ff0
movq 0x96c85(%rip), %rdx     # 0xb1fb0
movq %r14, %rdi
callq 0x22c0
jmp 0x1b337
jmp 0x1b337
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0x1b368
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1cb82
jmp 0x1b359
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1cef2
jmp 0x1b368
movq %rax, %rbx
movq %rbx, %rdi
callq 0x22d0