// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, -0x30(%rbp)
xorpd %xmm1, %xmm1
movapd %xmm0, -0x70(%rbp)
ucomisd %xmm0, %xmm1
ja 0x203e
movl %edx, %ebx
movl %edx, %eax
orl %esi, %eax
js 0x203e
movq %r9, %r14
movq %r8, %r15
movq -0x30(%rbp), %rax
movl (%rax), %eax
xorl %r13d, %r13d
testl %eax, %eax
jle 0x1c6a
movapd -0x70(%rbp), %xmm0
movq -0x30(%rbp), %rdx
ucomisd 0xfa08(%rdx,%r13,8), %xmm0
jae 0x1c6a
incq %r13
cmpq %r13, %rax
jne 0x1c53
movl %eax, %r13d
cmpl %ebx, %r13d
jl 0x2045
cmpl %esi, %r13d
jl 0x2045
testl %r13d, %r13d
je 0x2045
movl %ecx, -0x5c(%rbp)
movq 0x10(%rbp), %rcx
cmpl %ebx, %esi
movq %rbx, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
cmovll %esi, %ebx
xorl %eax, %eax
movq %rax, (%r15)
movq %rax, (%rcx)
movq -0x30(%rbp), %r12
movsd 0x5dc8(%r12,%rbx,8), %xmm0
movsd %xmm0, (%r14)
movapd -0x70(%rbp), %xmm0
divsd 0xfa08(%r12,%rbx,8), %xmm0
callq 0x1090
movq %rbx, %rcx
movapd %xmm0, %xmm4
movsd 0xfa08(%r12,%rbx,8), %xmm0
movapd 0x233f(%rip), %xmm1     # 0x4020
movapd -0x70(%rbp), %xmm2
xorpd %xmm2, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm0
movapd %xmm1, -0xc0(%rbp)
addsd %xmm1, %xmm0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0x1d11
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x1d2e
movq %rcx, %rbx
movsd %xmm4, -0x38(%rbp)
callq 0x1130
movsd -0x38(%rbp), %xmm4
xorpd %xmm3, %xmm3
movq %rbx, %rcx
movapd %xmm0, %xmm1
leal 0x1(%rcx), %eax
movl $0x1, %r12d
cmpl %r13d, %eax
movq -0x48(%rbp), %rdx
jge 0x1ea7
movq %r15, -0x78(%rbp)
movq %r14, -0x80(%rbp)
movl %eax, %eax
movl %r13d, %r12d
subl %ecx, %r12d
decq %r12
movslq %edx, %rcx
movslq -0x40(%rbp), %r14
cmpq %r14, %rcx
movq %r14, -0x90(%rbp)
movq %rcx, -0x88(%rbp)
cmovlq %rcx, %r14
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax,8), %r15
addq $0xfa08, %r15           # imm = 0xFA08
xorl %ebx, %ebx
xorpd %xmm5, %xmm5
movsd %xmm1, -0xa0(%rbp)
movsd %xmm5, -0xa8(%rbp)
movsd %xmm4, -0x38(%rbp)
movsd %xmm3, -0x58(%rbp)
leal (%r14,%rbx), %eax
movq -0x30(%rbp), %rcx
movsd 0xdac8(%rcx,%rax,8), %xmm0
movsd %xmm0, -0x98(%rbp)
movapd -0x70(%rbp), %xmm0
divsd (%r15,%rbx,8), %xmm0
callq 0x1090
movapd %xmm0, %xmm2
movsd (%r15,%rbx,8), %xmm0
mulsd %xmm0, %xmm0
addsd -0xc0(%rbp), %xmm0
ucomisd 0x22cf(%rip), %xmm0     # 0x40b8
jb 0x1df1
sqrtsd %xmm0, %xmm0
jmp 0x1e00
movsd %xmm2, -0x50(%rbp)
callq 0x1130
movsd -0x50(%rbp), %xmm2
movsd -0x38(%rbp), %xmm1
subsd %xmm2, %xmm1
movsd -0x98(%rbp), %xmm4
mulsd %xmm4, %xmm1
movsd -0x58(%rbp), %xmm3
addsd %xmm1, %xmm3
movsd -0xa0(%rbp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm4, %xmm1
movsd -0xa8(%rbp), %xmm5
addsd %xmm1, %xmm5
movsd -0x9c40(%r15,%rbx,8), %xmm1
movq -0x80(%rbp), %rax
movsd %xmm1, 0x8(%rax,%rbx,8)
movapd %xmm3, %xmm1
mulsd 0x2256(%rip), %xmm1     # 0x40b0
movq -0x78(%rbp), %rax
movsd %xmm1, 0x8(%rax,%rbx,8)
movq 0x10(%rbp), %rax
movsd %xmm5, 0x8(%rax,%rbx,8)
incq %rbx
movapd %xmm0, %xmm1
movapd %xmm2, %xmm4
cmpq %rbx, %r12
jne 0x1d88
leal 0x1(%rbx), %r12d
movq -0x90(%rbp), %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
cmovlq %rax, %rcx
addl %ebx, %ecx
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r15
jmp 0x1eb3
movapd %xmm4, %xmm2
movapd %xmm1, %xmm0
xorpd %xmm5, %xmm5
movl %ecx, %eax
movq -0x30(%rbp), %rcx
movsd 0xdac8(%rcx,%rax,8), %xmm1
mulsd %xmm1, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm1, %xmm0
addsd %xmm5, %xmm0
movsd %xmm0, -0x38(%rbp)
mulsd 0x21d1(%rip), %xmm2     # 0x40b0
movsd %xmm2, -0x50(%rbp)
movl %r12d, %ebx
movsd %xmm2, (%r15,%rbx,8)
movsd 0xbb88(%rcx,%rax,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movapd -0x70(%rbp), %xmm0
divsd 0xfa08(%rcx,%rax,8), %xmm0
callq 0x1050
movsd -0x38(%rbp), %xmm3
movsd -0x50(%rbp), %xmm2
mulsd -0x58(%rbp), %xmm0
movsd 0x216b(%rip), %xmm1     # 0x4090
subsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbx,8)
movq 0x10(%rbp), %r9
movsd %xmm3, (%r9,%rbx,8)
incl %r12d
leaq -0x8(%r14), %rax
leaq -0x8(%r9), %rcx
leaq -0x8(%r15), %rdx
addsd %xmm2, %xmm2
addsd %xmm3, %xmm3
movq %r15, %rsi
movq %r9, %rdi
movq %r14, %r8
movapd %xmm2, %xmm0
subsd (%rdx,%rbx,8), %xmm0
movsd %xmm0, (%rsi,%rbx,8)
movapd %xmm3, %xmm0
subsd (%rcx,%rbx,8), %xmm0
movsd %xmm0, (%rdi,%rbx,8)
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%r8,%rbx,8)
decl %r12d
addq $-0x8, %rax
addq $-0x8, %rcx
addq $-0x8, %rdx
addq $0x8, %r8
addq $0x8, %rdi
addq $0x8, %rsi
cmpl $0x1, %r12d
jg 0x1f59
movl -0x5c(%rbp), %edx
cmpl $0x1, %edx
jne 0x1fbf
leal (,%r13,2), %eax
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdi
subl %edi, %eax
subl %esi, %eax
jmp 0x1fd1
movq -0x40(%rbp), %rdi
movl %edi, %eax
movq -0x48(%rbp), %rsi
subl %esi, %eax
jg 0x1fd1
movl %esi, %eax
subl %edi, %eax
incl %eax
movl %edi, %ecx
subl %esi, %ecx
jle 0x204f
addl %r13d, %r13d
addl %edi, %esi
subl %esi, %r13d
cmpl $0x1, %edx
cmovel %ecx, %r13d
testl %eax, %eax
jle 0x204f
movslq %r13d, %rdi
leaq (%r15,%rdi,8), %rcx
movsd (%rcx), %xmm0
leaq (%r9,%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, (%r15,%r8,8)
movsd (%rdx,%r8,8), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%r9,%r8,8)
movsd (%rdi,%r8,8), %xmm2
movsd %xmm2, (%r14,%r8,8)
incq %r8
cmpq %r8, %rsi
jne 0x2008
jmp 0x204f
callq 0x12c4
jmp 0x204a
callq 0x1295
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq