// File: /workspace/github_cpp_makefile_star2_O2/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 $0x90, %rsp
movl %esi, %ebx
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movsd (%rdi), %xmm0
movsd 0x8(%rdi), %xmm1
movupd (%rdx), %xmm6
movapd %xmm0, %xmm2
subsd %xmm6, %xmm2
movapd %xmm6, %xmm5
unpckhpd %xmm6, %xmm5            # xmm5 = xmm5[1],xmm6[1]
movapd %xmm1, %xmm3
subsd %xmm5, %xmm3
mulsd %xmm3, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm3, %xmm2
movslq %esi, %rax
leaq 0x18(%rdx), %rcx
xorl %r14d, %r14d
pushq $0x1
popq %rsi
cmpq %rax, %rsi
jge 0x104f4
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
cmoval %esi, %r14d
minsd %xmm2, %xmm3
incq %rsi
addq $0x10, %rcx
movapd %xmm3, %xmm2
jmp 0x104b9
testl %r14d, %r14d
je 0x10609
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 0x100b4
movq %rsp, %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x100b4
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 0x10715
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 0x10715
movapd %xmm5, 0x10(%rsp)
decl %ebx
leaq 0x70(%rsp), %r14
movapd %xmm6, (%r14)
movups 0x10(%rdx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
movslq %ebx, %rcx
shlq $0x4, %rcx
movups (%rdx,%rcx), %xmm0
leaq 0x40(%rsp), %r12
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, (%r12)
leaq 0x60(%rsp), %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r13, %rcx
movapd %xmm6, 0x30(%rsp)
callq 0x100b4
movq %rsp, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x100b4
movapd 0x30(%rsp), %xmm4
movsd (%r13), %xmm0
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
movapd %xmm4, %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 0x10715
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
subsd %xmm1, %xmm4
movapd 0x80(%rsp), %xmm5
movapd %xmm5, %xmm3
subsd %xmm1, %xmm3
mulsd %xmm4, %xmm3
movapd 0x10(%rsp), %xmm1
subsd %xmm2, %xmm1
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
subsd %xmm2, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm3, %xmm5
xorl %eax, %eax
ucomisd %xmm5, %xmm0
cmovbl %eax, %ebx
movl %ebx, %r14d
movl %r14d, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq