// File: /workspace/github_c_makefile_star2_O1/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 0x2476
movl %edx, %ebx
movl %edx, %eax
orl %esi, %eax
js 0x2476
movq %r9, %r12
movq %rdi, %r15
movslq (%rdi), %rax
testq %rax, %rax
jle 0x20b8
xorl %r14d, %r14d
ucomisd 0xfa08(%r15,%r14,8), %xmm1
jae 0x20bb
incq %r14
cmpq %r14, %rax
jne 0x20a4
xorl %r14d, %r14d
testl %r14d, %r14d
cmovel %eax, %r14d
movsd 0x1ff6(%rip), %xmm0     # 0x40c0
ucomisd %xmm1, %xmm0
cmoval %eax, %r14d
cmpl %ebx, %r14d
jle 0x247b
cmpl %esi, %r14d
jle 0x247b
testl %r14d, %r14d
je 0x247b
movl %ecx, -0x58(%rbp)
movq %r8, -0x70(%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, -0x30(%rbp)
movsd 0xfa08(%r15,%rbx,8), %xmm0
movapd 0x1eec(%rip), %xmm2     # 0x4020
xorpd %xmm1, %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm0, %xmm0
movapd %xmm2, -0xb0(%rbp)
addsd %xmm2, %xmm0
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
movsd %xmm2, -0x48(%rbp)
ucomisd %xmm3, %xmm0
jb 0x2165
sqrtsd %xmm0, %xmm4
jmp 0x2173
callq 0x1130
movapd -0x80(%rbp), %xmm1
movapd %xmm0, %xmm4
movq %r12, -0x88(%rbp)
cmpl %r13d, %ebx
jge 0x228f
movsd %xmm4, -0x50(%rbp)
movl %ebx, %eax
leaq (%r15,%rax,8), %r12
addq $0x11948, %r12          # imm = 0x11948
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm0, -0x48(%rbp)
movsd -0x3e80(%r12), %xmm0
movsd %xmm0, -0x68(%rbp)
movapd %xmm1, %xmm0
divsd -0x1f38(%r12), %xmm0
callq 0x1090
movapd %xmm0, %xmm3
movsd -0x1f38(%r12), %xmm0
mulsd %xmm0, %xmm0
addsd -0xb0(%rbp), %xmm0
ucomisd 0x1ecc(%rip), %xmm0     # 0x40b8
jb 0x21f7
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x220a
movsd %xmm3, -0x60(%rbp)
callq 0x1130
movsd -0x60(%rbp), %xmm3
movapd %xmm0, %xmm4
movsd (%r12), %xmm0
movsd 0x1e80(%rip), %xmm1     # 0x4098
ucomisd %xmm0, %xmm1
movapd -0x80(%rbp), %xmm1
ja 0x247b
movsd -0x50(%rbp), %xmm6
subsd %xmm4, %xmm6
movsd -0x68(%rbp), %xmm7
mulsd %xmm7, %xmm6
movsd -0x38(%rbp), %xmm2
addsd %xmm6, %xmm2
movsd %xmm2, -0x38(%rbp)
movsd -0x30(%rbp), %xmm5
subsd %xmm3, %xmm5
mulsd %xmm7, %xmm5
movsd -0x48(%rbp), %xmm2
addsd %xmm5, %xmm2
movsd %xmm2, -0x48(%rbp)
divsd %xmm0, %xmm6
movsd -0x40(%rbp), %xmm0
addsd %xmm6, %xmm0
movsd %xmm0, -0x40(%rbp)
addq $0x8, %r12
incl %ebx
movsd %xmm4, -0x50(%rbp)
movsd %xmm3, -0x30(%rbp)
cmpl %ebx, %r13d
jg 0x21a8
jmp 0x22a2
movsd -0x30(%rbp), %xmm3
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq 0x10(%rbp), %rcx
cmpl $0x1, -0x58(%rbp)
jne 0x23d0
leal 0x1(%r13), %eax
xorpd %xmm2, %xmm2
cmpl %r14d, %eax
jge 0x23f5
movl %eax, %eax
leaq (%r15,%rax,8), %rbx
addq $0xfa08, %rbx           # imm = 0xFA08
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
movsd %xmm6, -0x68(%rbp)
movsd %xmm2, -0x58(%rbp)
movsd %xmm5, -0x50(%rbp)
movsd %xmm4, -0x30(%rbp)
movsd %xmm3, -0x60(%rbp)
movl %r13d, %r12d
movsd 0xdac8(%r15,%r12,8), %xmm0
movsd %xmm0, -0x98(%rbp)
movapd %xmm1, %xmm0
divsd (%rbx), %xmm0
callq 0x1090
movapd %xmm0, %xmm7
movsd (%rbx), %xmm0
mulsd %xmm0, %xmm0
addsd -0xb0(%rbp), %xmm0
ucomisd 0x1d8b(%rip), %xmm0     # 0x40b8
jb 0x2335
sqrtsd %xmm0, %xmm0
jmp 0x234a
movsd %xmm7, -0x90(%rbp)
callq 0x1130
movsd -0x90(%rbp), %xmm7
movsd 0x11948(%r15,%r12,8), %xmm1
movsd 0x1d3c(%rip), %xmm2     # 0x4098
ucomisd %xmm1, %xmm2
movsd -0x60(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movsd -0x50(%rbp), %xmm5
movsd -0x68(%rbp), %xmm6
ja 0x247b
subsd %xmm0, %xmm3
movsd -0x98(%rbp), %xmm4
mulsd %xmm4, %xmm3
addsd %xmm3, %xmm5
subsd %xmm7, %xmm2
mulsd %xmm4, %xmm2
movsd -0x58(%rbp), %xmm4
addsd %xmm2, %xmm4
movapd %xmm4, %xmm2
divsd %xmm1, %xmm3
addsd %xmm3, %xmm6
leal 0x1(%r12), %r13d
addq $0x8, %rbx
addl $0x2, %r12d
movapd %xmm0, %xmm4
movapd %xmm7, %xmm3
cmpl %r12d, %r14d
movapd -0x80(%rbp), %xmm1
jg 0x22d6
jmp 0x2405
movsd -0x48(%rbp), %xmm0
mulsd 0x1cd3(%rip), %xmm0     # 0x40b0
movq -0x70(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%rcx)
movsd -0x40(%rbp), %xmm1
jmp 0x2467
movapd %xmm3, %xmm7
movapd %xmm4, %xmm0
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
decl %r14d
movsd 0xdac8(%r15,%r14,8), %xmm1
mulsd %xmm1, %xmm7
mulsd %xmm1, %xmm0
addsd %xmm0, %xmm5
divsd 0x11948(%r15,%r14,8), %xmm0
addsd %xmm2, %xmm7
addsd %xmm6, %xmm0
addsd %xmm7, %xmm7
addsd -0x48(%rbp), %xmm7
mulsd 0x1c6f(%rip), %xmm7     # 0x40b0
movq -0x70(%rbp), %rax
movsd %xmm7, (%rax)
addsd %xmm5, %xmm5
addsd -0x38(%rbp), %xmm5
movq 0x10(%rbp), %rax
movsd %xmm5, (%rax)
addsd %xmm0, %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movq -0x88(%rbp), %rax
movsd %xmm1, (%rax)
xorl %eax, %eax
jmp 0x2480
callq 0x12f3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq