// File: /workspace/github_cpp_makefile_star2_O2/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), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x31a0
movq 0xdda3(%rip), %rdi      # 0x11fd8
leaq 0x7eeb(%rip), %rsi      # 0xc127
callq 0x3280
movq %rax, %rdi
callq 0x3170
movl (%rbx), %r14d
movl %r14d, -0x2c(%rbp)
movsd 0x10(%rbx), %xmm0
movsd 0x7df3(%rip), %xmm1     # 0xc050
divsd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, (%rsp)
leaq -0x54(%rbp), %r12
leaq -0x50(%rbp), %r13
leaq -0x4c(%rbp), %r15
leaq -0x48(%rbp), %r8
leaq -0x44(%rbp), %r9
movl %r14d, %edi
movapd %xmm0, -0x90(%rbp)
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xb567
movl (%r12), %edi
movl (%r13), %esi
movl (%r15), %edx
leaq -0xc0(%rbp), %r15
leaq -0xbc(%rbp), %r12
movq %r15, %rcx
movq %r12, %r8
callq 0xa599
movl (%r15), %edx
movl (%r12), %ecx
leaq 0x7e6d(%rip), %rsi      # 0xc146
leaq -0x2c0(%rbp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
leaq 0x7e63(%rip), %rsi      # 0xc154
movq %r15, %rdi
callq 0x3300
testq %rax, %rax
je 0x48de
movq %rbx, %rdi
movq %rax, -0x60(%rbp)
movq %rax, %rsi
callq 0x4920
leaq 0x20(%rbx), %rax
movq %rax, -0x70(%rbp)
movapd -0x90(%rbp), %xmm0
leaq -0x78(%rbp), %rax
movq %rax, (%rsp)
movl %r14d, %edi
leaq -0x54(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x44(%rbp), %r9
callq 0xb567
movl -0x54(%rbp), %edx
movl -0x50(%rbp), %ecx
movl -0x4c(%rbp), %r8d
movl -0x48(%rbp), %r9d
movl -0x44(%rbp), %eax
movsd -0x78(%rbp), %xmm0
movl %eax, (%rsp)
movq -0x60(%rbp), %rdi
leaq 0x7df8(%rip), %rsi      # 0xc161
movb $0x1, %al
callq 0x33b0
movq 0xd78(%rbx), %r14
movq 0xd80(%rbx), %rax
movq %rax, -0x90(%rbp)
cmpq -0x90(%rbp), %r14
je 0x4870
movq 0xd48(%rbx), %rax
movq 0xd50(%rbx), %rcx
movl -0x2c(%rbp), %edx
movsd -0x38(%rbp), %xmm0
xorl %r15d, %r15d
movsd 0x7c75(%rip), %xmm1     # 0xc028
movsd 0x7c75(%rip), %xmm3     # 0xc030
movapd 0x7cbd(%rip), %xmm4     # 0xc080
cmpq %rcx, %rax
je 0x43f7
movl (%rax), %esi
subl %edx, %esi
cvtsi2sd %esi, %xmm2
mulsd %xmm3, %xmm2
addsd 0x8(%rax), %xmm2
subsd %xmm0, %xmm2
andpd %xmm4, %xmm2
ucomisd %xmm2, %xmm1
cmovaq %rax, %r15
minsd %xmm1, %xmm2
addq $0x40, %rax
movapd %xmm2, %xmm1
jmp 0x43c3
ucomisd 0x7c39(%rip), %xmm1     # 0xc038
ja 0x4867
testq %r15, %r15
je 0x4867
leaq 0x10(%r15), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x6b44
addq $0x18, %r15
cmpq %r15, %rax
je 0x4867
movq (%r14), %r15
movl -0x2c(%rbp), %r12d
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x5430
leaq 0x4(%rax), %r8
movq -0x70(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
movsd -0x40(%rbp), %xmm0
leaq -0x1c0(%rbp), %rcx
callq 0xa0d4
testl %eax, %eax
je 0x4867
movq %r13, %rdi
movq %r14, %rsi
callq 0x5430
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x28(%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
pushq $0x3
popq %r12
movl %r12d, %edi
leaq -0x1c0(%rbp), %r15
movq %r15, %rsi
leaq -0xe0(%rbp), %rdx
movq %rbx, %r13
leaq -0x158(%rbp), %rbx
movq %rbx, %rcx
callq 0xb6ad
movq %r15, %rdi
leaq -0x1a8(%rbp), %rsi
leaq -0x100(%rbp), %r15
movq %r15, %rdx
callq 0xb72e
movl %r12d, %edi
movq %r15, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb6ad
movq %r15, %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x120(%rbp), %r15
movq %r15, %rdx
callq 0xb72e
movl %r12d, %edi
movq %r15, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r13, %rbx
callq 0xb6ad
movsd -0x120(%rbp), %xmm3
movsd -0x118(%rbp), %xmm0
movsd -0x98(%rbp), %xmm8
movapd %xmm8, %xmm2
mulsd %xmm3, %xmm2
movsd -0xe0(%rbp), %xmm4
movsd -0xd8(%rbp), %xmm1
movapd %xmm4, %xmm5
movsd -0xa0(%rbp), %xmm11
mulsd %xmm11, %xmm5
addsd %xmm2, %xmm5
movsd -0x100(%rbp), %xmm6
movsd -0xf8(%rbp), %xmm2
movapd %xmm6, %xmm7
movsd -0xa8(%rbp), %xmm10
mulsd %xmm10, %xmm7
addsd %xmm5, %xmm7
movsd %xmm7, -0x150(%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, -0x148(%rbp)
movsd -0xd0(%rbp), %xmm5
movsd -0x110(%rbp), %xmm8
mulsd %xmm8, %xmm9
movapd %xmm11, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm9, %xmm7
movapd %xmm7, %xmm9
movsd -0xf0(%rbp), %xmm7
mulsd %xmm7, %xmm10
addsd %xmm9, %xmm10
movsd %xmm10, -0x140(%rbp)
movsd -0xb0(%rbp), %xmm9
mulsd %xmm9, %xmm3
movsd -0x68(%rbp), %xmm10
mulsd %xmm10, %xmm4
addsd %xmm3, %xmm4
movsd -0x40(%rbp), %xmm3
mulsd %xmm3, %xmm6
addsd %xmm4, %xmm6
movsd %xmm6, -0x138(%rbp)
mulsd %xmm9, %xmm0
mulsd %xmm10, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x130(%rbp)
mulsd %xmm8, %xmm9
mulsd %xmm10, %xmm5
addsd %xmm9, %xmm5
mulsd %xmm3, %xmm7
addsd %xmm5, %xmm7
movsd %xmm7, -0x128(%rbp)
xorl %eax, %eax
movsd 0x798c(%rip), %xmm2     # 0xc030
movsd 0x79ac(%rip), %xmm5     # 0xc058
movapd 0x79cc(%rip), %xmm3     # 0xc080
cmpq $0x6, %rax
je 0x46da
movsd -0x1c0(%rbp,%rax,8), %xmm0
subsd -0x150(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x190(%rbp,%rax,8)
incq %rax
jmp 0x46b4
movq 0xd60(%rbx), %rax
movq 0xd68(%rbx), %rcx
movl -0x2c(%rbp), %edx
movsd -0x38(%rbp), %xmm0
movsd 0x7930(%rip), %xmm4     # 0xc028
xorl %r15d, %r15d
cmpq %rcx, %rax
je 0x472f
movl (%rax), %esi
subl %edx, %esi
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
addsd 0x8(%rax), %xmm1
subsd %xmm0, %xmm1
andpd %xmm3, %xmm1
ucomisd %xmm1, %xmm4
cmovaq %rax, %r15
minsd %xmm4, %xmm1
addq $0x40, %rax
movapd %xmm1, %xmm4
jmp 0x46fb
movsd 0x7901(%rip), %xmm0     # 0xc038
movsd %xmm4, -0x40(%rbp)
ucomisd %xmm4, %xmm0
movapd %xmm5, %xmm3
jbe 0x4809
movapd %xmm5, %xmm3
testq %r15, %r15
je 0x4809
leaq 0x10(%r15), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x6cfc
movsd 0x78ea(%rip), %xmm5     # 0xc058
addq $0x18, %r15
movapd %xmm5, %xmm3
cmpq %r15, %rax
je 0x4809
movq (%r14), %r15
movl -0x2c(%rbp), %r12d
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x549a
leaq 0x4(%rax), %r8
movq -0x70(%rbp), %rdi
movq %r15, %rsi
movl %r12d, %edx
movsd -0x68(%rbp), %xmm0
leaq -0xb8(%rbp), %rcx
callq 0xa190
movsd 0x7896(%rip), %xmm5     # 0xc058
movapd %xmm5, %xmm3
testl %eax, %eax
je 0x4809
movq %r13, %rdi
movq %r14, %rsi
callq 0x549a
movsd 0x787b(%rip), %xmm5     # 0xc058
movsd 0x18(%rax), %xmm3
movsd -0x40(%rbp), %xmm0
mulsd %xmm0, %xmm3
addsd 0x10(%rax), %xmm3
mulsd %xmm0, %xmm3
addsd 0x8(%rax), %xmm3
divsd 0x783f(%rip), %xmm3     # 0xc040
addsd -0xb8(%rbp), %xmm3
movq (%r14), %rdx
movsd -0x190(%rbp), %xmm0
movsd -0x188(%rbp), %xmm1
movsd 0x783c(%rip), %xmm4     # 0xc060
divsd %xmm4, %xmm0
divsd %xmm4, %xmm1
movsd -0x180(%rbp), %xmm2
divsd %xmm4, %xmm2
movapd %xmm3, %xmm4
mulsd 0x7824(%rip), %xmm4     # 0xc068
cmpneqsd %xmm5, %xmm3
andpd %xmm3, %xmm4
andnpd %xmm5, %xmm3
orpd %xmm4, %xmm3
movq -0x60(%rbp), %rdi
leaq 0x791d(%rip), %rsi      # 0xc17d
movb $0x4, %al
callq 0x33b0
addq $0x20, %r14
jmp 0x4385
movl -0x2c(%rbp), %edi
movsd -0x38(%rbp), %xmm0
movsd 0x77d0(%rip), %xmm1     # 0xc050
leaq -0x2c(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0xb032
movl -0x2c(%rbp), %r14d
movl %r14d, %eax
subl 0x4(%rbx), %eax
cvtsi2sd %eax, %xmm2
movsd -0x38(%rbp), %xmm0
movsd 0x7788(%rip), %xmm1     # 0xc030
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
subsd 0x18(%rbx), %xmm2
ucomisd 0x778b(%rip), %xmm2     # 0xc048
jb 0x4321
movq -0x60(%rbp), %rdi
callq 0x3290
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd6f3(%rip), %rdi      # 0x11fd8
leaq 0x77c7(%rip), %rsi      # 0xc0b3
callq 0x3280
leaq -0x2c0(%rbp), %rsi
movq %rax, %rdi
callq 0x3280
leaq 0x784f(%rip), %rsi      # 0xc156
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3170
pushq $0x1
popq %rdi
callq 0x3320
nop