// File: /workspace/github_cpp_makefile_star2_O2/jamesliu516[P]amrdg2d/tvbm_limiter4all.cpp
// Function: pos_limiter(bodygridclass*)
void pos_limiter(OctCell *pc)
{
    double dx,dy,xc,yc;
    double dof[4][6]={0.0};
    double cgsx[3];
    double cgsy[3];
    
    double cgslbx[4]={0.0};
    double cgslby[4]={0.0};

    double gslbwei[4]={0.0};
    double gslbpt[4]={0.0};
    double qmin=0.0, rtp,tht1,tht2, pre0;
    double u, v, gma1;
    double pre1;
    double ush[4],ujb[4];

    dx=pc->dx;
    dy=pc->dy;
    xc=pc->xc1;
    yc=pc->yc1;

    for(int i=0;i<4;i++) 
        for(int j=0; j< nDOF; j++) dof[i][j]=pc->dof[i][j];


    if(NLBpt==3){
        for(int i=0; i<3;++i){
            gslbwei[i]=weiLB3[i];
            gslbpt[i]=gsLB3pt[i];
        }
    }
    else{ 
        for(int i=0; i<4;++i){
            gslbwei[i]=weiLB4[i];
            gslbpt[i]=gsLB4pt[i];
        }
    }

    for(int i=0; i<3;++i){
        cgsx[i]=xc+0.5*dx*gspt[i];
        cgsy[i]=yc+0.5*dy*gspt[i];
    }

    for(int i=0; i<NLBpt;++i){
        cgslbx[i]=xc+0.5*dx*gslbpt[i];
        cgslby[i]=yc+0.5*dy*gslbpt[i];
    }

//limit density
    if(dof[0][0]<epsPos){
    //    dof[0][0]=epsPos;
        for(int mm=1;mm<nDOF;++mm)
            dof[0][mm]=0.0;
    }
    else{
        qmin=1.0e20;
        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                rtp=n_approx_sol(cgslbx[j],cgsy[i],
                    xc,yc,dx,dy, dof[0],nDOF);
                if(rtp<qmin) qmin=rtp;
            }
        }

        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                rtp=n_approx_sol(cgsx[i],cgslby[j],
                    xc,yc,dx,dy, dof[0],nDOF);
                if(rtp<qmin) qmin=rtp;
            }
        }

        tht1=min2(fabs((dof[0][0]-epsPos)/(dof[0][0]-qmin)), 1.0);

        for(int i=1;i<nDOF;++i)
            dof[0][i]*=tht1;
    }


    gma1=GAMMA-1.0;

    u=dof[1][0]/dof[0][0];
    v=dof[2][0]/dof[0][0];

    pre0=gma1*(dof[3][0]-0.5*dof[0][0]*(u*u+v*v));

    if(pre0<epsPos){
        for(int i=0;i<4;++i){
            for(int j=1;j<nDOF;j++){
                dof[i][j]=0.0;
            }
        }
    }
    else{
        tht2=1.0e20;
        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                for(int a=0;a<4;++a){
                    ush[a]=n_approx_sol(cgsx[i],cgslby[j],
                        xc,yc,dx,dy, dof[a],nDOF);
                }
                shbl2jbbl(ush,ujb);
                pre1=ujb[3];
                if(pre1>epsPos){
                    rtp=1.0;
                }
                else{
                    rtp=(pre0-epsPos)/(pre0-pre1);
                }

                if(rtp<tht2) tht2=rtp;
            }
        }


        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                for(int a=0;a<4;++a){
                    ush[a]=n_approx_sol(cgslbx[j],cgsy[i],
                        xc,yc,dx,dy, dof[a],nDOF);
                }
                shbl2jbbl(ush,ujb);
                pre1=ujb[3];
                if(pre1>epsPos){
                    rtp=1.0;
                }
                else{
                    rtp=(pre0-epsPos)/(pre0-pre1);
                }

                if(rtp<tht2) tht2=rtp;
            }
        }

        for(int j=0;j<4;++j)
            for(int i=1;i<nDOF;++i)
                dof[j][i]*=tht2;
    }

    for(int i=0;i<4;i++) 
        for(int j=1; j< nDOF; j++) pc->dof[i][j]= dof[i][j];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, %rbx
leaq 0x110(%rsp), %r14
xorl %r15d, %r15d
movl $0xc0, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3100
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movsd 0x288(%rbx), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x290(%rbx), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x280(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x30(%rbx), %rax
cmpq $0x4, %r15
je 0x17218
xorl %ecx, %ecx
cmpq $0x6, %rcx
je 0x1720b
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rcx,8)
incq %rcx
jmp 0x171f5
incq %r15
addq $0x30, %rax
addq $0x30, %r14
jmp 0x171ed
xorl %eax, %eax
leaq 0x486f(%rip), %rcx      # 0x1ba90
cmpq $0x18, %rax
je 0x1723b
movsd (%rax,%rcx), %xmm0
movsd %xmm0, 0xb0(%rsp,%rax)
addq $0x8, %rax
jmp 0x17221
movsd 0x3f35(%rip), %xmm0     # 0x1b178
movsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x28(%rsp), %xmm0
xorl %eax, %eax
leaq 0x4854(%rip), %rcx      # 0x1bab0
cmpq $0x18, %rax
je 0x17297
movsd (%rax,%rcx), %xmm2
movapd %xmm1, %xmm3
mulsd %xmm2, %xmm3
addsd 0x20(%rsp), %xmm3
movsd %xmm3, 0xf0(%rsp,%rax)
mulsd %xmm0, %xmm2
addsd 0x18(%rsp), %xmm2
movsd %xmm2, 0xd0(%rsp,%rax)
addq $0x8, %rax
jmp 0x1725c
xorl %eax, %eax
cmpq $0x3, %rax
je 0x172d4
movsd 0xb0(%rsp,%rax,8), %xmm2
movapd %xmm1, %xmm3
mulsd %xmm2, %xmm3
addsd 0x20(%rsp), %xmm3
movsd %xmm3, 0x90(%rsp,%rax,8)
mulsd %xmm0, %xmm2
addsd 0x18(%rsp), %xmm2
movsd %xmm2, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x17299
movsd 0x110(%rsp), %xmm1
leaq 0xbf34(%rip), %rax      # 0x23218
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1730d
movapd %xmm1, %xmm3
pushq $0x1
popq %rax
cmpq $0x6, %rax
je 0x17463
andq $0x0, 0x110(%rsp,%rax,8)
incq %rax
jmp 0x172f5
pushq $0x1
popq %r15
movsd 0x4767(%rip), %xmm0     # 0x1ba80
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x110(%rsp), %r14
pushq $0x6
popq %rbp
cmpq $0x3, %r13
je 0x17394
movsd 0xd0(%rsp,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x1738f
movsd 0x90(%rsp,%r12,8), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movsd 0x30(%rsp), %xmm4
movsd 0x28(%rsp), %xmm5
movq %r14, %rdi
movl %ebp, %esi
callq 0xca49
minsd 0x8(%rsp), %xmm0
incq %r12
movsd %xmm0, 0x8(%rsp)
jmp 0x17346
incq %r13
jmp 0x1732d
xorl %r13d, %r13d
leaq 0x110(%rsp), %r14
pushq $0x6
popq %rbp
cmpq $0x3, %r13
je 0x17406
movsd 0xf0(%rsp,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x17401
movsd 0x70(%rsp,%r12,8), %xmm1
movsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movsd 0x30(%rsp), %xmm4
movsd 0x28(%rsp), %xmm5
movq %r14, %rdi
movl %ebp, %esi
callq 0xca49
minsd 0x8(%rsp), %xmm0
incq %r12
movsd %xmm0, 0x8(%rsp)
jmp 0x173bb
incq %r13
jmp 0x173a2
movsd 0x110(%rsp), %xmm2
leaq 0xbe02(%rip), %rax      # 0x23218
movsd (%rax), %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movapd %xmm2, %xmm3
subsd 0x8(%rsp), %xmm2
divsd %xmm2, %xmm1
andpd 0x3e08(%rip), %xmm1     # 0x1b240
minsd 0x3de0(%rip), %xmm1     # 0x1b220
cmpq $0x6, %r15
je 0x17463
movsd 0x110(%rsp,%r15,8), %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, 0x110(%rsp,%r15,8)
incq %r15
jmp 0x17440
movsd 0x140(%rsp), %xmm1
movsd 0x170(%rsp), %xmm2
divsd %xmm3, %xmm1
divsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
mulsd 0x3ce3(%rip), %xmm3     # 0x1b170
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm3
addsd 0x1a0(%rsp), %xmm3
mulsd 0x3d82(%rip), %xmm3     # 0x1b228
movsd %xmm3, 0x48(%rsp)
ucomisd %xmm3, %xmm0
jbe 0x174e5
leaq 0x110(%rsp), %rax
xorl %ecx, %ecx
pushq $0x1
popq %rdx
cmpq $0x4, %rcx
je 0x17784
movq %rdx, %rsi
cmpq $0x6, %rsi
je 0x174dc
andq $0x0, (%rax,%rsi,8)
incq %rsi
jmp 0x174cc
incq %rcx
addq $0x30, %rax
jmp 0x174bf
movsd 0x4593(%rip), %xmm1     # 0x1ba80
xorl %eax, %eax
leaq 0x110(%rsp), %r13
pushq $0x6
popq %rbp
cmpq $0x3, %rax
je 0x1761b
movq %rax, 0x38(%rsp)
movsd 0xf0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x1760e
movsd %xmm1, 0x40(%rsp)
movsd 0x70(%rsp,%r12,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %r14
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x17582
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movsd 0x30(%rsp), %xmm4
movsd 0x28(%rsp), %xmm5
movq %r14, %rdi
movl %ebp, %esi
callq 0xca49
movsd %xmm0, 0x50(%rsp,%r15,8)
incq %r15
addq $0x30, %r14
jmp 0x1753e
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0x60(%rsp), %xmm2
divsd %xmm0, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
mulsd 0x3bc0(%rip), %xmm0     # 0x1b170
mulsd %xmm1, %xmm0
addsd 0x68(%rsp), %xmm0
mulsd 0x3c66(%rip), %xmm0     # 0x1b228
leaq 0xbc4f(%rip), %rax      # 0x23218
movsd (%rax), %xmm1
movsd 0x48(%rsp), %xmm3
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
subsd %xmm0, %xmm3
divsd %xmm3, %xmm2
cmpltsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andnpd %xmm2, %xmm0
movsd 0x3c28(%rip), %xmm2     # 0x1b220
andpd %xmm2, %xmm1
orpd %xmm0, %xmm1
minsd 0x40(%rsp), %xmm1
incq %r12
jmp 0x1751b
movq 0x38(%rsp), %rax
incq %rax
jmp 0x174fa
xorl %eax, %eax
leaq 0x110(%rsp), %r13
pushq $0x6
popq %rbp
cmpq $0x3, %rax
je 0x1774c
movq %rax, 0x38(%rsp)
movsd 0xd0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x1773f
movsd %xmm1, 0x40(%rsp)
movsd 0x90(%rsp,%r12,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %r14
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x176b3
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movsd 0x30(%rsp), %xmm4
movsd 0x28(%rsp), %xmm5
movq %r14, %rdi
movl %ebp, %esi
callq 0xca49
movsd %xmm0, 0x50(%rsp,%r15,8)
incq %r15
addq $0x30, %r14
jmp 0x1766f
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0x60(%rsp), %xmm2
divsd %xmm0, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
mulsd 0x3a8f(%rip), %xmm0     # 0x1b170
mulsd %xmm1, %xmm0
addsd 0x68(%rsp), %xmm0
mulsd 0x3b35(%rip), %xmm0     # 0x1b228
leaq 0xbb1e(%rip), %rax      # 0x23218
movsd (%rax), %xmm1
movsd 0x48(%rsp), %xmm3
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
subsd %xmm0, %xmm3
divsd %xmm3, %xmm2
cmpltsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andnpd %xmm2, %xmm0
movsd 0x3af7(%rip), %xmm2     # 0x1b220
andpd %xmm2, %xmm1
orpd %xmm0, %xmm1
minsd 0x40(%rsp), %xmm1
incq %r12
jmp 0x17649
movq 0x38(%rsp), %rax
incq %rax
jmp 0x17628
leaq 0x110(%rsp), %rax
xorl %ecx, %ecx
pushq $0x1
popq %rdx
cmpq $0x4, %rcx
je 0x17784
movq %rdx, %rsi
cmpq $0x6, %rsi
je 0x1777b
movsd (%rax,%rsi,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
jmp 0x17762
incq %rcx
addq $0x30, %rax
jmp 0x17759
leaq 0x118(%rsp), %rax
addq $0x38, %rbx
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x177bc
xorl %edx, %edx
cmpq $0x5, %rdx
je 0x177af
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rbx,%rdx,8)
incq %rdx
jmp 0x1779a
incq %rcx
addq $0x30, %rax
addq $0x30, %rbx
jmp 0x17792
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq