// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rcx, 0x48(%rsp)
movq %rdx, 0x78(%rsp)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
xorl %esi, %esi
callq 0x24ac
movq %rax, %rdi
xorl %esi, %esi
movq $0x0, 0x10(%rsp)
movl $0x0, 0x5c(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x70(%rsp)
movq %r14, 0x60(%rsp)
movq %rax, 0x38(%rsp)
movslq 0x8(%r14), %rax
cmpq %rax, %rsi
jge 0x187d4
movq 0x18(%r14), %rax
imulq $0x68, %rsi, %rcx
andl $0x0, 0x20(%rsp)
leaq (%rax,%rcx), %r13
movq 0x48(%rax,%rcx), %rax
movslq 0xc(%rax), %rdx
cmpl 0x5c(%rsp), %edx
movq %rsi, 0x80(%rsp)
jbe 0x181ca
movq 0x10(%rsp), %rsi
callq 0x2774
movq %rax, 0x10(%rsp)
movq 0x48(%r13), %rax
movl 0xc(%rax), %eax
movl %eax, 0x5c(%rsp)
xorl %ebx, %ebx
xorl %ecx, %ecx
movl 0xc(%r13), %esi
cmpl %esi, %ebx
jge 0x18242
xorl %eax, %eax
movq 0x48(%r13), %rdx
testl %ebx, %ebx
jne 0x181e3
movl 0x10(%rdx), %eax
movslq 0x8(%r13), %rdi
movq 0x20(%r14), %r8
addq %rbx, %rdi
imulq $0x14, %rdi, %rdi
movl 0x8(%r8,%rdi), %r8d
shlq $0x4, %r8
decl %esi
cmpl %esi, %ebx
jge 0x1820d
movq 0x78(%rsp), %rsi
movl 0x8(%rsi,%r8), %r9d
jmp 0x18211
movl 0x14(%rdx), %r9d
subl %eax, %r9d
leal (%r9,%rcx), %ebp
cmpl 0xc(%rdx), %ebp
ja 0x187eb
addq 0x78(%rsp), %r8
movl %ecx, %edi
addq 0x10(%rsp), %rdi
movslq %eax, %rsi
addq (%r8), %rsi
movslq %r9d, %rdx
callq 0x2230
incq %rbx
movl %ebp, %ecx
jmp 0x181ce
movq 0x48(%r13), %rax
cmpl 0xc(%rax), %ecx
jne 0x18829
movl (%rax), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movslq 0x18(%r13), %rsi
movq %rsi, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %edi, %edi
movq %r13, 0x68(%rsp)
cmpq %rcx, %rdx
je 0x18347
movq 0x18(%rax,%rdx,8), %r10
movl %r10d, %r11d
andl $0xf, %r11d
movq %r10, %r9
shrq $0x4, %r9
cmpq $0x7, %r11
setne %bpl
testb $0x7, %r10b
setne %r10b
testb %bpl, %r10b
jne 0x182f5
movq %rcx, %rsi
movq %rax, %rcx
incl %edi
movq 0x48(%rsp), %r10
movq 0x18(%rsp), %rax
addq %rax, %r10
movq 0x10(%rsp), %rax
leaq (%rax,%r8), %r11
xorl %r14d, %r14d
cmpq %r14, %r9
je 0x18311
movzbl (%r11,%r14), %r15d
leaq 0x16030(%rip), %rax     # 0x2e300
movb (%rax,%r15), %bpl
movzbl (%r10,%r14), %r15d
leaq 0x9(%rbx), %r12
leal 0x2(%rdi), %r13d
cmpb (%rax,%r15), %bpl
cmovneq %r12, %rbx
cmovel %edi, %r13d
incq %r14
movl %r13d, %edi
jmp 0x182bf
leaq (%rbx,%r9), %r10
incq %r10
cmpl $0x2, %r11d
je 0x18332
cmpl $0x1, %r11d
jne 0x18339
incl %edi
addq %r9, 0x18(%rsp)
jmp 0x1833c
addq $0x6, %rbx
addq %r9, %r8
addq %r9, 0x18(%rsp)
movq %rbx, %r10
movq 0x60(%rsp), %r14
movq 0x68(%rsp), %r13
movq %rcx, %rax
movq %rsi, %rcx
jmp 0x1833c
incl %edi
addq %r9, %r8
jmp 0x1833c
movq %rbx, %r10
incq %rdx
movq %r10, %rbx
jmp 0x1826d
cmpl 0x70(%rsp), %edi
jle 0x18384
movl %edi, %eax
shrl %eax
addl %edi, %eax
addl $0x10, %eax
movq %rax, 0x70(%rsp)
leaq (,%rax,4), %rdx
movq 0x38(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x2774
movq %r15, %rdi
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rsi
jmp 0x1838e
leaq 0x20(%rsp), %rsi
movq 0x38(%rsp), %rdi
movl %ebx, %edx
callq 0x1bc09
movslq 0x18(%r13), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
movq $0x0, 0x40(%rsp)
xorl %r10d, %r10d
movq 0x48(%rsp), %r8
movq 0x48(%r13), %rax
movslq (%rax), %rcx
movslq %r10d, %r15
cmpq %rcx, %rdx
jge 0x1876b
cmpl 0x70(%rsp), %r10d
jge 0x1880a
movq %rdx, 0x88(%rsp)
movq 0x18(%rax,%rdx,8), %rdx
movl %edx, %ecx
andl $0xf, %ecx
movq %rdx, %r11
shrq $0x4, %r11
cmpl $0x7, %ecx
setne %sil
testb $0x7, %dl
setne %dl
testb %sil, %dl
jne 0x184fd
movq 0x30(%rsp), %rax
leaq (%r8,%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx), %r8
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x8(%rsp), %rsi
movq %r11, 0x18(%rsp)
movq %rdi, 0x50(%rsp)
movq %r8, 0x90(%rsp)
movslq %r10d, %rax
cmpq %r13, %r11
je 0x18563
movzbl (%r8,%r13), %ecx
leaq 0x15eb6(%rip), %rdx     # 0x2e300
movzbl (%rdx,%rcx), %r14d
movzbl (%rdi,%r13), %ecx
movzbl (%rdx,%rcx), %ebp
cmpb %bpl, %r14b
jne 0x18465
incl %r15d
jmp 0x184f5
testl %r15d, %r15d
leaq 0x20(%rsp), %rdx
movq 0x38(%rsp), %r12
jle 0x184a2
movl 0x20(%rsp), %ecx
leal 0x1(%rax), %ebx
movl %ecx, (%rsi,%rax,4)
movq %r12, %rdi
movq %rdx, %rsi
leaq 0xb0af(%rip), %rdx      # 0x2353a
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1bf2b
leaq 0x20(%rsp), %rdx
movl %ebx, %r10d
movq 0x8(%rsp), %rsi
movl 0x20(%rsp), %eax
movslq %r10d, %rcx
incl %r10d
movl %eax, (%rsi,%rcx,4)
leaq 0xc669(%rip), %rax      # 0x24b1f
movsbl (%r14,%rax), %ecx
movsbl (%rbp,%rax), %r8d
xorl %r15d, %r15d
movq %r12, %rdi
movq %rdx, %rsi
leaq 0xc648(%rip), %rdx      # 0x24b19
xorl %eax, %eax
movl %r10d, %ebx
callq 0x1bf2b
movl %ebx, %r10d
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r11
movq 0x50(%rsp), %rdi
movq 0x90(%rsp), %r8
incq %r13
jmp 0x18432
cmpl $0x1, %ecx
je 0x185bb
cmpl $0x2, %ecx
jne 0x18601
movq 0x40(%rsp), %rbp
leaq (%r11,%rbp), %rbx
leaq 0x1(%r11), %r13
movq %r11, %rcx
negq %rcx
movq 0x10(%rsp), %rdx
addq %r11, %rdx
leaq -0x1(%rbp), %rdi
pushq $0x1
popq %rsi
leaq (%rcx,%rsi), %r8
cmpq $0x1, %r8
je 0x1860e
testq %rdi, %rdi
js 0x1860b
movb (%rdx,%rdi), %r8b
movq 0x10(%rsp), %r9
cmpb (%r9,%rdi), %r8b
jne 0x1860b
incq %rsi
decq %rdi
jmp 0x18531
testl %r15d, %r15d
jle 0x1859a
movl 0x20(%rsp), %ecx
leal 0x1(%rax), %ebx
movq 0x8(%rsp), %rdx
movl %ecx, (%rdx,%rax,4)
movq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xafb2(%rip), %rdx      # 0x2353a
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1bf2b
movq 0x18(%rsp), %r11
movl %ebx, %r10d
addq %r11, 0x40(%rsp)
movq 0x30(%rsp), %r15
addq %r11, %r15
movq 0x60(%rsp), %r14
movq 0x48(%rsp), %r8
movq 0x68(%rsp), %r13
jmp 0x18756
movq %r15, %r9
movq 0x30(%rsp), %rdx
leaq (%r11,%rdx), %r15
leaq 0x1(%r11), %rbp
movq %r11, %rax
negq %rax
leaq (%r8,%r11), %rcx
leaq -0x1(%rdx), %rsi
pushq $0x1
popq %rdx
leaq (%rax,%rdx), %rdi
cmpq $0x1, %rdi
je 0x18657
movslq 0x18(%r13), %rdi
cmpq %rdi, %rsi
jl 0x18654
movb (%rcx,%rsi), %dil
cmpb (%r8,%rsi), %dil
jne 0x18654
incq %rdx
decq %rsi
jmp 0x185dc
movq 0x30(%rsp), %r15
jmp 0x18756
movq %rsi, %r13
decq %r13
movq 0x10(%rsp), %rsi
leaq (%rsi,%rbx), %rdx
addq %rsi, %rbp
xorl %r12d, %r12d
movq 0x38(%rsp), %rdi
movq %rbp, 0x40(%rsp)
movq %r13, 0x50(%rsp)
movq %rcx, %rsi
addq %r12, %rsi
je 0x186a3
leaq (%rbx,%r12), %rsi
movslq 0xc(%rax), %r8
cmpq %r8, %rsi
jge 0x186a6
movb (%rbp,%r12), %sil
cmpb (%rdx,%r12), %sil
jne 0x186a6
incq %r12
jmp 0x1862f
movq %rdx, %rbp
decq %rbp
movq 0x30(%rsp), %rcx
leaq (%r8,%rcx), %r12
leaq (%r12,%r11), %rax
addq %r11, %rcx
movq %rcx, %r8
xorl %ebx, %ebx
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r11, 0x18(%rsp)
movq %rbp, 0x50(%rsp)
cmpq %rbx, %r11
je 0x186ff
leaq (%r8,%rbx), %rcx
movslq 0x1c(%r13), %rdx
cmpq %rdx, %rcx
jge 0x18702
movb (%rax,%rbx), %cl
cmpb (%r12,%rbx), %cl
jne 0x18702
incq %rbx
jmp 0x18683
movq %r11, %r12
movq %r11, %rbp
movl 0x20(%rsp), %eax
incl %r10d
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%r15,4)
leaq 0x20(%rsp), %r15
movq %r15, %rsi
leaq 0xaca8(%rip), %rdx      # 0x23370
xorl %eax, %eax
movq %rdi, %r13
movl %r10d, %r14d
callq 0x1bf2b
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x40(%rsp), %rcx
movq %r12, %r8
movq 0x50(%rsp), %r9
callq 0x18848
movl %r14d, %r10d
movq 0x30(%rsp), %r15
movq %rbx, 0x40(%rsp)
jmp 0x18747
movq %r11, %rbx
movl 0x20(%rsp), %eax
incl %r10d
movl %eax, (%rsi,%r9,4)
leaq 0x20(%rsp), %r13
movq %r13, %rsi
leaq 0xc409(%rip), %rdx      # 0x24b25
xorl %eax, %eax
movq %rdi, %rbp
movl %r10d, %r14d
callq 0x1bf2b
movq %rbp, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
movq 0x50(%rsp), %r9
callq 0x18848
movl %r14d, %r10d
movq 0x48(%rsp), %r8
movq 0x68(%rsp), %r13
movq 0x60(%rsp), %r14
movq 0x88(%rsp), %rdx
incq %rdx
movq %r15, 0x30(%rsp)
jmp 0x183b1
movl 0x20(%rsp), %esi
movl %esi, 0x50(%r13)
movq (%r14), %rdi
incl %esi
pushq $0x1
popq %rdx
callq 0x24f0
movq %rax, 0x60(%r13)
movq 0x28(%rsp), %rsi
movl 0x20(%rsp), %edx
movq %rax, %rdi
callq 0x2230
movl %r15d, 0x54(%r13)
movq (%r14), %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
callq 0x24f0
movq %rax, 0x58(%r13)
shlq $0x2, %r15
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x2230
movq 0x80(%rsp), %rsi
incq %rsi
movq 0x38(%rsp), %rdi
jmp 0x1817b
callq 0x253c
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc281(%rip), %rdi      # 0x24a73
leaq 0xc154(%rip), %rsi      # 0x2494d
leaq 0xc295(%rip), %rcx      # 0x24a95
movl $0xcb, %edx
callq 0x2160
leaq 0xc2fa(%rip), %rdi      # 0x24b0b
leaq 0xc135(%rip), %rsi      # 0x2494d
leaq 0xc276(%rip), %rcx      # 0x24a95
movl $0xe9, %edx
callq 0x2160
leaq 0xc2c5(%rip), %rdi      # 0x24af5
leaq 0xc116(%rip), %rsi      # 0x2494d
leaq 0xc257(%rip), %rcx      # 0x24a95
movl $0xcf, %edx
callq 0x2160