// File: /workspace/github_cpp_makefile_star2_O1/DoubleString[P]eph_converter/src/B2bDecoder.cpp
// Function: B2b::m_geneph()
void B2b::m_geneph(){
	FILE* fp = NULL;
	char filename[256] = {0};
	int mjd,iyear,imon,iday,ih,im,gweek,gd;
	double dx[6],length,A[3],Cr[3],R[3],dxx[6],sec;
	double sod,dintv = 900,dt,dtmin,dtlimit =120,xsat_out[6],xsat[6];
	/******** generate the ephemeris here *****************/
	cout << "begin to recover ephemeris ..." << endl;
	mjd = mjd0;
	sod = sod0;
	sod = ((int)(sod / dintv)) * dintv; /**force it to the epoch**/
	mjd2date(mjd,sod,&iyear,&imon,&iday,&ih,&im,&sec);
	gpsweek(iyear,imon,iday,&gweek,&gd);
	sprintf(filename,"cov%04d%d.sp3",gweek,gd);
	if(!(fp = fopen(filename,"w"))){cout << "can't open file " << filename << " to write!" << endl;exit(1);}
	m_wtephheader(fp);
	while(true){
		mjd2date(mjd,sod,&iyear,&imon,&iday,&ih,&im,&sec);
		fprintf(fp,"*   %4d%3d%3d%3d%3d%12.8lf\n",iyear,imon,iday,ih,im,sec);
		for(auto& cprn : cprn_list){
			/*************get the minus ssr*****************/
			dtmin = 9e9;
			orbit* ssr_selected = nullptr;
			for(auto& ssr : ORB_ALL){
				dt = (ssr.mjd - mjd) * 86400.0 + ssr.sod - sod;
				if(dtmin > fabs(dt)){
					dtmin = fabs(dt);
					ssr_selected = &ssr;
				}
			}
			if(dtmin > dtlimit || ssr_selected == nullptr) continue;
			/*************request for the brdm *****************/
			if(ssr_selected->ORB.find(cprn) == ssr_selected->ORB.end()) continue;
			if(!(nav.v_readOrbit(cprn.c_str(),mjd,sod,xsat_out,&ssr_selected->ORB[cprn].iode))){
//				cout << "ssr do not match ephemeris for " << cprn << " " << mjd << " " << sod << endl;
				continue;
			}
			/*************update the eph here *****************/
			memcpy(dx,ssr_selected->ORB[cprn].orb,sizeof(double) * 6);
			unit_vector(3,xsat_out,R,&length);
			cross(xsat_out,xsat_out + 3,Cr);
			unit_vector(3, Cr, Cr, &length);
			cross(Cr, R, A);
			unit_vector(3, A, A, &length);

//			unit_vector(3, xsat_out + 3, A, &length);
//			cross(xsat_out, xsat_out + 3, Cr);
//			unit_vector(3, Cr, Cr, &length);
//			cross(A, Cr, R);
//			unit_vector(3, R, R, &length);

			memset(dxx, 0, sizeof(dxx));
			dxx[0] = R[0] * dx[0] + A[0] * dx[1] + Cr[0] * dx[2];
			dxx[1] = R[1] * dx[0] + A[1] * dx[1] + Cr[1] * dx[2];
			dxx[2] = R[2] * dx[0] + A[2] * dx[1] + Cr[2] * dx[2];

			// velocity
			dxx[3] = R[0] * dx[3] + A[0] * dx[4] + Cr[0] * dx[5];
			dxx[4] = R[1] * dx[3] + A[1] * dx[4] + Cr[1] * dx[5];
			dxx[5] = R[2] * dx[3] + A[2] * dx[4] + Cr[2] * dx[5];

			for (int i = 0; i < 6; i++) {
				xsat[i] = xsat_out[i] - dxx[i];
			}
			double clock_sp3 = 999.0;
			/**** clock part *****/
			{
				double xclk_out,c[3],dclk;
				dtmin = 9e9;
				clk *ssr_selected = nullptr;
				for (auto &ssr : CLK_ALL) {
					dt = (ssr.mjd - mjd) * 86400.0 + ssr.sod - sod;
					if (dtmin > fabs(dt)) {
						dtmin = fabs(dt);
						ssr_selected = &ssr;
					}
				}
				if (dtmin < dtlimit && ssr_selected != nullptr){
					/*************request for the brdm *****************/
					if (ssr_selected->CLK.find(cprn) != ssr_selected->CLK.end()){
						if ((nav.v_readClk(cprn.c_str(), mjd, sod, &xclk_out, &ssr_selected->CLK[cprn].iode))) {
							/*************update the eph here *****************/
							memcpy(c, ssr_selected->CLK[cprn].clk, sizeof(double) * 3);
							dclk = c[0] + dtmin * (c[1] + dtmin * c[2]);
							clock_sp3 = xclk_out - dclk / VEL_LIGHT;
						}
					}
				}
			}
			/**************write it into orbit file *********/
			fprintf(fp,"P%s%14.6lf%14.6lf%14.6lf%14.6lf\n",cprn.c_str(), xsat[0] / 1000.0 , xsat[1] / 1000.0,xsat[2] / 1000.0,clock_sp3 != 999.0 ? clock_sp3 * 1E6 : 999.0);
		}
		timinc(mjd,sod,dintv,&mjd,&sod);
		if( (mjd - mjd1) * 86400.0 + sod - sod1 >= 0.0)
			break;
	}
	fclose(fp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, %rbx
leaq -0x2c0(%rbp), %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
movq 0xd9c4(%rip), %r15      # 0x11fd8
leaq 0x8b1c(%rip), %rsi      # 0xd137
movl $0x1e, %edx
movq %r15, %rdi
callq 0x3330
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31f0
movl (%rbx), %edi
movl %edi, -0x2c(%rbp)
movsd 0x10(%rbx), %xmm0
movsd 0x89ef(%rip), %xmm1     # 0xd050
divsd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, (%rsp)
leaq -0x4c(%rbp), %r15
leaq -0x48(%rbp), %r12
leaq -0x44(%rbp), %r13
leaq -0x40(%rbp), %r8
leaq -0x3c(%rbp), %r9
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0xbef6
movl (%r15), %edi
movl (%r12), %esi
movl (%r13), %edx
leaq -0xc0(%rbp), %r15
leaq -0xbc(%rbp), %r12
movq %r15, %rcx
movq %r12, %r8
callq 0xabdd
movl (%r15), %edx
movl (%r12), %ecx
leaq 0x8a81(%rip), %rsi      # 0xd156
movq %r14, %rdi
xorl %eax, %eax
callq 0x3080
leaq 0x8a7e(%rip), %rsi      # 0xd164
movq %r14, %rdi
callq 0x3350
testq %rax, %rax
je 0x4d13
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4d56
leaq 0x20(%rbx), %rax
movq %rax, -0x80(%rbp)
movq %r14, -0x78(%rbp)
movl -0x2c(%rbp), %edi
movsd -0x38(%rbp), %xmm0
leaq -0x88(%rbp), %rax
movq %rax, (%rsp)
leaq -0x4c(%rbp), %rsi
leaq -0x48(%rbp), %rdx
leaq -0x44(%rbp), %rcx
leaq -0x40(%rbp), %r8
leaq -0x3c(%rbp), %r9
callq 0xbef6
movl -0x4c(%rbp), %edx
movl -0x48(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x40(%rbp), %r9d
movl -0x3c(%rbp), %eax
movsd -0x88(%rbp), %xmm0
movl %eax, (%rsp)
movq %r14, %rdi
leaq 0x8a0e(%rip), %rsi      # 0xd171
movb $0x1, %al
callq 0x33e0
movq 0xd78(%rbx), %r12
movq 0xd80(%rbx), %rax
movq %rax, -0xb0(%rbp)
cmpq %rax, %r12
je 0x4ca7
movq 0xd48(%rbx), %rax
movq 0xd50(%rbx), %rcx
cmpq %rcx, %rax
je 0x4800
movl -0x2c(%rbp), %edx
movsd -0x38(%rbp), %xmm0
movsd 0x887d(%rip), %xmm1     # 0xd028
xorl %r14d, %r14d
movsd 0x887a(%rip), %xmm3     # 0xd030
movapd 0x88c2(%rip), %xmm4     # 0xd080
movapd %xmm1, %xmm2
movl (%rax), %esi
subl %edx, %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm3, %xmm1
addsd 0x8(%rax), %xmm1
subsd %xmm0, %xmm1
andpd %xmm4, %xmm1
ucomisd %xmm1, %xmm2
cmovaq %rax, %r14
minsd %xmm2, %xmm1
addq $0x40, %rax
cmpq %rcx, %rax
jne 0x47be
ucomisd 0x883d(%rip), %xmm1     # 0xd038
seta %al
jmp 0x4805
movb $0x1, %al
xorl %r14d, %r14d
testb %al, %al
jne 0x4c96
testq %r14, %r14
je 0x4c96
leaq 0x10(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x6ef4
addq $0x18, %r14
cmpq %r14, %rax
je 0x4c96
movq (%r12), %r15
movl -0x2c(%rbp), %r14d
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x595a
leaq 0x4(%rax), %r8
movq -0x80(%rbp), %rdi
movq %r15, %rsi
movl %r14d, %edx
movsd -0x70(%rbp), %xmm0
leaq -0x1c0(%rbp), %rcx
callq 0xa90e
testl %eax, %eax
je 0x4c96
movq %r13, %rdi
movq %r12, %rsi
callq 0x595a
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0x28(%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x3, %edi
leaq -0x1c0(%rbp), %r14
movq %r14, %rsi
leaq -0x110(%rbp), %r15
movq %r15, %rdx
leaq -0x158(%rbp), %r13
movq %r13, %rcx
callq 0xc046
movq %r14, %rdi
leaq -0x1a8(%rbp), %rsi
leaq -0x130(%rbp), %r14
movq %r14, %rdx
callq 0xc0d9
movl $0x3, %edi
movq %r14, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0xc046
movq %r14, %rdi
movq %r15, %rsi
leaq -0x150(%rbp), %r14
movq %r14, %rdx
callq 0xc0d9
movl $0x3, %edi
movq %r14, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0xc046
xorps %xmm0, %xmm0
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movsd -0x150(%rbp), %xmm3
movsd -0x148(%rbp), %xmm0
movsd -0x90(%rbp), %xmm8
movapd %xmm8, %xmm2
mulsd %xmm3, %xmm2
movsd -0x110(%rbp), %xmm4
movsd -0x108(%rbp), %xmm1
movapd %xmm4, %xmm5
movsd -0x98(%rbp), %xmm11
mulsd %xmm11, %xmm5
addsd %xmm2, %xmm5
movsd -0x130(%rbp), %xmm6
movsd -0x128(%rbp), %xmm2
movapd %xmm6, %xmm7
movsd -0xa0(%rbp), %xmm10
mulsd %xmm10, %xmm7
addsd %xmm5, %xmm7
movsd %xmm7, -0xf0(%rbp)
movapd %xmm8, %xmm5
movapd %xmm8, %xmm9
mulsd %xmm0, %xmm5
movapd %xmm1, %xmm7
mulsd %xmm11, %xmm7
addsd %xmm5, %xmm7
movapd %xmm2, %xmm5
mulsd %xmm10, %xmm5
addsd %xmm7, %xmm5
movsd %xmm5, -0xe8(%rbp)
movsd -0x100(%rbp), %xmm5
movsd -0x140(%rbp), %xmm8
mulsd %xmm8, %xmm9
movapd %xmm11, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm9, %xmm7
movapd %xmm7, %xmm9
movsd -0x120(%rbp), %xmm7
mulsd %xmm7, %xmm10
addsd %xmm9, %xmm10
movsd %xmm10, -0xe0(%rbp)
movsd -0xa8(%rbp), %xmm9
mulsd %xmm9, %xmm3
movsd -0x58(%rbp), %xmm10
mulsd %xmm10, %xmm4
addsd %xmm3, %xmm4
movsd -0x70(%rbp), %xmm3
mulsd %xmm3, %xmm6
addsd %xmm4, %xmm6
movsd %xmm6, -0xd8(%rbp)
mulsd %xmm9, %xmm0
mulsd %xmm10, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0xd0(%rbp)
mulsd %xmm8, %xmm9
mulsd %xmm10, %xmm5
addsd %xmm9, %xmm5
mulsd %xmm3, %xmm7
addsd %xmm5, %xmm7
movsd %xmm7, -0xc8(%rbp)
xorl %eax, %eax
movsd -0x1c0(%rbp,%rax,8), %xmm0
subsd -0xf0(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x190(%rbp,%rax,8)
incq %rax
cmpq $0x6, %rax
jne 0x4ab8
movq 0xd60(%rbx), %rax
movq 0xd68(%rbx), %rcx
cmpq %rcx, %rax
movsd 0x853b(%rip), %xmm2     # 0xd030
movsd 0x855b(%rip), %xmm5     # 0xd058
movapd 0x857b(%rip), %xmm3     # 0xd080
je 0x4b51
movl -0x2c(%rbp), %edx
movsd -0x38(%rbp), %xmm0
xorl %r14d, %r14d
movsd 0x850e(%rip), %xmm1     # 0xd028
movl (%rax), %esi
subl %edx, %esi
xorps %xmm4, %xmm4
cvtsi2sd %esi, %xmm4
mulsd %xmm2, %xmm4
addsd 0x8(%rax), %xmm4
subsd %xmm0, %xmm4
andpd %xmm3, %xmm4
ucomisd %xmm4, %xmm1
cmovaq %rax, %r14
minsd %xmm1, %xmm4
addq $0x40, %rax
movapd %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x4b1a
jmp 0x4b5c
movsd 0x84cf(%rip), %xmm4     # 0xd028
xorl %r14d, %r14d
movsd 0x84d4(%rip), %xmm0     # 0xd038
movapd %xmm4, -0x70(%rbp)
ucomisd %xmm4, %xmm0
movapd %xmm5, %xmm3
jbe 0x4c37
movapd %xmm5, %xmm3
testq %r14, %r14
je 0x4c37
leaq 0x10(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x7080
movsd 0x84bd(%rip), %xmm5     # 0xd058
addq $0x18, %r14
movapd %xmm5, %xmm3
cmpq %r14, %rax
je 0x4c37
movq (%r12), %r14
movl -0x2c(%rbp), %r15d
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x59da
leaq 0x4(%rax), %r8
movq -0x80(%rbp), %rdi
movq %r14, %rsi
movl %r15d, %edx
movsd -0x58(%rbp), %xmm0
leaq -0xb8(%rbp), %rcx
callq 0xa9ca
movsd 0x8468(%rip), %xmm5     # 0xd058
movapd %xmm5, %xmm3
testl %eax, %eax
je 0x4c37
movq %r13, %rdi
movq %r12, %rsi
callq 0x59da
movsd 0x844d(%rip), %xmm5     # 0xd058
movsd 0x18(%rax), %xmm3
movapd -0x70(%rbp), %xmm0
mulsd %xmm0, %xmm3
addsd 0x10(%rax), %xmm3
mulsd %xmm0, %xmm3
addsd 0x8(%rax), %xmm3
divsd 0x8411(%rip), %xmm3     # 0xd040
addsd -0xb8(%rbp), %xmm3
movq (%r12), %rdx
movsd -0x190(%rbp), %xmm0
movsd -0x188(%rbp), %xmm1
movsd 0x840d(%rip), %xmm4     # 0xd060
divsd %xmm4, %xmm0
divsd %xmm4, %xmm1
movsd -0x180(%rbp), %xmm2
divsd %xmm4, %xmm2
movapd %xmm3, %xmm4
mulsd 0x83f5(%rip), %xmm4     # 0xd068
cmpneqsd %xmm5, %xmm3
andpd %xmm3, %xmm4
andnpd %xmm5, %xmm3
orpd %xmm4, %xmm3
movq -0x78(%rbp), %rdi
leaq 0x84fe(%rip), %rsi      # 0xd18d
movb $0x4, %al
callq 0x33e0
addq $0x20, %r12
cmpq -0xb0(%rbp), %r12
jne 0x4788
movl -0x2c(%rbp), %edi
movsd -0x38(%rbp), %xmm0
movsd 0x8399(%rip), %xmm1     # 0xd050
leaq -0x2c(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0xb6d4
movl -0x2c(%rbp), %eax
subl 0x4(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x8357(%rip), %xmm1     # 0xd030
mulsd %xmm1, %xmm0
addsd -0x38(%rbp), %xmm0
subsd 0x18(%rbx), %xmm0
ucomisd 0x8359(%rip), %xmm0     # 0xd048
movq -0x78(%rbp), %r14
jb 0x4711
movq %r14, %rdi
callq 0x32c0
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd2be(%rip), %rdi      # 0x11fd8
leaq 0x83a2(%rip), %rsi      # 0xd0c3
callq 0x32b0
leaq -0x2c0(%rbp), %rsi
movq %rax, %rdi
callq 0x32b0
leaq 0x842a(%rip), %rsi      # 0xd166
movq %rax, %rdi
callq 0x32b0
movq %rax, %rdi
callq 0x3170
movl $0x1, %edi
callq 0x3370