// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x58(%rsp)
movl %r8d, %ebp
movl %ecx, %r13d
movq %rdx, %r14
movl %esi, %r12d
movq %rdi, %r15
movq 0xb0(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x4(%rdi), %esi
movl $0x4, %edx
xorl %edi, %edi
callq 0x3054
movq %rax, %rbx
movl %r12d, %r11d
movl %r12d, 0x10(%rsp)
testl %r12d, %r12d
jle 0x1f282
xorl %eax, %eax
movq (%r14,%rax,8), %rcx
testq %rcx, %rcx
je 0x1f276
cmpl $0x0, 0x8(%rcx)
je 0x1f276
cmpl $0x0, 0x8(%rcx)
jle 0x1f276
movq 0x18(%rcx), %rdx
xorl %esi, %esi
imulq $0x68, %rsi, %rdi
cmpl $0x0, 0xc(%rdx,%rdi)
jle 0x1f7df
addq %rdx, %rdi
cmpl $0x0, 0x10(%rdi)
jle 0x1f7df
movzbl 0x44(%rdi), %r8d
cmpl %r13d, %r8d
jl 0x1f26a
cmpl %ebp, 0x2c(%rdi)
jl 0x1f26a
cmpl $0x0, 0xc(%rdi)
jle 0x1f26a
movq 0x20(%rcx), %r8
xorl %r9d, %r9d
movslq 0x8(%rdi), %r10
addq %r9, %r10
leaq (%r10,%r10,4), %r10
movl 0x8(%r8,%r10,4), %r10d
shrl %r10d
incl (%rbx,%r10,4)
incq %r9
cmpl 0xc(%rdi), %r9d
jl 0x1f24a
incq %rsi
movslq 0x8(%rcx), %rdi
cmpq %rdi, %rsi
jl 0x1f212
incq %rax
cmpq %r11, %rax
jne 0x1f1f7
movq %r11, 0x48(%rsp)
movq %r14, 0x50(%rsp)
movl %ebp, 0x30(%rsp)
movl 0x4(%r15), %esi
incl %esi
shlq $0x2, %rsi
xorl %edi, %edi
callq 0x31c1
movq %rax, %r14
movl $0x0, (%rax)
movl 0x4(%r15), %edx
testl %edx, %edx
je 0x1f2cb
movl (%r14), %eax
xorl %ecx, %ecx
addl (%rbx,%rcx,4), %eax
movl %eax, 0x4(%r14,%rcx,4)
movl 0x4(%r15), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x1f2b7
shll $0x2, %edx
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2180
movl 0x4(%r15), %eax
movq %r14, 0x18(%rsp)
movslq (%r14,%rax,4), %rsi
shlq $0x4, %rsi
xorl %edi, %edi
callq 0x31c1
movq %rax, %r14
movq %r15, 0x8(%rsp)
movq 0x40(%r15), %rsi
movl $0x4, %edx
xorl %edi, %edi
callq 0x3054
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x1f63a
xorl %ecx, %ecx
movl %r13d, 0x34(%rsp)
movq 0x50(%rsp), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %rbp
testq %rbp, %rbp
je 0x1f627
cmpl $0x0, 0x8(%rbp)
je 0x1f627
cmpl $0x0, 0x8(%rbp)
jle 0x1f627
movq 0x18(%rbp), %rdi
movzbl 0x44(%rdi), %eax
cmpl %r13d, %eax
jl 0x1f618
movl 0x30(%rsp), %eax
cmpl %eax, 0x2c(%rdi)
jl 0x1f618
movl 0xc(%rdi), %eax
testl %eax, %eax
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %r13
jle 0x1f469
xorl %ecx, %ecx
movq 0x20(%rbp), %r9
movslq 0x8(%rdi), %rdx
addq %rcx, %rdx
leaq (%rdx,%rdx,4), %r10
leaq (%r9,%r10,4), %rdx
xorl %r8d, %r8d
testl %ecx, %ecx
jne 0x1f3be
movq 0x28(%rbp), %r11
movslq (%rdx), %rbx
shlq $0x4, %rbx
movl (%r11,%rbx), %r8d
movzbl 0xc(%r11,%rbx), %r11d
subl %r11d, %r8d
incl %r8d
movq 0x10(%r12), %rbx
movl 0x8(%r9,%r10,4), %r11d
movl %r11d, %r9d
shrl %r9d
movq %r9, %r10
shlq $0x6, %r10
movl (%rbx,%r10), %r10d
decl %eax
movl %r10d, %ebx
cmpl %eax, %ecx
jne 0x1f3fb
movq 0x28(%rbp), %rax
movslq (%rdx), %rbx
movslq 0x4(%rdx), %r15
addq %rbx, %r15
shlq $0x4, %r15
movl -0x10(%rax,%r15), %ebx
incl %ebx
movl %r10d, %eax
subl %r8d, %eax
subl %ebx, %r10d
testb $0x1, %r11b
cmovel %r8d, %r10d
cmovel %ebx, %eax
movslq (%r13,%r9,4), %r8
movslq (%rsi,%r9,4), %r11
addq %r11, %r8
shlq $0x4, %r8
incl %r11d
movl %r11d, (%rsi,%r9,4)
movl %r10d, (%r14,%r8)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x4(%r14,%r8), %r9d
movl $0x80000000, %r10d      # imm = 0x80000000
andl %r10d, %r9d
orl %eax, %r9d
movl %r9d, 0x4(%r14,%r8)
movl 0x8(%rdx), %edx
shll $0x1f, %edx
orl %eax, %edx
movl %edx, 0x4(%r14,%r8)
movq $-0x1, 0x8(%r14,%r8)
movl 0xc(%rdi), %eax
incq %rcx
cmpl %eax, %ecx
jl 0x1f389
cmpl $0x2, 0xc(%rdi)
jl 0x1f618
movq 0x20(%rbp), %rax
movq %rax, 0x70(%rsp)
addq $-0xc, %rax
movq %rax, 0x68(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rax), %r10
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(%r10), %rax
movq %rax, 0x38(%rsp)
movl $0x1, %r13d
movq %rbp, %r11
movslq 0x8(%rdi), %rax
movslq %r13d, %rdx
addq %rax, %rdx
shlq $0x2, %rdx
leaq (%rdx,%rdx,4), %rax
movq 0x68(%rsp), %rcx
movl (%rcx,%rax), %r15d
movq 0x70(%rsp), %rcx
movl 0x8(%rcx,%rax), %r12d
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %rdx, %rax
shrq $0x20, %rax
shlq $0x5, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
je 0x1f51e
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax), %r8
xorl %ebx, %ebx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpl %r12d, (%r8)
sete %cl
cmovel %ebx, %r9d
addl %ecx, %ebp
incq %rbx
addq $0x20, %r8
cmpq %rbx, %rdx
jne 0x1f4ff
movl %r9d, %edx
jmp 0x1f525
xorl %ebp, %ebp
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x1, %ebp
jne 0x1f53d
addq %r10, %rax
shlq $0x5, %rdx
addq %rax, %rdx
subq %r10, %rdx
sarq $0x5, %rdx
jmp 0x1f548
xorl %edx, %edx
testl %ebp, %ebp
sete %dl
orq $-0x2, %rdx
xorq $0x1, %r12
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rax
movq %rax, %r12
shrq $0x20, %r12
shlq $0x5, %r12
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rcx, %rax
je 0x1f5a3
movq 0x38(%rsp), %rcx
leaq (%rcx,%r12), %r9
xorl %ebx, %ebx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movl (%r9), %ecx
xorl %r15d, %ecx
xorl %esi, %esi
cmpl $0x1, %ecx
sete %sil
cmovel %ebx, %r8d
addl %esi, %ebp
incq %rbx
addq $0x20, %r9
cmpq %rbx, %rax
jne 0x1f57d
movl %r8d, %eax
jmp 0x1f5aa
xorl %ebp, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %ebp
jne 0x1f5c2
addq %r10, %r12
shlq $0x5, %rax
addq %r12, %rax
subq %r10, %rax
sarq $0x5, %rax
jmp 0x1f5cd
xorl %eax, %eax
testl %ebp, %ebp
sete %al
orq $-0x2, %rax
movq %r11, %rbp
testq %rdx, %rdx
js 0x1f7c0
testq %rax, %rax
js 0x1f7c0
movq %rdx, %rcx
shlq $0x5, %rcx
movq %rax, %rsi
shlq $0x5, %rsi
movq 0x18(%r10,%rsi), %rsi
xorq 0x18(%r10,%rcx), %rsi
jns 0x1f7a1
movq 0x28(%rsp), %rcx
incl (%rcx,%rdx,4)
incl (%rcx,%rax,4)
incl %r13d
cmpl 0xc(%rdi), %r13d
jl 0x1f4a6
cmpl $0x0, 0x8(%rbp)
movl 0x34(%rsp), %r13d
jg 0x1f34f
movq 0x60(%rsp), %rcx
incq %rcx
cmpq 0x48(%rsp), %rcx
jne 0x1f324
movq 0x8(%rsp), %rdx
movq 0x40(%rdx), %rax
testq %rax, %rax
movq 0x40(%rsp), %rsi
je 0x1f678
xorl %ecx, %ecx
movsd 0x9a69(%rip), %xmm0     # 0x290c0
movq 0x28(%rsp), %rdi
cmpl $0x0, (%rdi,%rcx,4)
jle 0x1f670
movsd (%rsi,%rcx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x1f65c
cmpl $0x0, 0x4(%rdx)
je 0x1f765
leaq 0x4(%r14), %r12
xorl %r15d, %r15d
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rbx
movslq 0x4(%rbx,%r15,4), %rsi
movslq (%rbx,%r15,4), %rdi
movl %esi, %eax
subl %edi, %eax
movq 0x20(%rsp), %rcx
cmpl %eax, (%rcx,%r15,4)
jne 0x1f7fe
leaq 0x1(%r15), %rbp
shlq $0x4, %rdi
addq %r14, %rdi
shlq $0x4, %rsi
addq %r14, %rsi
callq 0x3e61
movslq (%rbx,%r15,4), %rax
movslq (%rbx,%rbp,4), %rcx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jge 0x1f729
subq %rax, %rcx
shlq $0x4, %rax
addq %r12, %rax
xorl %r8d, %r8d
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
movl -0x4(%rax), %r9d
cmpl %esi, %r9d
jbe 0x1f702
addl %r8d, %esi
addl %esi, %edx
movl (%rax), %esi
andl %r13d, %esi
movl %r9d, %edi
jmp 0x1f70f
movl (%rax), %r8d
andl %r13d, %r8d
cmpl %esi, %r8d
cmovgl %r8d, %esi
movl %edi, %r8d
negl %r8d
addq $0x10, %rax
decq %rcx
jne 0x1f6ea
subl %edi, %esi
addl %edx, %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rax
movq %r15, %rcx
shlq $0x6, %rcx
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%rcx), %xmm1
divsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
addsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
movl 0x4(%rdx), %eax
movq %rbp, %r15
cmpq %rax, %rbp
jb 0x1f694
xorl %edi, %edi
movq 0x28(%rsp), %rsi
callq 0x30f6
xorl %edi, %edi
movq %r14, %rsi
callq 0x30f6
xorl %edi, %edi
movq 0x18(%rsp), %rsi
callq 0x30f6
xorl %edi, %edi
movq 0x20(%rsp), %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30f6
leaq 0xa590(%rip), %rdi      # 0x29d38
leaq 0xa4aa(%rip), %rsi      # 0x29c59
leaq 0xa5ad(%rip), %rcx      # 0x29d63
movl $0x71, %edx
callq 0x2160
leaq 0xa601(%rip), %rdi      # 0x29dc8
leaq 0xa48b(%rip), %rsi      # 0x29c59
leaq 0xa58e(%rip), %rcx      # 0x29d63
movl $0x70, %edx
callq 0x2160
leaq 0xa453(%rip), %rdi      # 0x29c39
leaq 0xa46c(%rip), %rsi      # 0x29c59
leaq 0xa56f(%rip), %rcx      # 0x29d63
movl $0x45, %edx
callq 0x2160
leaq 0xa5d8(%rip), %rdi      # 0x29ddd
leaq 0xa44d(%rip), %rsi      # 0x29c59
leaq 0xa550(%rip), %rcx      # 0x29d63
movl $0x7d, %edx
callq 0x2160
nopl (%rax)