// File: /workspace/github_c_makefile_star2_O3/lh3[P]minigraph/galign.c
// Function: mg_gchain_gen_ds
void mg_gchain_gen_ds(void *km, const gfa_t *g, const gfa_edseq_t *es, const char *qseq, mg_gchains_t *gt)
{
	int32_t i, m_off = 0, n_off = 0, *off = 0;
	void *km2;
	kstring_t str = {0,0,0}, seq = {0,0,0};
	km2 = km_init2(km, 0);
	for (i = 0; i < gt->n_gc; ++i) {
		mg_gchain_t *gc = &gt->gc[i];
		int32_t j;
		int64_t x, y, ds_len;
		str.l = seq.l = n_off = 0;
		if (gc->p->aplen > seq.m) {
			seq.s = Krealloc(km2, char, seq.s, gc->p->aplen);
			seq.m = gc->p->aplen;
		}
		for (j = 0, seq.l = 0; j < gc->cnt; ++j) { // extract the aligned sequence in the graph
			int32_t k = gc->off + j;
			uint32_t v = gt->lc[k].v;
			int32_t slen = es[v].len;
			int32_t st = j > 0? 0 : gc->p->ss;
			int32_t en = j < gc->cnt - 1? slen : gc->p->ee;
			assert(seq.l + (en - st) <= gc->p->aplen);
			memcpy(&seq.s[seq.l], &es[v].seq[st], en - st);
			seq.l += en - st;
		}
		assert(seq.l == gc->p->aplen);
		for (j = 0, x = 0, y = gc->qs, ds_len = 0, n_off = 0; j < gc->p->n_cigar; ++j) { // estimate the approximate length of ds
			int64_t op = gc->p->cigar[j]&0xf, len = gc->p->cigar[j]>>4, z;
			if (op == 0 || op == 7 || op == 8) { // alignment match
				int32_t l = 0;
				++n_off;
				for (z = 0; z < len; ++z) {
					if (mg_get_nucl(seq.s, x+z) != mg_get_nucl(qseq, y+z))
						ds_len += 3, ds_len += 6, n_off += 2, l = 0;
					else ++l;
				}
				ds_len += 6;
				x += len, y += len;
			} else if (op == 1) { // insertion
				ds_len += len + 1, ++n_off, y += len;
			} else if (op == 2) { // deletion
				ds_len += len + 1, ++n_off, x += len;
			}
		}
		if (n_off > m_off) {
			m_off = n_off + (n_off>>1) + 16;
			off = Krealloc(km2, int32_t, off, m_off);
		}
		mg_str_reserve(km2, &str, ds_len);
		for (j = 0, x = 0, y = gc->qs, n_off = 0; j < gc->p->n_cigar; ++j) { // write ds
			int64_t op = gc->p->cigar[j]&0xf, len = gc->p->cigar[j]>>4;
			assert(n_off < m_off);
			if (op == 0 || op == 7 || op == 8) { // alignment match
				int64_t z;
				int32_t l = 0;
				for (z = 0; z < len; ++z) {
					uint8_t cx = mg_get_nucl(seq.s, x+z);
					uint8_t cy = mg_get_nucl(qseq, y+z);
					if (cx != cy) {
						if (l > 0) {
							off[n_off++] = str.l;
							mg_sprintf_km(km2, &str, ":%d", l);
						}
						off[n_off++] = str.l;
						mg_sprintf_km(km2, &str, "*%c%c", "acgtn"[cx], "acgtn"[cy]);
						l = 0;
					} else ++l;
				}
				if (l > 0) {
					off[n_off++] = str.l;
					mg_sprintf_km(km2, &str, ":%d", l);
				}
				x += len, y += len;
			} else if (op == 1) { // insertion
				int64_t z, ll, lr;
				for (z = 1; z <= len; ++z)
					if (y - z < gc->qs || qseq[y + len - z] != qseq[y - z])
						break;
				lr = z - 1;
				for (z = 0; z < len; ++z)
					if (y + len + z >= gc->qe || qseq[y + len + z] != qseq[y + z])
						break;
				ll = z;
				off[n_off++] = str.l;
				mg_sprintf_km(km2, &str, "+");
				write_indel(km2, &str, len, &qseq[y], ll, lr);
				y += len;
			} else if (op == 2) { // deletion
				int64_t z, ll, lr;
				for (z = 1; z <= len; ++z)
					if (x - z < 0 || seq.s[x + len - z] != seq.s[x - z])
						break;
				lr = z - 1;
				for (z = 0; z < len; ++z)
					if (x + len + z >= gc->p->aplen || seq.s[x + z] != seq.s[x + len + z])
						break;
				ll = z;
				off[n_off++] = str.l;
				mg_sprintf_km(km2, &str, "-");
				write_indel(km2, &str, len, &seq.s[x], ll, lr);
				x += len;
			}
		}
		gc->ds.len = str.l;
		gc->ds.ds = Kcalloc(gt->km, char, str.l + 1);
		memcpy(gc->ds.ds, str.s, str.l);
		gc->ds.n_off = n_off;
		gc->ds.off = Kcalloc(gt->km, int32_t, n_off);
		memcpy(gc->ds.off, off, n_off * sizeof(int32_t));
	}
	km_destroy(km2); // this frees both str.s and seq.s
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %r15
movq %rcx, %r14
movq %rdx, 0x78(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %esi, %esi
callq 0x2f3c
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x1bf14
xorl %esi, %esi
movq $0x0, 0x68(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x0, 0x54(%rsp)
movq $0x0, 0x10(%rsp)
movq %r15, 0x48(%rsp)
movq %r14, 0x40(%rsp)
movq 0x18(%r15), %rax
imulq $0x68, %rsi, %rcx
leaq (%rax,%rcx), %r12
movl $0x0, 0x20(%rsp)
movq 0x48(%rax,%rcx), %r13
movslq 0xc(%r13), %rdx
cmpl 0x54(%rsp), %edx
movq %rsi, 0x80(%rsp)
jbe 0x1b84b
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x31ed
movq %rax, 0x10(%rsp)
movq 0x48(%r12), %r13
movl 0xc(%r13), %eax
movl %eax, 0x54(%rsp)
movl 0xc(%r12), %ecx
testl %ecx, %ecx
jle 0x1b8d8
xorl %ebx, %ebx
xorl %eax, %eax
movq 0x48(%r12), %rsi
movl $0x0, %edx
testl %ebx, %ebx
jne 0x1b86d
movl 0x10(%rsi), %edx
movslq 0x8(%r12), %rdi
movq 0x20(%r15), %r8
addq %rbx, %rdi
leaq (%rdi,%rdi,4), %rdi
movl 0x8(%r8,%rdi,4), %r8d
shlq $0x4, %r8
decl %ecx
cmpl %ecx, %ebx
jge 0x1b898
movq 0x78(%rsp), %rcx
movl 0x8(%rcx,%r8), %ecx
jmp 0x1b89b
movl 0x14(%rsi), %ecx
subl %edx, %ecx
leal (%rcx,%rax), %ebp
cmpl 0xc(%rsi), %ebp
ja 0x1bf30
addq 0x78(%rsp), %r8
movl %eax, %edi
addq 0x10(%rsp), %rdi
movslq %edx, %rsi
addq (%r8), %rsi
movslq %ecx, %rdx
callq 0x2230
movl 0xc(%r12), %ecx
incq %rbx
movl %ebp, %eax
cmpl %ecx, %ebx
jl 0x1b85c
movq 0x48(%r12), %r13
jmp 0x1b8da
xorl %ebp, %ebp
cmpl 0xc(%r13), %ebp
jne 0x1bf6e
movslq (%r13), %rdx
testq %rdx, %rdx
movq %r12, 0x60(%rsp)
jle 0x1b9ec
movslq 0x18(%r12), %rcx
movq %rcx, (%rsp)
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
xorl %ebx, %ebx
movq 0x18(%r13,%rsi,8), %r11
movl %r11d, %r10d
andl $0xf, %r10d
movq %r11, %r9
shrq $0x4, %r9
cmpq $0x7, %r10
setne %bpl
testb $0x7, %r11b
setne %r14b
testb %bpl, %r14b
jne 0x1b941
movq %r13, %rcx
incl %r8d
cmpq $0x10, %r11
jae 0x1b964
movl %r8d, %ebp
jmp 0x1b9b0
cmpl $0x2, %r10d
je 0x1b9cd
cmpl $0x1, %r10d
jne 0x1b9d9
addq %r9, %rbx
incq %rbx
incl %r8d
addq %r9, (%rsp)
jmp 0x1b9d9
movq 0x40(%rsp), %r10
movq (%rsp), %rax
addq %rax, %r10
movq 0x10(%rsp), %rax
leaq (%rax,%rdi), %r11
xorl %r14d, %r14d
leaq 0x1697d(%rip), %rax     # 0x32300
movzbl (%r11,%r14), %r15d
movb (%rax,%r15), %r15b
movzbl (%r10,%r14), %r12d
leaq 0x9(%rbx), %r13
leal 0x2(%r8), %ebp
cmpb (%rax,%r12), %r15b
cmovneq %r13, %rbx
cmovel %r8d, %ebp
incq %r14
movl %ebp, %r8d
cmpq %r14, %r9
jne 0x1b983
addq $0x6, %rbx
addq %r9, %rdi
addq %r9, (%rsp)
movl %ebp, %r8d
movq 0x48(%rsp), %r15
movq 0x60(%rsp), %r12
movq %rcx, %r13
jmp 0x1b9d9
addq %r9, %rbx
incq %rbx
incl %r8d
addq %r9, %rdi
incq %rsi
cmpq %rdx, %rsi
jne 0x1b908
movq 0x40(%rsp), %r14
jmp 0x1b9f1
xorl %ebx, %ebx
xorl %r8d, %r8d
cmpl 0x68(%rsp), %r8d
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
jle 0x1ba29
movl %r8d, %eax
shrl %eax
addl %r8d, %eax
addl $0x10, %eax
movq %rax, 0x68(%rsp)
leaq (,%rax,4), %rdx
callq 0x31ed
movq 0x18(%rsp), %rdi
movq %rax, 0x30(%rsp)
leaq 0x20(%rsp), %rsi
movl %ebx, %edx
callq 0x1f8de
movq 0x48(%r12), %rcx
cmpl $0x0, (%rcx)
jle 0x1be97
movslq 0x18(%r12), %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
xorl %r11d, %r11d
xorl %ebp, %ebp
cmpl 0x68(%rsp), %r11d
jge 0x1bf4f
movq 0x18(%rcx,%rbx,8), %rax
movl %eax, %edx
andl $0xf, %edx
movq %rax, %r8
shrq $0x4, %r8
cmpl $0x7, %edx
setne %sil
testb $0x7, %al
setne %dil
testb %sil, %dil
movq %r8, (%rsp)
jne 0x1bc0c
cmpq $0x10, %rax
jb 0x1bbf9
movq %rbx, 0x38(%rsp)
movq 0x8(%rsp), %rax
leaq (%r14,%rax), %rdx
movq 0x10(%rsp), %rax
movq %rbp, 0x58(%rsp)
leaq (%rax,%rbp), %rsi
xorl %r13d, %r13d
xorl %ebp, %ebp
leaq 0x16844(%rip), %rcx     # 0x32300
movq %rdx, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movzbl (%rsi,%rbp), %eax
movzbl (%rcx,%rax), %r14d
movzbl (%rdx,%rbp), %eax
movzbl (%rcx,%rax), %r12d
cmpb %r12b, %r14b
jne 0x1baeb
incl %r13d
jmp 0x1bb98
testl %r13d, %r13d
jle 0x1bb31
movl 0x20(%rsp), %eax
movslq %r11d, %rdx
incl %r11d
movl %r11d, 0x70(%rsp)
movq %r13, %rcx
movq 0x30(%rsp), %r13
movl %eax, (%r13,%rdx,4)
movq 0x18(%rsp), %r15
movq %r15, %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
leaq 0xca0e(%rip), %rdx      # 0x28531
xorl %eax, %eax
callq 0x1fdf5
movl 0x70(%rsp), %r11d
jmp 0x1bb40
movq 0x18(%rsp), %r15
leaq 0x20(%rsp), %rbx
movq 0x30(%rsp), %r13
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%r13,%rcx,4)
leaq 0xdfb5(%rip), %rax      # 0x29b0b
movsbl (%r14,%rax), %ecx
movsbl (%r12,%rax), %r8d
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rsi
leaq 0xdf95(%rip), %rdx      # 0x29b05
xorl %eax, %eax
movl %r11d, %ebx
callq 0x1fdf5
movl %ebx, %r11d
leaq 0x1677c(%rip), %rcx     # 0x32300
movq (%rsp), %r8
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %rsi
incq %rbp
cmpq %rbp, %r8
jne 0x1bacc
testl %r13d, %r13d
jle 0x1bbe0
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movq 0x30(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xc965(%rip), %rdx      # 0x28531
movl %r13d, %ecx
xorl %eax, %eax
movl %r11d, %ebx
callq 0x1fdf5
movq (%rsp), %r8
movl %ebx, %r11d
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %r14
movq 0x60(%rsp), %r12
movq 0x38(%rsp), %rbx
movq 0x58(%rsp), %rbp
movq 0x8(%rsp), %rax
addq %r8, %rbp
addq %r8, %rax
movq %rax, %rdx
jmp 0x1bdf1
cmpl $0x1, %edx
je 0x1bc29
cmpl $0x2, %edx
jne 0x1bc56
cmpq $0x10, %rax
movq %rbx, 0x38(%rsp)
jae 0x1bc60
xorl %r13d, %r13d
jmp 0x1bcb7
movq %r8, %rdi
movq 0x8(%rsp), %r8
leaq (%rdi,%r8), %r10
cmpq $0x10, %rax
movq %rbx, 0x38(%rsp)
movq %rbp, 0x58(%rsp)
movq %r10, 0x70(%rsp)
jae 0x1bd3b
xorl %r13d, %r13d
jmp 0x1bd88
movq 0x8(%rsp), %rdx
jmp 0x1bdf1
leaq 0x1(%r8), %rdx
movq %r8, %rsi
negq %rsi
movq 0x10(%rsp), %rdi
addq %r8, %rdi
leaq -0x1(%rbp), %r8
movl $0x1, %r13d
testq %r8, %r8
js 0x1bca6
movb (%rdi,%r8), %r9b
movq 0x10(%rsp), %r10
cmpb (%r10,%r8), %r9b
jne 0x1bca6
leaq (%rsi,%r13), %r9
incq %r9
incq %r13
decq %r8
cmpq $0x1, %r9
jne 0x1bc7c
movq %rdx, %r13
decq %r13
cmpq $0x10, %rax
movq (%rsp), %r8
jae 0x1be0f
xorl %r15d, %r15d
leaq (%r8,%rbp), %rax
movq %rax, (%rsp)
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movq 0x30(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movq %rbp, %rbx
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0xc685(%rip), %rdx      # 0x28370
xorl %eax, %eax
movl %r11d, %r14d
movq %r8, %r12
callq 0x1fdf5
addq 0x10(%rsp), %rbx
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %r15, %r8
movq %r13, %r9
callq 0x1bf8d
movl %r14d, %r11d
movq 0x8(%rsp), %rdx
movq (%rsp), %rbp
movq 0x60(%rsp), %r12
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %rbx
jmp 0x1bdf1
movslq 0x18(%r12), %rsi
leaq 0x1(%rdi), %rdx
movq %rdi, %rcx
negq %rcx
addq %r14, %rdi
decq %r8
movl $0x1, %r13d
cmpq %rsi, %r8
jl 0x1bd7b
movb (%rdi,%r8), %r9b
cmpb (%r14,%r8), %r9b
jne 0x1bd7b
leaq (%rcx,%r13), %r9
incq %r9
incq %r13
decq %r8
cmpq $0x1, %r9
jne 0x1bd56
movq %rdx, %r13
decq %r13
cmpq $0x10, %rax
jae 0x1be51
xorl %r15d, %r15d
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movq 0x30(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
leaq 0xdd60(%rip), %rdx      # 0x29b11
xorl %eax, %eax
movl %r11d, %ebp
callq 0x1fdf5
movq 0x8(%rsp), %rcx
addq %r14, %rcx
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
movq (%rsp), %rdx
movq %r15, %r8
movq %r13, %r9
callq 0x1bf8d
movl %ebp, %r11d
movq 0x48(%rsp), %r15
movq 0x38(%rsp), %rbx
movq 0x58(%rsp), %rbp
movq 0x70(%rsp), %rdx
incq %rbx
movq 0x48(%r12), %rcx
movslq (%rcx), %rax
movq %rdx, 0x8(%rsp)
cmpq %rax, %rbx
jl 0x1ba54
jmp 0x1be9a
movslq 0xc(%rcx), %rax
movq 0x10(%rsp), %rcx
addq %rbp, %rcx
leaq (%rcx,%r8), %rdx
leaq (%r8,%rbp), %rsi
xorl %r15d, %r15d
leaq (%rsi,%r15), %rdi
cmpq %rax, %rdi
jge 0x1bcba
movb (%rcx,%r15), %dil
cmpb (%rdx,%r15), %dil
jne 0x1bcba
incq %r15
cmpq %r15, %r8
jne 0x1be26
movq %r8, %r15
jmp 0x1bcba
movslq 0x1c(%r12), %rax
movq 0x8(%rsp), %rdx
addq %r14, %rdx
leaq (%r14,%r10), %rsi
xorl %r15d, %r15d
movq (%rsp), %r8
leaq (%r10,%r15), %rdi
cmpq %rax, %rdi
jge 0x1bd8b
movb (%rsi,%r15), %dil
cmpb (%rdx,%r15), %dil
jne 0x1bd8b
incq %r15
movq %rcx, %rdi
addq %r15, %rdi
jne 0x1be69
movq %r8, %r15
jmp 0x1bd8b
xorl %r11d, %r11d
movl 0x20(%rsp), %esi
movl %esi, 0x50(%r12)
movq (%r15), %rdi
incl %esi
movl $0x1, %edx
movl %r11d, %ebx
callq 0x2f84
movq %rax, 0x60(%r12)
movq 0x28(%rsp), %rsi
movl 0x20(%rsp), %edx
movq %rax, %rdi
callq 0x2230
movl %ebx, 0x54(%r12)
movq (%r15), %rdi
movslq %ebx, %rbx
movl $0x4, %edx
movq %rbx, %rsi
callq 0x2f84
movq %rax, 0x58(%r12)
shlq $0x2, %rbx
movq %rax, %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rdx
callq 0x2230
movq 0x80(%rsp), %rsi
incq %rsi
movslq 0x8(%r15), %rax
cmpq %rax, %rsi
jl 0x1b7ff
movq 0x18(%rsp), %rdi
callq 0x2fe1
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdb28(%rip), %rdi      # 0x29a5f
leaq 0xd9fb(%rip), %rsi      # 0x29939
leaq 0xdb3c(%rip), %rcx      # 0x29a81
movl $0xcb, %edx
callq 0x2160
leaq 0xdba1(%rip), %rdi      # 0x29af7
leaq 0xd9dc(%rip), %rsi      # 0x29939
leaq 0xdb1d(%rip), %rcx      # 0x29a81
movl $0xe9, %edx
callq 0x2160
leaq 0xdb6c(%rip), %rdi      # 0x29ae1
leaq 0xd9bd(%rip), %rsi      # 0x29939
leaq 0xdafe(%rip), %rcx      # 0x29a81
movl $0xcf, %edx
callq 0x2160