// File: /workspace/github_c_makefile_star2_O2/YunyiQian[P]multitel3/./raytrace.c
// Function: raytrace2
int raytrace2(MODEL *m,double p,int ks,int ke,int TURN,double *xc,double *tt,double *ti){

	int i,k;
	int l0,l1;
	int nturn;
	double w;
	double pdiv0,pdiv1;
	double psq0,psq1;
	double xsum,xtmp;
	double tsum,ttmp;
	double dt;
	double tstar,temp;
	double p2;

	if (ks < 0 || p < 0 || ke < 0){
		fprintf(stderr,"Input is illegal\n");
		return -1;
	}

	for (i=0,nturn=0;i < m->nlay;i++){
		if (m->p[i] <= p){
			nturn = i;
			break;
		}
	}

	if (p < 1.4 || nturn == 0){
		nturn = m->nlay;
	}

	if (ks >= nturn || nturn == 0 || ke >= nturn){
//if (nturn > 45)
//printf("ks=%d\tke=%d\tnturn=%d\n",ks,ke,nturn);
//		fprintf(stderr,"The wave cannot arrives the ke layer\n");
		return -1;
	}

	if ( ks > ke ){
		l0 = ke;
		l1 = ks;
	}else{
		l0 = ks;
		l1 = ke;
	}

	p2 = p*p;
	xsum  = 0.;
	tsum  = 0.;
	tstar = 0.;
	pdiv0 = acos(p/m->p[l0]);
	psq0  = sqrt(m->p[l0]*m->p[l0] - p2);

	for (i=l0+1;i<=l1;i++){
		w = m->B[i-1];
		pdiv1  = acos(p/m->p[i]);
		psq1   = sqrt(m->p[i]*m->p[i] - p2);
		xsum  += w*(pdiv0 - pdiv1);
		dt     = w*(psq0 - psq1);
		tsum  += dt;
		if ( m->Q[i-1] < SMALL ){
//			printf ("Bad Q value in raytrace2\n");
			return -1;
		}else{
			tstar += dt/m->Q[i-1];
		}
		pdiv0  = pdiv1;
		psq0   = psq1;
	}

	if (TURN == 1){
		xtmp = xsum;
		ttmp = tsum;
		temp = tstar;
		xsum = 0.;
		tsum = 0.;
		tstar = 0.;

		for (i=l1+1;i<nturn;i++){
			w = m->B[i-1];
			pdiv1  = acos(p/m->p[i]);
			psq1   = sqrt(m->p[i]*m->p[i] - p2);
			xsum  += w*(pdiv0 - pdiv1);
			dt     = w*(psq0 - psq1);
			tsum  += dt;
			if ( m->Q[i-1] < SMALL ){
//				printf ("Bad Q value in raytrace2\n");
				return -1;
			}else{
				tstar += dt/m->Q[i-1];
			}
			pdiv0  = pdiv1;
			psq0   = psq1;
		}
		w = m->B[nturn-1];
		xsum  += w*pdiv0;
		dt     = w*psq0;
		tsum  += dt;
		tstar += dt/m->Q[nturn-1];
		
		*xc = R2D*(2.*xsum + xtmp);
		*ti = 2.*tsum + ttmp;
//		*bot = Rearth - m->radius[nturn-1]*pow(p/m->p[nturn-1],w);
		*tt = 2.*tstar + temp;
	}else{
		*xc = xsum*R2D;
		*ti = tsum;
//		*bot = m->depth[l1];
		*tt = tstar;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x2163
movl %edx, %ebx
movl %edx, %eax
orl %esi, %eax
js 0x2163
movl %ecx, %r12d
movq %rdi, %r15
movl (%rdi), %r14d
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
cmpq %rax, %rcx
je 0x1e07
ucomisd 0xfa08(%r15,%rax,8), %xmm1
jae 0x1ded
incq %rax
jmp 0x1dd7
testq %rax, %rax
cmovel %r14d, %eax
movsd 0x22c4(%rip), %xmm0     # 0x40c0
ucomisd %xmm1, %xmm0
cmoval %r14d, %eax
movl %eax, %r14d
pushq $-0x1
popq %rax
cmpl %ebx, %r14d
jle 0x2182
cmpl %esi, %r14d
jle 0x2182
testl %r14d, %r14d
je 0x2182
movq %r8, -0x70(%rbp)
movq %r9, -0x88(%rbp)
cmpl %ebx, %esi
movl %ebx, %r13d
cmovgl %esi, %r13d
cmovll %esi, %ebx
movapd %xmm1, %xmm0
divsd 0xfa08(%r15,%rbx,8), %xmm0
movapd %xmm1, -0x80(%rbp)
callq 0x1090
movapd -0x80(%rbp), %xmm1
movsd %xmm0, -0x38(%rbp)
movsd 0xfa08(%r15,%rbx,8), %xmm0
movapd 0x21b0(%rip), %xmm2     # 0x4020
xorpd %xmm1, %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm0, %xmm0
movapd %xmm2, -0xb0(%rbp)
addsd %xmm2, %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movl %r12d, -0x58(%rbp)
jb 0x1e9c
sqrtsd %xmm0, %xmm0
jmp 0x1ea6
callq 0x1130
movapd -0x80(%rbp), %xmm1
movsd %xmm0, -0x30(%rbp)
leaq (%r15,%rbx,8), %r12
addq $0x11948, %r12          # imm = 0x11948
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd %xmm0, -0x48(%rbp)
movsd %xmm0, -0x50(%rbp)
cmpl %ebx, %r13d
jle 0x1fac
movsd -0x3e80(%r12), %xmm0
movsd %xmm0, -0x68(%rbp)
movapd %xmm1, %xmm0
divsd -0x1f38(%r12), %xmm0
callq 0x1090
movapd %xmm0, %xmm5
movsd -0x1f38(%r12), %xmm0
mulsd %xmm0, %xmm0
addsd -0xb0(%rbp), %xmm0
ucomisd 0x21a2(%rip), %xmm0     # 0x40b8
jb 0x1f1e
sqrtsd %xmm0, %xmm0
jmp 0x1f2d
movsd %xmm5, -0x60(%rbp)
callq 0x1130
movsd -0x60(%rbp), %xmm5
movsd (%r12), %xmm1
movsd 0x215d(%rip), %xmm2     # 0x4098
ucomisd %xmm1, %xmm2
ja 0x217f
movsd -0x30(%rbp), %xmm4
subsd %xmm0, %xmm4
movsd -0x68(%rbp), %xmm3
mulsd %xmm3, %xmm4
movsd -0x48(%rbp), %xmm2
addsd %xmm4, %xmm2
movsd %xmm2, -0x48(%rbp)
movsd -0x38(%rbp), %xmm2
subsd %xmm5, %xmm2
mulsd %xmm3, %xmm2
movsd -0x40(%rbp), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, -0x40(%rbp)
divsd %xmm1, %xmm4
movsd -0x50(%rbp), %xmm1
addsd %xmm4, %xmm1
movsd %xmm1, -0x50(%rbp)
addq $0x8, %r12
incl %ebx
movsd %xmm5, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
movapd -0x80(%rbp), %xmm1
jmp 0x1ec9
cmpl $0x1, -0x58(%rbp)
jne 0x20bf
movl %r13d, %eax
leaq (%r15,%rax,8), %rbx
addq $0x11948, %rbx          # imm = 0x11948
incl %r13d
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
cmpl %r13d, %r14d
jle 0x20e8
movsd %xmm5, -0x60(%rbp)
movsd %xmm4, -0x68(%rbp)
movsd %xmm3, -0x58(%rbp)
movsd -0x3e80(%rbx), %xmm0
movsd %xmm0, -0x98(%rbp)
movapd %xmm1, %xmm0
divsd -0x1f38(%rbx), %xmm0
callq 0x1090
movapd %xmm0, %xmm6
movsd -0x1f38(%rbx), %xmm0
mulsd %xmm0, %xmm0
addsd -0xb0(%rbp), %xmm0
ucomisd 0x208c(%rip), %xmm0     # 0x40b8
jb 0x2034
sqrtsd %xmm0, %xmm0
jmp 0x2049
movsd %xmm6, -0x90(%rbp)
callq 0x1130
movsd -0x90(%rbp), %xmm6
movsd (%rbx), %xmm1
movsd 0x2043(%rip), %xmm2     # 0x4098
ucomisd %xmm1, %xmm2
movsd -0x68(%rbp), %xmm4
movsd -0x60(%rbp), %xmm5
ja 0x217f
movsd -0x30(%rbp), %xmm3
subsd %xmm0, %xmm3
movsd -0x98(%rbp), %xmm7
mulsd %xmm7, %xmm3
addsd %xmm3, %xmm4
movsd -0x38(%rbp), %xmm2
subsd %xmm6, %xmm2
mulsd %xmm7, %xmm2
addsd %xmm2, %xmm5
divsd %xmm1, %xmm3
movsd -0x58(%rbp), %xmm1
addsd %xmm3, %xmm1
movapd %xmm1, %xmm3
addq $0x8, %rbx
incl %r13d
movsd %xmm6, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
movapd -0x80(%rbp), %xmm1
jmp 0x1fd3
movsd -0x40(%rbp), %xmm0
mulsd 0x1fe4(%rip), %xmm0     # 0x40b0
movq -0x70(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x10(%rbp), %rax
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x50(%rbp), %xmm0
jmp 0x2154
decl %r14d
movsd 0xdac8(%r15,%r14,8), %xmm0
movsd -0x38(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm4
divsd 0x11948(%r15,%r14,8), %xmm2
addsd %xmm5, %xmm1
addsd %xmm2, %xmm3
addsd %xmm1, %xmm1
addsd -0x40(%rbp), %xmm1
mulsd 0x1f82(%rip), %xmm1     # 0x40b0
movq -0x70(%rbp), %rax
movsd %xmm1, (%rax)
addsd %xmm4, %xmm4
addsd -0x48(%rbp), %xmm4
movq 0x10(%rbp), %rax
movsd %xmm4, (%rax)
addsd %xmm3, %xmm3
movsd -0x50(%rbp), %xmm0
addsd %xmm3, %xmm0
movq -0x88(%rbp), %rax
movsd %xmm0, (%rax)
xorl %eax, %eax
jmp 0x2182
movq 0x3e8e(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x20d9(%rip), %rdi      # 0x424d
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
pushq $-0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq