// File: /workspace/github_c_makefile_star2_O3/stonezwr[P]SpiKL-IP/network.C
// Function: Network::LSMAddSynapse(Neuron*, NeuronGroup*, int, int, int, bool)
void Network::LSMAddSynapse(Neuron * pre, NeuronGroup * post, int npost, int value, int random, bool fixed){
    Neuron * neuron;
    double factor;
    double weight_limit;
    bool liquid = false;
    int D_weight_limit;
    if(fixed == true){
        /***** this is for the case of input synapses: *****/
        weight_limit = value;
        D_weight_limit = value;
    }else{
        weight_limit = value;
        D_weight_limit = value;
    }

    if(npost < 0){
        assert(npost == -1);
        for(neuron = post->First(); neuron != NULL; neuron = post->Next()){
            if(random == 2){
                /*** This is for the case of continuous readout weights: ***/
                /*** Remember to change the netlist to                   ***/
                /*** lsmsynapse reservoir output -1 -1 8 2, 2 is random  ***/
                /*** The initial weights are in W_max*(-1 to 1)          ***/
                factor = rand()%100000;
                factor = factor*2/99999-1;
            }else if(random == 0) factor = 0;
            else assert(0);
            double weight = value*factor/weight_limit;
            // set the sign of the readout weight the same as pre-neuron E/I might be hampful for the performance
            // if((!pre->IsExcitatory() && weight > 0) ||(pre->IsExcitatory() && weight < 0))  weight = -1*weight; 
            //if(weight > 0)  weight = weight*0.5;
            LSMAddSynapse(pre,neuron,weight,fixed,weight_limit,liquid);
        }
    }else{
        /*** This is for adding input synapses!! ***/
        assert(random == 1);
        for(int i = 0; i < npost; i++){
            neuron = post->Order(rand()%post->Size());
            factor = rand()%2;
            factor = factor*2-1;
            LSMAddSynapse(pre,neuron,value*factor,fixed,weight_limit,liquid);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r9d, %ebp
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
cvtsi2sd %r8d, %xmm0
movsd %xmm0, (%rsp)
testl %ecx, %ecx
js 0xd889
cmpl $0x1, %ebp
jne 0xd945
testl %r12d, %r12d
je 0xd936
movzbl 0x40(%rsp), %ebp
callq 0x30e0
movq 0x30(%rbx), %rcx
subq 0x28(%rbx), %rcx
shrq $0x3, %rcx
cltd
idivl %ecx
movq %rbx, %rdi
movl %edx, %esi
callq 0x9114
movq %rax, %r13
callq 0x30e0
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
andl $-0x2, %ecx
subl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
addsd 0x9e78(%rip), %xmm0     # 0x176d8
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xa854
decl %r12d
jne 0xd81b
jmp 0xd936
cmpl $-0x1, %r12d
jne 0xd964
movq %rbx, %rdi
callq 0x8fe4
movq %rax, %r13
testq %rax, %rax
movb 0x40(%rsp), %al
je 0xd936
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jne 0xd983
movzbl %al, %r12d
xorpd %xmm0, %xmm0
cmpl $0x2, %ebp
jne 0xd902
callq 0x30e0
cltq
imulq $0x14f8b589, %rax, %rcx # imm = 0x14F8B589
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x2d, %rcx
addl %edx, %ecx
imull $0x186a0, %ecx, %ecx    # imm = 0x186A0
subl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
divsd 0xaaa6(%rip), %xmm0     # 0x183a0
addsd 0x9dd6(%rip), %xmm0     # 0x176d8
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm0
divsd %xmm1, %xmm0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r12d, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xa854
movq %rbx, %rdi
callq 0x902a
movq %rax, %r13
testq %rax, %rax
jne 0xd8bb
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xac1b(%rip), %rdi      # 0x18567
leaq 0xaa7f(%rip), %rsi      # 0x183d2
leaq 0xabc3(%rip), %rcx      # 0x1851d
movl $0xbc, %edx
callq 0x31b0
leaq 0xac16(%rip), %rdi      # 0x18581
leaq 0xaa60(%rip), %rsi      # 0x183d2
leaq 0xaba4(%rip), %rcx      # 0x1851d
movl $0xa9, %edx
callq 0x31b0
leaq 0xaa46(%rip), %rdi      # 0x183d0
leaq 0xaa41(%rip), %rsi      # 0x183d2
leaq 0xab85(%rip), %rcx      # 0x1851d
movl $0xb3, %edx
callq 0x31b0