// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/gchain1.c
// Function: mg_gchain_gen
mg_gchains_t *mg_gchain_gen(void *km_dst, void *km, const gfa_t *g, const gfa_edseq_t *es, int32_t n_u, const uint64_t *u,
							mg_lchain_t *lc, const mg128_t *a, uint32_t hash, int32_t min_gc_cnt, int32_t min_gc_score,
							int32_t gdp_max_ed, int32_t n_seg, const char *qseq)
{
	mg_gchains_t *gc;
	int32_t i, j, k, st, kmer_size;
	bridge_aux_t aux;

	// preallocate gc->gc and gc->a
	KCALLOC(km_dst, gc, 1);
	for (i = 0, st = 0; i < n_u; ++i) {
		int32_t m = 0, nui = (int32_t)u[i];
		for (j = 0; j < nui; ++j) m += lc[st + j].cnt; // m is the number of anchors in this gchain
		if (m >= min_gc_cnt && u[i]>>32 >= min_gc_score)
			gc->n_gc++, gc->n_a += m;
		st += nui;
	}
	if (gc->n_gc == 0) return gc;
	gc->km = km_dst;
	KCALLOC(km_dst, gc->gc, gc->n_gc);
	KMALLOC(km_dst, gc->a, gc->n_a);

	// core loop
	memset(&aux, 0, sizeof(aux));
	aux.km = km, aux.g = g, aux.es = es, aux.n_seg = n_seg, aux.qseq = qseq;
	kmer_size = a[0].y>>32&0xff;
	for (i = k = 0, st = 0, aux.n_a = 0; i < n_u; ++i) {
		int32_t n_a0 = aux.n_a, n_llc0 = aux.n_llc, m = 0, nui = (int32_t)u[i];
		for (j = 0; j < nui; ++j) m += lc[st + j].cnt;
		if (m >= min_gc_cnt && u[i]>>32 >= min_gc_score) {
			uint32_t h = hash;
			int32_t j0;
			gc->gc[k].score = u[i]>>32;
			gc->gc[k].off = n_llc0;
			for (j = 0; j < nui; ++j) {
				const mg_lchain_t *p = &lc[st + j];
				h += kh_hash_uint32(p->qs) + kh_hash_uint32(p->re) + kh_hash_uint32(p->v);
			}
			gc->gc[k].hash = kh_hash_uint32(h);

			for (j = 1; j < nui; ++j)
				resolve_overlap(&lc[st + j - 1], &lc[st + j], a);

			if (aux.n_llc == aux.m_llc) KEXPAND(aux.km, aux.llc, aux.m_llc);
			copy_lchain(&aux.llc[aux.n_llc++], &lc[st], &aux.n_a, gc->a, a, -1); // copy the first lchain
			for (j0 = 0, j = 1; j < nui; ++j) {
				const mg_lchain_t *l0 = &lc[st + j0], *l1 = &lc[st + j];
				if (l1->cnt > 0) {
					int32_t ret, t;
					ret = bridge_lchains(gc, &aux, kmer_size, gdp_max_ed, l0, l1, a);
					if (ret < 0) {
						for (t = j0; t < j; ++t) {
							ret = bridge_lchains(gc, &aux, kmer_size, gdp_max_ed, &lc[st + t], &lc[st + t + 1], a);
							assert(ret >= 0);
						}
					}
					j0 = j;
				}
			}

			gc->gc[k].cnt = aux.n_llc - n_llc0;
			gc->gc[k].n_anchor = aux.n_a - n_a0;
			++k;
		}
		st += nui;
	}
	assert(aux.n_a <= gc->n_a);

	gc->n_a = aux.n_a;
	gc->n_lc = aux.n_llc;
	KMALLOC(km_dst, gc->lc, aux.n_llc);
	memcpy(gc->lc, aux.llc, aux.n_llc * sizeof(mg_llchain_t));
	kfree(km, aux.llc);

	mg_gchain_extra(g, gc);
	mg_gchain_sort_by_score(km, gc);
	return gc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %r14
movl %r8d, %r15d
movq %rcx, %r12
movq %rdx, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x120(%rsp), %rbp
movslq 0x140(%rsp), %rbx
pushq $0x1
popq %rsi
pushq $0x30
popq %rdx
movq %rdi, 0x50(%rsp)
callq 0x24f0
movq %rax, %r13
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x4(%rbp), %rcx
movq %rcx, 0x80(%rsp)
xorl %ecx, %ecx
cmpq %r15, %rax
je 0x16b08
movq (%r14,%rax,8), %rdx
movslq %ecx, %rsi
xorl %edi, %edi
testl %edx, %edx
movl $0x0, %r8d
cmovgl %edx, %r8d
imulq $0x28, %r8, %r8
imulq $0x28, %rsi, %r9
addq 0x80(%rsp), %r9
xorl %esi, %esi
cmpq %rdi, %r8
je 0x16ae4
movl (%r9,%rdi), %r10d
addl %r10d, %r10d
sarl %r10d
addl %r10d, %esi
addq $0x28, %rdi
jmp 0x16acc
cmpl 0x138(%rsp), %esi
jl 0x16b01
movq %rdx, %rdi
shrq $0x20, %rdi
cmpq %rbx, %rdi
jb 0x16b01
incl 0x8(%r13)
addl %esi, 0x10(%r13)
addl %edx, %ecx
incq %rax
jmp 0x16aa0
movslq 0x8(%r13), %rsi
testq %rsi, %rsi
je 0x17124
movq %r14, 0x68(%rsp)
movq 0x50(%rsp), %r14
movq %r14, (%r13)
pushq $0x68
popq %rdx
movq %r14, %rdi
callq 0x24f0
movq %rax, 0x18(%r13)
movslq 0x10(%r13), %rsi
shlq $0x4, %rsi
movq %r14, %rdi
movq 0x68(%rsp), %r14
callq 0x266c
movq %rax, 0x28(%r13)
leaq 0xdc(%rsp), %rcx
andq $0x0, 0x4(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, -0xc(%rcx)
movq 0x60(%rsp), %rax
movq %rax, -0x2c(%rcx)
movq 0x58(%rsp), %rax
movq %rax, -0x24(%rcx)
movq %r12, -0x1c(%rcx)
movl 0x150(%rsp), %eax
movl %eax, -0xc(%rcx)
movq 0x158(%rsp), %rax
movq %rax, -0x14(%rcx)
movq 0x128(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0xa8(%rsp)
movzbl 0xc(%rax), %ecx
movl %ecx, 0x24(%rsp)
movq 0x120(%rsp), %rcx
addq $0x14, %rcx
movq %rcx, 0x90(%rsp)
addq $-0x8, %rax
movq %rax, 0xa0(%rsp)
xorl %edx, %edx
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movq $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movq %r13, 0x48(%rsp)
movq %r15, 0x98(%rsp)
movq %rbx, 0x88(%rsp)
movslq %edx, %r8
cmpq %r15, %rax
je 0x170bb
movq %rdx, 0x10(%rsp)
movq %rax, 0x38(%rsp)
movq (%r14,%rax,8), %rsi
movslq 0x28(%rsp), %rcx
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %edx
movq %rsi, 0x40(%rsp)
cmovgl %esi, %edx
imulq $0x28, %rdx, %r15
imulq $0x28, %rcx, %rdi
movq 0x80(%rsp), %rcx
addq %rdi, %rcx
xorl %edx, %edx
cmpq %rax, %r15
je 0x16c52
movl (%rcx,%rax), %esi
addl %esi, %esi
sarl %esi
addl %esi, %edx
addq $0x28, %rax
jmp 0x16c3e
cmpl 0x138(%rsp), %edx
jge 0x16c6a
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rdx
jmp 0x1709d
movq 0x40(%rsp), %rax
shrq $0x20, %rax
cmpq %rbx, %rax
movq 0x10(%rsp), %rdx
jb 0x17098
movq %r8, 0x70(%rsp)
movq 0x18(%r13), %rsi
movslq 0x20(%rsp), %rcx
imulq $0x68, %rcx, %rcx
movl %eax, 0x14(%rsi,%rcx)
movq %rcx, 0x78(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x8(%rsi,%rcx)
movq 0x90(%rsp), %rax
movq %rdi, 0x18(%rsp)
leaq (%rax,%rdi), %rbp
xorl %r12d, %r12d
movl 0x130(%rsp), %r14d
cmpq %r12, %r15
je 0x16cff
movl (%rbp,%r12), %edi
callq 0x17196
movl %eax, %r13d
movl -0x4(%rbp,%r12), %edi
callq 0x17196
movl %eax, %ebx
movl -0xc(%rbp,%r12), %edi
callq 0x17196
addl %r14d, %r13d
addl %ebx, %eax
addl %r13d, %eax
movq 0x48(%rsp), %r13
addq $0x28, %r12
movl %eax, %r14d
jmp 0x16cc1
movl %r14d, %edi
callq 0x17196
movq 0x78(%rsp), %rcx
movq 0x30(%rsp), %rdx
movl %eax, 0x38(%rdx,%rcx)
movslq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rsi
addq 0x120(%rsp), %rsi
pushq $0x1
popq %rax
movq %rsi, 0x18(%rsp)
cmpq 0x30(%rsp), %rax
jge 0x16f0c
imulq $0x28, %rax, %rdi
leaq (%rsi,%rdi), %rcx
movslq (%rsi,%rdi), %rbx
movq %rbx, %r8
shlq $0x4, %r8
movq 0x128(%rsp), %rdx
movl (%rdx,%r8), %r10d
movl 0x8(%rdx,%r8), %r11d
movl -0x24(%rsi,%rdi), %edx
leal (%rdx,%rdx), %r9d
movslq -0x28(%rsi,%rdi), %rdi
movq %rdx, %r14
shlq $0x21, %r14
sarq $0x21, %r14
movq %rdi, %r15
shlq $0x4, %r15
addq 0xa0(%rsp), %r15
movq %r14, %r12
shlq $0x4, %r12
addq %r15, %r12
movl %r9d, %r15d
sarl %r15d
sarl $0x1f, %r9d
andl %r15d, %r9d
testq %r14, %r14
jle 0x16dc7
cmpl %r11d, (%r12)
jg 0x16dbb
movl -0x20(%rcx), %ebp
cmpl 0x8(%rcx), %ebp
jne 0x16dc4
cmpl %r10d, -0x8(%r12)
jle 0x16dc4
addq $-0x10, %r12
decq %r14
jmp 0x16da1
movl %r14d, %r9d
movl %r15d, %r11d
subl %r9d, %r11d
addl %edi, %r15d
movslq %r15d, %r9
shlq $0x4, %r9
movq 0x128(%rsp), %r10
movl -0x10(%r10,%r9), %r14d
movl -0x8(%r10,%r9), %r15d
movl 0x4(%rcx), %r9d
leal (%r9,%r9), %r13d
sarl %r13d
testl %r13d, %r13d
movl $0x0, %r10d
cmovlel %r10d, %r13d
addq 0xa8(%rsp), %r8
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x17139
cmpl %r15d, (%r8)
jl 0x16e28
movl -0x20(%rcx), %ebp
cmpl 0x8(%rcx), %ebp
jne 0x16e31
cmpl %r14d, -0x8(%r8)
jge 0x16e31
incq %r10
addq $0x10, %r8
jmp 0x16e0c
testl %r11d, %r11d
jle 0x16e89
movl %edx, %r8d
subl %r11d, %r8d
movl %r8d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %r11d, %edx
movl %edx, -0x24(%rcx)
addl %r8d, %r8d
movq 0x48(%rsp), %r13
je 0x16e8e
sarl %r8d
addl %edi, %r8d
movslq %r8d, %rdi
shlq $0x4, %rdi
movq 0x128(%rsp), %r11
movl -0x8(%r11,%rdi), %r8d
incl %r8d
movl %r8d, -0x10(%rcx)
movl -0x10(%r11,%rdi), %edi
incl %edi
movl %edi, -0x18(%rcx)
jmp 0x16e8e
movq 0x48(%rsp), %r13
testq %r10, %r10
je 0x16ee5
addl %r10d, %ebx
movl %ebx, (%rcx)
movl %r9d, %edi
subl %r10d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl $0x80000000, %r9d       # imm = 0x80000000
orl %edi, %r9d
movl %r9d, 0x4(%rcx)
movslq %ebx, %rsi
shlq $0x4, %rsi
movq 0x128(%rsp), %r9
movq 0x8(%r9,%rsi), %rdi
movq %rdi, %r8
shrq $0x20, %r8
movzbl %r8b, %r8d
subl %r8d, %edi
incl %edi
movl %edi, 0x14(%rcx)
movl (%r9,%rsi), %esi
subl %r8d, %esi
incl %esi
movl %esi, 0xc(%rcx)
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jne 0x16eff
movl 0xc(%rcx), %edx
movl 0x14(%rcx), %esi
movl %esi, -0x10(%rcx)
movl %esi, -0x14(%rcx)
movl %edx, -0x18(%rcx)
movl %edx, -0x1c(%rcx)
incq %rax
movq 0x18(%rsp), %rsi
jmp 0x16d34
movq 0xe0(%rsp), %rax
movq 0x10(%rsp), %rdx
cmpl 0xd8(%rsp), %edx
jne 0x16f61
movl %edx, %ecx
sarl %ecx
addl %edx, %ecx
cmpl $0x4, %edx
pushq $0x10
popq %rdx
cmovll %edx, %ecx
movl %ecx, 0xd8(%rsp)
movq 0xb0(%rsp), %rdi
movslq %ecx, %rcx
imulq $0x14, %rcx, %rdx
movq %rax, %rsi
callq 0x2774
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rax, 0xe0(%rsp)
movq 0x70(%rsp), %rdi
leal 0x1(%rdx), %ecx
movl %ecx, 0xd4(%rsp)
imulq $0x14, %rdi, %rdi
addq %rax, %rdi
movq 0x28(%r13), %rcx
leaq 0xdc(%rsp), %rdx
movq 0x128(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x171c1
xorl %r15d, %r15d
pushq $0x1
popq %r14
cmpq 0x30(%rsp), %r14
jge 0x17056
imulq $0x28, %r14, %r9
movq 0x18(%rsp), %rbx
movl 0x4(%rbx,%r9), %eax
addl %eax, %eax
testl %eax, %eax
jle 0x17001
addq %rbx, %r9
movq 0x28(%rsp), %rax
addl %r15d, %eax
cltq
imulq $0x28, %rax, %r8
addq 0x120(%rsp), %r8
movq 0x128(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xb0(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x148(%rsp), %ecx
callq 0x1721e
testl %eax, %eax
js 0x17006
movl %r14d, %r15d
incq %r14
jmp 0x16f9b
movslq %r15d, %r15
imulq $0x28, %r15, %r8
addq %rbx, %r8
decq %r15
incq %r15
cmpq %r14, %r15
jge 0x16ffe
leaq 0x28(%r8), %r12
leaq 0x28(%r8), %r9
movq 0x128(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xb0(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x148(%rsp), %ecx
callq 0x1721e
movq %r12, %r8
testl %eax, %eax
jns 0x17013
jmp 0x17158
movl 0xd4(%rsp), %eax
movl %eax, %ecx
subl 0x10(%rsp), %ecx
movq 0x18(%r13), %rdx
movq 0x78(%rsp), %rdi
movl %ecx, 0xc(%rdx,%rdi)
movl 0xdc(%rsp), %ecx
movl %ecx, %esi
subl 0xc(%rsp), %esi
movl %esi, 0x10(%rdx,%rdi)
incl 0x20(%rsp)
movl %eax, %edx
movl %ecx, 0xc(%rsp)
movq 0x68(%rsp), %r14
movq 0x88(%rsp), %rbx
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
addl 0x40(%rsp), %ecx
movq %rcx, 0x28(%rsp)
incq %rax
movq 0x98(%rsp), %r15
jmp 0x16bf9
movl 0xc(%rsp), %eax
cmpl 0x10(%r13), %eax
jg 0x17177
movl %eax, 0x10(%r13)
movl %edx, 0xc(%r13)
imulq $0x14, %r8, %rbx
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x266c
movq %rax, 0x20(%r13)
movq 0xe0(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2230
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2577
movq 0x58(%rsp), %rdi
movq %r13, %rsi
callq 0x16683
movq %rbx, %rdi
movq %r13, %rsi
callq 0x18c6f
movq %r13, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd702(%rip), %rdi      # 0x24842
leaq 0xd389(%rip), %rsi      # 0x244d0
leaq 0xd705(%rip), %rcx      # 0x24853
movl $0x1aa, %edx            # imm = 0x1AA
callq 0x2160
leaq 0xd5c3(%rip), %rdi      # 0x24722
leaq 0xd36a(%rip), %rsi      # 0x244d0
leaq 0xd5be(%rip), %rcx      # 0x2472b
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x2160
leaq 0xd674(%rip), %rdi      # 0x247f2
leaq 0xd34b(%rip), %rsi      # 0x244d0
leaq 0xd59f(%rip), %rcx      # 0x2472b
movl $0x1fd, %edx            # imm = 0x1FD
callq 0x2160