// File: /workspace/github_c_makefile_star2_O2/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, %ebp
movl %ecx, 0x34(%rsp)
movl %esi, 0x30(%rsp)
movq %rdi, %rbx
movq %r8, 0x18(%rsp)
movl 0x4(%r8), %esi
pushq $0x4
popq %r14
movq %r14, %rdx
callq 0x24f0
movq %rax, %r12
movslq %ebp, %r15
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x24f0
movq %rax, 0x8(%rsp)
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movq %rcx, 0x38(%rsp)
xorl %ebp, %ebp
cmpq 0x38(%rsp), %rax
je 0x1da32
movq 0xb0(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
xorl %edx, %edx
movslq 0x8(%rcx), %rsi
cmpq %rsi, %rdx
jge 0x1da2a
movq 0x18(%rcx), %rsi
imulq $0x68, %rdx, %rdi
movl (%rsi,%rdi), %r8d
cmpl 0x4(%rsi,%rdi), %r8d
jne 0x1da25
addq %rdi, %rsi
movl 0x34(%rsp), %edi
cmpl %edi, 0x2c(%rsi)
jl 0x1da25
movzbl 0x44(%rsi), %edi
cmpl 0x30(%rsp), %edi
jl 0x1da25
movl 0x10(%rsi), %edi
movq 0x8(%rsp), %r8
incl (%r8,%rax,4)
xorl %r8d, %r8d
cmpl 0xc(%rsi), %r8d
jge 0x1da20
movq 0x20(%rcx), %r9
movslq 0x8(%rsi), %r10
addq %r8, %r10
imulq $0x14, %r10, %r10
movl 0x8(%r9,%r10), %r9d
shrl %r9d
incl (%r12,%r9,4)
incq %r8
jmp 0x1d9fa
cmpl %ebp, %edi
cmovgl %edi, %ebp
incq %rdx
jmp 0x1d9b9
incq %rax
jmp 0x1d9a0
testl %ebp, %ebp
je 0x1de67
movq 0x18(%rsp), %rax
movl 0x4(%rax), %esi
incl %esi
shlq $0x2, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x266c
movq %rax, %r14
leaq 0x4(,%r15,4), %rsi
movq %rbx, %rdi
callq 0x266c
movq %r14, 0x28(%rsp)
andl $0x0, (%r14)
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movl 0x4(%rdx), %edx
cmpq %rdx, %rax
ja 0x1da9a
addl -0x4(%r12,%rax,4), %ecx
movq 0x28(%rsp), %rdx
movl %ecx, (%rdx,%rax,4)
incq %rax
jmp 0x1da7b
movq 0x10(%rsp), %rax
andl $0x0, (%rax)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, 0x38(%rsp)
je 0x1dac3
movq 0x8(%rsp), %rdx
addl (%rdx,%rax,4), %ecx
movq 0x10(%rsp), %rdx
movl %ecx, 0x4(%rdx,%rax,4)
incq %rax
jmp 0x1daa6
movl %ebp, 0x54(%rsp)
movq 0x18(%rsp), %rbx
movl 0x4(%rbx), %edx
shll $0x2, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2180
leal (,%r15,4), %eax
movslq %eax, %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2180
movl 0x4(%rbx), %eax
movq 0x28(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
shlq $0x4, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x266c
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %rax
movslq (%rax,%r15,4), %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x266c
xorl %esi, %esi
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r12, 0x58(%rsp)
cmpq 0x38(%rsp), %rsi
je 0x1dd77
movq 0xb0(%rsp), %rax
movq (%rax,%rsi,8), %r8
xorl %edx, %edx
movq %rsi, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movslq 0x8(%r8), %rax
cmpq %rax, %rdx
jge 0x1dd6f
movq 0x18(%r8), %r9
imulq $0x68, %rdx, %rax
movl (%r9,%rax), %ecx
cmpl 0x4(%r9,%rax), %ecx
jne 0x1dd67
addq %rax, %r9
movl 0x34(%rsp), %eax
cmpl %eax, 0x2c(%r9)
jl 0x1dd67
movzbl 0x44(%r9), %eax
cmpl 0x30(%rsp), %eax
jl 0x1dd67
movq %rdx, 0x70(%rsp)
movq 0x10(%rsp), %rax
movslq (%rax,%rsi,4), %rax
movq 0x8(%rsp), %rdx
movslq (%rdx,%rsi,4), %rcx
addq %rcx, %rax
shlq $0x4, %rax
incl %ecx
movl %ecx, (%rdx,%rsi,4)
movl 0x18(%r9), %ecx
movq 0x40(%rsp), %rdx
movl %ecx, (%rdx,%rax)
movl 0x1c(%r9), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
movl %ecx, 0x4(%rdx,%rax)
orq $-0x1, 0x8(%rdx,%rax)
xorl %r10d, %r10d
movl 0xc(%r9), %edx
cmpl %edx, %r10d
jge 0x1dd5d
movq 0x20(%r8), %rax
movslq 0x8(%r9), %rcx
addq %r10, %rcx
imulq $0x14, %rcx, %rcx
leaq (%rax,%rcx), %r11
movslq 0x4(%rax,%rcx), %rax
testq %rax, %rax
jle 0x1dcea
movq %r9, %rsi
movq %r13, %r9
movq 0x28(%r8), %rcx
movslq (%r11), %r8
movq %r8, %rdi
shlq $0x4, %rdi
movq (%rcx,%rdi), %r13
movzbl 0xc(%rcx,%rdi), %edi
movl %r13d, %r15d
subl %edi, %r15d
incl %r15d
js 0x1de95
addq %rax, %r8
shlq $0x4, %r8
movq -0x10(%rcx,%r8), %r14
movl %r14d, %r12d
subl %r15d, %r12d
jle 0x1de95
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x8(%r11), %ecx
movl %ecx, %eax
shrl %eax
movq %rax, %r8
shlq $0x6, %r8
movl (%rdi,%r8), %r8d
cmpl %r14d, %r8d
jle 0x1de95
xorl %ebx, %ebx
testl %r10d, %r10d
jne 0x1dc96
movq 0x48(%rsi), %rdi
movl %r15d, %ebx
testq %rdi, %rdi
je 0x1dc96
movl 0x10(%rdi), %ebx
decl %edx
movl %r8d, %r15d
cmpl %edx, %r10d
jne 0x1dcb0
movq 0x48(%rsi), %rdx
movl %r14d, %r15d
testq %rdx, %rdx
je 0x1dcb0
movl 0x14(%rdx), %r15d
movslq %r12d, %rdx
addq %rdx, %rbp
shrq $0x20, %r14
shrq $0x20, %r13
subq %r13, %r9
leaq (%r9,%r14), %r13
incq %r13
testb $0x1, %cl
je 0x1dcdb
movl %r8d, %ecx
subl %r15d, %ecx
subl %ebx, %r8d
movl %ecx, %ebx
movl %r8d, %r15d
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %r8
movq %rsi, %r9
jmp 0x1dd06
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x8(%r11), %eax
shrl %eax
movq %rax, %rdx
shlq $0x6, %rdx
movl (%rcx,%rdx), %r15d
xorl %ebx, %ebx
movq 0x28(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movslq (%r12,%rax,4), %rdx
addq %rdx, %rcx
shlq $0x4, %rcx
incl %edx
movl %edx, (%r12,%rax,4)
movq 0x48(%rsp), %rdx
movl %ebx, (%rdx,%rcx)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl 0x4(%rdx,%rcx), %eax
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %eax
orl %r15d, %eax
movl %eax, 0x4(%rdx,%rcx)
movl 0x8(%r11), %eax
shll $0x1f, %eax
orl %r15d, %eax
movl %eax, 0x4(%rdx,%rcx)
orq $-0x1, 0x8(%rdx,%rcx)
incq %r10
jmp 0x1dbe9
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
incq %rdx
jmp 0x1db5e
incq %rsi
jmp 0x1db3b
cvtsi2sd %r13, %xmm0
cvtsi2sd %rbp, %xmm1
divsd %xmm1, %xmm0
movq 0xb8(%rsp), %rax
movsd %xmm0, (%rax)
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
cmpq %rax, %rbx
jae 0x1ddcc
movq 0x28(%rsp), %rax
movl 0x4(%rax,%rbx,4), %edi
movslq (%rax,%rbx,4), %rsi
subl %esi, %edi
cmpl (%r12,%rbx,4), %edi
jne 0x1deb2
shlq $0x4, %rsi
addq 0x48(%rsp), %rsi
callq 0x32e3
incq %rbx
jmp 0x1dd93
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2577
xorl %ebx, %ebx
cmpq %rbx, 0x38(%rsp)
je 0x1de12
movq 0x10(%rsp), %rax
movl 0x4(%rax,%rbx,4), %edi
movslq (%rax,%rbx,4), %rsi
subl %esi, %edi
movq 0x8(%rsp), %rax
cmpl (%rax,%rbx,4), %edi
jne 0x1decb
shlq $0x4, %rsi
addq 0x40(%rsp), %rsi
callq 0x32e3
incq %rbx
jmp 0x1dddb
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2577
movq 0xd0(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, (%rax)
movq 0xd8(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq 0xc0(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0xc8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movl 0x54(%rsp), %ebp
jmp 0x1de84
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2577
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x2577
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7244(%rip), %rdi      # 0x250e0
leaq 0x7271(%rip), %rsi      # 0x25114
leaq 0x7275(%rip), %rcx      # 0x2511f
pushq $0x46
popq %rdx
callq 0x2160
leaq 0x72fe(%rip), %rdi      # 0x251b7
leaq 0x7254(%rip), %rsi      # 0x25114
leaq 0x7258(%rip), %rcx      # 0x2511f
pushq $0x58
jmp 0x1deac
leaq 0x7304(%rip), %rdi      # 0x251d6
leaq 0x723b(%rip), %rsi      # 0x25114
leaq 0x723f(%rip), %rcx      # 0x2511f
pushq $0x5d
jmp 0x1deac