// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/connected_layer.c
// Function: make_connected_layer
layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam)
{
    int i;
    layer l = {0};
    l.learning_rate_scale = 1;
    l.type = CONNECTED;

    l.inputs = inputs;
    l.outputs = outputs;
    l.batch=batch;
    l.batch_normalize = batch_normalize;
    l.h = 1;
    l.w = 1;
    l.c = inputs;
    l.out_h = 1;
    l.out_w = 1;
    l.out_c = outputs;

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

    l.weight_updates = calloc(inputs*outputs, sizeof(float));
    l.bias_updates = calloc(outputs, sizeof(float));

    l.weights = calloc(outputs*inputs, sizeof(float));
    l.biases = calloc(outputs, sizeof(float));

    l.forward = forward_connected_layer;
    l.backward = backward_connected_layer;
    l.update = update_connected_layer;

    //float scale = 1./sqrt(inputs);
    float scale = sqrt(2./inputs);
    for(i = 0; i < outputs*inputs; ++i){
        l.weights[i] = scale*rand_uniform(-1, 1);
    }

    for(i = 0; i < outputs; ++i){
        l.biases[i] = 0;
    }

    if(adam){
        l.m = calloc(l.inputs*l.outputs, sizeof(float));
        l.v = calloc(l.inputs*l.outputs, sizeof(float));
        l.bias_m = calloc(l.outputs, sizeof(float));
        l.scale_m = calloc(l.outputs, sizeof(float));
        l.bias_v = calloc(l.outputs, sizeof(float));
        l.scale_v = calloc(l.outputs, sizeof(float));
    }
    if(batch_normalize){
        l.scales = calloc(outputs, sizeof(float));
        l.scale_updates = calloc(outputs, sizeof(float));
        for(i = 0; i < outputs; ++i){
            l.scales[i] = 1;
        }

        l.mean = calloc(outputs, sizeof(float));
        l.mean_delta = calloc(outputs, sizeof(float));
        l.variance = calloc(outputs, sizeof(float));
        l.variance_delta = calloc(outputs, sizeof(float));

        l.rolling_mean = calloc(outputs, sizeof(float));
        l.rolling_variance = calloc(outputs, sizeof(float));

        l.x = calloc(batch*outputs, sizeof(float));
        l.x_norm = calloc(batch*outputs, sizeof(float));
    }

    l.activation = activation;
    fprintf(stderr, "connected                            %4d  ->  %4d\n", inputs, outputs);
    return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %ebp
movl %r8d, 0xc(%rsp)
movl %ecx, %r13d
movl %edx, %r12d
movl %esi, %r14d
movq %rdi, %rbx
xorl %r15d, %r15d
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
movl $0x3f800000, 0xec(%rbx) # imm = 0x3F800000
movl $0x2, (%rbx)
movl %r12d, 0x54(%rbx)
movl %r13d, 0x58(%rbx)
movl %r14d, 0x48(%rbx)
movl %ebp, 0x8(%rsp)
movl %ebp, 0x40(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x6c(%rbx)
movl %r12d, 0x74(%rbx)
movl %r12d, 0x4(%rsp)
movq %rax, 0x78(%rbx)
movl %r13d, 0x80(%rbx)
imull %r13d, %r14d
movslq %r14d, %r14
pushq $0x4
popq %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
movl %r13d, %ebp
imull %r12d, %ebp
movslq %ebp, %r14
movq %r14, %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movl %r13d, 0x10(%rsp)
movslq %r13d, %r13
movq %r13, %rdi
pushq $0x4
popq %r12
movq %r12, %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r14
movq %rax, 0x250(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r12
movq %rax, 0x230(%rbx)
leaq 0x233(%rip), %rax       # 0x244cc
movq %rax, 0x10(%rbx)
leaq 0x323(%rip), %rax       # 0x245c7
movq %rax, 0x18(%rbx)
leaq 0x43a(%rip), %rax       # 0x246e9
movq %rax, 0x20(%rbx)
cvtsi2sdl 0x4(%rsp), %xmm1
movsd 0x27ee7(%rip), %xmm0    # 0x4c1a8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x242d5
sqrtsd %xmm0, %xmm0
jmp 0x242da
callq 0x3460
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
testl %ebp, %ebp
cmovlel %r15d, %ebp
cmpq %r15, %rbp
je 0x24315
movss 0x27f6d(%rip), %xmm0    # 0x4c264
movss 0x27d09(%rip), %xmm1    # 0x4c008
callq 0x4c09
mulss 0x14(%rsp), %xmm0
movss %xmm0, (%r14,%r15,4)
incq %r15
jmp 0x242ea
xorl %eax, %eax
movl 0x10(%rsp), %ebp
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
cmpq %rax, %r15
je 0x24336
andl $0x0, (%r12,%rax,4)
incq %rax
jmp 0x24327
cmpl $0x0, 0x60(%rsp)
je 0x243b2
pushq $0x4
popq %r14
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2d0(%rbx)
movq %r12, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2d8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2e0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2f0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2e8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2f8(%rbx)
cmpl $0x0, 0x8(%rsp)
je 0x24495
pushq $0x4
popq %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r14
movq %rax, 0x240(%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
xorl %eax, %eax
cmpq %rax, %r15
je 0x243fc
movl $0x3f800000, (%r14,%rax,4) # imm = 0x3F800000
incq %rax
jmp 0x243ea
pushq $0x4
popq %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x290(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2a0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x298(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2a8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2b0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2b8(%rbx)
movq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rbx)
movq 0x3fb55(%rip), %rax     # 0x63ff8
movq (%rax), %rdi
leaq 0x2b921(%rip), %rsi     # 0x4fdce
movl 0x4(%rsp), %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3270
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq