// File: /workspace/github_cpp_makefile_star2_O3/jamesliu516[P]amrdg2d/wall4amrdg2013.cpp
// Function: getIndexAtWallPoint(pointxy const*, int, pointxy*)
int getIndexAtWallPoint(const PXYZ *gst, int NumberPoint,PXYZ wallpnt[]   )
{
    int ils, ia,ib,i;
    double dis,dis2;
    PXYZ pa,pi1,pi2,pb1,pb2;

    ia=0;
    //	ib=NumberAirfoilPoint;

    dis=(gst->x-wallpnt[0].x)*(gst->x-wallpnt[0].x)
        +(gst->y-wallpnt[0].y)*(gst->y-wallpnt[0].y);

    for(i=1;i<=NumberPoint-1;++i)
    {
        dis2 = (gst->x-wallpnt[i].x)*(gst->x-wallpnt[i].x)
            +(gst->y-wallpnt[i].y)*(gst->y-wallpnt[i].y);
        if(dis2<dis){
            dis=dis2;
            ia=i;
        }
    }
    if(ia==0)
    {
        pa.x = wallpnt[ia].x;
        pa.y = wallpnt[ia].y;
        pb1.x = wallpnt[ia+1].x; 
        pb1.y = wallpnt[ia+1].y;		

        pb2.x = wallpnt[NumberPoint-1].x;
        pb2.y = wallpnt[NumberPoint-1].y;
        getInterAB(gst, &pa, &pb1, &pi1);
        getInterAB(gst, &pa, &pb2, &pi2);

        //	if((pi1.x-pa.x)*(pb1.x-pa.x) + (pi1.y-pa.y)*(pb1.y-pa.y) >= 0.0) ia=ia; //2011-12-23�޸�,��������
        //	else if((pi2.x-pa.x)*(pb2.x-pa.x) + (pi2.y-pa.y)*(pb2.y-pa.y) >= 0.0) ia=NumberAirfoilPoint-1;
        if((pa.x-pi1.x)*(pb1.x-pi1.x) + (pa.y-pi1.y)*(pb1.y-pi1.y) <= 0.0) ia=ia; //2011-12-23�޸�,��������
        else if((pa.x-pi2.x)*(pb2.x-pi2.x) + (pa.y-pi2.y)*(pb2.y-pi2.y) <= 0.0) ia=NumberPoint-1;
        else ia=ia;	
    }
    else
    {
        pa.x = wallpnt[ia].x;
        pa.y = wallpnt[ia].y;
        pb1.x = wallpnt[ia+1].x; 
        pb1.y = wallpnt[ia+1].y;		

        pb2.x = wallpnt[ia-1].x;
        pb2.y = wallpnt[ia-1].y;
        getInterAB(gst, &pa, &pb1, &pi1);
        getInterAB(gst, &pa, &pb2, &pi2);

        //	if((pi1.x-pa.x)*(pb1.x-pa.x) + (pi1.y-pa.y)*(pb1.y-pa.y) >= 0.0) ia=ia;
        //	else if((pi2.x-pa.x)*(pb2.x-pa.x) + (pi2.y-pa.y)*(pb2.y-pa.y) >= 0.0) ia=ia-1;

        if((pa.x-pi1.x)*(pb1.x-pi1.x) + (pa.y-pi1.y)*(pb1.y-pi1.y) <= 0.0) ia=ia; //2011-12-23�޸�,��������
        else if((pa.x-pi2.x)*(pb2.x-pi2.x) + (pa.y-pi2.y)*(pb2.y-pi2.y) <= 0.0) ia=ia-1;
        else ia=ia;		
    }
    return ia;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %esi, %ebx
movq %rdi, %r15
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, (%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movupd (%rdx), %xmm5
cmpl $0x2, %esi
jl 0x11a9f
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movapd %xmm0, %xmm2
subsd %xmm5, %xmm2
movapd %xmm5, %xmm3
unpckhpd %xmm5, %xmm3            # xmm3 = xmm3[1],xmm5[1]
movapd %xmm1, %xmm4
subsd %xmm3, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm2, %xmm2
addsd %xmm4, %xmm2
movl %ebx, %eax
leaq 0x18(%rdx), %rcx
xorl %r14d, %r14d
movl $0x1, %esi
movapd %xmm0, %xmm3
subsd -0x8(%rcx), %xmm3
movapd %xmm1, %xmm4
subsd (%rcx), %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm3, %xmm2
minsd %xmm2, %xmm3
cmoval %esi, %r14d
incq %rsi
addq $0x10, %rcx
movapd %xmm3, %xmm2
cmpq %rsi, %rax
jne 0x11951
testl %r14d, %r14d
je 0x11a9f
movslq %r14d, %rcx
shlq $0x4, %rcx
movups (%rdx,%rcx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x70(%rsp), %rbx
movaps %xmm0, (%rbx)
movups 0x10(%rdx,%rcx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
movups -0x10(%rdx,%rcx), %xmm0
leaq 0x40(%rsp), %r12
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%r12)
leaq 0x60(%rsp), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq %r13, %rcx
callq 0x1152d
movq %rsp, %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1152d
movapd 0x20(%rsp), %xmm5
movsd (%r13), %xmm0
movapd %xmm5, %xmm1
subsd %xmm0, %xmm1
movapd %xmm5, %xmm2
movapd 0x30(%rsp), %xmm3
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
subsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
movapd (%r13), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
subpd %xmm0, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
mulsd %xmm2, %xmm1
addsd %xmm3, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x11ba1
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movapd %xmm5, %xmm3
subsd %xmm1, %xmm3
movapd 0x10(%rsp), %xmm6
movapd %xmm6, %xmm4
subsd %xmm1, %xmm4
mulsd %xmm3, %xmm4
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
subsd %xmm2, %xmm5
unpckhpd %xmm6, %xmm6            # xmm6 = xmm6[1,1]
subsd %xmm2, %xmm6
mulsd %xmm5, %xmm6
addsd %xmm4, %xmm6
ucomisd %xmm6, %xmm0
leal -0x1(%r14), %eax
cmovbl %r14d, %eax
movl %eax, %r14d
jmp 0x11ba1
movslq %ebx, %rcx
shlq $0x4, %rcx
leaq 0x70(%rsp), %r14
movapd %xmm5, (%r14)
movups 0x10(%rdx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
movups -0x10(%rcx,%rdx), %xmm0
leaq 0x40(%rsp), %r12
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%r12)
leaq 0x60(%rsp), %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r13, %rcx
movapd %xmm5, 0x30(%rsp)
callq 0x1152d
movq %rsp, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1152d
movapd 0x30(%rsp), %xmm5
movsd (%r13), %xmm0
movapd %xmm5, %xmm1
subsd %xmm0, %xmm1
movapd %xmm5, %xmm2
movapd 0x20(%rsp), %xmm3
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
subsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
movapd (%r13), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
subpd %xmm0, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
mulsd %xmm2, %xmm1
addsd %xmm3, %xmm1
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x11ba1
decl %ebx
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movapd %xmm5, %xmm3
subsd %xmm1, %xmm3
movapd 0x10(%rsp), %xmm6
movapd %xmm6, %xmm4
subsd %xmm1, %xmm4
mulsd %xmm3, %xmm4
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
subsd %xmm2, %xmm5
unpckhpd %xmm6, %xmm6            # xmm6 = xmm6[1,1]
subsd %xmm2, %xmm6
mulsd %xmm5, %xmm6
addsd %xmm4, %xmm6
xorl %r14d, %r14d
ucomisd %xmm6, %xmm0
cmovael %ebx, %r14d
movl %r14d, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq