// File: /workspace/github_c_makefile_star2_O3/SimpleDoger[P]Darknet_CPU_Only/deconvolutional_layer.c
// Function: make_deconvolutional_layer
layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam)
{
    int i;
    layer l = {0};
    l.type = DECONVOLUTIONAL;

    l.h = h;
    l.w = w;
    l.c = c;
    l.n = n;
    l.batch = batch;
    l.stride = stride;
    l.size = size;

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

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

    l.biases = calloc(n, sizeof(float));
    l.bias_updates = calloc(n, sizeof(float));
    //float scale = n/(size*size*c);
    //printf("scale: %f\n", scale);
    float scale = .02;
    for(i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_normal();
    //bilinear_init(l);
    for(i = 0; i < n; ++i){
        l.biases[i] = 0;
    }
    l.pad = padding;

    l.out_h = (l.h - 1) * l.stride + l.size - 2*l.pad;
    l.out_w = (l.w - 1) * l.stride + l.size - 2*l.pad;
    l.out_c = n;
    l.outputs = l.out_w * l.out_h * l.out_c;
    l.inputs = l.w * l.h * l.c;

    scal_cpu(l.nweights, (float)l.out_w*l.out_h/(l.w*l.h), l.weights, 1);

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

    l.forward = forward_deconvolutional_layer;
    l.backward = backward_deconvolutional_layer;
    l.update = update_deconvolutional_layer;

    l.batch_normalize = batch_normalize;

    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(c*n*size*size, sizeof(float));
        l.v = calloc(c*n*size*size, 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.activation = activation;
    l.workspace_size = get_workspace_size(l);

    fprintf(stderr, "deconv%5d %2d x%2d /%2d  %4d x%4d x%4d   ->  %4d x%4d x%4d\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);

    return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %r13d
movl %r8d, %r12d
movl %ecx, %r14d
movl %edx, %r15d
movl %esi, 0x8(%rsp)
movq %rdi, %rbx
movl 0x60(%rsp), %ebp
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
movl $0x1, (%rbx)
movq %r15, 0x18(%rsp)
movl %r15d, 0x6c(%rbx)
movq %r14, 0x20(%rsp)
movl %r14d, 0x70(%rbx)
movl %r12d, 0x74(%rbx)
movl %r13d, 0x84(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x48(%rbx)
movl 0x68(%rsp), %eax
movl %eax, 0x98(%rbx)
movl %ebp, 0x90(%rbx)
movl %r13d, %eax
movq %r12, 0x8(%rsp)
imull %r12d, %eax
imull %ebp, %eax
imull %ebp, %eax
movl %eax, 0x5c(%rbx)
movl %r13d, 0x60(%rbx)
movslq %eax, %r15
pushq $0x4
popq %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, %r12
movq %rax, 0x250(%rbx)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movslq %r13d, %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x230(%rbx)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
testl %r15d, %r15d
jle 0x36e0
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xfb17
mulss 0x51ac5(%rip), %xmm0    # 0x55190
movq 0x250(%rbx), %r12
movss %xmm0, (%r12,%rbp,4)
incq %rbp
cmpq %rbp, %r15
jne 0x36bc
movl 0x70(%rsp), %ebp
movl %r13d, %eax
movq %rax, 0x10(%rsp)
testl %r13d, %r13d
jle 0x370c
movq 0x230(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq (,%rax,4), %rdx
xorl %esi, %esi
callq 0x31c0
movl %ebp, 0xa8(%rbx)
movl 0x5c(%rbx), %edi
movl 0x6c(%rbx), %ecx
leal -0x1(%rcx), %eax
movl 0x98(%rbx), %edx
imull %edx, %eax
movl 0x90(%rbx), %esi
addl %ebp, %ebp
subl %ebp, %esi
addl %esi, %eax
movl %eax, 0x78(%rbx)
movl 0x70(%rbx), %r8d
leal -0x1(%r8), %r9d
imull %edx, %r9d
addl %esi, %r9d
movl %r9d, 0x7c(%rbx)
movl %r13d, 0x80(%rbx)
cvtsi2ss %eax, %xmm1
imull %r13d, %eax
imull %ecx, %r8d
cvtsi2ss %r8d, %xmm2
imull %r9d, %eax
movl %eax, 0x58(%rbx)
imull 0x74(%rbx), %r8d
cvtsi2ss %r9d, %xmm0
movl %r8d, 0x54(%rbx)
mulss %xmm1, %xmm0
divss %xmm2, %xmm0
pushq $0x1
popq %rdx
movq %r12, %rsi
callq 0x29d61
movslq 0x48(%rbx), %rax
movslq 0x58(%rbx), %rbp
imulq %rax, %rbp
pushq $0x4
popq %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
leaq 0xc670(%rip), %rax      # 0xfe31
movq %rax, 0x10(%rbx)
leaq 0xc8b7(%rip), %rax      # 0x10083
movq %rax, 0x18(%rbx)
leaq 0xcab4(%rip), %rax      # 0x1028b
movq %rax, 0x20(%rbx)
movl 0x80(%rsp), %eax
movl %eax, 0x40(%rbx)
testl %eax, %eax
je 0x39b3
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x3250
movq %rax, %r12
movq %rax, 0x240(%rbx)
movq %r14, %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
testl %r13d, %r13d
jle 0x391f
leal 0x3(%r13), %eax
movq 0x10(%rsp), %rcx
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x5192e(%rip), %xmm1    # 0x55170
movdqa 0x51836(%rip), %xmm2    # 0x55080
xorl %ecx, %ecx
movdqa 0x5183c(%rip), %xmm3    # 0x55090
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x5191c(%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 0x38a0
movl $0x3f800000, (%r12,%rcx) # imm = 0x3F800000
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x38b7
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 0x38f7
movl $0x3f800000, 0x8(%r12,%rcx) # imm = 0x3F800000
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x390a
movl $0x3f800000, 0xc(%r12,%rcx) # imm = 0x3F800000
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x3864
pushq $0x4
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x290(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x298(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2a0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2a8(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2b0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2b8(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
movl 0x78(%rsp), %ebp
cmpl $0x0, 0x88(%rsp)
je 0x3a31
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 %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2e0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2f0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2e8(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2f8(%rbx)
movl %ebp, 0x4(%rbx)
movslq 0x6c(%rbx), %rax
movslq 0x70(%rbx), %rcx
imulq %rax, %rcx
movslq 0x90(%rbx), %rax
movslq 0x84(%rbx), %rdx
imulq %rax, %rax
imulq %rcx, %rax
imulq %rdx, %rax
shlq $0x2, %rax
movq %rax, 0x498(%rbx)
movq 0x6858c(%rip), %rax     # 0x6bff8
movq (%rax), %rdi
movl 0x78(%rbx), %r10d
movl 0x7c(%rbx), %r11d
movl 0x80(%rbx), %r14d
leaq 0x5170f(%rip), %rsi     # 0x55194
movl %r13d, %edx
movl 0x60(%rsp), %ecx
movl %ecx, %r8d
movl 0x68(%rsp), %r9d
xorl %eax, %eax
pushq %r14
pushq %r10
pushq %r11
pushq 0x20(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x3270
addq $0x30, %rsp
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq