// File: /workspace/github_c_makefile_star2_O2/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, 0x4(%rsp)
movq %rdi, %rbx
movl 0x60(%rsp), %r14d
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
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 0x4(%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
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 0x3250
movq %rax, %r13
movq %rax, 0x250(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movl %ebp, 0x4(%rsp)
movslq %ebp, %r14
movq %r15, %rbp
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x230(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
testl %ebp, %ebp
movl $0x0, %r15d
movq %rbp, 0x8(%rsp)
cmovgl %ebp, %r15d
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x50e3
xorl %eax, %eax
callq 0x4cf7
mulss 0x47090(%rip), %xmm0    # 0x4c160
movq 0x250(%rbx), %r13
movss %xmm0, (%r13,%r12,4)
incq %r12
jmp 0x50bc
movq 0x230(%rbx), %rax
xorl %ecx, %ecx
movl 0x4(%rsp), %edx
testl %edx, %edx
movl $0x0, %r15d
cmovgl %edx, %r15d
cmpq %rcx, %r15
je 0x510a
andl $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x50fc
movl 0x70(%rsp), %r8d
movl %r8d, 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 %r8d, %r8d
subl %r8d, %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 0x4(%rsp), %edx
movl %edx, 0x80(%rbx)
cvtsi2ss %eax, %xmm1
imull %edx, %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 %r13, %rsi
callq 0x1b423
movslq 0x48(%rbx), %rax
movslq 0x58(%rbx), %r13
imulq %rax, %r13
pushq $0x4
popq %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
leaq 0x214(%rip), %rax       # 0x53dd
movq %rax, 0x10(%rbx)
leaq 0x40a(%rip), %rax       # 0x55de
movq %rax, 0x18(%rbx)
leaq 0x5fa(%rip), %rax       # 0x57d9
movq %rax, 0x20(%rbx)
movl 0x80(%rsp), %eax
movl %eax, 0x40(%rbx)
testl %eax, %eax
je 0x52c7
pushq $0x4
popq %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, %r12
movq %rax, 0x240(%rbx)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
xorl %eax, %eax
cmpq %rax, %r15
je 0x5233
movl $0x3f800000, (%r12,%rax,4) # imm = 0x3F800000
incq %rax
jmp 0x5221
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 %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
cmpl $0x0, 0x88(%rsp)
je 0x5346
pushq $0x4
popq %r12
movq 0x8(%rsp), %r15
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 0x78(%rsp), %eax
movl %eax, 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 0x5ec73(%rip), %rax     # 0x63ff8
movq (%rax), %rdi
movl 0x78(%rbx), %r10d
movl 0x7c(%rbx), %r11d
movl 0x80(%rbx), %r14d
leaq 0x46dc6(%rip), %rsi     # 0x4c164
movl 0x4(%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 0x3270
addq $0x30, %rsp
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq