// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: dump_gaf(mg_gres_a*, mg_gchains_t const*, unsigned int)
void dump_gaf(mg_gres_a *hits, const mg_gchains_t *gs, uint32_t only_p)
{
	if (gs == NULL || gs->n_gc == 0 || gs->n_lc == 0) return;
	uint64_t i, j;
	int64_t q_span;
	mg_gres_t *p = NULL;
	kv_pushp(mg_gres_t, *hits, &p); memset(p, 0, sizeof(*p));
	p->n_gc = 0; p->n_lc = 0; p->qid = gs->qid; p->qlen = gs->qlen;
	// p->n_gc = gs->n_gc; p->n_lc = gs->n_lc; p->qid = gs->qid; p->qlen = gs->qlen;
	// MALLOC(p->gc, p->n_gc); memcpy(p->gc, gs->gc, p->n_gc);
	for (i = 0; i < (uint64_t)gs->n_gc; ++i) {
		const mg_gchain_t *t = &gs->gc[i];///one of the gchain
		if(only_p && t->id != t->parent) continue;
		if (t->cnt == 0) continue;
		p->n_gc++; p->n_lc += t->cnt;
	}
	if (p->n_gc == 0) {
		hits->n--;
		return;
	}
	MALLOC(p->gc, p->n_gc); MALLOC(p->lc, p->n_lc); 
	p->n_gc = p->n_lc = 0;
	for (i = 0; i < (uint64_t)gs->n_gc; ++i) {
		const mg_gchain_t *t = &gs->gc[i];///one of the gchain
		if(only_p && t->id != t->parent) continue;
		if (t->cnt == 0) continue;
		p->gc[p->n_gc] = *t; p->gc[p->n_gc].off = p->n_lc;
		for (j = 0; j < (uint64_t)t->cnt; ++j) {
			const mg_llchain_t *q = &gs->lc[t->off + j];
			p->lc[p->n_lc+j].cnt = q->cnt;
			p->lc[p->n_lc+j].score = q->score;
			p->lc[p->n_lc+j].v = q->v;
			if(q->cnt) {
				q_span = (int32_t)(gs->a[q->off].y>>32&0xff);
				p->lc[p->n_lc+j].qs = (int32_t)gs->a[q->off].y + 1 - q_span;///calculated by the first lchain
				p->lc[p->n_lc+j].ts = (int32_t)gs->a[q->off].x + 1 - q_span;///calculated by the first lchain
				p->lc[p->n_lc+j].qe = (int32_t)gs->a[q->off + q->cnt - 1].y + 1;
				p->lc[p->n_lc+j].te = (int32_t)gs->a[q->off + q->cnt - 1].x + 1;
			} else {
				p->lc[p->n_lc+j].qs = p->lc[p->n_lc+j].qe = p->lc[p->n_lc+j].ts = p->lc[p->n_lc+j].te = (uint32_t)-1;
			}
			
			// mg_sprintf_lite(s, "%c%s", "><"[q->v&1], g->seg[q->v>>1].name);
		}
		p->n_gc++; p->n_lc += t->cnt;
	}
}

// Assembly:
testq %rsi, %rsi
je 0x1cd49f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
cmpl $0x0, 0x8(%rsi)
je 0x1cd491
cmpl $0x0, 0xc(%r14)
je 0x1cd491
movl %edx, %ebx
movq %rdi, %r15
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x1cd286
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
jmp 0x1cd28a
movq 0x10(%r15), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
leaq (%rcx,%rcx,4), %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%rcx,8)
movups %xmm0, (%rax,%rcx,8)
movq $0x0, 0x20(%rax,%rcx,8)
movups 0x30(%r14), %xmm0
movups %xmm0, 0x18(%rax,%rcx,8)
movslq 0x8(%r14), %rbp
testq %rbp, %rbp
je 0x1cd48e
leaq (%rax,%rcx,8), %r13
movq 0x18(%r14), %rdx
xorl %ecx, %ecx
movq %rbp, %rsi
xorl %eax, %eax
testl %ebx, %ebx
je 0x1cd2db
movl (%rdx), %edi
cmpl 0x4(%rdx), %edi
jne 0x1cd2ee
movl 0xc(%rdx), %edi
testl %edi, %edi
je 0x1cd2ee
incl %eax
movl %eax, (%r13)
addl %edi, %ecx
movl %ecx, 0x4(%r13)
addq $0x48, %rdx
decq %rsi
jne 0x1cd2d0
testl %eax, %eax
je 0x1cd48e
movslq %ecx, %rcx
leaq (%rcx,%rcx,8), %rdx
movl %ebx, 0xc(%rsp)
leaq (%rdx,%rdx,2), %r12
addq %rcx, %r12
cltq
shlq $0x3, %rax
leaq (%rax,%rax,8), %rdi
callq 0x2280
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%r13)
movq %r12, %rdi
movl 0xc(%rsp), %ebx
callq 0x2280
movq %rax, 0x10(%r13)
movq $0x0, (%r13)
testl %ebp, %ebp
je 0x1cd491
xorl %ecx, %ecx
xorl %edx, %edx
leaq (%rdx,%rdx,8), %rax
shlq $0x3, %rax
addq 0x18(%r14), %rax
testl %ebx, %ebx
je 0x1cd368
movl (%rax), %esi
cmpl 0x4(%rax), %esi
jne 0x1cd47d
cmpl $0x0, 0xc(%rax)
je 0x1cd47d
movslq %ecx, %rcx
leaq (%rcx,%rcx,8), %rcx
movq 0x10(%rsp), %rsi
leaq (%rsi,%rcx,8), %rdi
movl $0x12, %ecx
movq %rax, %rsi
rep  movsl (%rsi), %es:(%rdi)
movslq 0x4(%r13), %rsi
movq 0x8(%r13), %r8
movslq (%r13), %rcx
leaq (%rcx,%rcx,8), %rdi
movq %r8, 0x10(%rsp)
movl %esi, 0x8(%r8,%rdi,8)
movslq 0xc(%rax), %rdi
testq %rdi, %rdi
je 0x1cd469
movslq 0x8(%rax), %rax
shlq $0x4, %rax
addq 0x20(%r14), %rax
movq 0x10(%r13), %r8
leaq (%rsi,%rsi,8), %r9
leaq (%r9,%r9,2), %r9
addq %rsi, %r9
addq %r9, %r8
addq $0x18, %r8
movq %rdi, %r9
movl 0xc(%rax), %r10d
movl %r10d, -0x10(%r8)
movq 0x4(%rax), %r11
movq %r11, -0x18(%r8)
testl %r11d, %r11d
je 0x1cd432
movq 0x28(%r14), %r10
movslq (%rax), %r12
movslq %r11d, %r15
addq %r12, %r15
shlq $0x4, %r12
movq 0x8(%r10,%r12), %r11
movq %r11, %rbp
shrq $0x20, %rbp
movzbl %bpl, %ebx
subl %ebx, %r11d
incl %r11d
movl (%r10,%r12), %ebp
subl %ebx, %ebp
incl %ebp
shlq $0x4, %r15
movl -0x10(%r10,%r15), %r12d
movl -0x8(%r10,%r15), %r10d
incl %r10d
incl %r12d
jmp 0x1cd449
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %r12d, (%r8)
movl %ebp, -0x4(%r8)
movl %r10d, -0x8(%r8)
movl %r11d, -0xc(%r8)
addq $0x10, %rax
addq $0x1c, %r8
decq %r9
jne 0x1cd3d8
incl %ecx
movl %ecx, (%r13)
addl %esi, %edi
movl %edi, 0x4(%r13)
movl 0x8(%r14), %ebp
movl 0xc(%rsp), %ebx
incq %rdx
movslq %ebp, %rax
cmpq %rax, %rdx
jb 0x1cd34d
jmp 0x1cd491
decq (%r15)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq