// File: /workspace/github_cpp_makefile_star2_O1/jamesliu516[P]amrdg2d/wall4amrdg2013.cpp
// Function: getIndexAtWallPoint(pointxy const*, int&)
int getIndexAtWallPoint(const PXYZ *gst, int &nb)
{
    int ils, ia,ib,i;
    double dis,dis2;
    PXYZ pa,pi1,pi2,pb1,pb2;
    int wy;

    //	ib=NumberAirfoilPoint;
    dis=1.0e10;
    //  ia=0;
    for(wy=0; wy<N_BODY;++wy){
        for(i=0;i<=NWallPts[wy]-1;++i)
        {
            dis2 = (gst->x-MultiCircle[wy][i].x)*(gst->x-MultiCircle[wy][i].x)
                +(gst->y-MultiCircle[wy][i].y)*(gst->y-MultiCircle[wy][i].y);
            if(dis2<dis){
                dis=dis2;
                ia=i;
                nb=wy;
            }
        }
    }

    if(ia==0)
    {
        pa.x = MultiCircle[nb][ia].x;
        pa.y = MultiCircle[nb][ia].y;
        pb1.x = MultiCircle[nb][ia+1].x; 
        pb1.y = MultiCircle[nb][ia+1].y;		

        pb2.x = MultiCircle[nb][NWallPts[nb]-1].x;
        pb2.y = MultiCircle[nb][NWallPts[nb]-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=NWallPts[nb]-1;
        else ia=ia;	
    }
    else
    {
        pa.x = MultiCircle[nb][ia].x;
        pa.y = MultiCircle[nb][ia].y;
        pb1.x = MultiCircle[nb][ia+1].x; 
        pb1.y = MultiCircle[nb][ia+1].y;		

        pb2.x = MultiCircle[nb][ia-1].x;
        pb2.y = MultiCircle[nb][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
movq %rdi, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, (%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
leaq 0x136f8(%rip), %rax     # 0x25200
cmpl $0x0, (%rax)
jle 0x11b6d
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
leaq 0x13861(%rip), %rcx     # 0x25380
addq $0x8, %rcx
movsd 0xcd45(%rip), %xmm2     # 0x1e870
xorl %edx, %edx
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
jbe 0x11b5c
movl $0x0, (%rsi)
movapd %xmm3, %xmm2
movl %edx, %ebx
incq %rdx
movslq (%rax), %rdi
addq $0x10, %rcx
cmpq %rdi, %rdx
jl 0x11b2d
jmp 0x11b6d
movslq (%rsi), %rcx
testl %ebx, %ebx
je 0x11c8d
movslq %ebx, %rax
imulq $0x1f40, %rcx, %rcx     # imm = 0x1F40
addq 0x13447(%rip), %rcx     # 0x24fd0
shlq $0x4, %rax
movaps (%rax,%rcx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x70(%rsp), %r15
movaps %xmm0, (%r15)
movaps 0x10(%rax,%rcx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps -0x10(%rax,%rcx), %xmm0
leaq 0x40(%rsp), %r12
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%r12)
leaq 0x60(%rsp), %r13
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rcx
callq 0x11a17
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x11a17
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 0x11da0
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(%rbx), %eax
cmovbl %ebx, %eax
movl %eax, %ebx
jmp 0x11da0
imulq $0x1f40, %rcx, %rdx     # imm = 0x1F40
leaq 0x136e5(%rip), %rsi     # 0x25380
leaq (%rsi,%rdx), %rdi
movaps (%rsi,%rdx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x70(%rsp), %rbx
movaps %xmm0, (%rbx)
movaps 0x10(%rsi,%rdx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rdx
movaps %xmm0, (%rdx)
movslq (%rax,%rcx,4), %r13
decq %r13
movq %r13, %rax
shlq $0x4, %rax
movaps (%rax,%rdi), %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%r15)
leaq 0x60(%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x11a17
movq %rsp, %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x11a17
movapd 0x20(%rsp), %xmm5
movsd (%r12), %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 (%r12), %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 %ebx, %ebx
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x11da0
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
jb 0x11da0
movl %r13d, %ebx
movl %ebx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq