// File: /workspace/github_cpp_makefile_star2_O2/DoubleString[P]RtConverter/src/Rnxobs/Com.cpp
// Function: bamboo::phase_windup_itrs(int*, double (*) [3], double (*) [3], double*, double*, double*, double*, double*, double*)
void phase_windup_itrs(int* lfirst, double (*rot_f2j)[3], double (*rot_l2f)[3],
		double* xbf, double* ybf, double* zbf, double* xrec2sat, double* dphi0,
		double* dphi) {
	// purpose  : phase wind-up correction ( the receiver and satellite antenna orientation dependent
	//            phase corrections). See Wu J.T., et al., Manuscripta Geogetica (1993) 18, pp91-98
	//           lfirst -- first call for this satellite-station pair
	//           rot_f2j -- rotation matrix from earth-fixed to inertial (J2000)
	//           rot_l2f -- rotation matrix from station system (enu right-hand system) to earth-fixed
	//           xbf,ybf,zbf -- unit vectors of spacecraft-fixed system (rotation matrix from body-fixed
	//                          to inertial)
	//           xrec2sat -- vector from rec. to satellite  for k-direction
	//           dphi0 -- initial dphi
	//           dphi  --  phase correction
	int j, n;
	double x_f[3], y_f[3], dummy[3], rlength, kusi;
	double k[3], d_r[3], d_s[3];
	// right hand system
	double x_l[3] = { 0.0, 1.0, 0.0 };
	double y_l[3] = { -1.0, 0.0, 0.0 };

	unit_vector(3, xrec2sat, k, &rlength);

	for (j = 0; j < 3; j++)
		k[j] = -k[j];

	/*equivalent antenna dipole for both receiver and satellite antenna
	 D = x_j - k (k . x_j) - k x y_j
	 k is the unit vector of signal transmitting direction

	 transfer the unit vector of the antenna dipole unit in local/antenna system to
	 inertial system.
	 For ground receiver local   => earth fixed      => inertial
	 For LEO    receiver antenna => spacecraft fixed => inertial*/

	matmpy((double*)rot_l2f, x_l, x_f, 3, 3, 1);
	matmpy((double*)rot_l2f, y_l, y_f, 3, 3, 1);
	// D = x_j - k(k . x_j) + k x y_j
	rlength = dot(3, k, x_f);
	cross(k, y_f, dummy);

	for (j = 0; j < 3; j++)
		d_r[j] = x_f[j] - rlength * k[j] + dummy[j];

	unit_vector(3, d_r, d_r, &rlength);

	// the same for the satellite antenna
	rlength = dot(3, k, xbf);
	cross(k, ybf, dummy);
	for (j = 0; j < 3; j++)
		d_s[j] = xbf[j] - rlength * k[j] - dummy[j];
	unit_vector(3, d_s, d_s, &rlength);

	cross(d_s, d_r, dummy);

	kusi = dot(3, k, dummy);
	*dphi = dot(3, d_s, d_r);

	if ((ABS(*dphi)) > 1.0)
		*dphi = NINT(*dphi) * 1.0;
	*dphi = SIGN(1.0,kusi) * acos(*dphi) / (2 * PI);

	if (*lfirst) {
		*lfirst = false;
		n = 0;
	} else
		n = NINT(*dphi0 - *dphi);
	*dphi = n + *dphi;

	// save for the next epoch
	(*dphi0) = (*dphi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r8, -0x30(%rbp)
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, -0x78(%rbp)
movq 0x10(%rbp), %rsi
pushq $0x3
popq %rdi
leaq -0x50(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0x1b27e
xorl %eax, %eax
movapd 0x4552d(%rip), %xmm0    # 0x60ac0
cmpq $0x3, %rax
je 0x1b5ae
movsd -0x50(%rbp,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, -0x50(%rbp,%rax,8)
incq %rax
jmp 0x1b593
leaq 0x4569b(%rip), %rsi     # 0x60c50
leaq -0xd0(%rbp), %rdx
pushq $0x3
popq %rbx
pushq $0x1
popq %r15
movq %r12, %rdi
movl %ebx, %ecx
movl %ebx, %r8d
movl %r15d, %r9d
callq 0x1b061
leaq 0x45696(%rip), %rsi     # 0x60c70
leaq -0x110(%rbp), %r13
movq %r12, %rdi
movq %r13, %rdx
movl %ebx, %ecx
movl %ebx, %r8d
movl %r15d, %r9d
callq 0x1b061
leaq -0x50(%rbp), %r15
movl %ebx, %edi
movq %r15, %rsi
leaq -0xd0(%rbp), %rdx
callq 0x1b2ff
movsd %xmm0, -0x38(%rbp)
movsd (%r15), %xmm1
movsd 0x8(%r15), %xmm2
movsd 0x10(%r13), %xmm3
movsd 0x10(%r15), %xmm4
movsd (%r13), %xmm5
movsd 0x8(%r13), %xmm6
movapd %xmm2, %xmm7
mulsd %xmm3, %xmm7
mulsd %xmm1, %xmm3
mulsd %xmm6, %xmm1
mulsd %xmm4, %xmm6
subsd %xmm6, %xmm7
mulsd %xmm5, %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm5
subsd %xmm5, %xmm1
movsd %xmm7, -0x70(%rbp)
movsd %xmm4, -0x68(%rbp)
movsd %xmm1, -0x60(%rbp)
xorpd 0x45450(%rip), %xmm0    # 0x60ac0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1b69f
movsd -0x50(%rbp,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0xd0(%rbp,%rax,8), %xmm1
addsd -0x70(%rbp,%rax,8), %xmm1
movsd %xmm1, -0xb0(%rbp,%rax,8)
incq %rax
jmp 0x1b672
pushq $0x3
popq %rbx
leaq -0xb0(%rbp), %rdx
leaq -0x38(%rbp), %r15
movl %ebx, %edi
movq %rdx, %rsi
movq %r15, %rcx
callq 0x1b27e
leaq -0x50(%rbp), %r12
movl %ebx, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x1b2ff
movapd %xmm0, -0x90(%rbp)
movsd %xmm0, (%r15)
leaq -0x70(%rbp), %rdx
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x1b226
movapd -0x90(%rbp), %xmm1
xorpd 0x453c8(%rip), %xmm1    # 0x60ac0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1b724
movsd -0x50(%rbp,%rax,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r14,%rax,8), %xmm0
subsd -0x70(%rbp,%rax,8), %xmm0
movsd %xmm0, -0xf0(%rbp,%rax,8)
incq %rax
jmp 0x1b6fa
pushq $0x3
popq %r14
leaq -0xf0(%rbp), %r15
leaq -0x38(%rbp), %rcx
movl %r14d, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x1b27e
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
leaq -0xb0(%rbp), %r12
movsd 0x10(%r12), %xmm2
movsd 0x10(%r15), %xmm3
movsd (%r12), %xmm4
movsd 0x8(%r12), %xmm5
movapd %xmm1, %xmm6
mulsd %xmm2, %xmm6
mulsd %xmm0, %xmm2
mulsd %xmm5, %xmm0
mulsd %xmm3, %xmm5
subsd %xmm5, %xmm6
mulsd %xmm4, %xmm3
subsd %xmm2, %xmm3
mulsd %xmm1, %xmm4
subsd %xmm4, %xmm0
leaq -0x70(%rbp), %rdx
movsd %xmm6, (%rdx)
movsd %xmm3, 0x8(%rdx)
movsd %xmm0, 0x10(%rdx)
leaq -0x50(%rbp), %rsi
movl %r14d, %edi
callq 0x1b2ff
movsd %xmm0, -0x30(%rbp)
movl %r14d, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b2ff
movapd 0x452f2(%rip), %xmm1    # 0x60ac0
xorpd %xmm0, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm1, %xmm2
ucomisd 0x4413e(%rip), %xmm2    # 0x5f920
jbe 0x1b805
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq 0x452d8(%rip), %rcx     # 0x60ad0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
callq 0x85f0
movapd 0x452ae(%rip), %xmm2    # 0x60ac0
xorpd %xmm0, %xmm2
xorpd %xmm1, %xmm1
cmpltsd -0x30(%rbp), %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
divsd 0x450d4(%rip), %xmm1    # 0x60908
xorpd %xmm0, %xmm0
movq 0x20(%rbp), %rsi
movsd %xmm1, (%rsi)
movq -0x78(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1b852
andl $0x0, (%rax)
movq 0x18(%rbp), %rdx
jmp 0x1b87f
movq 0x18(%rbp), %rdx
movsd (%rdx), %xmm0
subsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm0
seta %al
leaq 0x4525e(%rip), %rcx     # 0x60ad0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi)
movsd %xmm1, (%rdx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq