// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
leaq 0x60(%rsp), %rbx
movl 0x84(%rbx), %ebp
movl 0x8c(%rbx), %ecx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, (%rsp)
movl 0x90(%rbx), %eax
imull %eax, %eax
imull 0x74(%rbx), %eax
cltd
idivl %ecx
movl %eax, %r15d
movl 0x78(%rbx), %r14d
imull 0x7c(%rbx), %r14d
movq 0x268(%rbx), %rdi
movl 0x4(%rbx), %edx
movl 0x48(%rbx), %eax
movl 0x58(%rbx), %esi
movl %eax, %r12d
imull %eax, %esi
movq 0x260(%rbx), %r13
movq %r13, %rcx
callq 0x1a748
cmpl $0x0, 0x40(%rbx)
je 0x66a7
subq $0x5b0, %rsp            # imm = 0x5B0
movl $0x94, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
pushq $0x22
popq %rcx
leaq 0xab0(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2fe3b
addq $0x5b0, %rsp            # imm = 0x5B0
jmp 0x66be
movq 0x238(%rbx), %rdi
movq %r13, %rsi
movl %r12d, %edx
movl %ebp, %ecx
movl %r14d, %r8d
callq 0x6dbf
movl (%rsp), %eax
imull %r14d, %eax
movl %eax, 0x4(%rsp)
xorl %ebp, %ebp
leaq 0x500(%rsp), %r9
movq %r15, 0x10(%rsp)
movq %r14, 0x20(%rsp)
cmpl 0x48(%rbx), %ebp
jge 0x68ea
xorl %r13d, %r13d
movl %ebp, 0x8(%rsp)
movl 0x8c(%rbx), %edi
cmpl %edi, %r13d
jge 0x68e3
movl %ebp, %ecx
imull %edi, %ecx
addl %r13d, %ecx
movl 0x4(%rsp), %eax
imull %ecx, %eax
movl %r13d, %edx
movslq %eax, %r13
shlq $0x2, %r13
addq 0x260(%rbx), %r13
movl 0x5c(%rbx), %eax
movl %edx, 0xc(%rsp)
imull %edx, %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, %r12
shlq $0x2, %r12
movq 0xd8(%r9), %r10
addq %r12, %r10
addq 0xe8(%r9), %r12
movl 0x90(%rbx), %r8d
cmpl $0x1, %r8d
jne 0x677c
movq %r9, %rbp
jmp 0x67ba
movq 0xf0(%r9), %r15
movl %esi, %eax
cltd
idivl %edi
movq %r9, %rbp
movl 0x98(%rbx), %r9d
movl 0xa8(%rbx), %edx
movq %rdx, 0x18(%rsp)
movq %r10, %rdi
movl %eax, %esi
movl %r11d, %edx
pushq %r15
pushq 0x20(%rsp)
callq 0x1a7d7
popq %rax
popq %rcx
movq %r15, %r10
movq 0x10(%rsp), %r15
subq $0x8, %rsp
xorl %edi, %edi
pushq $0x1
popq %rsi
movl 0x8(%rsp), %edx
movl %r15d, %ecx
movq 0x28(%rsp), %r8
movss 0x45831(%rip), %xmm0    # 0x4c008
movq %r13, %r9
movaps %xmm0, %xmm1
pushq %r15
pushq %r14
movq %r8, %r14
pushq %r8
pushq %r10
pushq %r8
callq 0x3954
addq $0x30, %rsp
cmpq $0x0, 0xe8(%rbp)
movq %rbp, %r9
movl 0x8(%rsp), %ebp
movl 0xc(%rsp), %r13d
je 0x68db
movl 0x5c(%rbx), %eax
imull %r13d, %eax
movl 0x8c(%rbx), %ecx
movl 0x90(%rbx), %esi
cltd
idivl %ecx
movq %r12, %r10
cmpl $0x1, %esi
je 0x6832
movq 0xf0(%r9), %r10
movslq %eax, %r9
shlq $0x2, %r9
addq 0x250(%rbx), %r9
imull %ebp, %ecx
addl %r13d, %ecx
imull 0x4(%rsp), %ecx
movslq %ecx, %rax
shlq $0x2, %rax
addq 0x260(%rbx), %rax
subq $0x8, %rsp
xorps %xmm1, %xmm1
pushq $0x1
popq %rdi
xorl %esi, %esi
movl %r15d, %edx
movl %r14d, %ecx
movl 0x8(%rsp), %r8d
movss 0x45790(%rip), %xmm0    # 0x4c008
pushq %r14
pushq %r10
pushq %r14
pushq %rax
pushq %r15
callq 0x3954
leaq 0x530(%rsp), %r9
addq $0x30, %rsp
movl 0x90(%rbx), %r8d
cmpl $0x1, %r8d
je 0x68db
movq 0xf0(%r9), %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 %r12
pushq %r10
callq 0x1a969
leaq 0x510(%rsp), %r9
addq $0x10, %rsp
incl %r13d
jmp 0x66ed
incl %ebp
jmp 0x66dd
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq