// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rdx, 0x78(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %esi, %esi
callq 0x300c
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x1bf0a
xorl %esi, %esi
movq $0x0, 0x70(%rsp)
movq $0x0, (%rsp)
movl $0x0, 0x6c(%rsp)
movq $0x0, 0x10(%rsp)
movq %r15, 0x58(%rsp)
movq %r13, 0x40(%rsp)
movq 0x18(%r15), %rax
imulq $0x68, %rsi, %rcx
leaq (%rax,%rcx), %r12
movl $0x0, 0x20(%rsp)
movq 0x48(%rax,%rcx), %rax
movslq 0xc(%rax), %rdx
cmpl 0x6c(%rsp), %edx
movq %rsi, 0x80(%rsp)
jbe 0x1b84a
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x32c4
movq %rax, 0x10(%rsp)
movq 0x48(%r12), %rax
movl 0xc(%rax), %eax
movl %eax, 0x6c(%rsp)
movl 0xc(%r12), %ecx
testl %ecx, %ecx
jle 0x1b8da
xorl %ebx, %ebx
xorl %eax, %eax
movq 0x78(%rsp), %r14
movl $0x0, %edx
testl %ebx, %ebx
jne 0x1b871
movq 0x48(%r12), %rdx
movl 0x10(%rdx), %edx
movslq 0x8(%r12), %rsi
movq 0x20(%r15), %rdi
addq %rbx, %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x8(%rdi,%rsi,4), %r8d
shlq $0x4, %r8
decl %ecx
cmpl %ecx, %ebx
jge 0x1b897
movl 0x8(%r14,%r8), %ecx
jmp 0x1b89f
movq 0x48(%r12), %rcx
movl 0x14(%rcx), %ecx
subl %edx, %ecx
leal (%rcx,%rax), %ebp
movq 0x48(%r12), %rsi
cmpl 0xc(%rsi), %ebp
ja 0x1bf26
addq %r14, %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 0x1b860
jmp 0x1b8dc
xorl %ebp, %ebp
movq 0x48(%r12), %rdx
cmpl 0xc(%rdx), %ebp
jne 0x1bf64
movslq (%rdx), %rcx
testq %rcx, %rcx
movq %r12, 0x60(%rsp)
jle 0x1b9fa
movslq 0x18(%r12), %r13
xorl %esi, %esi
xorl %r8d, %r8d
movq $0x0, 0x38(%rsp)
xorl %ebx, %ebx
movq 0x18(%rdx,%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 0x1b94c
movq %rcx, %rax
incl %r8d
cmpq $0x10, %r11
jae 0x1b96e
movq %r13, %rdi
movl %r8d, %ebp
jmp 0x1b9bd
cmpl $0x2, %r10d
je 0x1b9de
cmpl $0x1, %r10d
jne 0x1b9ec
addq %r9, %rbx
incq %rbx
incl %r8d
addq %r9, %r13
jmp 0x1b9ec
movq 0x40(%rsp), %r10
movq %r13, %rdi
addq %r13, %r10
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %r11
addq %rcx, %r11
xorl %r14d, %r14d
leaq 0x16970(%rip), %rcx     # 0x32300
movzbl (%r11,%r14), %r15d
movb (%rcx,%r15), %r15b
movzbl (%r10,%r14), %r12d
leaq 0x9(%rbx), %r13
leal 0x2(%r8), %ebp
cmpb (%rcx,%r12), %r15b
cmovneq %r13, %rbx
cmovel %r8d, %ebp
incq %r14
movl %ebp, %r8d
cmpq %r14, %r9
jne 0x1b990
addq $0x6, %rbx
addq %r9, 0x38(%rsp)
movq %rdi, %r13
addq %r9, %r13
movl %ebp, %r8d
movq 0x58(%rsp), %r15
movq 0x60(%rsp), %r12
movq %rax, %rcx
jmp 0x1b9ec
addq %r9, %rbx
incq %rbx
incl %r8d
addq %r9, 0x38(%rsp)
incq %rsi
cmpq %rcx, %rsi
jne 0x1b910
jmp 0x1b9ff
xorl %ebx, %ebx
xorl %r8d, %r8d
cmpl 0x70(%rsp), %r8d
movq 0x40(%rsp), %r13
movq 0x18(%rsp), %rdi
jle 0x1ba3a
movl %r8d, %eax
shrl %eax
addl %r8d, %eax
addl $0x10, %eax
movq %rax, 0x70(%rsp)
leaq (,%rax,4), %rdx
movq (%rsp), %rsi
callq 0x32c4
movq 0x18(%rsp), %rdi
movq %rax, (%rsp)
leaq 0x20(%rsp), %rsi
movl %ebx, %edx
callq 0x1f88b
movq 0x48(%r12), %rcx
cmpl $0x0, (%rcx)
jle 0x1be8e
movslq 0x18(%r12), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r11d, %r11d
movq $0x0, 0x48(%rsp)
cmpl 0x70(%rsp), %r11d
jge 0x1bf45
movq 0x18(%rcx,%rbp,8), %rax
movl %eax, %edx
andl $0xf, %edx
movq %rax, %r14
shrq $0x4, %r14
cmpl $0x7, %edx
setne %sil
testb $0x7, %al
setne %dil
testb %sil, %dil
movq %r14, 0x38(%rsp)
jne 0x1bab6
movq %rbp, 0x50(%rsp)
xorl %r15d, %r15d
cmpq $0x10, %rax
jae 0x1bada
movq (%rsp), %rdx
jmp 0x1bbe1
cmpl $0x1, %edx
je 0x1bc3d
cmpl $0x2, %edx
jne 0x1bc4f
cmpq $0x10, %rax
jae 0x1bc59
xorl %r15d, %r15d
jmp 0x1bca6
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rsi
movq 0x10(%rsp), %rax
movq 0x48(%rsp), %rcx
leaq (%rax,%rcx), %rdi
xorl %r15d, %r15d
xorl %ebp, %ebp
leaq 0x16803(%rip), %rcx     # 0x32300
movq (%rsp), %rdx
movq %rsi, 0x90(%rsp)
movq %rdi, 0x88(%rsp)
movzbl (%rdi,%rbp), %eax
movzbl (%rcx,%rax), %r13d
movzbl (%rsi,%rbp), %eax
movzbl (%rcx,%rax), %r12d
cmpb %r12b, %r13b
jne 0x1bb30
incl %r15d
jmp 0x1bbd5
testl %r15d, %r15d
jle 0x1bb6f
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
leaq 0xc9d8(%rip), %rdx      # 0x28531
movl %r15d, %ecx
xorl %eax, %eax
movl %r11d, %r15d
callq 0x1fd8a
movl %r15d, %r11d
movq (%rsp), %rdx
jmp 0x1bb79
movq 0x18(%rsp), %rbx
leaq 0x20(%rsp), %r14
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%rdx,%rcx,4)
leaq 0xdf0a(%rip), %rax      # 0x29a97
movsbl (%r13,%rax), %ecx
movsbl (%r12,%rax), %r8d
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
leaq 0xdee9(%rip), %rdx      # 0x29a91
xorl %eax, %eax
movl %r11d, %ebx
callq 0x1fd8a
movl %ebx, %r11d
movq (%rsp), %rdx
leaq 0x16740(%rip), %rcx     # 0x32300
movq 0x38(%rsp), %r14
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdi
incq %rbp
cmpq %rbp, %r14
jne 0x1bb11
testl %r15d, %r15d
jle 0x1bc14
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xc92d(%rip), %rdx      # 0x28531
movl %r15d, %ecx
xorl %eax, %eax
movl %r11d, %ebx
callq 0x1fd8a
movl %ebx, %r11d
movq 0x58(%rsp), %r15
movq 0x40(%rsp), %r13
movq 0x60(%rsp), %r12
movq 0x50(%rsp), %rbp
addq %r14, 0x48(%rsp)
movq %r14, %rax
movq 0x8(%rsp), %r14
addq %rax, %r14
jmp 0x1be73
cmpq $0x10, %rax
jae 0x1bd6e
xorl %r15d, %r15d
jmp 0x1bdb8
movq 0x8(%rsp), %r14
jmp 0x1be73
leaq 0x1(%r14), %rdx
movq %r14, %rsi
negq %rsi
movq 0x10(%rsp), %rdi
addq %r14, %rdi
movq 0x48(%rsp), %r8
decq %r8
movl $0x1, %r15d
testq %r8, %r8
js 0x1bca3
movb (%rdi,%r8), %r9b
movq 0x10(%rsp), %r10
cmpb (%r10,%r8), %r9b
jne 0x1bca3
leaq (%rsi,%r15), %r9
incq %r9
incq %r15
decq %r8
cmpq $0x1, %r9
jne 0x1bc79
movq %rdx, %r15
decq %r15
movq (%rsp), %rdx
cmpq $0x10, %rax
movq %rbp, 0x50(%rsp)
jae 0x1bcb9
xorl %ebx, %ebx
jmp 0x1bcf5
movslq 0xc(%rcx), %rax
movq 0x10(%rsp), %rcx
movq 0x48(%rsp), %rsi
addq %rsi, %rcx
leaq (%rcx,%r14), %rdx
addq %r14, %rsi
xorl %ebx, %ebx
leaq (%rsi,%rbx), %rdi
cmpq %rax, %rdi
jge 0x1bcf1
movb (%rcx,%rbx), %dil
cmpb (%rdx,%rbx), %dil
jne 0x1bcf1
incq %rbx
cmpq %rbx, %r14
jne 0x1bcd3
movq %r14, %rbx
movq (%rsp), %rdx
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
leaq 0xc657(%rip), %rdx      # 0x28370
xorl %eax, %eax
movl %r11d, %r13d
callq 0x1fd8a
movq 0x10(%rsp), %rax
movq 0x48(%rsp), %rbp
leaq (%rax,%rbp), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdx
movq %rbx, %r8
movq %r15, %r9
callq 0x1bf83
movl %r13d, %r11d
addq %r14, %rbp
movq %rbp, 0x48(%rsp)
movq 0x8(%rsp), %r14
movq 0x60(%rsp), %r12
movq 0x58(%rsp), %r15
movq 0x40(%rsp), %r13
jmp 0x1be6e
movslq 0x18(%r12), %rdx
leaq 0x1(%r14), %rcx
movq %r14, %rsi
negq %rsi
leaq (%r14,%r13), %rdi
movq 0x8(%rsp), %r8
decq %r8
movl $0x1, %r15d
cmpq %rdx, %r8
jl 0x1bdb5
movb (%rdi,%r8), %r9b
cmpb (%r13,%r8), %r9b
jne 0x1bdb5
leaq (%rsi,%r15), %r9
incq %r9
incq %r15
decq %r8
cmpq $0x1, %r9
jne 0x1bd8f
movq %rcx, %r15
decq %r15
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r14, %rsi
addq %rcx, %r14
cmpq $0x10, %rax
movq %rbp, 0x50(%rsp)
jae 0x1bdd6
xorl %ebx, %ebx
jmp 0x1be11
movslq 0x1c(%r12), %rax
addq %r13, %rcx
movq %rsi, %rdx
negq %rdx
leaq (%r14,%r13), %rsi
xorl %ebx, %ebx
leaq (%r14,%rbx), %rdi
cmpq %rax, %rdi
jge 0x1be0d
movb (%rsi,%rbx), %dil
cmpb (%rcx,%rbx), %dil
jne 0x1be0d
incq %rbx
movq %rdx, %rdi
addq %rbx, %rdi
jne 0x1bdea
movq 0x38(%rsp), %rbx
movq (%rsp), %rdx
movl 0x20(%rsp), %eax
movslq %r11d, %rcx
incl %r11d
movl %eax, (%rdx,%rcx,4)
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0xdc6b(%rip), %rdx      # 0x29a9d
xorl %eax, %eax
movl %r11d, %r12d
callq 0x1fd8a
movq 0x40(%rsp), %r13
movq 0x8(%rsp), %rcx
addq %r13, %rcx
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %r8
movq %r15, %r9
callq 0x1bf83
movl %r12d, %r11d
movq 0x60(%rsp), %r12
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rbp
incq %rbp
movq 0x48(%r12), %rcx
movslq (%rcx), %rax
movq %r14, 0x8(%rsp)
cmpq %rax, %rbp
jl 0x1ba6c
jmp 0x1be91
xorl %r11d, %r11d
movl 0x20(%rsp), %esi
movl %esi, 0x50(%r12)
movq (%r15), %rdi
incl %esi
movl $0x1, %edx
movl %r11d, %ebx
callq 0x3054
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 0x3054
movq %rax, 0x58(%r12)
shlq $0x2, %rbx
movq %rax, %rdi
movq (%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 0x30b8
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdabe(%rip), %rdi      # 0x299eb
leaq 0xd991(%rip), %rsi      # 0x298c5
leaq 0xdad2(%rip), %rcx      # 0x29a0d
movl $0xcb, %edx
callq 0x2160
leaq 0xdb37(%rip), %rdi      # 0x29a83
leaq 0xd972(%rip), %rsi      # 0x298c5
leaq 0xdab3(%rip), %rcx      # 0x29a0d
movl $0xe9, %edx
callq 0x2160
leaq 0xdb02(%rip), %rdi      # 0x29a6d
leaq 0xd953(%rip), %rsi      # 0x298c5
leaq 0xda94(%rip), %rcx      # 0x29a0d
movl $0xcf, %edx
callq 0x2160