// File: DoubleString[P]RtConverter/src/Rnxobs/Com.cpp
// Function: bamboo::phase_windup(int*, double (*) [3], double (*) [3], double*, double*, double*, double*, double*, double*)
void phase_windup(int* lfirst, double (*rot_f2j)[3], double (*rot_l2f)[3],
		double* xbf, double* ybf, double* zbf, double* xrec2sat, double* dphi0,
		double* dphi) {

	int j, n;
	double x_f[3], y_f[3], x_j[3], y_j[3], dummy[3], rlength, kusi;
	double k[3], d_r[3], d_s[3];

	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);
	matmpy((double*)rot_f2j, x_f, x_j, 3, 3, 1);
	matmpy((double*)rot_f2j, y_f, y_j, 3, 3, 1);

	// D = x_j - k(k . x_j) + k x y_j
	rlength = dot(3, k, x_j);
	cross(k, y_j, dummy);

	for (j = 0; j < 3; j++)
		d_r[j] = x_j[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 $0x190, %rsp            # imm = 0x190
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 0x8730a(%rip), %rax     # 0xbd1e0
movq %rax, -0x160(%rbp)
movq 0x87304(%rip), %rax     # 0xbd1e8
movq %rax, -0x158(%rbp)
movq 0x872fe(%rip), %rax     # 0xbd1f0
movq %rax, -0x150(%rbp)
movq 0x87300(%rip), %rax     # 0xbd200
movq %rax, -0x180(%rbp)
movq 0x872fa(%rip), %rax     # 0xbd208
movq %rax, -0x178(%rbp)
movq 0x872f4(%rip), %rax     # 0xbd210
movq %rax, -0x170(%rbp)
movq 0x10(%rbp), %rsi
leaq -0x100(%rbp), %rdx
movl $0x3, %edi
leaq -0xd8(%rbp), %rcx
callq 0x356a0
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x35f88
movslq -0x34(%rbp), %rax
movsd -0x100(%rbp,%rax,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0x100(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x35f46
movq -0x18(%rbp), %rdi
leaq -0x160(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
movq -0x18(%rbp), %rdi
leaq -0x180(%rbp), %rsi
leaq -0x70(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
movl $0x3, %r8d
movl $0x1, %r9d
movl %r8d, %ecx
callq 0x35310
leaq -0x100(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0xd8(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x35590
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x360c1
movslq -0x34(%rbp), %rax
movsd -0x90(%rbp,%rax,8), %xmm1
movsd -0xd8(%rbp), %xmm0
movslq -0x34(%rbp), %rax
movsd -0x100(%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 -0xd0(%rbp,%rax,8), %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0x120(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x36055
leaq -0x120(%rbp), %rsi
leaq -0x120(%rbp), %rdx
movl $0x3, %edi
leaq -0xd8(%rbp), %rcx
callq 0x356a0
leaq -0x100(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0xd8(%rbp)
leaq -0x100(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x35590
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x36187
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0xd8(%rbp), %xmm0
movslq -0x34(%rbp), %rax
movsd -0x100(%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 -0xd0(%rbp,%rax,8), %xmm0
movslq -0x34(%rbp), %rax
movsd %xmm0, -0x140(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3611b
leaq -0x140(%rbp), %rsi
leaq -0x140(%rbp), %rdx
movl $0x3, %edi
leaq -0xd8(%rbp), %rcx
callq 0x356a0
leaq -0x140(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x35590
leaq -0x100(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movsd %xmm0, -0xe0(%rbp)
leaq -0x140(%rbp), %rsi
leaq -0x120(%rbp), %rdx
movl $0x3, %edi
callq 0x35770
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x39000
movsd 0x85c4b(%rip), %xmm1    # 0xbbe60
ucomisd %xmm1, %xmm0
jbe 0x36240
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x39050
cvtsi2sd %eax, %xmm0
movsd 0x85c2c(%rip), %xmm1    # 0xbbe60
mulsd %xmm1, %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0xe0(%rbp), %xmm1
movsd 0x85c10(%rip), %xmm0    # 0xbbe60
callq 0x390a0
movsd %xmm0, -0x188(%rbp)
movq 0x20(%rbp), %rax
movsd (%rax), %xmm0
callq 0x9720
movaps %xmm0, %xmm1
movsd -0x188(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x86bdf(%rip), %xmm1    # 0xbce60
divsd %xmm1, %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, (%rax)
je 0x362a9
movq -0x8(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x38(%rbp)
jmp 0x362c1
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 $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nop