// File: /workspace/github_c_makefile_star2_O1/lh3[P]minigraph/ggsimple.c
// Function: mg_gc_index
int32_t mg_gc_index(void *km, int min_mapq, int min_map_len, int min_depth_len, const gfa_t *g, int32_t n_seq, mg_gchains_t *const* gcs,
					double *a_dens, int32_t **soff_, int32_t **qoff_, mg_intv_t **sintv_, mg_intv_t **qintv_)
{
	int32_t t, i, j, max_acnt, *scnt, *soff, *qcnt, *qoff;
	int64_t sum_acnt, sum_alen;
	mg_intv_t *sintv, *qintv;

	// count the number of intervals on each segment
	KCALLOC(km, scnt, g->n_seg);
	KCALLOC(km, qcnt, n_seq);
	for (t = 0, max_acnt = 0; t < n_seq; ++t) {
		const mg_gchains_t *gt = gcs[t];
		for (i = 0; i < gt->n_gc; ++i) {
			const mg_gchain_t *gc = &gt->gc[i];
			if (gc->id != gc->parent) continue;
			if (gc->blen < min_depth_len || gc->mapq < min_mapq) continue;
			if (gc->n_anchor > max_acnt) max_acnt = gc->n_anchor;
			++qcnt[t];
			for (j = 0; j < gc->cnt; ++j)
				++scnt[gt->lc[gc->off + j].v>>1];
		}
	}
	if (max_acnt == 0) { // no gchain
		kfree(km, scnt); kfree(km, qcnt);
		return 0;
	}

	// compute soff[] and qoff[]
	KMALLOC(km, soff, g->n_seg + 1);
	KMALLOC(km, qoff, n_seq + 1);
	for (soff[0] = 0, i = 1; i <= g->n_seg; ++i)
		soff[i] = soff[i - 1] + scnt[i - 1];
	for (qoff[0] = 0, i = 1; i <= n_seq; ++i)
		qoff[i] = qoff[i - 1] + qcnt[i - 1];

	// populate the interval list
	memset(scnt, 0, 4 * g->n_seg);
	memset(qcnt, 0, 4 * n_seq);
	KMALLOC(km, sintv, soff[g->n_seg]);
	KMALLOC(km, qintv, qoff[n_seq]);
	sum_acnt = sum_alen = 0;
	for (t = 0; t < n_seq; ++t) {
		const mg_gchains_t *gt = gcs[t];
		for (i = 0; i < gt->n_gc; ++i) {
			const mg_gchain_t *gc = &gt->gc[i];
			mg_intv_t *p;
			if (gc->id != gc->parent) continue;
			if (gc->blen < min_depth_len || gc->mapq < min_mapq) continue;
			p = &qintv[qoff[t] + qcnt[t]];
			++qcnt[t];
			p->st = gc->qs, p->en = gc->qe, p->rev = 0, p->far = -1, p->i = -1;
			for (j = 0; j < gc->cnt; ++j) {
				const mg_llchain_t *lc = &gt->lc[gc->off + j];
				int32_t rs, re, tmp;
				if (lc->cnt > 0) { // compute start and end on the forward strand on the segment
					const mg128_t *qs = &gt->a[lc->off];
					const mg128_t *qe = &gt->a[lc->off + lc->cnt - 1];
					int32_t rs0 = (int32_t)qs->x + 1 - (int32_t)(qs->y>>32&0xff);
					int32_t re0 = (int32_t)qe->x;
					assert(rs0 >= 0 && re0 > rs0 && re0 < g->seg[lc->v>>1].len);
					sum_alen += re0 - rs0, sum_acnt += (qe->x>>32) - (qs->x>>32) + 1;
					rs = 0, re = g->seg[lc->v>>1].len;
					if (j == 0) rs = gc->p? gc->p->ss : rs0;
					if (j == gc->cnt - 1) re = gc->p? gc->p->ee : re0;
					if (lc->v&1) // swap rs and re
						tmp = rs, rs = g->seg[lc->v>>1].len - re, re = g->seg[lc->v>>1].len - tmp;
				} else rs = 0, re = g->seg[lc->v>>1].len;
				p = &sintv[soff[lc->v>>1] + scnt[lc->v>>1]];
				++scnt[lc->v>>1];
				p->st = rs, p->en = re, p->rev = lc->v&1, p->far = -1, p->i = -1;
			}
		}
	}
	*a_dens = (double)sum_acnt / sum_alen;

	// sort and index intervals
	for (i = 0; i < g->n_seg; ++i) {
		assert(soff[i+1] - soff[i] == scnt[i]);
		mg_intv_index(soff[i+1] - soff[i], &sintv[soff[i]]);
	}
	kfree(km, scnt);
	for (i = 0; i < n_seq; ++i) {
		assert(qoff[i+1] - qoff[i] == qcnt[i]);
		mg_intv_index(qoff[i+1] - qoff[i], &qintv[qoff[i]]);
	}
	kfree(km, qcnt);

	*sintv_ = sintv, *qintv_ = qintv;
	*soff_ = soff, *qoff_ = qoff;
	return max_acnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, %r15d
movq %r8, %r13
movl %ecx, 0x4(%rsp)
movl %esi, 0x14(%rsp)
movq %rdi, %rbx
movl 0x4(%r8), %esi
movl $0x4, %edx
callq 0x3054
movq %rax, %rbp
movslq %r15d, %r12
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3054
movq %rax, %r14
movq %r15, 0x58(%rsp)
movl %r15d, %r15d
movq %r12, 0x8(%rsp)
testl %r12d, %r12d
jle 0x21a7a
xorl %eax, %eax
xorl %r11d, %r11d
movl 0x4(%rsp), %r9d
movq 0xb0(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x8(%rcx)
jle 0x21a6c
movq 0x18(%rcx), %rdx
xorl %esi, %esi
imulq $0x68, %rsi, %rdi
movl (%rdx,%rdi), %r8d
cmpl 0x4(%rdx,%rdi), %r8d
jne 0x21a60
addq %rdx, %rdi
cmpl %r9d, 0x2c(%rdi)
jl 0x21a60
movzbl 0x44(%rdi), %r8d
cmpl 0x14(%rsp), %r8d
jl 0x21a60
movl 0x10(%rdi), %r8d
cmpl %r11d, %r8d
cmovgl %r8d, %r11d
incl (%r14,%rax,4)
cmpl $0x0, 0xc(%rdi)
jle 0x21a60
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 (%rbp,%r10,4)
incq %r9
cmpl 0xc(%rdi), %r9d
jl 0x21a3a
movl 0x4(%rsp), %r9d
incq %rsi
movslq 0x8(%rcx), %rdi
cmpq %rdi, %rsi
jl 0x219fa
incq %rax
cmpq %r15, %rax
jne 0x219e2
jmp 0x21a7d
xorl %r11d, %r11d
testl %r11d, %r11d
je 0x21ded
movl %r11d, 0x4c(%rsp)
movl 0x4(%r13), %esi
incl %esi
shlq $0x2, %rsi
movq %rbx, %rdi
callq 0x31c1
movq %rax, %r12
movq 0x8(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
movq %rbx, %rdi
callq 0x31c1
movl $0x0, (%r12)
cmpl $0x0, 0x4(%r13)
je 0x21adf
movl (%r12), %esi
xorl %ecx, %ecx
addl (%rbp,%rcx,4), %esi
movl %esi, 0x4(%r12,%rcx,4)
movl 0x4(%r13), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x21aca
movq %r12, 0x38(%rsp)
movl $0x0, (%rax)
movq 0x58(%rsp), %r12
testl %r12d, %r12d
jle 0x21b08
movl (%rax), %edx
xorl %ecx, %ecx
addl (%r14,%rcx,4), %edx
movl %edx, 0x4(%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r15
jne 0x21af8
movq %r15, 0x60(%rsp)
movl 0x4(%r13), %edx
shll $0x2, %edx
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq %rax, %r15
callq 0x2180
leal (,%r12,4), %eax
movslq %eax, %rdx
movq %r14, 0x30(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2180
movq %r13, 0x18(%rsp)
movl 0x4(%r13), %eax
movq 0x38(%rsp), %r13
movslq (%r13,%rax,4), %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x31c1
movq %rax, %rbp
movq %r15, 0x28(%rsp)
movq 0x8(%rsp), %rax
movslq (%r15,%rax,4), %rsi
shlq $0x4, %rsi
movq %rbx, 0x50(%rsp)
movq %rbx, %rdi
callq 0x31c1
movq %rax, 0x20(%rsp)
testl %r12d, %r12d
jle 0x21e0a
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
movq $0x0, 0x70(%rsp)
movq 0xb0(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq (%rax,%rcx,8), %r8
cmpl $0x0, 0x8(%r8)
jle 0x21dc6
xorl %r9d, %r9d
movq 0x18(%r8), %r10
imulq $0x68, %r9, %rax
movl (%r10,%rax), %ecx
cmpl 0x4(%r10,%rax), %ecx
jne 0x21db6
addq %rax, %r10
movl 0x4(%rsp), %eax
cmpl %eax, 0x2c(%r10)
jl 0x21db6
movzbl 0x44(%r10), %eax
cmpl 0x14(%rsp), %eax
jl 0x21db6
movq 0x28(%rsp), %rax
movq 0x68(%rsp), %rsi
movslq (%rax,%rsi,4), %rax
movq 0x30(%rsp), %rdx
movslq (%rdx,%rsi,4), %rcx
addq %rcx, %rax
shlq $0x4, %rax
incl %ecx
movl %ecx, (%rdx,%rsi,4)
movl 0x18(%r10), %ecx
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx,%rax)
movl 0x1c(%r10), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
movl %ecx, 0x4(%rdx,%rax)
movq $-0x1, 0x8(%rdx,%rax)
movl 0xc(%r10), %ebx
testl %ebx, %ebx
jle 0x21db6
xorl %r11d, %r11d
movq 0x20(%r8), %rax
movslq 0x8(%r10), %rcx
addq %r11, %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,4), %rcx
movslq 0x4(%rcx), %rdi
testq %rdi, %rdi
jle 0x21d3e
movq 0x28(%r8), %r14
movslq (%rcx), %r15
movq %r15, %rdx
shlq $0x4, %rdx
movq (%r14,%rdx), %rax
movzbl 0xc(%r14,%rdx), %esi
movl %eax, %edx
subl %esi, %edx
incl %edx
js 0x21f07
addq %rdi, %r15
shlq $0x4, %r15
movq -0x10(%r14,%r15), %r12
movl %r12d, %r13d
subl %edx, %r13d
jle 0x21f07
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rsi
movl 0x8(%rcx), %edi
movl %edi, %r14d
shrl %r14d
shlq $0x6, %r14
movl (%rsi,%r14), %r14d
cmpl %r12d, %r14d
jle 0x21f07
xorl %r15d, %r15d
testl %r11d, %r11d
jne 0x21ce6
movq 0x48(%r10), %rsi
movl %edx, %r15d
testq %rsi, %rsi
je 0x21ce6
movl 0x10(%rsi), %r15d
decl %ebx
movl %r14d, %edx
cmpl %ebx, %r11d
jne 0x21cff
movq 0x48(%r10), %rsi
movl %r12d, %edx
testq %rsi, %rsi
je 0x21cff
movl 0x14(%rsi), %edx
movslq %r13d, %rsi
addq %rsi, 0x70(%rsp)
shrq $0x20, %r12
shrq $0x20, %rax
movq 0x8(%rsp), %rsi
subq %rax, %rsi
leaq (%rsi,%r12), %rax
incq %rax
testb $0x1, %dil
movq %rax, 0x8(%rsp)
je 0x21d37
movl %r14d, %eax
subl %edx, %eax
subl %r15d, %r14d
movl %eax, %r15d
movl %r14d, %edx
movq 0x38(%rsp), %r13
jmp 0x21d56
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x8(%rcx), %edx
shrl %edx
shlq $0x6, %rdx
movl (%rax,%rdx), %edx
xorl %r15d, %r15d
movl 0x8(%rcx), %eax
shrl %eax
movslq (%r13,%rax,4), %rsi
movq 0x40(%rsp), %rbx
movslq (%rbx,%rax,4), %rdi
addq %rdi, %rsi
shlq $0x4, %rsi
incl %edi
movl %edi, (%rbx,%rax,4)
movl %r15d, (%rbp,%rsi)
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%rbp,%rsi), %eax
movl $0x80000000, %edi       # imm = 0x80000000
andl %edi, %eax
orl %edx, %eax
movl %eax, 0x4(%rbp,%rsi)
movl 0x8(%rcx), %eax
shll $0x1f, %eax
orl %edx, %eax
movl %eax, 0x4(%rbp,%rsi)
movq $-0x1, 0x8(%rbp,%rsi)
movl 0xc(%r10), %ebx
incq %r11
cmpl %ebx, %r11d
jl 0x21c4f
incq %r9
movslq 0x8(%r8), %rax
cmpq %rax, %r9
jl 0x21bc2
movq 0x68(%rsp), %rcx
incq %rcx
cmpq 0x60(%rsp), %rcx
jne 0x21ba3
cvtsi2sdq 0x8(%rsp), %xmm0
cvtsi2sdq 0x70(%rsp), %xmm1
divsd %xmm1, %xmm0
jmp 0x21e12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x30f6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30f6
xorl %eax, %eax
jmp 0x21ef8
movsd 0x8236(%rip), %xmm0     # 0x2a048
movq 0xb8(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x21e61
xorl %ebx, %ebx
movl 0x4(%r13,%rbx,4), %edi
movslq (%r13,%rbx,4), %rsi
subl %esi, %edi
movq 0x40(%rsp), %rax
cmpl (%rax,%rbx,4), %edi
jne 0x21f26
incq %rbx
shlq $0x4, %rsi
addq %rbp, %rsi
callq 0x3e83
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
cmpq %rax, %rbx
jb 0x21e2b
movq 0x50(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x30f6
cmpl $0x0, 0x58(%rsp)
jle 0x21eae
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movl 0x4(%rax,%rbx,4), %edi
movslq (%rax,%rbx,4), %rsi
subl %esi, %edi
movq 0x30(%rsp), %rax
cmpl (%rax,%rbx,4), %edi
jne 0x21f45
incq %rbx
shlq $0x4, %rsi
addq 0x20(%rsp), %rsi
callq 0x3e83
cmpq %rbx, 0x60(%rsp)
jne 0x21e79
movq 0xd8(%rsp), %rbx
movq 0xd0(%rsp), %r14
movq 0xc8(%rsp), %r15
movq 0xc0(%rsp), %r12
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x30f6
movq %rbp, (%r14)
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
movq %r13, (%r12)
movq 0x28(%rsp), %rax
movq %rax, (%r15)
movl 0x4c(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x815a(%rip), %rdi      # 0x2a068
leaq 0x8187(%rip), %rsi      # 0x2a09c
leaq 0x818b(%rip), %rcx      # 0x2a0a7
movl $0x46, %edx
callq 0x2160
leaq 0x8212(%rip), %rdi      # 0x2a13f
leaq 0x8168(%rip), %rsi      # 0x2a09c
leaq 0x816c(%rip), %rcx      # 0x2a0a7
movl $0x58, %edx
callq 0x2160
leaq 0x8212(%rip), %rdi      # 0x2a15e
leaq 0x8149(%rip), %rsi      # 0x2a09c
leaq 0x814d(%rip), %rcx      # 0x2a0a7
movl $0x5d, %edx
callq 0x2160