// File: /workspace/github_c_makefile_star2_O1/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
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31d0
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)
movq %rax, 0x78(%rbx)
movl %r13d, 0x80(%rbx)
imull %r13d, %r14d
movslq %r14d, %r14
pushq $0x4
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3260
movq %rax, 0x268(%rbx)
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r15, %r14
callq 0x3260
movq %rax, 0x260(%rbx)
movl %r13d, %ebp
imull %r12d, %ebp
movslq %ebp, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x258(%rbx)
movl %r13d, 0x4(%rsp)
movslq %r13d, %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x238(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, %r14
movq %rax, 0x250(%rbx)
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x3260
movq %rax, 0x18(%rsp)
movq %rax, 0x230(%rbx)
leaq 0x2ca84(%rip), %rax     # 0x31644
movq %rax, 0x10(%rbx)
leaq 0x2cb56(%rip), %rax     # 0x31721
movq %rax, 0x18(%rbx)
leaq 0x2cc62(%rip), %rax     # 0x31838
movq %rax, 0x20(%rbx)
movl %r12d, 0x10(%rsp)
cvtsi2sd %r12d, %xmm1
movsd 0x4e5bc(%rip), %xmm0    # 0x531a8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4c00
sqrtsd %xmm0, %xmm0
jmp 0x4c05
callq 0x3470
testl %ebp, %ebp
jle 0x4c3f
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
xorl %r12d, %r12d
movss 0x4e64a(%rip), %xmm0    # 0x53268
movss 0x4e3e2(%rip), %xmm1    # 0x53008
callq 0xf201
mulss 0x14(%rsp), %xmm0
movss %xmm0, (%r14,%r12,4)
incq %r12
cmpq %r12, %r15
jne 0x4c16
movl 0x60(%rsp), %ebp
movl 0x4(%rsp), %eax
movl %eax, %r12d
testl %eax, %eax
jle 0x4c62
leaq (,%r12,4), %rdx
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x31d0
testl %ebp, %ebp
je 0x4cd6
pushq $0x4
popq %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2d0(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2d8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2e0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2f0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2e8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2f8(%rbx)
cmpl $0x0, 0x8(%rsp)
je 0x4dbe
pushq $0x4
popq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x3260
movq %rax, %r14
movq %rax, 0x240(%rbx)
movq %r13, %rdi
movq %r15, %rsi
callq 0x3260
movq %rax, 0x248(%rbx)
cmpl $0x0, 0x4(%rsp)
jle 0x4d25
xorl %eax, %eax
movl $0x3f800000, (%r14,%rax,4) # imm = 0x3F800000
incq %rax
cmpq %rax, %r12
jne 0x4d15
pushq $0x4
popq %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x290(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2a0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x298(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2a8(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2b0(%rbx)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2b8(%rbx)
movq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2c0(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, 0x2c8(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rbx)
movq 0x6522c(%rip), %rax     # 0x69ff8
movq (%rax), %rdi
leaq 0x51fa8(%rip), %rsi     # 0x56d7e
movl 0x10(%rsp), %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x3280
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq