// File: /workspace/github_c_makefile_star2_O2/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 0x462a3(%rip), %xmm0    # 0x4c1a8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5f19
sqrtsd %xmm0, %xmm0
jmp 0x5f1e
callq 0x3460
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
movslq 0x5c(%rbx), %r12
cmpq %r12, %r14
jge 0x5f53
xorl %eax, %eax
callq 0x4cf7
mulss 0x10(%rsp), %xmm0
movq 0x250(%rbx), %rax
movss %xmm0, (%rax,%r14,4)
incq %r14
jmp 0x5f2b
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), %r11d
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)
movl %r11d, 0x1c(%rsp)
imull %r11d, %edx
movq %rbp, %r14
movslq %edx, %rbp
pushq $0x4
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq %r14, %rbp
movq %r13, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
leaq 0x2ee(%rip), %rax       # 0x6308
movq %rax, 0x10(%rbx)
leaq 0x5db(%rip), %rax       # 0x6600
movq %rax, 0x18(%rbx)
leaq 0x8c9(%rip), %rax       # 0x68f9
movq %rax, 0x20(%rbx)
cmpl $0x0, 0xc0(%rsp)
je 0x6078
pushq $0x4
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x228(%rbx)
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x3250
movq %rax, 0x198(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x240(%rbx)
cmpl $0x0, 0xc8(%rsp)
je 0x60b2
pushq $0x4
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x228(%rbx)
movl 0x1c(%rsp), %eax
imull %r15d, %eax
movslq %eax, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x388(%rbx)
cmpl $0x0, 0xb8(%rsp)
je 0x61a8
pushq $0x4
popq %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, %r13
movq %rax, 0x240(%rbx)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
xorl %eax, %eax
movq 0x8(%rsp), %rdx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
cmpq %rax, %rcx
je 0x610f
movl $0x3f800000, (%r13,%rax,4) # imm = 0x3F800000
incq %rax
jmp 0x60fc
pushq $0x4
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x290(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x298(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2a0(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2a8(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2b0(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2b8(%rbx)
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
cmpl $0x0, 0xd0(%rsp)
je 0x6222
pushq $0x4
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2d0(%rbx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2d8(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2e0(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2f0(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2e8(%rbx)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3250
movq %rax, 0x2f8(%rbx)
movq 0x38(%rsp), %r11
movslq %r11d, %rax
movq 0x10(%rsp), %r10
movslq %r10d, %rcx
imulq %rax, %rcx
movq 0x30(%rsp), %rsi
movq %rsi, %rax
imulq %rsi, %rax
imulq %rcx, %rax
movq 0x28(%rsp), %rdi
imulq %rdi, %rax
movslq 0x8c(%rbx), %rcx
xorl %edx, %edx
divq %rcx
shlq $0x2, %rax
movq %rax, 0x498(%rbx)
movl 0xb0(%rsp), %eax
movl %eax, 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 0x5dd54(%rip), %rax     # 0x63ff8
mulsd %xmm1, %xmm2
cvtsi2sd %r10d, %xmm0
movq (%rax), %rdi
mulsd %xmm2, %xmm0
divsd 0x45ef4(%rip), %xmm0    # 0x4c1b0
leaq 0x45ef5(%rip), %rsi     # 0x4c1b8
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