// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %r9d, %r14d
movl %r8d, 0x14(%rsp)
movl %ecx, %r13d
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %rbx
movl $0x4a0, %edx            # imm = 0x4A0
xorl %esi, %esi
callq 0x31c0
movl $0x3f800000, 0xec(%rbx) # imm = 0x3F800000
movl $0x2, (%rbx)
movl %r15d, 0x54(%rbx)
movl %r13d, 0x58(%rbx)
movl %ebp, 0x48(%rbx)
movl %r14d, 0x10(%rsp)
movl %r14d, 0x40(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x6c(%rbx)
movl %r15d, 0x74(%rbx)
movq %rax, 0x78(%rbx)
movl %r13d, 0x80(%rbx)
imull %r13d, %ebp
movslq %ebp, %r14
pushq $0x4
popq %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x268(%rbx)
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x260(%rbx)
movl %r13d, %r14d
imull %r15d, %r14d
movslq %r14d, %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x258(%rbx)
movq %r13, 0x8(%rsp)
movslq %r13d, %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, 0x238(%rbx)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3250
movq %rax, %rbp
movq %rax, 0x250(%rbx)
movq %r13, 0x30(%rsp)
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, 0x20(%rsp)
movq %rax, 0x230(%rbx)
leaq 0x2e88d(%rip), %rax     # 0x335d8
movq %rax, 0x10(%rbx)
leaq 0x2e978(%rip), %rax     # 0x336ce
movq %rax, 0x18(%rbx)
leaq 0x2ea95(%rip), %rax     # 0x337f6
movq %rax, 0x20(%rbx)
movl %r15d, 0x18(%rsp)
cvtsi2sd %r15d, %xmm1
movsd 0x50461(%rip), %xmm0    # 0x551d8
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4d8b
sqrtsd %xmm0, %xmm0
jmp 0x4d90
callq 0x3460
testl %r14d, %r14d
jle 0x4dcc
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
xorl %r14d, %r14d
movss 0x504ea(%rip), %xmm0    # 0x55294
movss 0x50256(%rip), %xmm1    # 0x55008
callq 0xfa10
mulss 0x1c(%rsp), %xmm0
movss %xmm0, (%rbp,%r14,4)
incq %r14
cmpq %r14, %r12
jne 0x4da2
movl 0x70(%rsp), %r14d
movq 0x8(%rsp), %rax
movl %eax, %ebp
testl %eax, %eax
jle 0x4df0
leaq (,%rbp,4), %rdx
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x31c0
testl %r14d, %r14d
movq 0x30(%rsp), %r14
je 0x4e6a
pushq $0x4
popq %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2d0(%rbx)
movq %r12, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2d8(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2e0(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2f0(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2e8(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2f8(%rbx)
cmpl $0x0, 0x10(%rsp)
je 0x5045
pushq $0x4
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r15
movq %rax, 0x240(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, 0x248(%rbx)
cmpl $0x0, 0x8(%rsp)
jle 0x4fac
movq 0x8(%rsp), %rax
addl $0x3, %eax
decq %rbp
movq %rbp, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x502a1(%rip), %xmm1    # 0x55170
movdqa 0x501a9(%rip), %xmm2    # 0x55080
xorl %ecx, %ecx
movdqa 0x501af(%rip), %xmm3    # 0x55090
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x5028f(%rip), %xmm5    # 0x55180
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x4f2d
movl $0x3f800000, (%r15,%rcx) # imm = 0x3F800000
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x4f44
movl $0x3f800000, 0x4(%r15,%rcx) # imm = 0x3F800000
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0x4f84
movl $0x3f800000, 0x8(%r15,%rcx) # imm = 0x3F800000
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x4f97
movl $0x3f800000, 0xc(%r15,%rcx) # imm = 0x3F800000
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x4ef1
pushq $0x4
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x290(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2a0(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x298(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2a8(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2b0(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2b8(%rbx)
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2c0(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x3250
movq %rax, 0x2c8(%rbx)
movl 0x14(%rsp), %eax
movl %eax, 0x4(%rbx)
movq 0x66fa5(%rip), %rax     # 0x6bff8
movq (%rax), %rdi
leaq 0x53e21(%rip), %rsi     # 0x58e7e
movl 0x18(%rsp), %edx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x3270
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq