// File: /workspace/github_c_makefile_star2_O2/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 0xb901
cmpl $0x1, %ebp
jne 0xb9b2
movzbl 0x40(%rsp), %ebp
subl $0x1, %r12d
jb 0xb984
callq 0x30e0
movq 0x30(%rbx), %rcx
subq 0x28(%rbx), %rcx
shrq $0x3, %rcx
cltd
idivl %ecx
movq %rbx, %rdi
movl %edx, %esi
callq 0x79f2
movq %rax, %r13
callq 0x30e0
cltd
pushq $0x2
popq %rcx
idivl %ecx
cvtsi2sd %edx, %xmm0
addsd %xmm0, %xmm0
addsd 0x8dd8(%rip), %xmm0     # 0x146b8
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 0x8e5c
jmp 0xb89a
cmpl $-0x1, %r12d
jne 0xb9d1
movq %rbx, %rdi
callq 0x78c2
movq %rax, %r13
movzbl 0x40(%rsp), %r12d
testq %r13, %r13
je 0xb984
xorpd %xmm0, %xmm0
testl %ebp, %ebp
je 0xb953
cmpl $0x2, %ebp
jne 0xb993
callq 0x30e0
cltd
movl $0x186a0, %ecx          # imm = 0x186A0
idivl %ecx
cvtsi2sd %edx, %xmm0
addsd %xmm0, %xmm0
divsd 0x9a35(%rip), %xmm0     # 0x15380
addsd 0x8d65(%rip), %xmm0     # 0x146b8
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 0x8e5c
movq %rbx, %rdi
callq 0x7908
movq %rax, %r13
jmp 0xb91c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9a16(%rip), %rdi      # 0x153b0
leaq 0x9a11(%rip), %rsi      # 0x153b2
leaq 0x9b55(%rip), %rcx      # 0x154fd
movl $0xb3, %edx
callq 0x31b0
leaq 0x9b8e(%rip), %rdi      # 0x15547
leaq 0x99f2(%rip), %rsi      # 0x153b2
leaq 0x9b36(%rip), %rcx      # 0x154fd
movl $0xbc, %edx
callq 0x31b0
leaq 0x9b89(%rip), %rdi      # 0x15561
leaq 0x99d3(%rip), %rsi      # 0x153b2
leaq 0x9b17(%rip), %rcx      # 0x154fd
movl $0xa9, %edx
callq 0x31b0