// File: /workspace/github_cpp_makefile_star2_O3/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 0x12be2(%rip), %rax     # 0x24200
movl (%rax), %ecx
testl %ecx, %ecx
jle 0x117a8
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
leaq 0x12d46(%rip), %rdx     # 0x24380
addq $0x8, %rdx
movsd 0xc20a(%rip), %xmm2     # 0x1d850
xorl %edi, %edi
movapd %xmm0, %xmm3
subsd -0x8(%rdx), %xmm3
movapd %xmm1, %xmm4
subsd (%rdx), %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x11679
movl $0x0, (%rsi)
movl (%rax), %ecx
movapd %xmm3, %xmm2
movl %edi, %ebx
incq %rdi
movslq %ecx, %r8
addq $0x10, %rdx
cmpq %r8, %rdi
jl 0x11648
testl %ebx, %ebx
je 0x117a8
movslq (%rsi), %rax
movslq %ebx, %rcx
shlq $0x4, %rcx
imulq $0x1f40, %rax, %rax     # imm = 0x1F40
addq 0x12928(%rip), %rax     # 0x23fd0
movaps (%rcx,%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x70(%rsp), %r15
movaps %xmm0, (%r15)
movaps 0x10(%rcx,%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps -0x10(%rcx,%rax), %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 0x1152d
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %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 0x118be
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 0x118be
movslq (%rsi), %rcx
imulq $0x1f40, %rcx, %rdx     # imm = 0x1F40
leaq 0x12bc7(%rip), %rsi     # 0x24380
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 0x1152d
movq %rsp, %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1152d
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 0x118be
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 0x118be
movl %r13d, %ebx
movl %ebx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq