// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/cal_cov.c
// Function: mg_cov_asm
void mg_cov_asm(const gfa_t *g, int32_t n_seq, mg_gchains_t *const *gcs, int32_t min_mapq, int32_t min_blen, double *cov_seg, double *cov_link)
{
	int32_t i, j, t, *soff, *scnt, *cnt_link;
	int64_t k;
	mg_intv_t *sintv = 0;
	void *km = 0;

	// precalculate the size of sintv[] for each segment
	KCALLOC(km, scnt, g->n_seg);
	for (t = 0; t < n_seq; ++t) {
		const mg_gchains_t *gt = gcs[t];
		if (gt == 0 || gt->n_gc == 0) continue;
		for (i = 0; i < gt->n_gc; ++i) {
			const mg_gchain_t *gc = &gt->gc[i];
			assert(gc->cnt > 0 && gc->n_anchor > 0);
			if (gc->mapq < min_mapq || gc->blen < min_blen) continue;
			for (j = 0; j < gc->cnt; ++j) {
				const mg_llchain_t *lc = &gt->lc[gc->off + j];
				++scnt[lc->v>>1];
			}
		}
	}
	KMALLOC(km, soff, g->n_seg + 1);
	for (soff[0] = 0, i = 1; i <= g->n_seg; ++i)
		soff[i] = soff[i - 1] + scnt[i - 1];
	memset(scnt, 0, 4 * g->n_seg);
	KMALLOC(km, sintv, soff[g->n_seg]);

	// fill sintv[]
	KCALLOC(km, cnt_link, g->n_arc);
	for (t = 0; t < n_seq; ++t) {
		const mg_gchains_t *gt = gcs[t];
		if (gt == 0 || gt->n_gc == 0) continue;
		for (i = 0; i < gt->n_gc;) {
			const mg_gchain_t *gc = &gt->gc[i];
			if (gc->mapq < min_mapq || gc->blen < min_blen) continue;
			// count segment coverage
			for (j = 0; j < gc->cnt; ++j) {
				const mg_llchain_t *lc = &gt->lc[gc->off + j];
				int32_t s, e, tmp;
				mg_intv_t *p;
				s = 0, e = g->seg[lc->v>>1].len;
				if (j == 0) s = (int32_t)gt->a[lc->off].x + 1 - (int32_t)(gt->a[lc->off].y>>32&0xff);
				if (j == gc->cnt - 1) e = (int32_t)gt->a[lc->off + lc->cnt - 1].x + 1;
				if (lc->v&1) // convert to the forward strand of segment lc->v>>1
					tmp = g->seg[lc->v>>1].len - s, s = g->seg[lc->v>>1].len - e, e = tmp;
				p = &sintv[soff[lc->v>>1] + scnt[lc->v>>1]];
				++scnt[lc->v>>1];
				p->st = s, p->en = e, p->rev = lc->v&1, p->far = -1, p->i = -1;
			}
			// count link
			for (j = 1; j < gc->cnt; ++j) {
				const mg_llchain_t *lc0 = &gt->lc[gc->off + j - 1];
				const mg_llchain_t *lc1 = &gt->lc[gc->off + j];
				int64_t a01, a10;
				a01 = gfa_find_arc(g, lc0->v, lc1->v);
				a10 = gfa_find_arc(g, lc1->v^1, lc0->v^1);
				assert(a01 >= 0 && a10 >= 0);
				assert((g->arc[a01].comp ^ g->arc[a10].comp) == 1);
				++cnt_link[a01];
				++cnt_link[a10];
			}
		}
	}

	// update cov_link[] and cov_seg[]
	for (k = 0; k < g->n_arc; ++k)
		if (cnt_link[k] > 0) cov_link[k] += 1.0;
	for (i = 0; i < g->n_seg; ++i) {
		int32_t st = 0, en = 0, cov = 0;
		assert(scnt[i] == soff[i+1] - soff[i]);
		radix_sort_mg_intv(&sintv[soff[i]], &sintv[soff[i+1]]);
		for (j = soff[i]; j < soff[i+1]; ++j) {
			if (sintv[j].st > en)
				cov += en - st, st = sintv[j].st, en = sintv[j].en;
			else en = sintv[j].en > en? sintv[j].en : en;
		}
		cov += en - st;
		cov_seg[i] += (double)cov / g->seg[i].len;
	}

	// free
	kfree(km, cnt_link);
	kfree(km, sintv); kfree(km, soff); kfree(km, scnt);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x50(%rsp)
movl %r8d, %ebp
movl %ecx, %r13d
movq %rdx, %rbx
movl %esi, %r15d
movq 0xa0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rdi, 0x8(%rsp)
movl 0x4(%rdi), %esi
xorl %r14d, %r14d
pushq $0x4
popq %rdx
xorl %edi, %edi
callq 0x24f0
testl %r15d, %r15d
cmovlel %r14d, %r15d
cmpq %r15, %r14
je 0x1b75c
movq (%rbx,%r14,8), %r9
testq %r9, %r9
je 0x1b754
movl 0x8(%r9), %esi
testl %esi, %esi
je 0x1b754
xorl %ecx, %ecx
movslq %esi, %rdx
cmpq %rdx, %rcx
jge 0x1b754
movq 0x18(%r9), %rdx
imulq $0x68, %rcx, %r8
movl 0xc(%rdx,%r8), %edi
testl %edi, %edi
jle 0x1bb5f
addq %r8, %rdx
cmpl $0x0, 0x10(%rdx)
jle 0x1bb5f
movzbl 0x44(%rdx), %r8d
cmpl %r13d, %r8d
jl 0x1b74f
cmpl %ebp, 0x2c(%rdx)
jl 0x1b74f
xorl %esi, %esi
cmpl %edi, %esi
jge 0x1b74b
movq 0x20(%r9), %rdi
movslq 0x8(%rdx), %r8
addq %rsi, %r8
imulq $0x14, %r8, %r8
movl 0x8(%rdi,%r8), %edi
shrl %edi
incl (%rax,%rdi,4)
movl 0xc(%rdx), %edi
incq %rsi
jmp 0x1b726
movl 0x8(%r9), %esi
incq %rcx
jmp 0x1b6eb
incq %r14
jmp 0x1b6cf
movq %r15, 0x60(%rsp)
movq %rbx, 0x58(%rsp)
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rbx
movl 0x4(%rbx), %esi
incl %esi
shlq $0x2, %rsi
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x266c
movq %rbx, %rcx
movq 0x30(%rsp), %rbx
movq %rax, %r12
andl $0x0, (%rax)
pushq $0x1
popq %rax
movl 0x4(%rcx), %edx
cmpq %rdx, %rax
ja 0x1b7aa
addl -0x4(%rbx,%rax,4), %r14d
movl %r14d, (%r12,%rax,4)
incq %rax
jmp 0x1b794
shll $0x2, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2180
movq 0x8(%rsp), %rax
movl 0x4(%rax), %eax
movslq (%r12,%rax,4), %rsi
shlq $0x4, %rsi
xorl %edi, %edi
callq 0x266c
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rsi
pushq $0x4
popq %rdx
xorl %edi, %edi
callq 0x24f0
xorl %edx, %edx
movq 0x8(%rsp), %rdi
movq %rax, 0x18(%rsp)
movq %r12, 0x38(%rsp)
movl %ebp, 0x2c(%rsp)
movl %r13d, 0x28(%rsp)
cmpq 0x60(%rsp), %rdx
je 0x1ba11
movq 0x58(%rsp), %rax
movq (%rax,%rdx,8), %r9
testq %r9, %r9
je 0x1ba09
movl 0x8(%r9), %eax
testl %eax, %eax
je 0x1ba09
movq %r9, 0x20(%rsp)
testl %eax, %eax
jle 0x1ba09
movq 0x18(%r9), %r14
movzbl 0x44(%r14), %ecx
cmpl %r13d, %ecx
jl 0x1b82e
cmpl %ebp, 0x2c(%r14)
jl 0x1b82e
movq %rdx, 0x48(%rsp)
xorl %eax, %eax
movl 0xc(%r14), %edx
cmpl %edx, %eax
jge 0x1b946
movq 0x20(%r9), %r11
movslq 0x8(%r14), %rcx
addq %rax, %rcx
imulq $0x14, %rcx, %r8
leaq (%r11,%r8), %rcx
xorl %esi, %esi
testl %eax, %eax
jne 0x1b890
movq 0x28(%r9), %r9
movslq (%rcx), %r10
shlq $0x4, %r10
movl (%r9,%r10), %esi
movzbl 0xc(%r9,%r10), %r9d
subl %r9d, %esi
incl %esi
movq 0x10(%rdi), %r10
movl 0x8(%r11,%r8), %r9d
movl %r9d, %r15d
shrl %r15d
movq %r15, %r8
shlq $0x6, %r8
movl (%r10,%r8), %r8d
decl %edx
movl %r8d, %r10d
cmpl %edx, %eax
jne 0x1b8d7
movq 0x20(%rsp), %rdx
movq 0x28(%rdx), %rdx
movslq (%rcx), %r10
movslq 0x4(%rcx), %r11
addq %r10, %r11
shlq $0x4, %r11
movl -0x10(%rdx,%r11), %r10d
movq 0x8(%rsp), %rdi
incl %r10d
movl %r8d, %edx
subl %esi, %edx
subl %r10d, %r8d
testb $0x1, %r9b
cmovel %esi, %r8d
cmovel %r10d, %edx
movslq (%r12,%r15,4), %rsi
movslq (%rbx,%r15,4), %r9
addq %r9, %rsi
shlq $0x4, %rsi
incl %r9d
movl %r9d, (%rbx,%r15,4)
movq 0x10(%rsp), %r9
movl %r8d, (%r9,%rsi)
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%r9,%rsi), %r8d
movl $0x80000000, %r10d      # imm = 0x80000000
andl %r10d, %r8d
orl %edx, %r8d
movl %r8d, 0x4(%r9,%rsi)
movl 0x8(%rcx), %ecx
shll $0x1f, %ecx
orl %edx, %ecx
movl %ecx, 0x4(%r9,%rsi)
orq $-0x1, 0x8(%r9,%rsi)
incq %rax
movq 0x20(%rsp), %r9
jmp 0x1b851
pushq $0x1
popq %r13
cmpl %edx, %r13d
jge 0x1b9f2
movq 0x20(%r9), %rax
movslq 0x8(%r14), %rcx
addq %r13, %rcx
imulq $0x14, %rcx, %rcx
movl -0xc(%rax,%rcx), %r12d
movl 0x8(%rax,%rcx), %r15d
movl %r12d, %esi
movl %r15d, %edx
movq %rdi, %rbx
callq 0x1b618
movq %rax, %rbp
xorl $0x1, %r15d
xorl $0x1, %r12d
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x1b618
testq %rbp, %rbp
js 0x1bb95
testq %rax, %rax
js 0x1bb95
movq 0x8(%rsp), %rdi
movq 0x48(%rdi), %rcx
movq %rbp, %rdx
shlq $0x5, %rdx
movq %rax, %rsi
shlq $0x5, %rsi
movq 0x18(%rcx,%rsi), %rsi
xorq 0x18(%rcx,%rdx), %rsi
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r12
jns 0x1bb7c
movq 0x18(%rsp), %rcx
incl (%rcx,%rbp,4)
incl (%rcx,%rax,4)
movl 0xc(%r14), %edx
incq %r13
movq 0x20(%rsp), %r9
jmp 0x1b94a
movl 0x8(%r9), %eax
movl 0x2c(%rsp), %ebp
movl 0x28(%rsp), %r13d
movq 0x48(%rsp), %rdx
jmp 0x1b82e
incq %rdx
jmp 0x1b800
movq 0x40(%rdi), %rax
xorl %ecx, %ecx
movsd 0x8701(%rip), %xmm0     # 0x24120
movq 0x40(%rsp), %rdx
movq 0x18(%rsp), %rsi
cmpq %rcx, %rax
je 0x1ba47
cmpl $0x0, (%rsi,%rcx,4)
jle 0x1ba42
movsd (%rdx,%rcx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
jmp 0x1ba29
movq 0x10(%rsp), %rax
leaq 0x4(%rax), %r14
xorl %r15d, %r15d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x4(%rdi), %eax
cmpq %rax, %r15
jae 0x1bb25
movslq 0x4(%r12,%r15,4), %rsi
movslq (%r12,%r15,4), %rdi
movl %esi, %eax
subl %edi, %eax
cmpl %eax, (%rbx,%r15,4)
jne 0x1bbae
leaq 0x1(%r15), %r13
shlq $0x4, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
shlq $0x4, %rsi
addq %rax, %rsi
callq 0x32c5
movslq (%r12,%r15,4), %rax
movslq (%r12,%r13,4), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
addq %r14, %rdx
xorl %r8d, %r8d
xorl %esi, %esi
xorl %r10d, %r10d
movq 0x8(%rsp), %rdi
cmpq %rcx, %rax
jge 0x1bae9
movl -0x4(%rdx), %r9d
cmpl %esi, %r9d
jbe 0x1bad3
subl %r8d, %esi
addl %esi, %r10d
movl (%rdx), %esi
andl %ebp, %esi
movl %r9d, %r8d
jmp 0x1bae0
movl (%rdx), %r9d
andl %ebp, %r9d
cmpl %esi, %r9d
cmovgl %r9d, %esi
incq %rax
addq $0x10, %rdx
jmp 0x1bab6
subl %r8d, %esi
addl %r10d, %esi
cvtsi2sd %esi, %xmm0
movq 0x10(%rdi), %rax
movq %r15, %rcx
shlq $0x6, %rcx
cvtsi2sdl (%rax,%rcx), %xmm1
divsd %xmm1, %xmm0
movq 0x50(%rsp), %rax
addsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
movq %r13, %r15
movq 0x38(%rsp), %r12
jmp 0x1ba58
xorl %edi, %edi
movq 0x18(%rsp), %rsi
callq 0x2577
xorl %edi, %edi
movq 0x10(%rsp), %rsi
callq 0x2577
xorl %edi, %edi
movq %r12, %rsi
callq 0x2577
xorl %edi, %edi
movq %rbx, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2577
leaq 0x9153(%rip), %rdi      # 0x24cb9
leaq 0x916c(%rip), %rsi      # 0x24cd9
leaq 0x926f(%rip), %rcx      # 0x24de3
pushq $0x45
popq %rdx
callq 0x2160
leaq 0x9235(%rip), %rdi      # 0x24db8
leaq 0x914f(%rip), %rsi      # 0x24cd9
leaq 0x9252(%rip), %rcx      # 0x24de3
pushq $0x71
jmp 0x1bb76
leaq 0x92ac(%rip), %rdi      # 0x24e48
leaq 0x9136(%rip), %rsi      # 0x24cd9
leaq 0x9239(%rip), %rcx      # 0x24de3
pushq $0x70
jmp 0x1bb76
leaq 0x92a8(%rip), %rdi      # 0x24e5d
leaq 0x911d(%rip), %rsi      # 0x24cd9
leaq 0x9220(%rip), %rcx      # 0x24de3
pushq $0x7d
jmp 0x1bb76
nop