// File: /workspace/github_cpp_makefile_star2_O2/SneachChea[P]Wolf-Monte-Carlo-Cpp/main.cpp
// Function: main
int main(int argc, const char * argv[]) {
    const parameters params = get_params();
    model mod(params.L, params.Ntrials);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, mod.get_N()-1);
    std::uniform_real_distribution<> dis_real(0, 1);

    d_vector magnetization(mod.get_Ntrials());
    d_vector tNum = linspace(params.Tmin, params.Tmax, params.nT, true);
    d_vector meanMag(tNum.size());
    i_vector pocket, cluster;
    i_vector S(mod.get_N());
    double beta;
    double p;
    int k, s;
    std::ofstream txt;
    txt.open("magnetization_L"+std::to_string(mod.get_L())+"_Ntrials"+std::to_string(mod.get_Ntrials())+".dat");

    for(size_t i = 0; i<tNum.size();i++)
    {
        std::cout<<"Temperature ="<<tNum[i]<<std::endl;
        beta = 1./tNum[i];
        p = 1.- std::exp(-2*beta);
        initLattice(S);
        for(size_t j = 0; j<mod.get_Ntrials();j++)
        {
            k = dis(gen);
            pocket.push_back(k);
            cluster.push_back(k);
            while(pocket.size()!=0)
            {
                s = randomChoice(pocket);
                for(int k =0; k<4;k++)
                {
                    if(S[mod.get_nbr(s, k)]==S[s])
                    {
                        if(!isInVector(cluster,mod.get_nbr(s, k)))
                        {
                            if(dis_real(gen)<p)
                            {
                                pocket.push_back(mod.get_nbr(s, k));
                                cluster.push_back(mod.get_nbr(s, k));
                            }
                        }
                    }
                }
                pocket.erase(std::remove(pocket.begin(), pocket.end(), s), pocket.end());
            }
            for(size_t l=0; l<cluster.size();l++)
            {
                S[cluster[l]] = -S[cluster[l]];
            }
            cluster.resize(0);
            magnetization[j] = std::abs(std::accumulate(S.begin(), S.end(), 0.0));
        } 
        meanMag[i] = std::accumulate(magnetization.begin(), magnetization.end(), 0)/(1.*mod.get_Ntrials()*mod.get_N());
    }
     // */
    for(size_t i =0; i<tNum.size();i++)
    {
        txt<<tNum[i]<<"\t"<<meanMag[i]<<std::endl;
    }
    txt.close();
    std::cout<<"Done !"<<std::endl;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b78, %rsp           # imm = 0x2B78
leaq 0x1a8(%rsp), %rbx
movq %rbx, %rdi
callq 0x46f0
movl (%rbx), %esi
movl 0x4(%rbx), %edx
leaq 0x1c8(%rsp), %rdi
callq 0x2c3c
leaq 0x468(%rsp), %rdi
callq 0x2cd0
leaq 0x468(%rsp), %rdi
callq 0x2240
movl %eax, %esi
leaq 0x17f0(%rsp), %rdi
callq 0x3384
vxorpd %xmm0, %xmm0, %xmm0
vmovhpd 0x2ac4(%rip), %xmm0, %xmm0 # xmm0 = xmm0[0],mem[0]
movl 0x1cc(%rsp), %eax
andl $0x0, 0x50(%rsp)
movslq 0x1d0(%rsp), %rsi
decl %eax
movl %eax, 0x54(%rsp)
vmovapd %xmm0, 0xf0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x268(%rsp), %rdx
callq 0x2d26
vmovsd 0x1b8(%rsp), %xmm0
vmovsd 0x1c0(%rsp), %xmm1
movl 0x1b0(%rsp), %esi
leaq 0x38(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x4258
movq 0x40(%rsp), %rsi
subq 0x38(%rsp), %rsi
sarq $0x3, %rsi
leaq 0xd8(%rsp), %rdi
leaq 0x268(%rsp), %rdx
callq 0x2d26
andq $0x0, 0x20(%rsp)
andq $0x0, 0x80(%rsp)
movslq 0x1cc(%rsp), %rsi
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x10(%rsp)
vmovapd %xmm0, 0x70(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x268(%rsp), %rdx
callq 0x2d7a
leaq 0x268(%rsp), %rdi
callq 0x22f0
movl 0x1c8(%rsp), %esi
leaq 0x128(%rsp), %rdi
callq 0x2e77
leaq 0x2a06(%rip), %rsi      # 0x502c
leaq 0x148(%rsp), %rdi
leaq 0x128(%rsp), %rdx
callq 0x2e53
leaq 0x29fa(%rip), %rdx      # 0x503c
leaq 0x168(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x2dcd
movl 0x1d0(%rsp), %esi
leaq 0x108(%rsp), %rdi
callq 0x2e77
leaq 0x188(%rsp), %rdi
leaq 0x168(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x2dec
leaq 0x2b83(%rip), %rdx      # 0x5212
leaq 0xa0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x2dcd
leaq 0x268(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x20e0
leaq 0xa0(%rsp), %rdi
callq 0x20d0
leaq 0x188(%rsp), %rdi
callq 0x20d0
leaq 0x108(%rsp), %rdi
callq 0x20d0
leaq 0x168(%rsp), %rdi
callq 0x20d0
leaq 0x148(%rsp), %rdi
callq 0x20d0
leaq 0x128(%rsp), %rdi
callq 0x20d0
leaq 0x70(%rsp), %r12
leaq 0x250(%rsp), %r14
leaq 0x1c8(%rsp), %r15
leaq 0x238(%rsp), %r13
xorl %ebx, %ebx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rdx, %rbx
jae 0x29f9
movq 0x5883(%rip), %rdi      # 0x7fd0
leaq 0x28f1(%rip), %rsi      # 0x5045
callq 0x21c0
movq 0x38(%rsp), %rcx
vmovsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x22b0
movq %rax, %rdi
callq 0x20f0
movq 0x38(%rsp), %rax
vmovsd 0x2888(%rip), %xmm0     # 0x5008
vdivsd (%rax,%rbx,8), %xmm0, %xmm0
vmulsd 0x2883(%rip), %xmm0, %xmm0 # 0x5010
callq 0x2310
vmovsd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x43b4
vmovsd 0x285e(%rip), %xmm0     # 0x5008
xorl %ecx, %ecx
movq %rbx, 0x98(%rsp)
vsubsd 0x30(%rsp), %xmm0, %xmm0
vmovsd %xmm0, 0x30(%rsp)
movslq 0x1d0(%rsp), %rax
movq %rcx, 0x90(%rsp)
cmpq %rax, %rcx
jae 0x29b3
leaq 0x50(%rsp), %rdi
leaq 0x17f0(%rsp), %rsi
callq 0x2ed0
movl %eax, 0xc(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x2ed8
leaq 0xc(%rsp), %rsi
movq %r12, %rdi
callq 0x2ed8
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x2946
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x2fa6
movq %r14, %rdi
callq 0x2ef6
movq %r14, %rdi
movl %eax, 0x8(%rsp)
callq 0x349e
xorl %ebx, %ebx
cmpl $0x4, %ebx
je 0x2913
movl 0x8(%rsp), %esi
movq %r15, %rdi
movl %ebx, %edx
callq 0x3e88
movq 0x58(%rsp), %rcx
movslq 0x8(%rsp), %rdx
cltq
movl (%rcx,%rax,4), %eax
cmpl (%rcx,%rdx,4), %eax
jne 0x290c
movq %r13, %rdi
movq %r12, %rsi
callq 0x2fa6
movl 0x8(%rsp), %esi
movq %r15, %rdi
movl %ebx, %edx
callq 0x3e88
movq %r13, %rdi
movl %eax, %esi
callq 0x2ff5
movq %r13, %rdi
movl %eax, %ebp
callq 0x349e
testb %bpl, %bpl
jne 0x290c
leaq 0xf0(%rsp), %rdi
leaq 0x17f0(%rsp), %rsi
callq 0x301e
vmovsd 0x30(%rsp), %xmm1
vucomisd %xmm0, %xmm1
jbe 0x290c
movl 0x8(%rsp), %esi
movq %r15, %rdi
movl %ebx, %edx
callq 0x3e88
movl %eax, 0xa0(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3a60
movl 0x8(%rsp), %esi
movq %r15, %rdi
movl %ebx, %edx
callq 0x3e88
movl %eax, 0xa0(%rsp)
leaq 0xa0(%rsp), %rsi
movq %r12, %rdi
callq 0x3a60
incl %ebx
jmp 0x283e
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3b39
movq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3026
movq 0x98(%rsp), %rbx
jmp 0x280b
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
movq 0x58(%rsp), %rdx
xorl %esi, %esi
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rsi, %rcx
je 0x296f
movslq (%rax,%rsi,4), %rdi
incq %rsi
negl (%rdx,%rdi,4)
jmp 0x295e
movq %r12, %rdi
xorl %esi, %esi
callq 0x302c
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
vxorpd %xmm0, %xmm0, %xmm0
callq 0x305d
vandpd 0x2682(%rip){1to2}, %xmm0, %xmm0 # 0x5018
movq 0xc0(%rsp), %rax
movq 0x90(%rsp), %rcx
vmovlpd %xmm0, (%rax,%rcx,8)
incq %rcx
jmp 0x27c0
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
xorl %edx, %edx
callq 0x3071
vcvtdq2pd 0x1cc(%rsp), %xmm1
vcvtsi2sd %eax, %xmm3, %xmm0
movq 0xd8(%rsp), %rax
vshufpd $0x1, %xmm1, %xmm1, %xmm2 # xmm2 = xmm1[1,0]
vmulsd %xmm1, %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x2729
leaq 0x268(%rsp), %rbx
leaq 0x264b(%rip), %r14      # 0x5053
xorl %r15d, %r15d
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r15
jae 0x2a5d
vmovsd (%rax,%r15,8), %xmm0
movq %rbx, %rdi
callq 0x22b0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq 0xd8(%rsp), %rcx
vmovsd (%rcx,%r15,8), %xmm0
movq %rax, %rdi
callq 0x22b0
movq %rax, %rdi
callq 0x20f0
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
incq %r15
jmp 0x2a0b
leaq 0x268(%rsp), %rdi
callq 0x2250
movq 0x555f(%rip), %rdi      # 0x7fd0
leaq 0x25dd(%rip), %rsi      # 0x5055
callq 0x21c0
movq %rax, %rdi
callq 0x20f0
leaq 0x268(%rsp), %rdi
callq 0x2280
leaq 0x58(%rsp), %rdi
callq 0x349e
leaq 0x70(%rsp), %rdi
callq 0x349e
leaq 0x10(%rsp), %rdi
callq 0x349e
leaq 0xd8(%rsp), %rdi
callq 0x33e8
leaq 0x38(%rsp), %rdi
callq 0x33e8
leaq 0xc0(%rsp), %rdi
callq 0x33e8
leaq 0x468(%rsp), %rdi
callq 0x308c
leaq 0x1c8(%rsp), %rdi
callq 0x309c
xorl %eax, %eax
addq $0x2b78, %rsp           # imm = 0x2B78
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2bc7
leaq 0xa0(%rsp), %rdi
movq %rax, %rbx
callq 0x20d0
jmp 0x2b1c
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x20d0
jmp 0x2b2e
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x20d0
jmp 0x2b40
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x20d0
jmp 0x2b52
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x20d0
leaq 0x128(%rsp), %rdi
callq 0x20d0
jmp 0x2bca
movq %rax, %rbx
jmp 0x2b5f
jmp 0x2bc7
movq %rax, %rbx
jmp 0x2bd7
movq %rax, %rbx
jmp 0x2be1
movq %rax, %rbx
jmp 0x2c02
movq %rax, %rbx
jmp 0x2c0c
jmp 0x2b96
movq %rax, %rbx
jmp 0x2c26
movq %rax, %rbx
jmp 0x2c19
jmp 0x2bc7
jmp 0x2bc7
jmp 0x2bc7
jmp 0x2bc7
jmp 0x2bc7
jmp 0x2bc7
leaq 0x250(%rsp), %rdi
jmp 0x2bbd
jmp 0x2bc7
jmp 0x2bc7
leaq 0x238(%rsp), %rdi
movq %rax, %rbx
callq 0x349e
jmp 0x2bca
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
callq 0x2280
leaq 0x58(%rsp), %rdi
callq 0x349e
leaq 0x70(%rsp), %rdi
callq 0x349e
leaq 0x10(%rsp), %rdi
callq 0x349e
leaq 0xd8(%rsp), %rdi
callq 0x33e8
leaq 0x38(%rsp), %rdi
callq 0x33e8
leaq 0xc0(%rsp), %rdi
callq 0x33e8
leaq 0x468(%rsp), %rdi
callq 0x308c
leaq 0x1c8(%rsp), %rdi
callq 0x309c
movq %rbx, %rdi
callq 0x2300
nop