// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movl 0x140(%rsp), %ebp
movl $0x1, %esi
movl $0x30, %edx
movq %rdi, 0x50(%rsp)
callq 0x2f84
movq %rax, %r15
movslq %ebp, %rax
movq %rax, 0x78(%rsp)
movl %ebx, %eax
movq %rax, 0x80(%rsp)
testl %ebx, %ebx
jle 0x19f62
movq 0x120(%rsp), %rax
addq $0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq (%r12,%rcx,8), %rsi
movl $0x0, %edi
testl %esi, %esi
jle 0x19f34
movslq %edx, %rdi
movl %esi, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
leaq (%rdi,%rdi,4), %rdi
leaq (%rax,%rdi,8), %r9
xorl %edi, %edi
movl (%r9), %r10d
addl %r10d, %r10d
sarl %r10d
addl %r10d, %edi
addq $0x28, %r9
decq %r8
jne 0x19f1f
cmpl 0x138(%rsp), %edi
jl 0x19f53
movq %rsi, %r8
shrq $0x20, %r8
cmpq 0x78(%rsp), %r8
jb 0x19f53
incl 0x8(%r15)
addl %edi, 0x10(%r15)
addl %esi, %edx
incq %rcx
cmpq 0x80(%rsp), %rcx
jne 0x19efb
movslq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x1a6f4
movq %r12, 0x68(%rsp)
movq 0x158(%rsp), %r12
movl 0x150(%rsp), %ebp
movq 0x50(%rsp), %r13
movq %r13, (%r15)
movl $0x68, %edx
movq %r13, %rdi
callq 0x2f84
movq %rax, 0x18(%r15)
movslq 0x10(%r15), %rsi
shlq $0x4, %rsi
movq %r13, %rdi
callq 0x30e5
movq %rax, 0x28(%r15)
pxor %xmm0, %xmm0
movdqu %xmm0, 0xa8(%rsp)
movq $0x0, 0xb8(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %r14, 0x98(%rsp)
movl %ebp, 0xa8(%rsp)
movq %r12, 0xa0(%rsp)
testl %ebx, %ebx
jle 0x1a682
movq 0x128(%rsp), %rcx
movzbl 0xc(%rcx), %r14d
movq 0x120(%rsp), %rax
leaq 0x4(%rax), %r10
addq $0x10, %rax
movq %rax, 0xc0(%rsp)
leaq -0x8(%rcx), %rax
movq %rax, 0xd8(%rsp)
leaq 0x8(%rcx), %rax
movq %rax, 0xe0(%rsp)
xorl %r8d, %r8d
pcmpeqd %xmm2, %xmm2
xorl %r9d, %r9d
xorl %r11d, %r11d
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq %r10, 0xc8(%rsp)
movq (%rdi,%r11,8), %r12
movl $0x0, %eax
testl %r12d, %r12d
jle 0x1a09b
movslq 0x20(%rsp), %rax
movl %r12d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rax,%rax,4), %rax
leaq (%r10,%rax,8), %rdx
xorl %eax, %eax
movl (%rdx), %esi
addl %esi, %esi
sarl %esi
addl %esi, %eax
addq $0x28, %rdx
decq %rcx
jne 0x1a08a
cmpl 0x138(%rsp), %eax
jl 0x1a662
movq %r12, %rcx
shrq $0x20, %rcx
cmpq 0x78(%rsp), %rcx
jb 0x1a662
movl %r9d, 0x14(%rsp)
movslq %ebx, %rax
imulq $0x68, %rax, %rdx
movq 0x18(%r15), %rax
movl %ecx, 0x14(%rax,%rdx)
movq %r8, 0x38(%rsp)
movq %rdx, 0x70(%rsp)
movl %r8d, 0x8(%rax,%rdx)
movslq 0x20(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movl 0x130(%rsp), %edi
testl %r12d, %r12d
jle 0x1a1d6
movl %r12d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x30(%rsp), %rdx
leaq (%rdx,%rdx,4), %rdx
movq 0xc0(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movl 0x130(%rsp), %esi
movq (%rdx), %xmm0
movdqa %xmm0, %xmm1
pslld $0xf, %xmm1
pxor %xmm2, %xmm1
paddd %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0xa, %xmm0
pxor %xmm1, %xmm0
movdqa %xmm0, %xmm1
pslld $0x3, %xmm1
paddd %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0x6, %xmm0
pxor %xmm1, %xmm0
movdqa %xmm0, %xmm1
pslld $0xb, %xmm1
pxor %xmm2, %xmm1
paddd %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0x10, %xmm0
pxor %xmm1, %xmm0
movl -0x8(%rdx), %edi
movl %edi, %r8d
shll $0xf, %r8d
notl %r8d
addl %edi, %r8d
movl %r8d, %edi
shrl $0xa, %edi
xorl %r8d, %edi
leal (%rdi,%rdi,8), %edi
movl %edi, %r8d
shrl $0x6, %r8d
xorl %edi, %r8d
movl %r8d, %edi
shll $0xb, %edi
notl %edi
addl %r8d, %edi
movl %edi, %r8d
shrl $0x10, %r8d
xorl %edi, %r8d
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %r9d
movd %xmm0, %edi
addl %esi, %edi
addl %r9d, %edi
addl %r8d, %edi
addq $0x28, %rdx
movl %edi, %esi
decq %rcx
jne 0x1a11c
movl %ebx, 0x4c(%rsp)
movq %r11, 0xd0(%rsp)
movl %edi, %ecx
shll $0xf, %ecx
notl %ecx
addl %edi, %ecx
movl %ecx, %edx
shrl $0xa, %edx
xorl %ecx, %edx
leal (%rdx,%rdx,8), %ecx
movl %ecx, %edx
shrl $0x6, %edx
xorl %ecx, %edx
movl %edx, %ecx
shll $0xb, %ecx
notl %ecx
addl %edx, %ecx
movl %ecx, %edx
shrl $0x10, %edx
xorl %ecx, %edx
movq 0x70(%rsp), %rcx
movl %edx, 0x38(%rax,%rcx)
movq %r12, 0x28(%rsp)
cmpl $0x2, %r12d
jl 0x1a402
movq 0x28(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x40(%rsp)
movq 0x30(%rsp), %rax
leaq (%rax,%rax,4), %rcx
movq 0x120(%rsp), %rax
leaq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %edx
leaq (%rdx,%rdx,4), %rcx
movq 0x18(%rsp), %rax
leaq (%rax,%rcx,8), %rsi
movslq (%rsi), %r8
movq %r8, %r11
shlq $0x4, %r11
movl -0x24(%rsi), %edi
leal (%rdi,%rdi), %r10d
sarl %r10d
movslq -0x28(%rsi), %r9
movl %r10d, %ebx
testl %r10d, %r10d
jle 0x1a2db
movq 0x128(%rsp), %rax
movl (%rax,%r11), %ebp
movl 0x8(%rax,%r11), %r12d
movq %rdi, %rbx
shlq $0x21, %rbx
sarq $0x21, %rbx
movq %r9, %r13
shlq $0x4, %r13
addq 0xd8(%rsp), %r13
movq %rbx, %rcx
shlq $0x4, %rcx
addq %r13, %rcx
cmpl %r12d, (%rcx)
jg 0x1a2c8
movl -0x20(%rsi), %r13d
cmpl 0x8(%rsi), %r13d
jne 0x1a2db
cmpl %ebp, -0x8(%rcx)
jle 0x1a2db
decq %rbx
leaq 0x1(%rbx), %r13
addq $-0x10, %rcx
cmpq $0x1, %r13
jg 0x1a2b4
xorl %ebx, %ebx
movl 0x4(%rsi), %r12d
leal (%r12,%r12), %ecx
sarl %ecx
testl %ecx, %ecx
jle 0x1a709
movslq %r10d, %r13
subl %ebx, %r10d
addq %r9, %r13
shlq $0x4, %r13
movq 0x128(%rsp), %rax
movl -0x10(%rax,%r13), %ebp
movl -0x8(%rax,%r13), %r13d
movl %ecx, %ecx
addq 0xe0(%rsp), %r11
xorl %ebx, %ebx
cmpl %r13d, (%r11)
jl 0x1a32b
movl -0x20(%rsi), %eax
cmpl 0x8(%rsi), %eax
jne 0x1a33c
cmpl %ebp, -0x8(%r11)
jge 0x1a33c
incq %rbx
addq $0x10, %r11
cmpq %rbx, %rcx
jne 0x1a318
jmp 0x1a709
testl %r10d, %r10d
jle 0x1a384
movl %edi, %ecx
subl %r10d, %ecx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %edi       # imm = 0x80000000
orl %eax, %edi
movl %edi, -0x24(%rsi)
addl %ecx, %ecx
je 0x1a384
sarl %ecx
addl %r9d, %ecx
movslq %ecx, %rax
shlq $0x4, %rax
movq 0x128(%rsp), %r9
movl -0x8(%r9,%rax), %ecx
incl %ecx
movl %ecx, -0x10(%rsi)
movl -0x10(%r9,%rax), %eax
incl %eax
movl %eax, -0x18(%rsi)
testq %rbx, %rbx
je 0x1a3da
addl %ebx, %r8d
movl %r8d, (%rsi)
movl %r12d, %eax
subl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %r12d      # imm = 0x80000000
orl %eax, %r12d
movl %r12d, 0x4(%rsi)
movslq %r8d, %rax
shlq $0x4, %rax
movq 0x128(%rsp), %r9
movq 0x8(%r9,%rax), %rcx
movq %rcx, %r8
shrq $0x20, %r8
movzbl %r8b, %r8d
subl %r8d, %ecx
incl %ecx
movl %ecx, 0x14(%rsi)
movl (%r9,%rax), %eax
subl %r8d, %eax
incl %eax
movl %eax, 0xc(%rsi)
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jne 0x1a3f4
movl 0xc(%rsi), %eax
movl 0x14(%rsi), %ecx
movl %ecx, -0x10(%rsi)
movl %ecx, -0x14(%rsi)
movl %eax, -0x18(%rsi)
movl %eax, -0x1c(%rsi)
incq %rdx
cmpq 0x40(%rsp), %rdx
jne 0x1a252
movq 0xb8(%rsp), %r12
movq 0x38(%rsp), %rbx
cmpl 0xb0(%rsp), %ebx
jne 0x1a455
movl %ebx, %eax
sarl %eax
addl %ebx, %eax
cmpl $0x4, %ebx
movl $0x10, %ecx
cmovll %ecx, %eax
movl %eax, 0xb0(%rsp)
movq 0x88(%rsp), %rdi
cltq
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdx
movq %r12, %rsi
callq 0x31ed
movq %rax, %r12
movq %rax, 0xb8(%rsp)
movl 0x14(%rsp), %r8d
leal 0x1(%rbx), %eax
movl %eax, 0x18(%rsp)
movl %eax, 0xac(%rsp)
movslq %ebx, %rax
leaq (%rax,%rax,4), %r13
movq 0x30(%rsp), %rax
leaq (%rax,%rax,4), %rbp
movq 0x120(%rsp), %rcx
movl 0x4(%rcx,%rbp,8), %edx
leal (%rdx,%rdx), %eax
sarl %eax
movslq %r8d, %rdi
movq %rdi, 0x40(%rsp)
shlq $0x4, %rdi
addq 0x28(%r15), %rdi
movl %eax, 0x4(%r12,%r13,4)
movl 0x8(%rcx,%rbp,8), %eax
movl %eax, 0x8(%r12,%r13,4)
movl 0x1c(%rcx,%rbp,8), %eax
movl %eax, 0xc(%r12,%r13,4)
movl $0xffffffff, 0x10(%r12,%r13,4) # imm = 0xFFFFFFFF
movslq (%rcx,%rbp,8), %rsi
shlq $0x4, %rsi
addq 0x128(%rsp), %rsi
shlq $0x21, %rdx
sarq $0x21, %rdx
shlq $0x4, %rdx
movl %r8d, %ebx
callq 0x2230
movl 0x18(%rsp), %edi
movl 0x4(%r12,%r13,4), %eax
movq 0x40(%rsp), %rcx
addl %ecx, %eax
movl %ecx, (%r12,%r13,4)
movq 0x38(%rsp), %rdx
movl %eax, 0xb4(%rsp)
movq 0x28(%rsp), %r12
cmpl $0x2, %r12d
pcmpeqd %xmm2, %xmm2
jl 0x1a628
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, 0x18(%rsp)
movq 0x120(%rsp), %rax
leaq (%rax,%rbp,8), %rbx
xorl %r12d, %r12d
movl $0x1, %ebp
leaq (,%rbp,4), %rax
addq %rbp, %rax
movl 0x4(%rbx,%rax,8), %ecx
addl %ecx, %ecx
testl %ecx, %ecx
jle 0x1a5fe
leaq (%rbx,%rax,8), %r9
movq 0x20(%rsp), %rax
addl %r12d, %eax
cltq
leaq (%rax,%rax,4), %rax
movq 0x120(%rsp), %rcx
leaq (%rcx,%rax,8), %r8
movq 0x128(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x88(%rsp), %rsi
movl %r14d, %edx
movl 0x148(%rsp), %ecx
callq 0x1a766
testl %eax, %eax
sets %al
movslq %r12d, %r13
cmpq %r13, %rbp
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1a5f7
leaq (,%r13,4), %rax
addq %r13, %rax
leaq (%rbx,%rax,8), %r12
incq %r13
movq %r12, %r8
addq $0x28, %r12
movq 0x128(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x88(%rsp), %rsi
movl %r14d, %edx
movl 0x148(%rsp), %ecx
movq %r12, %r9
callq 0x1a766
testl %eax, %eax
js 0x1a728
movl %r13d, %eax
incq %r13
cmpq %rbp, %rax
jne 0x1a5b4
movl %ebp, %r12d
pcmpeqd %xmm2, %xmm2
incq %rbp
cmpq 0x18(%rsp), %rbp
jne 0x1a530
movl 0xac(%rsp), %edi
movl 0xb4(%rsp), %eax
movq 0x38(%rsp), %rdx
movl 0x14(%rsp), %ebx
movq 0x28(%rsp), %r12
movl %edi, %ecx
subl %edx, %ecx
movq 0x18(%r15), %rdx
movq 0x70(%rsp), %rsi
movl %ecx, 0xc(%rdx,%rsi)
movl %eax, %ecx
subl %ebx, %ecx
movl %ecx, 0x10(%rdx,%rsi)
movl 0x4c(%rsp), %ebx
incl %ebx
movl %edi, %r8d
movl %eax, %r9d
movq 0x68(%rsp), %rdi
movq 0xc8(%rsp), %r10
movq 0xd0(%rsp), %r11
movq 0x20(%rsp), %rax
addl %r12d, %eax
movq %rax, 0x20(%rsp)
incq %r11
cmpq 0x80(%rsp), %r11
jne 0x1a064
jmp 0x1a688
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl 0x10(%r15), %r9d
jg 0x1a747
movl %r9d, 0x10(%r15)
movl %r8d, 0xc(%r15)
movslq %r8d, %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rbx
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x30e5
movq %rax, 0x20(%r15)
movq 0xb8(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2230
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x301f
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x19aa0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1c405
movq %r15, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf11e(%rip), %rdi      # 0x2982e
leaq 0xeda5(%rip), %rsi      # 0x294bc
leaq 0xf121(%rip), %rcx      # 0x2983f
movl $0x1aa, %edx            # imm = 0x1AA
callq 0x2160
leaq 0xefdf(%rip), %rdi      # 0x2970e
leaq 0xed86(%rip), %rsi      # 0x294bc
leaq 0xefda(%rip), %rcx      # 0x29717
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x2160
leaq 0xf090(%rip), %rdi      # 0x297de
leaq 0xed67(%rip), %rsi      # 0x294bc
leaq 0xefbb(%rip), %rcx      # 0x29717
movl $0x1fd, %edx            # imm = 0x1FD
callq 0x2160