// File: /workspace/github_c_makefile_star2_O1/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 0xdb13
cmpl $0x1, %ebp
jne 0xdbcf
testl %r12d, %r12d
je 0xdbc0
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 0x930c
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 0x9b4e(%rip), %xmm0     # 0x17638
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 0xaa7c
decl %r12d
jne 0xdaa5
jmp 0xdbc0
cmpl $-0x1, %r12d
jne 0xdbee
movq %rbx, %rdi
callq 0x91dc
movq %rax, %r13
testq %rax, %rax
movb 0x40(%rsp), %al
je 0xdbc0
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jne 0xdc0d
movzbl %al, %r12d
xorpd %xmm0, %xmm0
cmpl $0x2, %ebp
jne 0xdb8c
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 0xa77c(%rip), %xmm0     # 0x18300
addsd 0x9aac(%rip), %xmm0     # 0x17638
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 0xaa7c
movq %rbx, %rdi
callq 0x9222
movq %rax, %r13
testq %rax, %rax
jne 0xdb45
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa8f1(%rip), %rdi      # 0x184c7
leaq 0xa755(%rip), %rsi      # 0x18332
leaq 0xa899(%rip), %rcx      # 0x1847d
movl $0xbc, %edx
callq 0x31b0
leaq 0xa8ec(%rip), %rdi      # 0x184e1
leaq 0xa736(%rip), %rsi      # 0x18332
leaq 0xa87a(%rip), %rcx      # 0x1847d
movl $0xa9, %edx
callq 0x31b0
leaq 0xa71c(%rip), %rdi      # 0x18330
leaq 0xa717(%rip), %rsi      # 0x18332
leaq 0xa85b(%rip), %rcx      # 0x1847d
movl $0xb3, %edx
callq 0x31b0