// File: /workspace/github_c_makefile_star2_O3/SimpleDoger[P]Darknet_CPU_Only/convolutional_layer.c
// Function: make_convolutional_layer
convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam)
{
    int i;
    convolutional_layer l = {0};
    l.type = CONVOLUTIONAL;

    l.groups = groups;
    l.h = h;
    l.w = w;
    l.c = c;
    l.n = n;
    l.binary = binary;
    l.xnor = xnor;
    l.batch = batch;
    l.stride = stride;
    l.size = size;
    l.pad = padding;
    l.batch_normalize = batch_normalize;

    l.weights = calloc(c/groups*n*size*size, sizeof(float));
    l.weight_updates = calloc(c/groups*n*size*size, sizeof(float));

    l.biases = calloc(n, sizeof(float));
    l.bias_updates = calloc(n, sizeof(float));

    l.nweights = c/groups*n*size*size;
    l.nbiases = n;

    // float scale = 1./sqrt(size*size*c);
    float scale = sqrt(2./(size*size*c/l.groups));
    //printf("convscale %f\n", scale);
    //scale = .02;
    //for(i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_uniform(-1, 1);
    for(i = 0; i < l.nweights; ++i) l.weights[i] = scale*rand_normal();
    int out_w = convolutional_out_width(l);
    int out_h = convolutional_out_height(l);
    l.out_h = out_h;
    l.out_w = out_w;
    l.out_c = n;
    l.outputs = l.out_h * l.out_w * l.out_c;
    l.inputs = l.w * l.h * l.c;

    l.output = calloc(l.batch*l.outputs, sizeof(float));
    l.delta  = calloc(l.batch*l.outputs, sizeof(float));

    l.forward = forward_convolutional_layer;
    l.backward = backward_convolutional_layer;
    l.update = update_convolutional_layer;
    if(binary){
        l.binary_weights = calloc(l.nweights, sizeof(float));
        l.cweights = calloc(l.nweights, sizeof(char));
        l.scales = calloc(n, sizeof(float));
    }
    if(xnor){
        l.binary_weights = calloc(l.nweights, sizeof(float));
        l.binary_input = calloc(l.inputs*l.batch, sizeof(float));
    }

    if(batch_normalize){
        l.scales = calloc(n, sizeof(float));
        l.scale_updates = calloc(n, sizeof(float));
        for(i = 0; i < n; ++i){
            l.scales[i] = 1;
        }

        l.mean = calloc(n, sizeof(float));
        l.variance = calloc(n, sizeof(float));

        l.mean_delta = calloc(n, sizeof(float));
        l.variance_delta = calloc(n, sizeof(float));

        l.rolling_mean = calloc(n, sizeof(float));
        l.rolling_variance = calloc(n, sizeof(float));
        l.x = calloc(l.batch*l.outputs, sizeof(float));
        l.x_norm = calloc(l.batch*l.outputs, sizeof(float));
    }
    if(adam){
        l.m = calloc(l.nweights, sizeof(float));
        l.v = calloc(l.nweights, sizeof(float));
        l.bias_m = calloc(n, sizeof(float));
        l.scale_m = calloc(n, sizeof(float));
        l.bias_v = calloc(n, sizeof(float));
        l.scale_v = calloc(n, sizeof(float));
    }

    l.workspace_size = get_workspace_size(l);
    l.activation = activation;

    fprintf(stderr, "conv  %5d %2d x%2d /%2d  %4d x%4d x%4d   ->  %4d x%4d x%4d  %5.3f BFLOPs\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c, (2.0 * l.n * l.size*l.size*l.c/l.groups * l.out_h*l.out_w)/1000000000.);

    return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %ebp
movl %r8d, %r13d
movl %ecx, %r15d
movl %edx, %r12d
movl %esi, %r14d
movq %rdi, %rbx
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
movl 0x90(%rsp), %ecx
movl %ecx, 0x8c(%rbx)
movq %r12, 0x48(%rsp)
movl %r12d, 0x6c(%rbx)
movq %r15, 0x50(%rsp)
movl %r15d, 0x70(%rbx)
movl %r13d, 0x74(%rbx)
movl %ebp, 0x84(%rbx)
movl 0xc0(%rsp), %eax
movl %eax, 0xb8(%rbx)
movl 0xc8(%rsp), %eax
movl %eax, 0xbc(%rbx)
movl %r14d, 0x48(%rbx)
movl 0xa0(%rsp), %eax
movl %eax, 0x98(%rbx)
movl 0x98(%rsp), %r14d
movl %r14d, 0x90(%rbx)
movl 0xa8(%rsp), %eax
movl %eax, 0xa8(%rbx)
movl 0xb8(%rsp), %eax
movl %eax, 0x40(%rbx)
movl %r13d, %eax
cltd
idivl %ecx
imull %r14d, %r14d
movl %r14d, %ecx
imull %ebp, %ecx
imull %eax, %ecx
movslq %ecx, %r15
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x250(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movq %rbp, 0x8(%rsp)
movslq %ebp, %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x230(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
movl %r15d, 0x5c(%rbx)
movl %ebp, 0x60(%rbx)
movq %r13, 0x40(%rsp)
imull %r13d, %r14d
movl %r14d, %eax
cltd
idivl 0x90(%rsp)
cvtsi2sd %eax, %xmm1
movsd 0x515de(%rip), %xmm0    # 0x551d8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x3c0e
sqrtsd %xmm0, %xmm0
jmp 0x3c13
callq 0x3460
movq %rbp, 0x20(%rsp)
movl 0x5c(%rbx), %r14d
testl %r14d, %r14d
jle 0x3c54
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xfb17
mulss 0x10(%rsp), %xmm0
movq 0x250(%rbx), %rax
movss %xmm0, (%rax,%r15,4)
incq %r15
movslq 0x5c(%rbx), %r14
cmpq %r14, %r15
jl 0x3c2e
movl 0x70(%rbx), %esi
movl 0xa8(%rbx), %ecx
leal (%rsi,%rcx,2), %eax
addl %ecx, %ecx
movslq 0x90(%rbx), %r10
subl %r10d, %eax
movl 0x98(%rbx), %edi
cltd
idivl %edi
movl %eax, %r9d
incl %r9d
movl 0x48(%rbx), %r13d
movl 0x6c(%rbx), %r8d
movq %r10, 0x30(%rsp)
subl %r10d, %ecx
addl %r8d, %ecx
movl %ecx, %eax
cltd
idivl %edi
incl %eax
movl %eax, 0x78(%rbx)
movl %r9d, 0x7c(%rbx)
movq 0x8(%rsp), %rcx
movl %ecx, 0x80(%rbx)
movq %r9, 0x10(%rsp)
movl %r9d, %edx
imull %ecx, %edx
movq %rax, 0x38(%rsp)
imull %eax, %edx
movl %edx, 0x58(%rbx)
imull %esi, %r8d
movslq 0x74(%rbx), %rax
movq %rax, 0x28(%rsp)
movl %eax, %r15d
imull %r8d, %r15d
movl %r15d, 0x54(%rbx)
imull %r13d, %edx
movslq %edx, %rbp
pushq $0x4
popq %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
leaq 0xcb65(%rip), %rax      # 0x10875
movq %rax, 0x10(%rbx)
leaq 0xcf0f(%rip), %rax      # 0x10c2a
movq %rax, 0x18(%rbx)
leaq 0xd214(%rip), %rax      # 0x10f3a
movq %rax, 0x20(%rbx)
movslq %r14d, %r14
cmpl $0x0, 0xc0(%rsp)
movq 0x20(%rsp), %rbp
je 0x3d76
pushq $0x4
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x228(%rbx)
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x3250
movq %rax, 0x198(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x240(%rbx)
cmpl $0x0, 0xc8(%rsp)
je 0x3dac
pushq $0x4
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x228(%rbx)
imull %r15d, %r13d
movslq %r13d, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x388(%rbx)
movq %r14, %r15
movl 0xd0(%rsp), %eax
cmpl $0x0, 0xb8(%rsp)
je 0x3f9d
pushq $0x4
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, %r12
movq %rax, 0x240(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
cmpl $0x0, 0x8(%rsp)
jle 0x3efd
movq 0x8(%rsp), %rax
movl %eax, %ecx
addl $0x3, %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x51350(%rip), %xmm1    # 0x55170
movdqa 0x51258(%rip), %xmm2    # 0x55080
xorl %ecx, %ecx
movdqa 0x5125e(%rip), %xmm3    # 0x55090
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x5133e(%rip), %xmm5    # 0x55180
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x3e7e
movl $0x3f800000, (%r12,%rcx) # imm = 0x3F800000
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x3e95
movl $0x3f800000, 0x4(%r12,%rcx) # imm = 0x3F800000
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0x3ed5
movl $0x3f800000, 0x8(%r12,%rcx) # imm = 0x3F800000
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x3ee8
movl $0x3f800000, 0xc(%r12,%rcx) # imm = 0x3F800000
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x3e42
pushq $0x4
popq %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x290(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x298(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2a0(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2a8(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2b0(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2b8(%rbx)
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
movl 0xd0(%rsp), %eax
movl 0xb0(%rsp), %r14d
testl %eax, %eax
je 0x4019
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2d0(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2d8(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2e0(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2f0(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2e8(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2f8(%rbx)
movq 0x38(%rsp), %r11
movslq %r11d, %rax
movq 0x10(%rsp), %r10
movslq %r10d, %rcx
movq 0x30(%rsp), %rsi
movq %rsi, %rdx
imulq %rsi, %rdx
imulq %rcx, %rdx
movq 0x28(%rsp), %rdi
imulq %rdi, %rax
imulq %rdx, %rax
movslq 0x8c(%rbx), %rcx
xorl %edx, %edx
divq %rcx
shlq $0x2, %rax
movq %rax, 0x498(%rbx)
movl %r14d, 0x4(%rbx)
cvtsi2sdl 0x84(%rbx), %xmm0
addsd %xmm0, %xmm0
cvtsi2sd %esi, %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
cvtsi2sd %edi, %xmm1
cvtsi2sd %ecx, %xmm2
mulsd %xmm0, %xmm1
divsd %xmm2, %xmm1
cvtsi2sd %r11d, %xmm2
movq 0x67f63(%rip), %rax     # 0x6bff8
mulsd %xmm1, %xmm2
cvtsi2sd %r10d, %xmm0
movq (%rax), %rdi
mulsd %xmm2, %xmm0
divsd 0x51133(%rip), %xmm0    # 0x551e0
leaq 0x51134(%rip), %rsi     # 0x551e8
movq 0x8(%rsp), %rdx
movl 0x98(%rsp), %ecx
movl %ecx, %r8d
movl 0xa0(%rsp), %r9d
movb $0x1, %al
pushq %rdx
pushq %r11
pushq %r10
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
callq 0x3270
addq $0x30, %rsp
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq