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

	int i,nturn,npt;
	int l0,il;
	int l;
	int k0;
	double w;
	double pdiv0,pdiv1;
	double psq0,psq1;
	double xsum;
	double tsum;
	double xtmp;
	double ttmp;
	double p2;

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

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

	if (ks > nturn || nturn == 0 || ke > nturn){
		fprintf(stderr,"The wave cannot arrive the ke layer\n");
		return -1;
	}

	l0 = ks < ke ? ks : ke;
	p2 = p*p;

	xsum  = 0.;
	tsum  = 0.;
	xc[0] = 0.;
	ti[0] = 0.;
	yc[0] = m->depth[l0];

	pdiv0 = acos(p/m->p[l0]);
	psq0  = sqrt(m->p[l0]*m->p[l0]-p2);

	for (l=l0+1,i=1;l < nturn;l++,i++){
		w = m->B[l-1];
		pdiv1 = acos(p/m->p[l]);
		psq1  = sqrt(m->p[l]*m->p[l] - p2);
		xsum += w*(pdiv0 - pdiv1);
		tsum += w*(psq0 - psq1);
		yc[i] = m->depth[l];
		xc[i] = xsum*R2D;
		ti[i] = tsum;

		pdiv0 = pdiv1;
		psq0  = psq1;
	}

	w = m->B[l-1];
	xsum += w*pdiv0;
	tsum += w*psq0;
	xsum  = xc[i] = xsum*R2D;
	yc[i] = Rearth - m->radius[l-1]*pow(p/m->p[l-1],w);
	ti[i] = tsum;
	
	for (l=i-1;l>=0;l--,i++){
		xc[i] = 2.*xsum - xc[l];
		ti[i] = 2.*tsum - ti[l];
		yc[i] = yc[l];
	}

	if (TURN == 1){
		npt = 2*nturn + 1 - ks - ke;
	}else{
		if ( ks <= ke ){
			npt = ke - ks + 1;
		}else{
			npt = ks - ke + 1;
		}
	}

	if ( ks <= ke ){
		return npt;
	}else{
		if (TURN == 1){
			k0 = ks - ke;
		}else{
			k0 = 2*nturn - ks - ke;
		}

		xtmp = xc[k0];
		ttmp = ti[k0];
		for (i=0,il=k0;i < npt;i++,il++){
			xc[i] = xc[il] - xtmp;
			ti[i] = ti[il] - ttmp;
			yc[i] = yc[il];
		}

		return npt;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x2078
movl %edx, %eax
orl %esi, %eax
js 0x2078
movq %rdi, %r15
movl (%rdi), %eax
testl %eax, %eax
jle 0x2071
movq %r9, %r14
movq 0x10(%rbp), %rdi
xorl %r12d, %r12d
ucomisd 0xfa08(%r15,%r12,8), %xmm1
jae 0x1ca8
incq %r12
cmpq %r12, %rax
jne 0x1c91
movl %eax, %r12d
cmpl %edx, %r12d
jl 0x2071
cmpl %esi, %r12d
jl 0x2071
testl %r12d, %r12d
je 0x2071
movl %ecx, -0x54(%rbp)
cmpl %edx, %esi
movq %rdx, -0x60(%rbp)
movq %rdx, %rbx
movq %rsi, -0x38(%rbp)
cmovll %esi, %ebx
xorl %eax, %eax
movq %r8, -0x48(%rbp)
movq %rax, (%r8)
movq %rax, (%rdi)
movsd 0x5dc8(%r15,%rbx,8), %xmm0
movsd %xmm0, (%r14)
movapd %xmm1, %xmm0
divsd 0xfa08(%r15,%rbx,8), %xmm0
movapd %xmm1, -0x80(%rbp)
callq 0x1090
movapd -0x80(%rbp), %xmm2
movapd %xmm0, %xmm5
movsd 0xfa08(%r15,%rbx,8), %xmm0
movapd 0x22fc(%rip), %xmm1     # 0x4020
xorpd %xmm2, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm0
movapd %xmm1, -0xc0(%rbp)
addsd %xmm1, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
jb 0x1d4f
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x1d6b
movsd %xmm5, -0x30(%rbp)
callq 0x1130
movsd -0x30(%rbp), %xmm5
xorpd %xmm4, %xmm4
movapd -0x80(%rbp), %xmm2
movapd %xmm0, %xmm1
leal 0x1(%rbx), %eax
movl $0x1, %r13d
cmpl %r12d, %eax
movq -0x48(%rbp), %rcx
jge 0x1ee1
movq %r14, -0x68(%rbp)
movl %eax, %ecx
movl %r12d, %eax
subl %ebx, %eax
movq %rax, -0x90(%rbp)
decq %rax
movq %rax, -0x98(%rbp)
movslq -0x60(%rbp), %rax
movslq -0x38(%rbp), %r13
cmpq %r13, %rax
cmovlq %rax, %r13
movq %rcx, -0x88(%rbp)
leaq (%r15,%rcx,8), %rbx
addq $0xfa08, %rbx           # imm = 0xFA08
xorl %r14d, %r14d
xorpd %xmm6, %xmm6
movsd %xmm1, -0xa8(%rbp)
movsd %xmm6, -0xb0(%rbp)
movsd %xmm5, -0x30(%rbp)
movsd %xmm4, -0x50(%rbp)
leal (%r14,%r13), %eax
movsd 0xdac8(%r15,%rax,8), %xmm0
movsd %xmm0, -0xa0(%rbp)
divsd (%rbx,%r14,8), %xmm2
movapd %xmm2, %xmm0
callq 0x1090
movapd %xmm0, %xmm3
movsd (%rbx,%r14,8), %xmm0
mulsd %xmm0, %xmm0
addsd -0xc0(%rbp), %xmm0
ucomisd 0x2296(%rip), %xmm0     # 0x40b8
jb 0x1e2a
sqrtsd %xmm0, %xmm0
jmp 0x1e39
movsd %xmm3, -0x40(%rbp)
callq 0x1130
movsd -0x40(%rbp), %xmm3
movsd -0x30(%rbp), %xmm1
subsd %xmm3, %xmm1
movsd -0xa0(%rbp), %xmm2
mulsd %xmm2, %xmm1
movsd -0x50(%rbp), %xmm4
addsd %xmm1, %xmm4
movsd -0xa8(%rbp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd -0xb0(%rbp), %xmm6
addsd %xmm1, %xmm6
movsd -0x9c40(%rbx,%r14,8), %xmm1
movq -0x68(%rbp), %rax
movsd %xmm1, 0x8(%rax,%r14,8)
movapd %xmm4, %xmm1
mulsd 0x221c(%rip), %xmm1     # 0x40b0
movq -0x48(%rbp), %rcx
movsd %xmm1, 0x8(%rcx,%r14,8)
movq 0x10(%rbp), %rax
movsd %xmm6, 0x8(%rax,%r14,8)
incq %r14
movapd %xmm0, %xmm1
movapd %xmm3, %xmm5
cmpq %r14, -0x98(%rbp)
movapd -0x80(%rbp), %xmm2
jne 0x1dc5
movq -0x88(%rbp), %rax
leal (%rax,%r14), %ebx
decl %ebx
movl -0x90(%rbp), %r13d
movq -0x68(%rbp), %r14
jmp 0x1eed
movapd %xmm5, %xmm3
movapd %xmm1, %xmm0
xorpd %xmm6, %xmm6
movsd 0xdac8(%r15,%rbx,8), %xmm1
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
mulsd %xmm1, %xmm0
addsd %xmm6, %xmm0
movsd %xmm0, -0x30(%rbp)
mulsd 0x219c(%rip), %xmm3     # 0x40b0
movsd %xmm3, -0x40(%rbp)
movsd %xmm3, (%rcx,%r13,8)
movsd 0xbb88(%r15,%rbx,8), %xmm0
movsd %xmm0, -0x50(%rbp)
divsd 0xfa08(%r15,%rbx,8), %xmm2
movapd %xmm2, %xmm0
callq 0x1050
movsd -0x30(%rbp), %xmm3
movsd -0x40(%rbp), %xmm2
movq -0x48(%rbp), %r10
mulsd -0x50(%rbp), %xmm0
movsd 0x2134(%rip), %xmm1     # 0x4090
subsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r13,8)
movq 0x10(%rbp), %r11
movsd %xmm3, (%r11,%r13,8)
leal 0x1(%r13), %eax
leaq -0x8(%r14), %rcx
leaq -0x8(%r11), %rdx
leaq -0x8(%r10), %rsi
addsd %xmm2, %xmm2
addsd %xmm3, %xmm3
movq %r10, %rdi
movq %r11, %r8
movq %r14, %r9
movapd %xmm2, %xmm0
subsd (%rsi,%r13,8), %xmm0
movsd %xmm0, (%rdi,%r13,8)
movapd %xmm3, %xmm0
subsd (%rdx,%r13,8), %xmm0
movsd %xmm0, (%r8,%r13,8)
movsd (%rcx,%r13,8), %xmm0
movsd %xmm0, (%r9,%r13,8)
decl %eax
addq $-0x8, %rcx
addq $-0x8, %rdx
addq $-0x8, %rsi
addq $0x8, %r9
addq $0x8, %r8
addq $0x8, %rdi
cmpl $0x1, %eax
jg 0x1f91
movl -0x54(%rbp), %edx
cmpl $0x1, %edx
movq -0x60(%rbp), %rsi
jne 0x1ff6
leal (%r12,%r12), %eax
movq -0x38(%rbp), %rdi
subl %edi, %eax
subl %esi, %eax
jmp 0x2004
movq -0x38(%rbp), %rdi
movl %edi, %eax
subl %esi, %eax
jg 0x2004
movl %esi, %eax
subl %edi, %eax
incl %eax
movl %edi, %ecx
subl %esi, %ecx
jle 0x2082
addl %r12d, %r12d
addl %edi, %esi
subl %esi, %r12d
cmpl $0x1, %edx
cmovel %ecx, %r12d
testl %eax, %eax
jle 0x2082
movslq %r12d, %rdi
leaq (%r10,%rdi,8), %rcx
movsd (%rcx), %xmm0
leaq (%r11,%rdi,8), %rdx
movsd (%rdx), %xmm1
movl %eax, %esi
leaq (%r14,%rdi,8), %rdi
xorl %r8d, %r8d
movsd (%rcx,%r8,8), %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, (%r10,%r8,8)
movsd (%rdx,%r8,8), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%r11,%r8,8)
movsd (%rdi,%r8,8), %xmm2
movsd %xmm2, (%r14,%r8,8)
incq %r8
cmpq %r8, %rsi
jne 0x203b
jmp 0x2082
callq 0x1295
jmp 0x207d
callq 0x12c4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq