// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/local_layer.c
// Function: make_local_layer
local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation)
{
    int i;
    local_layer l = {0};
    l.type = LOCAL;

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

    int out_h = local_out_height(l);
    int out_w = local_out_width(l);
    int locations = out_h*out_w;
    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.weights = calloc(c*n*size*size*locations, sizeof(float));
    l.weight_updates = calloc(c*n*size*size*locations, sizeof(float));

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

    // float scale = 1./sqrt(size*size*c);
    float scale = sqrt(2./(size*size*c));
    for(i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_uniform(-1,1);

    l.output = calloc(l.batch*out_h * out_w * n, sizeof(float));
    l.delta  = calloc(l.batch*out_h * out_w * n, sizeof(float));

    l.workspace_size = out_h*out_w*size*size*c;
    
    l.forward = forward_local_layer;
    l.backward = backward_local_layer;
    l.update = update_local_layer;

    l.activation = activation;

    fprintf(stderr, "Local Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h,w,c,n, out_h, out_w, n);

    return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x28(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, %r15d
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, %rbx
movl 0x90(%rsp), %r12d
xorl %r14d, %r14d
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
movl $0xc, (%rbx)
movl %r13d, 0x6c(%rbx)
movl %r15d, 0x70(%rbx)
movl 0xc(%rsp), %edi
movl %edi, 0x74(%rbx)
movq 0x28(%rsp), %r10
movl %r10d, 0x84(%rbx)
movl %ebp, 0x20(%rsp)
movl %ebp, 0x48(%rbx)
movl 0x88(%rsp), %esi
movl %esi, 0x98(%rbx)
movl 0x80(%rsp), %r8d
movl %r8d, 0x90(%rbx)
movl %r12d, 0xa8(%rbx)
movl %r8d, %eax
negl %eax
negl %r12d
movl $0x0, %ecx
sbbl %ecx, %ecx
orl %eax, %ecx
leal (%rcx,%r13), %eax
movq %r13, %r9
cltd
idivl %esi
movl %esi, %r11d
movl %eax, %esi
incl %esi
addl %r15d, %ecx
movl %ecx, %eax
cltd
idivl %r11d
incl %eax
movl %eax, %ecx
imull %esi, %ecx
movq %rsi, 0x38(%rsp)
movl %esi, 0x78(%rbx)
movq %rax, 0x30(%rsp)
movl %eax, 0x7c(%rbx)
movq %r10, %r13
movl %r13d, 0x80(%rbx)
movl %ecx, %ebp
imull %r13d, %ebp
movq %r10, %rdx
movl %ebp, 0x58(%rbx)
movl %r15d, 0x24(%rsp)
movq %r9, %rsi
movq %r9, 0x40(%rsp)
imull %esi, %r15d
imull %edi, %r15d
movl %r15d, 0x54(%rbx)
movl %r8d, %r13d
imull %edi, %r13d
imull %r8d, %r13d
movl %r13d, %r15d
imull %edx, %r15d
movl %ecx, 0x1c(%rsp)
movl %ecx, %eax
imull %r15d, %eax
movslq %eax, %rdi
movq %rdi, 0x10(%rsp)
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, %r12
movq %rax, 0x250(%rbx)
movq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movslq %ebp, %rbp
movq %rbp, %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x230(%rbx)
movq %rbp, %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
movl %r13d, 0x18(%rsp)
cvtsi2sd %r13d, %xmm1
movsd 0x1f82d(%rip), %xmm0    # 0x4c1a8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2c98f
sqrtsd %xmm0, %xmm0
jmp 0x2c994
callq 0x3460
movl 0x98(%rsp), %r13d
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
testl %r15d, %r15d
cmovlel %r14d, %r15d
cmpq %r14, %r15
je 0x2c9d8
movss 0x1f8aa(%rip), %xmm0    # 0x4c264
movss 0x1f646(%rip), %xmm1    # 0x4c008
callq 0x4c09
mulss 0x10(%rsp), %xmm0
movss %xmm0, (%r12,%r14,4)
incq %r14
jmp 0x2c9ad
movl 0x20(%rsp), %eax
movl 0x1c(%rsp), %ebp
imull %ebp, %eax
movq 0x28(%rsp), %r14
imull %r14d, %eax
movslq %eax, %r15
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
movl 0x18(%rsp), %eax
imull %ebp, %eax
cltq
movq %rax, 0x498(%rbx)
leaq 0x6b(%rip), %rax        # 0x2ca99
movq %rax, 0x10(%rbx)
leaq 0x2ad(%rip), %rax       # 0x2cce6
movq %rax, 0x18(%rbx)
leaq 0x588(%rip), %rax       # 0x2cfcc
movq %rax, 0x20(%rbx)
movl %r13d, 0x4(%rbx)
movq 0x375a5(%rip), %rax     # 0x63ff8
movq (%rax), %rdi
subq $0x8, %rsp
leaq 0x24059(%rip), %rsi     # 0x50aba
movq 0x48(%rsp), %rdx
movl 0x2c(%rsp), %ecx
movl 0x14(%rsp), %r8d
movl %r14d, %r9d
xorl %eax, %eax
pushq %r14
pushq 0x40(%rsp)
pushq 0x50(%rsp)
callq 0x3270
addq $0x20, %rsp
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq