// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movl %r9d, %r12d
movq %r8, %rbp
movl %ecx, 0xc(%rsp)
movl %esi, 0x1c(%rsp)
movq %rdi, %r14
movl 0x4(%r8), %esi
movl $0x4, %edx
callq 0x2f84
movq %rax, %rbx
movslq %r12d, %r13
movl $0x4, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2f84
movq %rax, %r15
movq %r13, 0x48(%rsp)
testl %r13d, %r13d
jle 0x21e0b
movl %r12d, %r13d
xorl %eax, %eax
xorl %r11d, %r11d
movl 0xc(%rsp), %r9d
movq 0xc0(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl $0x0, 0x8(%rcx)
jle 0x21a71
movq 0x18(%rcx), %rdx
xorl %esi, %esi
imulq $0x68, %rsi, %rdi
movl (%rdx,%rdi), %r8d
cmpl 0x4(%rdx,%rdi), %r8d
jne 0x21a65
addq %rdx, %rdi
cmpl %r9d, 0x2c(%rdi)
jl 0x21a65
movzbl 0x44(%rdi), %r8d
cmpl 0x1c(%rsp), %r8d
jl 0x21a65
movl 0x10(%rdi), %r8d
cmpl %r11d, %r8d
cmovgl %r8d, %r11d
incl (%r15,%rax,4)
cmpl $0x0, 0xc(%rdi)
jle 0x21a65
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 0x21a40
movl 0xc(%rsp), %r9d
incq %rsi
movslq 0x8(%rcx), %rdi
cmpq %rdi, %rsi
jl 0x21a00
incq %rax
cmpq %r13, %rax
jne 0x219e8
testl %r11d, %r11d
je 0x21e0b
movl %r11d, 0x5c(%rsp)
movl 0x4(%rbp), %esi
incl %esi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x30e5
movq %r12, %rcx
movq %rax, %r12
movq %rcx, 0x68(%rsp)
leal 0x1(%rcx), %eax
movl %eax, 0x10(%rsp)
movslq %eax, %rsi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x30e5
movl $0x0, (%r12)
cmpl $0x0, 0x4(%rbp)
je 0x21ae2
xorl %esi, %esi
xorl %ecx, %ecx
addl (%rbx,%rsi,4), %ecx
movl %ecx, 0x4(%r12,%rsi,4)
movl 0x4(%rbp), %edx
incq %rsi
cmpq %rdx, %rsi
jb 0x21acf
movq %r12, 0x40(%rsp)
movq %rbx, 0x20(%rsp)
movq %r13, 0x70(%rsp)
movl $0x0, (%rax)
movl 0x10(%rsp), %esi
movl $0x1, %ecx
xorl %edx, %edx
addl -0x4(%r15,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rsi
jne 0x21b02
movl 0x4(%rbp), %edx
shll $0x2, %edx
movq 0x20(%rsp), %rdi
movq %rax, %rbx
xorl %esi, %esi
callq 0x2180
movq 0x68(%rsp), %r12
leal (,%r12,4), %edx
movq %r15, 0x38(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2180
movq %rbp, 0x10(%rsp)
movl 0x4(%rbp), %eax
movq 0x40(%rsp), %r13
movslq (%r13,%rax,4), %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x30e5
movq %rax, 0x50(%rsp)
movq %rbx, 0x30(%rsp)
movq 0x48(%rsp), %rax
movslq (%rbx,%rax,4), %rsi
shlq $0x4, %rsi
movq %r14, 0x60(%rsp)
movq %r14, %rdi
callq 0x30e5
movq %rax, 0x28(%rsp)
testl %r12d, %r12d
jle 0x21e28
xorl %ecx, %ecx
xorl %r8d, %r8d
movq $0x0, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movq %rcx, 0x78(%rsp)
movq (%rax,%rcx,8), %rdi
cmpl $0x0, 0x8(%rdi)
jle 0x21de3
xorl %edx, %edx
movq 0x18(%rdi), %r10
imulq $0x68, %rdx, %rax
movl (%r10,%rax), %ecx
cmpl 0x4(%r10,%rax), %ecx
movq %rdx, 0x48(%rsp)
jne 0x21dce
addq %rax, %r10
movl 0xc(%rsp), %eax
cmpl %eax, 0x2c(%r10)
jl 0x21dce
movzbl 0x44(%r10), %eax
cmpl 0x1c(%rsp), %eax
jl 0x21dce
movq 0x30(%rsp), %rax
movq 0x78(%rsp), %rsi
movslq (%rax,%rsi,4), %rax
movq 0x38(%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 0x28(%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), %eax
testl %eax, %eax
jle 0x21dce
xorl %r11d, %r11d
movq 0x20(%rdi), %rcx
movslq 0x8(%r10), %rdx
addq %r11, %rdx
leaq (%rdx,%rdx,4), %rdx
leaq (%rcx,%rdx,4), %rcx
movslq 0x4(%rcx), %rsi
testq %rsi, %rsi
jle 0x21d4d
movq %r8, %r9
movq %rdi, %rbp
movq 0x28(%rdi), %rdi
movslq (%rcx), %rbx
movq %rbx, %r8
shlq $0x4, %r8
movq (%rdi,%r8), %rdx
movzbl 0xc(%rdi,%r8), %r8d
movl %edx, %r12d
subl %r8d, %r12d
incl %r12d
js 0x21f2e
addq %rsi, %rbx
shlq $0x4, %rbx
movq -0x10(%rdi,%rbx), %r14
movl %r14d, %esi
subl %r12d, %esi
jle 0x21f2e
movq 0x10(%rsp), %rdi
movq 0x10(%rdi), %rdi
movl 0x8(%rcx), %r13d
movl %r13d, %ebx
shrl %ebx
movq %rbx, %r8
shlq $0x6, %r8
movl (%rdi,%r8), %edi
cmpl %r14d, %edi
jle 0x21f2e
xorl %r15d, %r15d
testl %r11d, %r11d
jne 0x21cf6
movq 0x48(%r10), %r8
movl %r12d, %r15d
testq %r8, %r8
je 0x21cf6
movl 0x10(%r8), %r15d
decl %eax
movl %edi, %r12d
cmpl %eax, %r11d
jne 0x21d10
movq 0x48(%r10), %rax
movl %r14d, %r12d
testq %rax, %rax
je 0x21d10
movl 0x14(%rax), %r12d
movslq %esi, %rax
addq %rax, 0x80(%rsp)
shrq $0x20, %r14
shrq $0x20, %rdx
movq %r9, %r8
subq %rdx, %r8
addq %r14, %r8
incq %r8
testb $0x1, %r13b
je 0x21d43
movl %edi, %eax
subl %r12d, %eax
subl %r15d, %edi
movl %eax, %r15d
movl %edi, %r12d
movq 0x40(%rsp), %r13
movq %rbp, %rdi
jmp 0x21d69
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x8(%rcx), %ebx
shrl %ebx
movq %rbx, %rdx
shlq $0x6, %rdx
movl (%rax,%rdx), %r12d
xorl %r15d, %r15d
movslq (%r13,%rbx,4), %rax
movq 0x20(%rsp), %rsi
movslq (%rsi,%rbx,4), %rdx
addq %rdx, %rax
shlq $0x4, %rax
incl %edx
movl %edx, (%rsi,%rbx,4)
movq 0x50(%rsp), %r9
movl %r15d, (%r9,%rax)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl 0x4(%r9,%rax), %edx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %edx
orl %r12d, %edx
movl %edx, 0x4(%r9,%rax)
movl 0x8(%rcx), %ecx
shll $0x1f, %ecx
orl %r12d, %ecx
movl %ecx, 0x4(%r9,%rax)
movq $-0x1, 0x8(%r9,%rax)
movl 0xc(%r10), %eax
incq %r11
cmpl %eax, %r11d
jl 0x21c53
movq 0x48(%rsp), %rdx
incq %rdx
movslq 0x8(%rdi), %rax
cmpq %rax, %rdx
jl 0x21bc1
movq 0x78(%rsp), %rcx
incq %rcx
cmpq 0x70(%rsp), %rcx
jne 0x21ba4
cvtsi2sd %r8, %xmm0
cvtsi2sdq 0x80(%rsp), %xmm1
divsd %xmm1, %xmm0
jmp 0x21e30
movq %r14, %rdi
movq %rbx, %rsi
callq 0x301f
movq %r14, %rdi
movq %r15, %rsi
callq 0x301f
xorl %eax, %eax
jmp 0x21f1c
movsd 0x82b0(%rip), %xmm0     # 0x2a0e0
movq 0xc8(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x21e81
xorl %ebx, %ebx
movl 0x4(%r13,%rbx,4), %edi
movslq (%r13,%rbx,4), %rsi
subl %esi, %edi
movq 0x20(%rsp), %rax
cmpl (%rax,%rbx,4), %edi
jne 0x21f4d
incq %rbx
shlq $0x4, %rsi
addq 0x50(%rsp), %rsi
callq 0x3d72
movq 0x10(%rsp), %rax
movl 0x4(%rax), %eax
cmpq %rax, %rbx
jb 0x21e49
movq 0x60(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x301f
cmpl $0x0, 0x68(%rsp)
jle 0x21ece
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
movl 0x4(%rax,%rbx,4), %edi
movslq (%rax,%rbx,4), %rsi
subl %esi, %edi
movq 0x38(%rsp), %rax
cmpl (%rax,%rbx,4), %edi
jne 0x21f6c
incq %rbx
shlq $0x4, %rsi
addq 0x28(%rsp), %rsi
callq 0x3d72
cmpq %rbx, 0x70(%rsp)
jne 0x21e99
movq 0x60(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x301f
movq 0xe0(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
movq 0xe8(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0xd0(%rsp), %rax
movq %r13, (%rax)
movq 0xd8(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movl 0x5c(%rsp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x81cb(%rip), %rdi      # 0x2a100
leaq 0x81f8(%rip), %rsi      # 0x2a134
leaq 0x81fc(%rip), %rcx      # 0x2a13f
movl $0x46, %edx
callq 0x2160
leaq 0x8283(%rip), %rdi      # 0x2a1d7
leaq 0x81d9(%rip), %rsi      # 0x2a134
leaq 0x81dd(%rip), %rcx      # 0x2a13f
movl $0x58, %edx
callq 0x2160
leaq 0x8283(%rip), %rdi      # 0x2a1f6
leaq 0x81ba(%rip), %rsi      # 0x2a134
leaq 0x81be(%rip), %rcx      # 0x2a13f
movl $0x5d, %edx
callq 0x2160