// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/bwa.c
// Function: bwa_gen_cigar2
uint32_t *bwa_gen_cigar2(const int8_t mat[VALUE_SCORING], int o_del, int e_del, int o_ins, int e_ins, int w_, int64_t l_pac, const uint8_t *pac, int l_query, uint8_t *query, int64_t rb, int64_t re, int *score, int *n_cigar, int *NM)
{
	uint32_t *cigar = 0;
	uint8_t tmp, *rseq;
	int i;
	int64_t rlen;
	kstring_t str;
	const char *int2base;

	if (n_cigar) *n_cigar = 0;
	if (NM) *NM = -1;
	if (l_query <= 0 || rb >= re || (rb < l_pac && re > l_pac)) return 0; // reject if negative length or bridging the forward and reverse strand
	rseq = bns_get_seq(l_pac, pac, rb, re, &rlen);
	if (re - rb != rlen) goto ret_gen_cigar; // possible if out of range
	if (rb >= l_pac) { // then reverse both query and rseq; this is to ensure indels to be placed at the leftmost position
		for (i = 0; i < l_query>>1; ++i)
			tmp = query[i], query[i] = query[l_query - 1 - i], query[l_query - 1 - i] = tmp;
		for (i = 0; i < rlen>>1; ++i)
			tmp = rseq[i], rseq[i] = rseq[rlen - 1 - i], rseq[rlen - 1 - i] = tmp;
	}
	if (l_query == re - rb && w_ == 0) { // no gap; no need to do DP
		// UPDATE: we come to this block now... FIXME: due to an issue in mem_reg2aln(), we never come to this block. This does not affect accuracy, but it hurts performance.
		if (n_cigar) {
			cigar = malloc(4);
			cigar[0] = l_query<<4 | 0;
			*n_cigar = 1;
		}
		for (i = 0, *score = 0; i < l_query; ++i)
			*score += mat[rseq[i]*5 + query[i]];
	} else {
		int w, max_gap, max_ins, max_del, min_w;
		// set the band-width
		max_ins = (int)((double)(((l_query+1)>>1) * mat[0] - o_ins) / e_ins + 1.);
		max_del = (int)((double)(((l_query+1)>>1) * mat[0] - o_del) / e_del + 1.);
		max_gap = max_ins > max_del? max_ins : max_del;
		max_gap = max_gap > 1? max_gap : 1;
		w = (max_gap + abs(rlen - l_query) + 1) >> 1;
		w = w < w_? w : w_;
		min_w = abs(rlen - l_query) + 3;
		w = w > min_w? w : min_w;
		// NW alignment
		if (bwa_verbose >= 4) {
			printf("* Global bandwidth: %d\n", w);
			printf("* Global ref:   "); for (i = 0; i < rlen; ++i) putchar("ACGTN"[(int)rseq[i]]); putchar('\n');
			printf("* Global query: "); for (i = 0; i < l_query; ++i) putchar("ACGTN"[(int)query[i]]); putchar('\n');
		}
		*score = ksw_global2(l_query, query, rlen, rseq, VALUE_DEFINED, mat, o_del, e_del, o_ins, e_ins, w, n_cigar, &cigar);
	}
	if (NM && n_cigar) {// compute NM and MD
		int k, x, y, u, n_mm = 0, n_gap = 0;
		str.l = str.m = *n_cigar * 4; str.s = (char*)cigar; // append MD to CIGAR
		int2base = rb < l_pac? "ACGTN" : "TGCAN";
		for (k = 0, x = y = u = 0; k < *n_cigar; ++k) {
			int op, len;
			cigar = (uint32_t*)str.s;
			op  = cigar[k]&0xf, len = cigar[k]>>4;
			if (op == 0) { // match
				for (i = 0; i < len; ++i) {
					if (query[x + i] != rseq[y + i]) {
						kputw(u, &str);
						kputc(int2base[rseq[y+i]], &str);
						++n_mm; u = 0;
					} else ++u;
				}
				x += len; y += len;
			} else if (op == 2) { // deletion
				if (k > 0 && k < *n_cigar - 1) { // don't do the following if D is the first or the last CIGAR
					kputw(u, &str); kputc('^', &str);
					for (i = 0; i < len; ++i)
						kputc(int2base[rseq[y+i]], &str);
					u = 0; n_gap += len;
				}
				y += len;
			} else if (op == 1) x += len, n_gap += len; // insertion
		}
		kputw(u, &str); kputc(0, &str);
		*NM = n_mm + n_gap;
		cigar = (uint32_t*)str.s;
	}
	if (rb >= l_pac) // reverse back query
		for (i = 0; i < l_query>>1; ++i)
			tmp = query[i], query[i] = query[l_query - 1 - i], query[l_query - 1 - i] = tmp;

ret_gen_cigar:
	free(rseq);
	return cigar;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r10d
movq %rdi, %rbp
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
andq $0x0, 0x30(%rsp)
testq %rcx, %rcx
je 0x105a4
andl $0x0, (%rcx)
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %r12
movl 0xb0(%rsp), %r15d
testq %rax, %rax
je 0x105c4
orl $-0x1, (%rax)
testl %r15d, %r15d
setle %al
movq %rcx, %rbx
subq %r12, %rbx
setle %dil
orb %al, %dil
jne 0x105f3
movq 0xa0(%rsp), %r13
cmpq %r13, %r12
setl %al
cmpq %r13, %rcx
setg %dil
testb %dil, %al
je 0x105fa
xorl %eax, %eax
jmp 0x10b15
movl %r9d, 0x10(%rsp)
movq %r8, 0x28(%rsp)
movq %r10, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
movq 0xa8(%rsp), %rsi
leaq 0x40(%rsp), %r14
movq %r13, %rdi
movq %r12, %rdx
movq %r14, %r8
callq 0xfab6
movq %rax, 0x20(%rsp)
cmpq (%r14), %rbx
jne 0x10b06
movl %r15d, %esi
movq %rbx, %rdx
cmpq %r13, %r12
movq 0xd8(%rsp), %r14
movl 0x10(%rsp), %r10d
jl 0x106cc
movl %r15d, %eax
shrl %eax
movq 0xb8(%rsp), %rcx
movq %rsi, %r9
addq %rsi, %rcx
decq %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x1068f
movq 0xb8(%rsp), %r8
movb (%r8,%rdx), %sil
movb (%rcx), %dil
movb %dil, (%r8,%rdx)
movb %sil, (%rcx)
incq %rdx
decq %rcx
jmp 0x1066c
movq 0x20(%rsp), %rsi
leaq -0x1(%rsi), %rax
xorl %ecx, %ecx
movq %rbx, %rdx
movq %rdx, %rdi
sarq %rdi
cmpq %rcx, %rdi
jle 0x106c9
movb (%rsi), %dil
movb (%rax,%rdx), %dl
movb %dl, (%rsi)
movq 0x40(%rsp), %rdx
movb %dil, (%rax,%rdx)
incq %rcx
movq 0x40(%rsp), %rdx
incq %rsi
decq %rax
jmp 0x1069d
movq %r9, %rsi
movq 0xd0(%rsp), %r12
cmpq %rsi, %rbx
setne %al
testl %r10d, %r10d
setne %cl
orb %al, %cl
movq 0xe0(%rsp), %rdi
movq %rsi, %rbx
jne 0x1076b
testq %r14, %r14
je 0x1072b
leaq 0x12e0a(%rip), %rsi     # 0x23505
leaq 0x12e42(%rip), %rcx     # 0x23544
pushq $0x4
popq %rdi
movl $0x90, %edx
callq 0x1e045
movq 0xe0(%rsp), %rdi
movq %rax, 0x30(%rsp)
movl %r15d, %ecx
shll $0x4, %ecx
movl %ecx, (%rax)
movl $0x1, (%r14)
andl $0x0, (%r12)
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x20(%rsp), %r8
movq 0xb8(%rsp), %r13
cmpq %rcx, %rbx
je 0x108f2
movzbl (%r8,%rcx), %edx
leaq (%rdx,%rdx,4), %rdx
movzbl (%r13,%rcx), %esi
addq %rbp, %rdx
movsbl (%rsi,%rdx), %edx
addl %edx, %eax
movl %eax, (%r12)
incq %rcx
jmp 0x10741
leal 0x1(%r15), %eax
shrl %eax
movsbl (%rbp), %ecx
imull %eax, %ecx
movl %ecx, %eax
subl 0x18(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x28(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x12d34(%rip), %xmm1    # 0x234c8
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
subl 0x38(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm2
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
cmpl $0x2, %ecx
pushq $0x1
popq %rax
cmovgel %ecx, %eax
movl %edx, %ecx
subl %r15d, %ecx
movl %ecx, %r11d
negl %r11d
cmovsl %ecx, %r11d
addl %r11d, %eax
incl %eax
shrl %eax
cmpl %r10d, %eax
cmovgel %r10d, %eax
addl $0x3, %r11d
cmpl %r11d, %eax
cmovgl %eax, %r11d
cmpl $0x4, 0x1c64d(%rip)     # 0x2ce40
movq 0x20(%rsp), %r12
movq 0xb8(%rsp), %r13
jl 0x1089f
movq %rbx, %r14
leaq 0x12d43(%rip), %rdi     # 0x23553
xorl %r15d, %r15d
movq %r11, 0x10(%rsp)
movl %r11d, %esi
xorl %eax, %eax
callq 0x3060
leaq 0x12d42(%rip), %rdi     # 0x2356b
xorl %eax, %eax
callq 0x3060
leaq 0x12d45(%rip), %rbx     # 0x2357c
cmpq %r15, 0x40(%rsp)
jle 0x10851
movzbl (%r12,%r15), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %r15
jmp 0x10837
pushq $0xa
popq %rdi
callq 0x34c0
leaq 0x12d22(%rip), %rdi     # 0x23582
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x3060
cmpq %r15, %r14
je 0x10883
movzbl (%r13,%r15), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %r15
jmp 0x1086a
pushq $0xa
popq %rdi
callq 0x34c0
movl 0x40(%rsp), %edx
movl 0xb0(%rsp), %r15d
movq %r14, %rbx
movq 0x10(%rsp), %r11
subq $0x8, %rsp
leaq 0x38(%rsp), %rax
pushq $0x16
popq %r8
movl %r15d, %edi
movq %r13, %rsi
movq %r12, %rcx
movq %rbp, %r9
pushq %rax
movq 0xe8(%rsp), %rax
pushq %rax
pushq %r11
movq %rax, %r14
pushq 0x48(%rsp)
pushq 0x40(%rsp)
pushq 0x38(%rsp)
pushq 0x70(%rsp)
callq 0x1f60a
addq $0x40, %rsp
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x10af4
movq %rbx, 0x48(%rsp)
movslq (%r14), %rax
leaq (,%rax,4), %rcx
movq 0xa0(%rsp), %rdx
cmpq %rdx, 0xc0(%rsp)
leaq 0x50(%rsp), %rsi
movq %rcx, 0x8(%rsi)
movq %rcx, (%rsi)
movq 0x30(%rsp), %rcx
leaq 0x12c3e(%rip), %rdx     # 0x2357c
leaq 0x12c4e(%rip), %rdi     # 0x23593
cmovlq %rdx, %rdi
movq %rdi, 0x38(%rsp)
movq %rcx, 0x10(%rsi)
xorl %edx, %edx
movl $0x0, 0x28(%rsp)
xorl %esi, %esi
xorl %edi, %edi
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0x10ab7
movq 0x60(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %rdx, 0x8(%rsp)
movl (%rcx,%rdx,4), %ebp
movl %ebp, %ecx
andl $0xf, %ecx
shrl $0x4, %ebp
movl %esi, 0x18(%rsp)
movslq %esi, %r12
cmpl $0x2, %ecx
je 0x10a13
cmpl $0x1, %ecx
je 0x109fd
testl %ecx, %ecx
jne 0x10a05
movslq 0x28(%rsp), %r15
addq 0xb8(%rsp), %r15
addq 0x20(%rsp), %r12
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x10a6f
movb (%r15,%r14), %al
cmpb (%r12,%r14), %al
jne 0x109d1
incl %edi
jmp 0x109f8
leaq 0x50(%rsp), %r13
movq %r13, %rsi
callq 0x10b4c
movzbl (%r12,%r14), %eax
movq 0x38(%rsp), %rcx
movsbl (%rcx,%rax), %edi
movq %r13, %rsi
callq 0x10c38
incl %ebx
xorl %edi, %edi
incq %r14
jmp 0x109ba
addl %ebp, 0x28(%rsp)
addl %ebp, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movl 0x18(%rsp), %esi
jmp 0x10aac
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x10aa1
decl %eax
cltq
cmpq %rax, %rcx
jge 0x10aa1
leaq 0x50(%rsp), %r14
movq %r14, %rsi
callq 0x10b4c
pushq $0x5e
popq %rdi
movq %r14, %rsi
callq 0x10c38
movl %ebp, %r14d
addq 0x20(%rsp), %r12
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x10a8b
movzbl (%r12,%r15), %eax
movq 0x38(%rsp), %rcx
movsbl (%rcx,%rax), %edi
leaq 0x50(%rsp), %rsi
callq 0x10c38
incq %r15
jmp 0x10a4d
addl %ebp, 0x28(%rsp)
movl 0x18(%rsp), %esi
addl %ebp, %esi
movl 0xb0(%rsp), %r15d
movq 0xd8(%rsp), %r14
jmp 0x10aa7
addl %ebp, 0x10(%rsp)
xorl %edi, %edi
movl 0xb0(%rsp), %r15d
movq 0xd8(%rsp), %r14
addl 0x18(%rsp), %ebp
movl %ebp, %esi
movq 0x8(%rsp), %rdx
incq %rdx
movl (%r14), %eax
jmp 0x1096a
leaq 0x50(%rsp), %r14
movq %r14, %rsi
callq 0x10b4c
xorl %edi, %edi
movq %r14, %rsi
callq 0x10c38
movl 0x10(%rsp), %ecx
addl %ebx, %ecx
movq 0xe0(%rsp), %rax
movl %ecx, (%rax)
movq 0x10(%r14), %rax
movq %rax, 0x30(%rsp)
movq 0xb8(%rsp), %r13
movq 0x48(%rsp), %rbx
movq 0xa0(%rsp), %rax
cmpq %rax, 0xc0(%rsp)
jge 0x10b24
movq 0x20(%rsp), %rdi
callq 0x3390
movq 0x30(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shrl %r15d
leaq (%rbx,%r13), %rax
decq %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x10b06
movb (%r13,%rcx), %dl
movb (%rax), %sil
movb %sil, (%r13,%rcx)
movb %dl, (%rax)
incq %rcx
decq %rax
jmp 0x10b30