// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, -0x48(%rbp)
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq 0x10(%rbp), %rbx
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1ec0b
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rdi, -0xa8(%rbp)
jb 0x1ec38
sqrtsd %xmm0, %xmm0
jmp 0x1ec3d
callq 0x84e0
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x60(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1ec3f
xorl %eax, %eax
movapd 0x4be6f(%rip), %xmm0    # 0x6aad0
movsd -0x60(%rbp,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, -0x60(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1ec61
leaq 0x4bfdf(%rip), %rsi     # 0x6ac60
leaq -0x180(%rbp), %rbx
movq %r13, %rdi
movq %rbx, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
movl $0x1, %r9d
callq 0x1e02e
leaq 0x4bfd5(%rip), %rsi     # 0x6ac80
leaq -0x160(%rbp), %r15
movq %r13, %rdi
movq %r15, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
movl $0x1, %r9d
callq 0x1e02e
leaq -0x140(%rbp), %rdx
movq %r12, %rdi
movq %rbx, %rsi
movl $0x3, %ecx
movl $0x3, %r8d
movl $0x1, %r9d
callq 0x1e02e
leaq -0xc0(%rbp), %rdx
movq %r12, %rdi
movq %r15, %rsi
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 0x1ed1a
movsd -0x60(%rbp), %xmm7
movsd -0x58(%rbp), %xmm8
movsd -0xb0(%rbp), %xmm1
movsd -0x50(%rbp), %xmm9
movsd -0xc0(%rbp), %xmm2
movsd -0xb8(%rbp), %xmm3
movapd 0x4bd69(%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 0x1edce
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0xa0(%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1edff
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1ee29
sqrtsd %xmm0, %xmm0
jmp 0x1ee98
movapd %xmm7, -0x120(%rbp)
movapd %xmm8, -0x110(%rbp)
movapd %xmm9, -0x100(%rbp)
movapd %xmm10, -0xf0(%rbp)
movapd %xmm11, -0xe0(%rbp)
movapd %xmm12, -0xd0(%rbp)
callq 0x84e0
movapd -0xd0(%rbp), %xmm12
movapd -0xe0(%rbp), %xmm11
movapd -0xf0(%rbp), %xmm10
movapd -0x100(%rbp), %xmm9
movapd -0x110(%rbp), %xmm8
movapd -0x120(%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 0x1ee9a
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 0x1eebf
movq -0x48(%rbp), %rax
movsd 0x10(%rax), %xmm1
movsd (%rax), %xmm2
movsd 0x8(%rax), %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 0x4bba1(%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 0x1ef31
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd -0x80(%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x1ef5c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1ef83
sqrtsd %xmm0, %xmm0
jmp 0x1ef88
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 0x1ef8a
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 0x1f007
movsd %xmm1, -0x48(%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 0x1f02b
movapd 0x4ba81(%rip), %xmm1    # 0x6aad0
xorpd %xmm0, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm1, %xmm2
ucomisd 0x4a8bd(%rip), %xmm2    # 0x69920
jbe 0x1f086
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq 0x4ba67(%rip), %rcx     # 0x6aae0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
callq 0x85f0
movapd 0x4ba3d(%rip), %xmm2    # 0x6aad0
xorpd %xmm0, %xmm2
xorpd %xmm1, %xmm1
cmpltsd -0x48(%rbp), %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
divsd 0x4b863(%rip), %xmm1    # 0x6a918
xorpd %xmm0, %xmm0
movq 0x20(%rbp), %rsi
movsd %xmm1, (%rsi)
movq -0xa8(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1f0d9
movl $0x0, (%rax)
movq 0x18(%rbp), %rdx
jmp 0x1f106
movq 0x18(%rbp), %rdx
movsd (%rdx), %xmm0
subsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm0
seta %al
leaq 0x4b9e7(%rip), %rcx     # 0x6aae0
addsd (%rcx,%rax,8), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi)
movsd %xmm1, (%rdx)
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq