// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movl %r8d, %r13d
movl %ecx, %r15d
movl %edx, %r12d
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movl 0x60(%rsp), %r14d
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31d0
movl $0x1, (%rbx)
movq %r12, 0x18(%rsp)
movl %r12d, 0x6c(%rbx)
movq %r15, 0x20(%rsp)
movl %r15d, 0x70(%rbx)
movl %r13d, 0x74(%rbx)
movl %ebp, 0x84(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x48(%rbx)
movl 0x68(%rsp), %eax
movl %eax, 0x98(%rbx)
movl %r14d, 0x90(%rbx)
movl %ebp, %eax
movq %r13, 0x10(%rsp)
imull %r13d, %eax
movl %ebp, %r13d
imull %r14d, %eax
imull %r14d, %eax
movl %eax, 0x5c(%rbx)
movl %ebp, 0x60(%rbx)
movslq %eax, %r15
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x250(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x258(%rbx)
movslq %ebp, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x230(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x238(%rbx)
testl %r15d, %r15d
jle 0x36f3
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xf308
mulss 0x4fa82(%rip), %xmm0    # 0x53160
movq 0x250(%rbx), %rax
movss %xmm0, (%rax,%r12,4)
incq %r12
cmpq %r12, %r15
jne 0x36cf
movl 0x70(%rsp), %r12d
movl %r13d, %ebp
testl %r13d, %r13d
jle 0x3716
movq 0x230(%rbx), %rdi
leaq (,%rbp,4), %rdx
xorl %esi, %esi
callq 0x31d0
movl %r12d, 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 %r12d, %r12d
subl %r12d, %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
movl %r13d, 0xc(%rsp)
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
movq 0x250(%rbx), %rsi
pushq $0x1
popq %rdx
callq 0x28619
movslq 0x48(%rbx), %rax
movslq 0x58(%rbx), %r13
imulq %rax, %r13
pushq $0x4
popq %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x268(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x260(%rbx)
leaq 0xbe4a(%rip), %rax      # 0xf621
movq %rax, 0x10(%rbx)
leaq 0xbfe4(%rip), %rax      # 0xf7c6
movq %rax, 0x18(%rbx)
leaq 0xc1d9(%rip), %rax      # 0xf9c6
movq %rax, 0x20(%rbx)
movl 0x80(%rsp), %eax
movl %eax, 0x40(%rbx)
testl %eax, %eax
je 0x38d7
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x3260
movq %rax, %r12
movq %rax, 0x240(%rbx)
movq %r14, %rdi
pushq $0x4
popq %rsi
callq 0x3260
movq %rax, 0x248(%rbx)
cmpl $0x0, 0xc(%rsp)
jle 0x3843
xorl %eax, %eax
movl $0x3f800000, (%r12,%rax,4) # imm = 0x3F800000
incq %rax
cmpq %rax, %rbp
jne 0x3833
pushq $0x4
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x290(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x298(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2a0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2a8(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2b0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2b8(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2c0(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2c8(%rbx)
movl 0x78(%rsp), %ebp
cmpl $0x0, 0x88(%rsp)
je 0x3955
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2d0(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2d8(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2e0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2f0(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
movq %rax, 0x2e8(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3260
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 0x66668(%rip), %rax     # 0x69ff8
movq (%rax), %rdi
movl 0x78(%rbx), %r10d
movl 0x7c(%rbx), %r11d
movl 0x80(%rbx), %r14d
leaq 0x4f7bb(%rip), %rsi     # 0x53164
movl 0xc(%rsp), %edx
movl 0x60(%rsp), %ecx
movl %ecx, %r8d
movl 0x68(%rsp), %r9d
xorl %eax, %eax
pushq %r14
pushq %r10
pushq %r11
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x3280
addq $0x30, %rsp
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq