// File: chhylp123[P]hifiasm/inter.cpp
// Function: mg_gchain_extra(asg_t const*, mg_gchains_t*)
void mg_gchain_extra(const asg_t *g, mg_gchains_t *gs)
{
	int32_t i, j, k;
	for (i = 0; i < gs->n_gc; ++i) { // iterate over gchains
		mg_gchain_t *p = &gs->gc[i];
		const mg_llchain_t *q;
		const mg128_t *last_a;
		int32_t q_span, rest_pl, tmp, n_mini;

		p->qs = p->qe = p->ps = p->pe = -1, p->plen = p->blen = p->mlen = 0, p->div = -1.0f;
		if (p->cnt == 0) continue;
		///some linear chains in middle might be [].cnt == 0
		///but for the first and the last linear chains, [].cnt > 0
		assert(gs->lc[p->off].cnt > 0 && gs->lc[p->off + p->cnt - 1].cnt > 0); // first and last lchains can't be empty
		q = &gs->lc[p->off];
		q_span = (int32_t)(gs->a[q->off].y>>32&0xff);
		/**
		 * a[].x: idx_in_minimizer_arr(32)r_pos(32)
 		 * a[].y: weight(8)query_id(8)flag(8)span(8)q_pos(32)
		 * **/
		p->qs = (int32_t)gs->a[q->off].y + 1 - q_span;///calculated by the first lchain
		p->ps = (int32_t)gs->a[q->off].x + 1 - q_span;///calculated by the first lchain
		tmp = (int32_t)(gs->a[q->off].x>>32);
		assert(p->qs >= 0 && p->ps >= 0);
		q = &gs->lc[p->off + p->cnt - 1];///last lchain
		p->qe = (int32_t)gs->a[q->off + q->cnt - 1].y + 1;
		p->pe = g->seq[q->v>>1].len - (int32_t)gs->a[q->off + q->cnt - 1].x - 1; // this is temporary
		n_mini = (int32_t)(gs->a[q->off + q->cnt - 1].x>>32) - tmp + 1;
		assert(p->n_anchor > 0);

		rest_pl = 0; // this value is never used if the first lchain is not empty (which should always be true)
		last_a = &gs->a[gs->lc[p->off].off];///first minizers in the first linear chain
		for (j = 0; j < p->cnt; ++j) { // iterate over lchains
			const mg_llchain_t *q = &gs->lc[p->off + j];
			int32_t vlen = g->seq[q->v>>1].len;///node length in graph
			p->plen += vlen;
			for (k = 0; k < q->cnt; ++k) { // iterate over anchors
				const mg128_t *r = &gs->a[q->off + k];
				int32_t pl, ql = (int32_t)r->y - (int32_t)last_a->y;
				int32_t span = (int32_t)(r->y>>32&0xff);
				if (j == 0 && k == 0) { // the first anchor on the first lchain
					pl = ql = span;
				} else if (j > 0 && k == 0) { // the first anchor but not on the first lchain
					pl = (int32_t)r->x + 1 + rest_pl;
				} else {
					pl = (int32_t)r->x - (int32_t)last_a->x;
				}
				if (ql < 0) ql = -ql, n_mini += (int32_t)(last_a->x>>32) - (int32_t)(r->x>>32); // dealing with overlapping query at junctions
				p->blen += pl > ql? pl : ql;
				p->mlen += pl > span && ql > span? span : pl < ql? pl : ql;
				last_a = r;
			}
			if (q->cnt == 0) rest_pl += vlen;
			else rest_pl = vlen - (int32_t)gs->a[q->off + q->cnt - 1].x - 1;
		}
		p->pe = p->plen - p->pe;
		assert(p->pe >= p->ps);
		// here n_mini >= p->n_anchor should stand almost all the time
		p->div = n_mini >= p->n_anchor? log((double)n_mini / p->n_anchor) / q_span : log((double)p->n_anchor / n_mini) / q_span;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x28a0d6
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x24(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x1c(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x2c(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x28(%rbp), %rax
movss 0xf5526(%rip), %xmm0    # 0x37f070
movss %xmm0, 0x34(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x289b5e
jmp 0x28a0c8
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movslq 0x8(%rax), %rax
shlq $0x4, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpl $0x0, 0x4(%rcx)
movb %al, -0x6d(%rbp)
jle 0x289bad
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x28(%rbp), %rdx
addl 0xc(%rdx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
setg %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x289bb6
jmp 0x289bb8
jmp 0x289bd7
leaq 0x106f64(%rip), %rdi    # 0x390b23
leaq 0x106c69(%rip), %rsi    # 0x39082f
movl $0x746, %edx            # imm = 0x746
leaq 0x106f8f(%rip), %rcx    # 0x390b61
callq 0x2130
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x8(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xff, %rax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
addl $0x1, %ecx
subl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %ecx
addl $0x1, %ecx
subl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x18(%rcx)
movb %al, -0x6e(%rbp)
jl 0x289ca7
movq -0x28(%rbp), %rax
cmpl $0x0, 0x24(%rax)
setge %al
movb %al, -0x6e(%rbp)
movb -0x6e(%rbp), %al
testb $0x1, %al
jne 0x289cb0
jmp 0x289cb2
jmp 0x289cd1
leaq 0x106edc(%rip), %rdi    # 0x390b95
leaq 0x106b6f(%rip), %rsi    # 0x39082f
movl $0x750, %edx            # imm = 0x750
leaq 0x106e95(%rip), %rcx    # 0x390b61
callq 0x2130
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x28(%rbp), %rdx
addl 0xc(%rdx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movl (%rcx), %ecx
movq -0x30(%rbp), %rdx
addl 0x4(%rdx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rdx
movl (%rdx), %edx
movq -0x30(%rbp), %rsi
addl 0x4(%rsi), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rax), %rax
subl %eax, %ecx
subl $0x1, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movl (%rcx), %ecx
movq -0x30(%rbp), %rdx
addl 0x4(%rdx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
subl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x289db5
jmp 0x289dd4
leaq 0x106df2(%rip), %rdi    # 0x390bae
leaq 0x106a6c(%rip), %rsi    # 0x39082f
movl $0x755, %edx            # imm = 0x755
leaq 0x106d92(%rip), %rcx    # 0x390b61
callq 0x2130
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x28(%rbp), %rdx
movslq 0x8(%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x28a00f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %ecx
addl -0x18(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
movq -0x28(%rbp), %rax
addl 0x20(%rax), %ecx
movl %ecx, 0x20(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x289fba
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rcx
subl %ecx, %eax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xff, %rax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x289ee1
cmpl $0x0, -0x1c(%rbp)
jne 0x289ee1
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x289f14
cmpl $0x0, -0x18(%rbp)
jle 0x289eff
cmpl $0x0, -0x1c(%rbp)
jne 0x289eff
movq -0x60(%rbp), %rax
movq (%rax), %rax
addl $0x1, %eax
addl -0x40(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x289f12
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, -0x64(%rbp)
jmp 0x289f14
cmpl $0x0, -0x68(%rbp)
jge 0x289f40
xorl %eax, %eax
subl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
subl %ecx, %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x68(%rbp), %eax
jle 0x289f50
movl -0x64(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x289f56
movl -0x68(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %ecx
movq -0x28(%rbp), %rax
addl 0x2c(%rax), %ecx
movl %ecx, 0x2c(%rax)
movl -0x64(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jle 0x289f7b
movl -0x68(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jle 0x289f7b
movl -0x6c(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x289f97
movl -0x64(%rbp), %eax
cmpl -0x68(%rbp), %eax
jge 0x289f8b
movl -0x64(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x289f91
movl -0x68(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %ecx
movq -0x28(%rbp), %rax
addl 0x30(%rax), %ecx
movl %ecx, 0x30(%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x289e71
movq -0x50(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x289fcf
movl -0x54(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x289fff
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x50(%rbp), %rdx
movl (%rdx), %edx
movq -0x50(%rbp), %rsi
addl 0x4(%rsi), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
subl %ecx, %eax
subl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x28a001
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x289e0f
movq -0x28(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x28(%rbp), %rax
subl 0x28(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jl 0x28a036
jmp 0x28a055
leaq 0x106b81(%rip), %rdi    # 0x390bbe
leaq 0x1067eb(%rip), %rsi    # 0x39082f
movl $0x771, %edx            # imm = 0x771
leaq 0x106b11(%rip), %rcx    # 0x390b61
callq 0x2130
movl -0x48(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jl 0x28a08b
cvtsi2sdl -0x48(%rbp), %xmm0
movq -0x28(%rbp), %rax
cvtsi2sdl 0x10(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x2170
cvtsi2sdl -0x3c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0x28a0b3
movq -0x28(%rbp), %rax
cvtsi2sdl 0x10(%rax), %xmm0
cvtsi2sdl -0x48(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x2170
cvtsi2sdl -0x3c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0x34(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x289aca
addq $0x90, %rsp
popq %rbp
retq
nop