// File: /workspace/github_cpp_makefile_star2_O3/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 $0x118, %rsp            # imm = 0x118
movq %r8, %r15
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, %rbx
movq 0x10(%rbp), %r13
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1e718
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1e740
sqrtsd %xmm0, %xmm0
jmp 0x1e745
callq 0x84e0
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x60(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1e747
xorl %eax, %eax
movapd 0x4c365(%rip), %xmm0    # 0x6aad0
movsd -0x60(%rbp,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, -0x60(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1e76b
leaq 0x4c4d5(%rip), %rsi     # 0x6ac60
leaq -0x140(%rbp), %rdx
movq %r12, %rdi
movl $0x3, %ecx
movl $0x3, %r8d
movl $0x1, %r9d
callq 0x1e02e
leaq 0x4c4ce(%rip), %rsi     # 0x6ac80
leaq -0xd0(%rbp), %rdx
movq %r12, %rdi
movl $0x3, %ecx
movl $0x3, %r8d
movl $0x1, %r9d
callq 0x1e02e
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0x60(%rbp,%rax,8), %xmm1
mulsd -0x140(%rbp,%rax,8), %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1e7d8
movsd -0x60(%rbp), %xmm7
movsd -0x58(%rbp), %xmm8
movsd -0xc0(%rbp), %xmm1
movsd -0x50(%rbp), %xmm9
movsd -0xd0(%rbp), %xmm2
movsd -0xc8(%rbp), %xmm3
movapd 0x4c2ab(%rip), %xmm4    # 0x6aad0
movapd %xmm9, %xmm10
xorpd %xmm4, %xmm10
movapd %xmm7, %xmm5
mulsd %xmm3, %xmm5
mulsd %xmm9, %xmm3
movapd %xmm8, %xmm6
mulsd %xmm1, %xmm6
subsd %xmm3, %xmm6
movapd %xmm7, %xmm11
xorpd %xmm4, %xmm11
mulsd %xmm7, %xmm1
movapd %xmm9, %xmm3
mulsd %xmm2, %xmm3
subsd %xmm1, %xmm3
movapd %xmm8, %xmm12
xorpd %xmm4, %xmm12
mulsd %xmm8, %xmm2
subsd %xmm2, %xmm5
movsd %xmm6, -0x40(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm5, -0x30(%rbp)
xorpd %xmm4, %xmm0
xorl %eax, %eax
movsd -0x60(%rbp,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0x140(%rbp,%rax,8), %xmm1
addsd -0x40(%rbp,%rax,8), %xmm1
movsd %xmm1, -0xa0(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1e88c
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0xa0(%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1e8bd
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1e8e7
sqrtsd %xmm0, %xmm0
jmp 0x1e956
movapd %xmm7, -0xb0(%rbp)
movapd %xmm8, -0x120(%rbp)
movapd %xmm9, -0x110(%rbp)
movapd %xmm10, -0x100(%rbp)
movapd %xmm11, -0xf0(%rbp)
movapd %xmm12, -0xe0(%rbp)
callq 0x84e0
movapd -0xe0(%rbp), %xmm12
movapd -0xf0(%rbp), %xmm11
movapd -0x100(%rbp), %xmm10
movapd -0x110(%rbp), %xmm9
movapd -0x120(%rbp), %xmm8
movapd -0xb0(%rbp), %xmm7
xorl %eax, %eax
movsd -0xa0(%rbp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0xa0(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1e958
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0x60(%rbp,%rax,8), %xmm1
mulsd (%r14,%rax,8), %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1e97d
movsd 0x10(%r15), %xmm1
movsd (%r15), %xmm2
movsd 0x8(%r15), %xmm3
mulsd %xmm3, %xmm10
mulsd %xmm1, %xmm8
addsd %xmm10, %xmm8
mulsd %xmm1, %xmm11
mulsd %xmm2, %xmm9
addsd %xmm11, %xmm9
mulsd %xmm2, %xmm12
mulsd %xmm3, %xmm7
addsd %xmm12, %xmm7
movsd %xmm8, -0x40(%rbp)
movsd %xmm9, -0x38(%rbp)
movsd %xmm7, -0x30(%rbp)
xorpd 0x4c0e4(%rip), %xmm0    # 0x6aad0
xorl %eax, %eax
movsd -0x60(%rbp,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%rax,8), %xmm1
subsd -0x40(%rbp,%rax,8), %xmm1
movsd %xmm1, -0x80(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1e9ee
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0x80(%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1ea19
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1ea40
sqrtsd %xmm0, %xmm0
jmp 0x1ea45
callq 0x84e0
xorl %eax, %eax
movsd -0x80(%rbp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x80(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1ea47
movsd -0x80(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
movsd -0x90(%rbp), %xmm2
movsd -0x70(%rbp), %xmm3
movsd -0xa0(%rbp), %xmm4
movsd -0x98(%rbp), %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
movsd %xmm6, -0x40(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd -0x60(%rbp,%rax,8), %xmm0
mulsd -0x40(%rbp,%rax,8), %xmm0
addsd %xmm0, %xmm1
incq %rax
cmpq $0x3, %rax
jne 0x1eac4
movsd %xmm1, -0xb0(%rbp)
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0x80(%rbp,%rax,8), %xmm1
mulsd -0xa0(%rbp,%rax,8), %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1eaeb
movapd 0x4bfc1(%rip), %xmm1    # 0x6aad0
xorpd %xmm0, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm1, %xmm2
ucomisd 0x4adfd(%rip), %xmm2    # 0x69920
jbe 0x1eb46
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq 0x4bfa7(%rip), %rcx     # 0x6aae0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
callq 0x85f0
movapd 0x4bf7d(%rip), %xmm2    # 0x6aad0
xorpd %xmm0, %xmm2
xorpd %xmm1, %xmm1
cmpltsd -0xb0(%rbp), %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
divsd 0x4bda0(%rip), %xmm1    # 0x6a918
xorpd %xmm0, %xmm0
movq 0x20(%rbp), %rsi
movsd %xmm1, (%rsi)
cmpl $0x0, (%rbx)
je 0x1eb95
movl $0x0, (%rbx)
movq 0x18(%rbp), %rdx
jmp 0x1ebc2
movq 0x18(%rbp), %rdx
movsd (%rdx), %xmm0
subsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm0
seta %al
leaq 0x4bf2b(%rip), %rcx     # 0x6aae0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi)
movsd %xmm1, (%rdx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq