// File: /workspace/github_c_makefile_star2_O1/SimpleDoger[P]Darknet_CPU_Only/convolutional_layer.c
// Function: backward_convolutional_layer
void backward_convolutional_layer(convolutional_layer l, network net)
{
    int i, j;
    int m = l.n/l.groups;
    int n = l.size*l.size*l.c/l.groups;
    int k = l.out_w*l.out_h;

    gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);

    if(l.batch_normalize){
        backward_batchnorm_layer(l, net);
    } else {
        backward_bias(l.bias_updates, l.delta, l.batch, l.n, k);
    }

    for(i = 0; i < l.batch; ++i){
        for(j = 0; j < l.groups; ++j){
            float *a = l.delta + (i*l.groups + j)*m*k;
            float *b = net.workspace;
            float *c = l.weight_updates + j*l.nweights/l.groups;

            float *im  = net.input + (i*l.groups + j)*l.c/l.groups*l.h*l.w;
            float *imd = net.delta + (i*l.groups + j)*l.c/l.groups*l.h*l.w;

            if(l.size == 1){
                b = im;
            } else {
                im2col_cpu(im, l.c/l.groups, l.h, l.w, 
                        l.size, l.stride, l.pad, b);
            }

            gemm(0,1,m,n,k,1,a,k,b,k,1,c,n);

            if (net.delta) {
                a = l.weights + j*l.nweights/l.groups;
                b = l.delta + (i*l.groups + j)*m*k;
                c = net.workspace;
                if (l.size == 1) {
                    c = imd;
                }

                gemm(1,0,n,k,m,1,a,n,b,k,0,c,k);

                if (l.size != 1) {
                    col2im_cpu(net.workspace, l.c/l.groups, l.h, l.w, l.size, l.stride, l.pad, imd);
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x70(%rsp), %rbx
movl 0x84(%rbx), %eax
movl 0x8c(%rbx), %ecx
cltd
idivl %ecx
movl %eax, 0x10(%rsp)
movl 0x90(%rbx), %eax
imull %eax, %eax
imull 0x74(%rbx), %eax
cltd
idivl %ecx
movq %rax, 0x18(%rsp)
movl 0x78(%rbx), %r14d
imull 0x7c(%rbx), %r14d
leaq 0x510(%rsp), %r12
movq 0x268(%rbx), %rdi
movl 0x4(%rbx), %edx
movl 0x48(%rbx), %esi
imull 0x58(%rbx), %esi
movq 0x260(%rbx), %rcx
callq 0x27834
cmpl $0x0, 0x40(%rbx)
je 0x1034f
subq $0x5b0, %rsp            # imm = 0x5B0
movl $0x94, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
movl $0x22, %ecx
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x3a570
addq $0x5b0, %rsp            # imm = 0x5B0
jmp 0x1036e
movq 0x238(%rbx), %rdi
movq 0x260(%rbx), %rsi
movl 0x48(%rbx), %edx
movl 0x84(%rbx), %ecx
movl %r14d, %r8d
callq 0x10a7a
cmpl $0x0, 0x48(%rbx)
leaq 0x510(%rsp), %r15
jle 0x105a3
movl 0x10(%rsp), %eax
imull %r14d, %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0xc(%rsp)
movq %r14, 0x30(%rsp)
movl 0x8c(%rbx), %edi
testl %edi, %edi
jle 0x10590
xorl %r12d, %r12d
movl 0xc(%rsp), %ecx
imull %edi, %ecx
addl %r12d, %ecx
movl 0x14(%rsp), %eax
imull %ecx, %eax
movslq %eax, %r13
shlq $0x2, %r13
addq 0x260(%rbx), %r13
movl 0x5c(%rbx), %eax
imull %r12d, %eax
cltd
idivl %edi
movslq %eax, %r14
shlq $0x2, %r14
addq 0x258(%rbx), %r14
movl 0x74(%rbx), %esi
imull %esi, %ecx
movl %ecx, %eax
cltd
idivl %edi
movl 0x6c(%rbx), %r11d
movl 0x70(%rbx), %ecx
movl %r11d, %edx
imull %ecx, %edx
imull %eax, %edx
movslq %edx, %rbp
shlq $0x2, %rbp
movq 0xd8(%r15), %r10
addq %rbp, %r10
addq 0xe8(%r15), %rbp
movl 0x90(%rbx), %r8d
cmpl $0x1, %r8d
je 0x10462
movq %rbp, 0x28(%rsp)
movq 0xf0(%r15), %rbp
movl %esi, %eax
cltd
idivl %edi
movl 0x98(%rbx), %r9d
movl 0xa8(%rbx), %edx
movq %rdx, 0x20(%rsp)
movq %r10, %rdi
movl %eax, %esi
movl %r11d, %edx
pushq %rbp
pushq 0x28(%rsp)
callq 0x278bf
addq $0x10, %rsp
movq %rbp, %r10
movq 0x28(%rsp), %rbp
subq $0x8, %rsp
xorl %edi, %edi
movl $0x1, %esi
movl 0x18(%rsp), %edx
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %r8
movss 0x42b85(%rip), %xmm0    # 0x53008
movq %r13, %r9
movaps %xmm0, %xmm1
pushq %rcx
pushq %r14
movq %r8, %r14
pushq %r8
pushq %r10
pushq %r8
callq 0xdd4d
addq $0x30, %rsp
cmpq $0x0, 0xe8(%r15)
je 0x1057e
movl 0x5c(%rbx), %eax
imull %r12d, %eax
movl 0x8c(%rbx), %ecx
movl 0x90(%rbx), %esi
cltd
idivl %ecx
movq %rbp, %r10
cmpl $0x1, %esi
je 0x104d1
movq 0xf0(%r15), %r10
movslq %eax, %r9
shlq $0x2, %r9
addq 0x250(%rbx), %r9
imull 0xc(%rsp), %ecx
addl %r12d, %ecx
imull 0x14(%rsp), %ecx
movslq %ecx, %rax
shlq $0x2, %rax
addq 0x260(%rbx), %rax
subq $0x8, %rsp
xorps %xmm1, %xmm1
movl $0x1, %edi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
movl 0x18(%rsp), %r8d
movss 0x42aeb(%rip), %xmm0    # 0x53008
pushq %r14
pushq %r10
pushq %r14
pushq %rax
pushq %rdx
callq 0xdd4d
leaq 0x540(%rsp), %r15
addq $0x30, %rsp
movl 0x90(%rbx), %r8d
cmpl $0x1, %r8d
je 0x1057e
movq 0xf0(%r15), %rdi
movl 0x74(%rbx), %eax
cltd
idivl 0x8c(%rbx)
movl 0x6c(%rbx), %edx
movl 0x70(%rbx), %ecx
movl 0x98(%rbx), %r9d
movl 0xa8(%rbx), %r10d
movl %eax, %esi
pushq %rbp
pushq %r10
callq 0x27a81
leaq 0x520(%rsp), %r15
addq $0x10, %rsp
incl %r12d
movl 0x8c(%rbx), %edi
cmpl %edi, %r12d
jl 0x103aa
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0xc(%rsp)
cmpl 0x48(%rbx), %eax
jl 0x10399
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq