// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movl %ecx, %r10d
movq %rdi, %r15
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq $0x0, 0x40(%rsp)
testq %rcx, %rcx
je 0x122e4
movl $0x0, (%rcx)
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %rbp
movl 0xc0(%rsp), %ebx
testq %rax, %rax
je 0x12306
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
testl %ebx, %ebx
setle %al
movq %rcx, %r14
subq %rbp, %r14
setle %dil
orb %al, %dil
jne 0x12334
movq 0xb0(%rsp), %r13
cmpq %r13, %rbp
setl %al
cmpq %r13, %rcx
setg %dil
testb %dil, %al
je 0x1233b
xorl %eax, %eax
jmp 0x12afa
movl %r9d, 0x50(%rsp)
movq %r8, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movq 0xb8(%rsp), %rsi
leaq 0x68(%rsp), %r12
movq %r13, %rdi
movq %rbp, %rdx
movq %r12, %r8
callq 0x1161f
movq %rbp, %rcx
movq %rax, %rbp
cmpq (%r12), %r14
jne 0x12aed
movl %ebx, %edi
movq %r14, %rdx
cmpq %r13, %rcx
movq %rdi, 0x48(%rsp)
movq 0x8(%rsp), %r10
movl 0x50(%rsp), %r9d
jl 0x12420
cmpl $0x2, %ebx
jb 0x123d6
movl %ebx, %eax
shrl %eax
movq 0xc8(%rsp), %rsi
movq 0x48(%rsp), %rcx
addq %rsi, %rcx
decq %rcx
negq %rax
xorl %edx, %edx
movb (%rsi), %dil
movb (%rcx,%rdx), %r8b
movb %r8b, (%rsi)
movb %dil, (%rcx,%rdx)
incq %rsi
decq %rdx
cmpq %rdx, %rax
jne 0x123bd
movq %r14, %rdx
cmpq $0x2, %r14
movq 0xb0(%rsp), %r13
movq 0x48(%rsp), %rdi
jl 0x12420
leaq -0x1(%rbp), %rax
xorl %ecx, %ecx
movq %r14, %rdx
movb (%rbp,%rcx), %sil
movb (%rax,%rdx), %dl
movb %dl, (%rbp,%rcx)
movq 0x68(%rsp), %rdx
movb %sil, (%rax,%rdx)
incq %rcx
movq 0x68(%rsp), %rdx
movq %rdx, %rsi
sarq %rsi
decq %rax
cmpq %rcx, %rsi
jg 0x123f5
movq 0xe0(%rsp), %r12
cmpq %rdi, %r14
setne %al
testl %r9d, %r9d
setne %cl
orb %al, %cl
movq %rbp, 0x38(%rsp)
jne 0x124cb
movq 0xe8(%rsp), %rbx
testq %rbx, %rbx
je 0x1248f
leaq 0x16118(%rip), %rsi     # 0x2856d
leaq 0x16150(%rip), %rcx     # 0x285ac
movl $0x4, %edi
movl $0x90, %edx
callq 0x23213
movq 0x48(%rsp), %rdi
movq 0xb0(%rsp), %r13
movq %rax, 0x40(%rsp)
movl 0xc0(%rsp), %ecx
shll $0x4, %ecx
movl %ecx, (%rax)
movl $0x1, (%rbx)
movl $0x0, (%r12)
xorl %eax, %eax
xorl %ecx, %ecx
movq 0xc8(%rsp), %r14
movzbl (%rbp,%rax), %edx
leaq (%rdx,%rdx,4), %rdx
movzbl (%r14,%rax), %esi
addq %r15, %rdx
movsbl (%rsi,%rdx), %edx
addl %edx, %ecx
movl %ecx, (%r12)
incq %rax
cmpq %rax, %rdi
jne 0x124a3
jmp 0x12663
movl 0xc0(%rsp), %esi
leal 0x1(%rsi), %eax
shrl %eax
movsbl (%r15), %ecx
imull %eax, %ecx
movl %ecx, %eax
subl 0x10(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %r10d, %xmm1
divsd %xmm1, %xmm0
movsd 0x16037(%rip), %xmm1    # 0x28530
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
subl 0x60(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
cvtsi2sdl 0x58(%rsp), %xmm2
divsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movl %edx, %ecx
subl %esi, %ecx
movl %ecx, %ebp
negl %ebp
cmovsl %ecx, %ebp
addl %ebp, %eax
incl %eax
shrl %eax
cmpl %r9d, %eax
cmovgel %r9d, %eax
addl $0x3, %ebp
cmpl %ebp, %eax
cmovgl %eax, %ebp
cmpl $0x4, 0x1e8eb(%rip)     # 0x30e40
jl 0x12605
leaq 0x16059(%rip), %rdi     # 0x285bb
movl %ebp, %esi
xorl %eax, %eax
callq 0x3060
leaq 0x16061(%rip), %rdi     # 0x285d3
xorl %eax, %eax
callq 0x3060
cmpq $0x0, 0x68(%rsp)
leaq 0x1605e(%rip), %r14     # 0x285e4
movq 0x38(%rsp), %r13
jle 0x125aa
xorl %r12d, %r12d
movzbl (%r13,%r12), %eax
movsbl (%rax,%r14), %edi
callq 0x34c0
incq %r12
cmpq %r12, 0x68(%rsp)
jg 0x12590
movl $0xa, %edi
callq 0x34c0
leaq 0x1602f(%rip), %rdi     # 0x285ea
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x3060
movq 0xc8(%rsp), %r13
movq 0x48(%rsp), %rbx
movzbl (%r13,%r12), %eax
movsbl (%rax,%r14), %edi
callq 0x34c0
incq %r12
cmpq %r12, %rbx
jne 0x125d2
movl $0xa, %edi
callq 0x34c0
movl 0x68(%rsp), %edx
movq 0x8(%rsp), %r10
movq 0xe0(%rsp), %r12
subq $0x8, %rsp
leaq 0x48(%rsp), %rax
movl 0xc8(%rsp), %edi
movq 0xd0(%rsp), %r14
movq %r14, %rsi
movq 0x40(%rsp), %rcx
movl $0x16, %r8d
movq %r15, %r9
pushq %rax
pushq 0xf8(%rsp)
pushq %rbp
movq %rcx, %rbp
pushq %r10
pushq 0x38(%rsp)
pushq 0x88(%rsp)
pushq 0x98(%rsp)
callq 0x44e0
addq $0x40, %rsp
movl %eax, (%r12)
movq 0xb0(%rsp), %r13
cmpq $0x0, 0xf0(%rsp)
sete %al
movq 0xe8(%rsp), %rdx
testq %rdx, %rdx
sete %cl
orb %al, %cl
jne 0x12aa7
movslq (%rdx), %rcx
leaq (,%rcx,4), %rax
cmpq %r13, 0xd0(%rsp)
leaq 0x15f45(%rip), %rdx     # 0x285e4
leaq 0x15f55(%rip), %rsi     # 0x285fb
cmovlq %rdx, %rsi
movq %rsi, 0x60(%rsp)
movq %rax, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
testq %rcx, %rcx
jle 0x12a00
xorl %r9d, %r9d
xorl %r8d, %r8d
movl $0x0, 0x8(%rsp)
xorl %edi, %edi
xorl %r11d, %r11d
xorl %r10d, %r10d
movq 0xe8(%rsp), %rsi
movq %rax, 0x40(%rsp)
movl (%rax,%r9,4), %r15d
movl %r15d, %edx
andl $0xf, %edx
movl %r15d, %ebx
shrl $0x4, %ebx
cmpl $0x2, %edx
movq %rbx, 0x58(%rsp)
je 0x1285c
cmpl $0x1, %edx
je 0x12851
testl %edx, %edx
jne 0x129ef
movl %r11d, 0x10(%rsp)
movq %r9, 0x50(%rsp)
movl %r8d, 0x74(%rsp)
cmpl $0x10, %r15d
movl %r10d, 0x34(%rsp)
jb 0x1281d
movslq %r10d, %r13
movslq 0x10(%rsp), %rbp
addq 0xc8(%rsp), %r13
addq 0x38(%rsp), %rbp
xorl %r15d, %r15d
movb (%r13,%r15), %cl
cmpb (%rbp,%r15), %cl
jne 0x12767
incl %edi
jmp 0x12811
leaq 0x18(%rsp), %rsi
callq 0x12b09
movzbl (%rbp,%r15), %eax
movq 0x60(%rsp), %rcx
movb (%rcx,%rax), %r12b
movq 0x18(%rsp), %rbx
leaq 0x1(%rbx), %r14
cmpq 0x20(%rsp), %r14
jae 0x12797
movq 0x28(%rsp), %rax
jmp 0x127f2
movq %r14, %rax
shrq %rax
orq %r14, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x20(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x16000(%rip), %rdx     # 0x287dc
movl $0x37, %ecx
leaq 0x16006(%rip), %r8      # 0x287ee
callq 0x2324c
movq %rax, 0x28(%rsp)
movl 0x8(%rsp), %ecx
movq %r14, 0x18(%rsp)
movb %r12b, (%rax,%rbx)
movb $0x0, 0x1(%rax,%rbx)
incl %ecx
movl %ecx, 0x8(%rsp)
xorl %edi, %edi
movq 0x58(%rsp), %rbx
incq %r15
cmpq %r15, %rbx
jne 0x12754
movl 0x34(%rsp), %r10d
addl %ebx, %r10d
movl 0x10(%rsp), %r11d
addl %ebx, %r11d
movq 0x38(%rsp), %rbp
movq 0xc8(%rsp), %r14
movq 0xe8(%rsp), %rsi
movl 0x74(%rsp), %r8d
movq 0x50(%rsp), %r9
jmp 0x129ef
addl %ebx, %r10d
addl %ebx, %r8d
jmp 0x129ef
testq %r9, %r9
je 0x129e9
decl %ecx
movslq %ecx, %rcx
cmpq %rcx, %r9
jge 0x129e9
movl %r11d, 0x10(%rsp)
movl %r10d, 0x34(%rsp)
movq %r9, 0x50(%rsp)
movl %r8d, %r14d
leaq 0x18(%rsp), %rsi
callq 0x12b09
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %r13
leaq 0x1(%rbp), %r12
cmpq %r13, %r12
jae 0x128a9
movq 0x28(%rsp), %rax
jmp 0x12907
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %r13
shrq $0x10, %r13
orq %rcx, %r13
incq %r13
movq %r13, 0x20(%rsp)
movq 0x28(%rsp), %rdi
movq %r13, %rsi
leaq 0x15eeb(%rip), %rdx     # 0x287dc
movl $0x37, %ecx
leaq 0x15ef1(%rip), %r8      # 0x287ee
callq 0x2324c
movq %rax, 0x28(%rsp)
movq %r12, 0x18(%rsp)
movw $0x5e, (%rax,%rbp)
cmpl $0x10, %r15d
jb 0x129ae
movslq 0x10(%rsp), %r15
movl %ebx, %r12d
addq 0x38(%rsp), %r15
addq $0x2, %rbp
movzbl (%r15), %ecx
movq 0x60(%rsp), %rdx
movb (%rdx,%rcx), %bl
cmpq %r13, %rbp
jb 0x12990
movq %rbp, %rcx
shrq %rcx
orq %rbp, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %r13
shrq $0x10, %r13
orq %rdx, %r13
incq %r13
movq %rax, %rdi
movq %r13, %rsi
leaq 0x15e5d(%rip), %rdx     # 0x287dc
movl $0x37, %ecx
leaq 0x15e63(%rip), %r8      # 0x287ee
callq 0x2324c
movb %bl, -0x1(%rax,%rbp)
movb $0x0, (%rax,%rbp)
incq %r15
incq %rbp
decq %r12
jne 0x1292d
decq %rbp
movq %rbp, %r12
movq 0x58(%rsp), %rbx
movq %r12, 0x18(%rsp)
movq %r13, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movl %r14d, %r8d
addl %ebx, %r8d
xorl %edi, %edi
movq 0x38(%rsp), %rbp
movq 0xc8(%rsp), %r14
movq 0xe8(%rsp), %rsi
movq 0x50(%rsp), %r9
movl 0x34(%rsp), %r10d
movl 0x10(%rsp), %r11d
addl %r11d, %ebx
movl %ebx, %r11d
incq %r9
movslq (%rsi), %rcx
cmpq %rcx, %r9
jl 0x126ea
jmp 0x12a0d
xorl %edi, %edi
movl $0x0, 0x8(%rsp)
xorl %r8d, %r8d
movl %r8d, %r12d
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x12b09
movq (%r15), %rbx
leaq 0x1(%rbx), %rax
cmpq 0x8(%r15), %rax
jae 0x12a31
movq 0x28(%rsp), %rax
jmp 0x12a82
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq 0x28(%rsp), %rdi
leaq 0x15d6b(%rip), %rdx     # 0x287dc
leaq 0x15d76(%rip), %r8      # 0x287ee
movl $0x37, %ecx
callq 0x2324c
movq 0xf0(%rsp), %rcx
movq 0xb0(%rsp), %r13
movl 0x8(%rsp), %edx
movw $0x0, (%rax,%rbx)
addl %edx, %r12d
movl %r12d, (%rcx)
movq %rax, 0x40(%rsp)
cmpq %r13, 0xd0(%rsp)
setl %al
movl 0xc0(%rsp), %edi
cmpl $0x2, %edi
setb %cl
orb %al, %cl
jne 0x12aed
shrl %edi
movq 0x48(%rsp), %rax
addq %r14, %rax
decq %rax
negq %rdi
xorl %ecx, %ecx
movb (%r14), %dl
movb (%rax,%rcx), %sil
movb %sil, (%r14)
movb %dl, (%rax,%rcx)
incq %r14
decq %rcx
cmpq %rcx, %rdi
jne 0x12ad5
movq %rbp, %rdi
callq 0x3390
movq 0x40(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq