// File: DoubleString[P]eph_converter/src/B2bDecoder.cpp
// Function: B2b::m_genclock()
void B2b::m_genclock(){
	FILE* fp = NULL;
	char filename[128] = {0};
	int mjd,iyear,imon,iday,ih,im,gweek,gd;
	double sod,dintv = 30,dt,dtmin,dtlimit =120,xclk,xclk_out,dclk,c[3],sec;
	/******** generate the ephemeris here *****************/
	cout << "begin to recover clock ..." << 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.clk",gweek,gd);
	if(!(fp = fopen(filename,"w"))){cout << "can't open file " << filename << " to write!" << endl;exit(1);}
	m_wtclkheader(fp);
	while(true){
		mjd2date(mjd,sod,&iyear,&imon,&iday,&ih,&im,&sec);
		for (auto &cprn : cprn_list) {
			/*************get the minus ssr*****************/
			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)
				continue;
			/*************request for the brdm *****************/
			if (ssr_selected->CLK.find(cprn) == ssr_selected->CLK.end())
				continue;
			if (!(nav.v_readClk(cprn.c_str(),mjd,sod,&xclk_out,&ssr_selected->CLK[cprn].iode))){
				cout << "ssr do not match ephemeris for " << cprn << " " << mjd << " " << sod << endl;
				continue;
			}
			/*************update the eph here *****************/
			memcpy(c,ssr_selected->CLK[cprn].clk,sizeof(double) * 3);
			dclk = c[0] + dtmin * (c[1] + dtmin * c[2]);
			xclk = xclk_out - dclk / VEL_LIGHT;
			fprintf(fp,"AS %3s%6d%3d%3d%3d%3d%10.6lf%3d%22.12e\r\n", cprn.c_str(), iyear, imon, iday, ih, im, sec, 1,xclk);
		}
		timinc(mjd,sod,dintv,&mjd,&sod);
		if( (mjd - mjd1) * 86400.0 + sod - sod1 >= 0.0)
			break;
	}
	fclose(fp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
movq $0x0, -0x10(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x31a0
movsd 0x12cab(%rip), %xmm0    # 0x17028
movsd %xmm0, -0xc0(%rbp)
movsd 0x12c93(%rip), %xmm0    # 0x17020
movsd %xmm0, -0xd8(%rbp)
movq 0x1bc3c(%rip), %rdi     # 0x1ffd8
leaq 0x12e0b(%rip), %rsi     # 0x171ae
callq 0x32a0
movq %rax, %rdi
movq 0x1bc16(%rip), %rsi     # 0x1ffc8
callq 0x3300
movq -0x178(%rbp), %rax
movl (%rax), %ecx
movl %ecx, -0x94(%rbp)
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0xb8(%rbp), %xmm0
divsd -0xc0(%rbp), %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
mulsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0x94(%rbp), %edi
movsd -0xb8(%rbp), %xmm0
leaq -0x98(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
leaq -0xa4(%rbp), %r8
leaq -0xa8(%rbp), %r9
leaq -0x118(%rbp), %rax
movq %rax, (%rsp)
callq 0x162e0
movl -0x98(%rbp), %edi
movl -0x9c(%rbp), %esi
movl -0xa0(%rbp), %edx
leaq -0xac(%rbp), %rcx
leaq -0xb0(%rbp), %r8
callq 0x145a0
leaq -0x90(%rbp), %rdi
movl -0xac(%rbp), %edx
movl -0xb0(%rbp), %ecx
leaq 0x12d4e(%rip), %rsi     # 0x171c9
movb $0x0, %al
callq 0x3080
leaq -0x90(%rbp), %rdi
leaq 0x12cb4(%rip), %rsi     # 0x17144
callq 0x3340
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
jne 0x44e9
movq 0x1bb32(%rip), %rdi     # 0x1ffd8
leaq 0x12bf6(%rip), %rsi     # 0x170a3
callq 0x32a0
movq %rax, %rdi
leaq -0x90(%rbp), %rsi
callq 0x32a0
movq %rax, %rdi
leaq 0x12c7b(%rip), %rsi     # 0x17146
callq 0x32a0
movq %rax, %rdi
movq 0x1baee(%rip), %rsi     # 0x1ffc8
callq 0x3300
movl $0x1, %edi
callq 0x3360
movq -0x178(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6030
movl -0x94(%rbp), %edi
movsd -0xb8(%rbp), %xmm0
leaq -0x98(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
leaq -0xa4(%rbp), %r8
leaq -0xa8(%rbp), %r9
leaq -0x118(%rbp), %rax
movq %rax, (%rsp)
callq 0x162e0
movq -0x178(%rbp), %rax
addq $0xd78, %rax            # imm = 0xD78
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x68a0
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x68d0
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x6900
testb $0x1, %al
jne 0x4590
jmp 0x4959
leaq -0x128(%rbp), %rdi
callq 0x6940
movq %rax, %rcx
movq -0x178(%rbp), %rax
movq %rcx, -0x138(%rbp)
movsd 0x12a83(%rip), %xmm0    # 0x17038
movsd %xmm0, -0xd0(%rbp)
movq $0x0, -0x140(%rbp)
addq $0xd60, %rax            # imm = 0xD60
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rdi
callq 0x6bb0
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rdi
callq 0x6be0
movq %rax, -0x158(%rbp)
leaq -0x150(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x6c10
testb $0x1, %al
jne 0x4617
jmp 0x46c6
leaq -0x150(%rbp), %rdi
callq 0x6c50
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movl (%rax), %eax
subl -0x94(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x160(%rbp), %rax
movsd 0x8(%rax), %xmm1
movsd 0x129df(%rip), %xmm2    # 0x17030
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
subsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xd0(%rbp), %xmm0
movsd -0xc8(%rbp), %xmm1
movaps 0x129f0(%rip), %xmm2    # 0x17070
pand %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x46b3
movsd -0xc8(%rbp), %xmm0
movaps 0x129d7(%rip), %xmm1    # 0x17070
pand %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0x46b5
leaq -0x150(%rbp), %rdi
callq 0x6c70
jmp 0x45fb
movsd -0xd0(%rbp), %xmm0
ucomisd -0xd8(%rbp), %xmm0
ja 0x46e2
cmpq $0x0, -0x140(%rbp)
jne 0x46e7
jmp 0x4948
movq -0x140(%rbp), %rdi
addq $0x10, %rdi
movq -0x138(%rbp), %rsi
callq 0x6cc0
movq %rax, -0x168(%rbp)
movq -0x140(%rbp), %rdi
addq $0x10, %rdi
callq 0x6cf0
movq %rax, -0x170(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x6e20
testb $0x1, %al
jne 0x4735
jmp 0x473a
jmp 0x4948
movq -0x178(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x198(%rbp)
movq -0x138(%rbp), %rdi
callq 0x3130
movq %rax, -0x190(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x184(%rbp)
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movq -0x140(%rbp), %rdi
addq $0x10, %rdi
movq -0x138(%rbp), %rsi
callq 0x6d20
movq -0x198(%rbp), %rdi
movq -0x190(%rbp), %rsi
movl -0x184(%rbp), %edx
movsd -0x180(%rbp), %xmm0
movq %rax, %r8
addq $0x4, %r8
leaq -0xe8(%rbp), %rcx
callq 0x13820
cmpl $0x0, %eax
jne 0x4838
movq 0x1b80b(%rip), %rdi     # 0x1ffd8
leaq 0x129ba(%rip), %rsi     # 0x1718e
callq 0x32a0
movq %rax, %rdi
movq -0x138(%rbp), %rsi
callq 0x3250
movq %rax, %rdi
leaq 0x13881(%rip), %rsi     # 0x18073
callq 0x32a0
movq %rax, %rdi
movl -0x94(%rbp), %esi
callq 0x34a0
movq %rax, %rdi
leaq 0x13864(%rip), %rsi     # 0x18073
callq 0x32a0
movq %rax, %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x3510
movq %rax, %rdi
movq 0x1b79a(%rip), %rsi     # 0x1ffc8
callq 0x3300
jmp 0x4948
movq -0x140(%rbp), %rdi
addq $0x10, %rdi
movq -0x138(%rbp), %rsi
callq 0x6d20
movq 0x8(%rax), %rcx
movq %rcx, -0x110(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x108(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x100(%rbp)
movsd -0x110(%rbp), %xmm1
movsd -0xd0(%rbp), %xmm0
movsd -0x108(%rbp), %xmm3
movsd -0xd0(%rbp), %xmm2
movsd -0x100(%rbp), %xmm4
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0xe8(%rbp), %xmm0
movsd -0xf0(%rbp), %xmm1
movsd 0x12778(%rip), %xmm2    # 0x17040
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x138(%rbp), %rdi
callq 0x3130
movq -0x1a0(%rbp), %rdi
movq %rax, %rdx
movl -0x98(%rbp), %ecx
movl -0x9c(%rbp), %r8d
movl -0xa0(%rbp), %r9d
movl -0xa4(%rbp), %r10d
movl -0xa8(%rbp), %eax
movsd -0x118(%rbp), %xmm0
movsd -0xe0(%rbp), %xmm1
leaq 0x128a6(%rip), %rsi     # 0x171d7
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movb $0x2, %al
callq 0x3400
leaq -0x128(%rbp), %rdi
callq 0x6e00
jmp 0x4574
movl -0x94(%rbp), %edi
movsd -0xb8(%rbp), %xmm0
movsd -0xc0(%rbp), %xmm1
leaq -0x94(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x15590
movq -0x178(%rbp), %rax
movl -0x94(%rbp), %ecx
subl 0x4(%rax), %ecx
cvtsi2sd %ecx, %xmm0
movsd -0xb8(%rbp), %xmm1
movsd 0x1268a(%rip), %xmm2    # 0x17030
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
subsd 0x18(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x49be
jmp 0x49c3
jmp 0x44f9
movq -0x10(%rbp), %rdi
callq 0x32b0
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)