// File: /workspace/github_c_makefile_star2_O3/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, 0x48(%rsp)
movl %r8d, %ebp
movl %ecx, %r14d
movq %rdx, 0x38(%rsp)
movl %esi, %r15d
movq %rdi, %r12
movq 0xb0(%rsp), %r13
movl 0x4(%rdi), %esi
movl $0x4, %edx
xorl %edi, %edi
callq 0x2f84
movq %rax, %rbx
movl %r15d, %r11d
movl %r15d, 0x8(%rsp)
testl %r15d, %r15d
jle 0x1f2d1
xorl %eax, %eax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
testq %rcx, %rcx
je 0x1f2c5
movl 0x8(%rcx), %edi
testl %edi, %edi
jle 0x1f2c5
movq 0x18(%rcx), %rdx
xorl %esi, %esi
imulq $0x68, %rsi, %r8
cmpl $0x0, 0xc(%rdx,%r8)
jle 0x1f82c
addq %rdx, %r8
cmpl $0x0, 0x10(%r8)
jle 0x1f82c
movzbl 0x44(%r8), %r9d
cmpl %r14d, %r9d
jl 0x1f2ba
cmpl %ebp, 0x2c(%r8)
jl 0x1f2ba
movq 0x20(%rcx), %rdi
xorl %r9d, %r9d
movslq 0x8(%r8), %r10
addq %r9, %r10
leaq (%r10,%r10,4), %r10
movl 0x8(%rdi,%r10,4), %r10d
shrl %r10d
incl (%rbx,%r10,4)
incq %r9
cmpl 0xc(%r8), %r9d
jl 0x1f297
movl 0x8(%rcx), %edi
incq %rsi
movslq %edi, %r8
cmpq %r8, %rsi
jl 0x1f262
incq %rax
cmpq %r11, %rax
jne 0x1f247
movq %r11, 0x50(%rsp)
movl %r14d, 0x34(%rsp)
movl 0x4(%r12), %esi
incl %esi
shlq $0x2, %rsi
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x30e5
movq %rax, 0x10(%rsp)
movl $0x0, (%rax)
cmpl $0x0, 0x4(%r12)
je 0x1f326
xorl %eax, %eax
movq 0x10(%rsp), %rdx
addl (%rbx,%rax,4), %r14d
movl %r14d, 0x4(%rdx,%rax,4)
movl 0x4(%r12), %ecx
incq %rax
cmpq %rcx, %rax
jb 0x1f30a
shll $0x2, %ecx
movq %rcx, %r14
movl %ebp, 0x30(%rsp)
movq %r13, 0x40(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2180
movl 0x4(%r12), %eax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
shlq $0x4, %rsi
xorl %edi, %edi
callq 0x30e5
movq %rax, %r14
movq %r12, 0x18(%rsp)
movq 0x40(%r12), %rsi
movl $0x4, %edx
xorl %edi, %edi
callq 0x2f84
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x1f67d
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %rsi
testq %rsi, %rsi
je 0x1f66a
cmpl $0x0, 0x8(%rsi)
jle 0x1f66a
movq %rsi, 0x60(%rsp)
movq 0x18(%rsi), %rdi
movzbl 0x44(%rdi), %eax
cmpl 0x34(%rsp), %eax
jl 0x1f660
movl 0x30(%rsp), %eax
cmpl %eax, 0x2c(%rdi)
jl 0x1f660
movl 0xc(%rdi), %edx
testl %edx, %edx
jle 0x1f660
xorl %eax, %eax
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %r12
movq 0x20(%rsi), %r9
movslq 0x8(%rdi), %rcx
addq %rax, %rcx
leaq (%rcx,%rcx,4), %r10
leaq (%r9,%r10,4), %rcx
xorl %r8d, %r8d
testl %eax, %eax
jne 0x1f41b
movq 0x28(%rsi), %r11
movslq (%rcx), %r15
shlq $0x4, %r15
movl (%r11,%r15), %r8d
movzbl 0xc(%r11,%r15), %r11d
subl %r11d, %r8d
incl %r8d
movq 0x10(%r13), %r15
movl 0x8(%r9,%r10,4), %r11d
movl %r11d, %r9d
shrl %r9d
movq %r9, %r10
shlq $0x6, %r10
movl (%r15,%r10), %r10d
decl %edx
movl %r10d, %ebp
cmpl %edx, %eax
jne 0x1f45c
movq 0x28(%rsi), %rdx
movslq (%rcx), %r15
movslq 0x4(%rcx), %r12
addq %r15, %r12
shlq $0x4, %r12
movl -0x10(%rdx,%r12), %ebp
movq 0x10(%rsp), %r12
incl %ebp
movl %r10d, %edx
subl %r8d, %edx
subl %ebp, %r10d
testb $0x1, %r11b
cmovel %r8d, %r10d
cmovel %ebp, %edx
movslq (%r12,%r9,4), %r8
movslq (%rbx,%r9,4), %r11
addq %r11, %r8
shlq $0x4, %r8
incl %r11d
movl %r11d, (%rbx,%r9,4)
movl %r10d, (%r14,%r8)
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%r14,%r8), %r9d
movl $0x80000000, %r10d      # imm = 0x80000000
andl %r10d, %r9d
orl %edx, %r9d
movl %r9d, 0x4(%r14,%r8)
movl 0x8(%rcx), %ecx
shll $0x1f, %ecx
orl %edx, %ecx
movl %ecx, 0x4(%r14,%r8)
movq $-0x1, 0x8(%r14,%r8)
movl 0xc(%rdi), %edx
incq %rax
cmpl %edx, %eax
jl 0x1f3e6
cmpl $0x2, %edx
jl 0x1f660
movq 0x20(%rsi), %rax
movq %rax, 0x70(%rsp)
addq $-0xc, %rax
movq %rax, 0x68(%rsp)
movq 0x18(%rsp), %rax
movq 0x48(%rax), %r10
movq 0x58(%rax), %rbx
leaq 0x8(%r10), %rax
movq %rax, 0x8(%rsp)
movl $0x1, %r13d
movslq 0x8(%rdi), %rax
movslq %r13d, %rcx
addq %rax, %rcx
shlq $0x2, %rcx
leaq (%rcx,%rcx,4), %rax
movq 0x68(%rsp), %rcx
movl (%rcx,%rax), %ecx
movq 0x70(%rsp), %rdx
movl 0x8(%rdx,%rax), %r15d
movq (%rbx,%rcx,8), %rdx
movq %rdx, %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rbp
je 0x1f597
shrq $0x20, %rdx
shlq $0x5, %rdx
leaq (%r10,%rdx), %r12
addq 0x8(%rsp), %rdx
xorl %eax, %eax
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %r11d, %r11d
cmpl %r15d, (%rdx)
sete %r11b
cmovel %eax, %r9d
addl %r11d, %r8d
incq %rax
addq $0x20, %rdx
cmpq %rax, %rbp
jne 0x1f552
testl %r8d, %r8d
je 0x1f597
movq $-0x2, %rdx
cmpl $0x1, %r8d
jne 0x1f59e
movl %r9d, %eax
shlq $0x5, %rax
addq %rax, %r12
subq %r10, %r12
sarq $0x5, %r12
movq %r12, %rdx
jmp 0x1f59e
movq $-0x1, %rdx
xorq $0x1, %r15
movq (%rbx,%r15,8), %r12
movq %r12, %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rbp
je 0x1f7ee
movq %rbx, %rsi
shrq $0x20, %r12
shlq $0x5, %r12
leaq (%r10,%r12), %r15
addq 0x8(%rsp), %r12
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl (%r12), %r11d
xorl %ecx, %r11d
xorl %ebx, %ebx
cmpl $0x1, %r11d
sete %bl
cmovel %r8d, %r9d
addl %ebx, %eax
incq %r8
addq $0x20, %r12
cmpq %r8, %rbp
jne 0x1f5d6
cmpl $0x1, %eax
jne 0x1f7ee
testq %rdx, %rdx
js 0x1f7ee
movl %r9d, %eax
shlq $0x5, %rax
addq %rax, %r15
subq %r10, %r15
sarq $0x5, %r15
js 0x1f7ee
movq %rdx, %rax
shlq $0x5, %rax
movq %r15, %rcx
shlq $0x5, %rcx
movq 0x18(%r10,%rcx), %rcx
xorq 0x18(%r10,%rax), %rcx
jns 0x1f80d
movq 0x28(%rsp), %rax
incl (%rax,%rdx,4)
incl (%rax,%r15,4)
incl %r13d
cmpl 0xc(%rdi), %r13d
movq %rsi, %rbx
movq 0x60(%rsp), %rsi
jl 0x1f501
cmpl $0x0, 0x8(%rsi)
jg 0x1f3ab
movq 0x58(%rsp), %rcx
incq %rcx
cmpq 0x50(%rsp), %rcx
jne 0x1f385
movq 0x18(%rsp), %rdx
movq 0x40(%rdx), %rax
testq %rax, %rax
movq 0x48(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x40(%rsp), %rsi
je 0x1f6c5
xorl %ecx, %ecx
movsd 0x9a6c(%rip), %xmm0     # 0x29110
movq 0x28(%rsp), %rdi
cmpl $0x0, (%rdi,%rcx,4)
jle 0x1f6bd
movsd (%rsi,%rcx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x1f6a9
cmpl $0x0, 0x4(%rdx)
je 0x1f7b4
movl (%r15), %eax
leaq 0x4(%r14), %rcx
movq %rcx, 0x8(%rsp)
xorl %r13d, %r13d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movslq 0x4(%r15,%r13,4), %rsi
movl %esi, %ecx
subl %eax, %ecx
movq 0x20(%rsp), %rdx
cmpl %ecx, (%rdx,%r13,4)
jne 0x1f84b
leaq 0x1(%r13), %r12
movslq %eax, %rdi
shlq $0x4, %rdi
addq %r14, %rdi
shlq $0x4, %rsi
addq %r14, %rsi
callq 0x3d50
movslq (%r15,%r13,4), %rcx
movl (%r15,%r12,4), %eax
xorpd %xmm0, %xmm0
cmpl %eax, %ecx
jge 0x1f77d
movslq %eax, %rdx
subq %rcx, %rdx
shlq $0x4, %rcx
addq 0x8(%rsp), %rcx
xorl %r9d, %r9d
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movl -0x4(%rcx), %r10d
cmpl %edi, %r10d
jbe 0x1f755
addl %r9d, %edi
addl %edi, %esi
movl (%rcx), %edi
andl %ebp, %edi
movl %r10d, %r8d
jmp 0x1f762
movl (%rcx), %r9d
andl %ebp, %r9d
cmpl %edi, %r9d
cmovgl %r9d, %edi
movl %r8d, %r9d
negl %r9d
addq $0x10, %rcx
decq %rdx
jne 0x1f73e
subl %r8d, %edi
addl %esi, %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rcx
movq %r13, %rdx
shlq $0x6, %rdx
xorps %xmm1, %xmm1
cvtsi2sdl (%rcx,%rdx), %xmm1
divsd %xmm1, %xmm0
addsd (%rbx,%r13,8), %xmm0
movsd %xmm0, (%rbx,%r13,8)
movl 0x4(%rsi), %ecx
movq %r12, %r13
cmpq %rcx, %r12
jb 0x1f6e3
xorl %edi, %edi
movq 0x28(%rsp), %rsi
callq 0x301f
xorl %edi, %edi
movq %r14, %rsi
callq 0x301f
xorl %edi, %edi
movq %r15, %rsi
callq 0x301f
xorl %edi, %edi
movq 0x20(%rsp), %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x301f
leaq 0xa667(%rip), %rdi      # 0x29e5c
leaq 0xa4f1(%rip), %rsi      # 0x29ced
leaq 0xa5f4(%rip), %rcx      # 0x29df7
movl $0x70, %edx
callq 0x2160
leaq 0xa5b8(%rip), %rdi      # 0x29dcc
leaq 0xa4d2(%rip), %rsi      # 0x29ced
leaq 0xa5d5(%rip), %rcx      # 0x29df7
movl $0x71, %edx
callq 0x2160
leaq 0xa49a(%rip), %rdi      # 0x29ccd
leaq 0xa4b3(%rip), %rsi      # 0x29ced
leaq 0xa5b6(%rip), %rcx      # 0x29df7
movl $0x45, %edx
callq 0x2160
leaq 0xa61f(%rip), %rdi      # 0x29e71
leaq 0xa494(%rip), %rsi      # 0x29ced
leaq 0xa597(%rip), %rcx      # 0x29df7
movl $0x7d, %edx
callq 0x2160
nop