// File: 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
subq $0x150, %rsp            # imm = 0x150
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x876ca(%rip), %rax     # 0xbd1a0
movq %rax, -0x120(%rbp)
movq 0x876c4(%rip), %rax     # 0xbd1a8
movq %rax, -0x118(%rbp)
movq 0x876be(%rip), %rax     # 0xbd1b0
movq %rax, -0x110(%rbp)
movq 0x876c0(%rip), %rax     # 0xbd1c0
movq %rax, -0x140(%rbp)
movq 0x876ba(%rip), %rax     # 0xbd1c8
movq %rax, -0x138(%rbp)
movq 0x876b4(%rip), %rax     # 0xbd1d0
movq %rax, -0x130(%rbp)
movq 0x10(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movl $0x3, %edi
leaq -0x98(%rbp), %rcx
callq 0x356a0
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x35b88
movslq -0x34(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0xc0(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x35b46
movq -0x18(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x70(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
leaq -0xc0(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0x98(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x35590
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x35c72
movslq -0x34(%rbp), %rax
movsd -0x50(%rbp,%rax,8), %xmm1
movsd -0x98(%rbp), %xmm0
movslq -0x34(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x34(%rbp), %rax
addsd -0x90(%rbp,%rax,8), %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0xe0(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x35c09
leaq -0xe0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
movl $0x3, %edi
leaq -0x98(%rbp), %rcx
callq 0x356a0
leaq -0xc0(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0x98(%rbp)
leaq -0xc0(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x35590
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x35d38
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x98(%rbp), %xmm0
movslq -0x34(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x34(%rbp), %rax
subsd -0x90(%rbp,%rax,8), %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0x100(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x35ccc
leaq -0x100(%rbp), %rsi
leaq -0x100(%rbp), %rdx
movl $0x3, %edi
leaq -0x98(%rbp), %rcx
callq 0x356a0
leaq -0x100(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x35590
leaq -0xc0(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0xa0(%rbp)
leaq -0x100(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x39000
movsd 0x8609a(%rip), %xmm1    # 0xbbe60
ucomisd %xmm1, %xmm0
jbe 0x35df1
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x39050
cvtsi2sd %eax, %xmm0
movsd 0x8607b(%rip), %xmm1    # 0xbbe60
mulsd %xmm1, %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0xa0(%rbp), %xmm1
movsd 0x8605f(%rip), %xmm0    # 0xbbe60
callq 0x390a0
movsd %xmm0, -0x148(%rbp)
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x9720
movaps %xmm0, %xmm1
movsd -0x148(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x8702e(%rip), %xmm1    # 0xbce60
divsd %xmm1, %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, (%rax)
je 0x35e5a
movq -0x8(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x38(%rbp)
jmp 0x35e72
movq 0x18(%rbp), %rax
movsd (%rax), %xmm0
movq 0x20(%rbp), %rax
subsd (%rax), %xmm0
callq 0x39050
movl %eax, -0x38(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
movq 0x20(%rbp), %rax
addsd (%rax), %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
movq 0x18(%rbp), %rax
movsd %xmm0, (%rax)
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq