// File: /workspace/github_cpp_makefile_star2_O2/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
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movb $0x1, %dil
movsd 0xb6dd(%rip), %xmm0     # 0x1b880
leaq 0x131d6(%rip), %rcx     # 0x23380
leaq 0x8(%rcx), %rdx
leaq 0x1304b(%rip), %rax     # 0x23200
testb $0x1, %dil
je 0x1020c
movl (%rax), %r8d
movq %rdx, %rdi
xorl %r9d, %r9d
movslq %r8d, %r10
cmpq %r10, %r9
jge 0x10208
movsd (%r14), %xmm1
subsd -0x8(%rdi), %xmm1
movsd 0x8(%r14), %xmm2
subsd (%rdi), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x101ff
andl $0x0, (%rsi)
movl (%rax), %r8d
movapd %xmm1, %xmm0
movl %r9d, %ebx
incq %r9
addq $0x10, %rdi
jmp 0x101c4
xorl %edi, %edi
jmp 0x101b5
movslq (%rsi), %rsi
testl %ebx, %ebx
je 0x1032c
movslq %ebx, %rax
imulq $0x1f40, %rsi, %rcx     # imm = 0x1F40
addq 0x12da8(%rip), %rcx     # 0x22fd0
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 0x100b4
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %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 0x10438
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 0x10438
imulq $0x1f40, %rsi, %rdx     # imm = 0x1F40
leaq (%rcx,%rdx), %rdi
movaps (%rcx,%rdx), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x70(%rsp), %rbx
movaps %xmm0, (%rbx)
movaps 0x10(%rcx,%rdx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rdx
movaps %xmm0, (%rdx)
movslq (%rax,%rsi,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 0x100b4
movq %rsp, %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x100b4
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 0x10438
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 0x10438
movl %r13d, %ebx
movl %ebx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq