// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movl %edx, -0x38(%rbp)
movl %ecx, -0x3c(%rbp)
movl %r8d, -0x40(%rbp)
movl %r9d, -0x44(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $0x0, 0x48(%rbp)
je 0x18fc3
movq 0x48(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x50(%rbp)
je 0x18fd4
movq 0x50(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rbp)
jle 0x18ff8
movq 0x30(%rbp), %rax
cmpq 0x38(%rbp), %rax
jge 0x18ff8
movq 0x30(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x19005
movq 0x38(%rbp), %rax
cmpq 0x10(%rbp), %rax
jle 0x19005
movq $0x0, -0x28(%rbp)
jmp 0x19801
movq 0x10(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq 0x30(%rbp), %rdx
movq 0x38(%rbp), %rcx
leaq -0x70(%rbp), %r8
callq 0x17e20
movq %rax, -0x60(%rbp)
movq 0x38(%rbp), %rax
subq 0x30(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x19035
jmp 0x197f0
movq 0x30(%rbp), %rax
cmpq 0x10(%rbp), %rax
jl 0x1910f
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl 0x20(%rbp), %ecx
sarl %ecx
cmpl %ecx, %eax
jge 0x190a3
movq 0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x51(%rbp)
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %ecx
subl $0x1, %ecx
subl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x51(%rbp), %dl
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %ecx
subl $0x1, %ecx
subl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1904a
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq -0x70(%rbp), %rcx
sarq %rcx
cmpq %rcx, %rax
jge 0x1910d
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x51(%rbp)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
movslq -0x64(%rbp), %rdx
subq %rdx, %rcx
movb (%rax,%rcx), %dl
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x51(%rbp), %dl
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
movslq -0x64(%rbp), %rsi
subq %rsi, %rcx
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x190aa
jmp 0x1910f
movslq 0x20(%rbp), %rax
movq 0x38(%rbp), %rcx
subq 0x30(%rbp), %rcx
cmpq %rcx, %rax
jne 0x191c8
cmpl $0x0, -0x44(%rbp)
jne 0x191c8
cmpq $0x0, 0x48(%rbp)
je 0x1916f
movl $0x4, %edi
leaq 0x21284(%rip), %rsi     # 0x3a3c5
movl $0x90, %edx
leaq 0x212b7(%rip), %rcx     # 0x3a404
callq 0x31ca0
movq %rax, -0x50(%rbp)
movl 0x20(%rbp), %ecx
shll $0x4, %ecx
orl $0x0, %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
movq 0x48(%rbp), %rax
movl $0x1, (%rax)
movl $0x0, -0x64(%rbp)
movq 0x40(%rbp), %rax
movl $0x0, (%rax)
movl -0x64(%rbp), %eax
cmpl 0x20(%rbp), %eax
jge 0x191c3
movq -0x30(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq -0x64(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
imull $0x5, %ecx, %ecx
movq 0x28(%rbp), %rdx
movslq -0x64(%rbp), %rsi
movzbl (%rdx,%rsi), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ecx
movq 0x40(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19180
jmp 0x1946a
movl 0x20(%rbp), %eax
addl $0x1, %eax
sarl %eax
movq -0x30(%rbp), %rcx
movsbl (%rcx), %ecx
imull %ecx, %eax
subl -0x3c(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x40(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x21196(%rip), %xmm1    # 0x3a388
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x9c(%rbp)
movl 0x20(%rbp), %eax
addl $0x1, %eax
sarl %eax
movq -0x30(%rbp), %rcx
movsbl (%rcx), %ecx
imull %ecx, %eax
subl -0x34(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x38(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x2115e(%rip), %xmm1    # 0x3a388
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xa0(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jle 0x19254
movl -0x9c(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x19260
movl -0xa0(%rbp), %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x1, -0x98(%rbp)
jle 0x19283
movl -0x98(%rbp), %eax
movl %eax, -0xcc(%rbp)
jmp 0x19290
movl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x19290
movl -0xcc(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movq -0x70(%rbp), %rcx
movslq 0x20(%rbp), %rdx
subq %rdx, %rcx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
addl $0x1, %eax
sarl %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x192da
movl -0x94(%rbp), %eax
movl %eax, -0xd0(%rbp)
jmp 0x192e3
movl -0x44(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x70(%rbp), %rax
movslq 0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
addl $0x3, %eax
movl %eax, -0xa4(%rbp)
movl -0x94(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x19326
movl -0x94(%rbp), %eax
movl %eax, -0xd4(%rbp)
jmp 0x19332
movl -0xa4(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x94(%rbp)
cmpl $0x4, 0x27aeb(%rip)     # 0x40e30
jl 0x19403
movl -0x94(%rbp), %esi
leaq 0x210bb(%rip), %rdi     # 0x3a413
movb $0x0, %al
callq 0x3060
leaq 0x210c5(%rip), %rdi     # 0x3a42b
movb $0x0, %al
callq 0x3060
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x70(%rbp), %rax
jge 0x193a8
movq -0x60(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x210a8(%rip), %rax     # 0x3a43c
movsbl (%rax,%rcx), %edi
callq 0x34a0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19374
movl $0xa, %edi
callq 0x34a0
leaq 0x21089(%rip), %rdi     # 0x3a442
movb $0x0, %al
callq 0x3060
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl 0x20(%rbp), %eax
jge 0x193f9
movq 0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x21057(%rip), %rax     # 0x3a43c
movsbl (%rax,%rcx), %edi
callq 0x34a0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x193c7
movl $0xa, %edi
callq 0x34a0
movl 0x20(%rbp), %edi
movq 0x28(%rbp), %rsi
movq -0x70(%rbp), %rax
movl %eax, %edx
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %r9
movl -0x34(%rbp), %r12d
movl -0x38(%rbp), %r15d
movl -0x3c(%rbp), %r14d
movl -0x40(%rbp), %ebx
movl -0x94(%rbp), %r11d
movq 0x48(%rbp), %r10
movl $0x16, %r8d
leaq -0x50(%rbp), %rax
movl %r12d, (%rsp)
movl %r15d, 0x8(%rsp)
movl %r14d, 0x10(%rsp)
movl %ebx, 0x18(%rsp)
movl %r11d, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x35750
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x50(%rbp)
je 0x19782
cmpq $0x0, 0x48(%rbp)
je 0x19782
movl $0x0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movq 0x48(%rbp), %rax
movl (%rax), %eax
shll $0x2, %eax
cltq
movq %rax, -0x80(%rbp)
movq %rax, -0x88(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x30(%rbp), %rdx
movq 0x10(%rbp), %rsi
leaq 0x20f92(%rip), %rax     # 0x3a453
leaq 0x20f74(%rip), %rcx     # 0x3a43c
cmpq %rsi, %rdx
cmovlq %rcx, %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb0(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xa8(%rbp), %eax
movq 0x48(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x19748
movq -0x78(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0xc0(%rbp)
movq -0x50(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc0(%rbp)
jne 0x19635
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x1960c
movq 0x28(%rbp), %rax
movl -0xac(%rbp), %ecx
addl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x60(%rbp), %rcx
movl -0xb0(%rbp), %edx
addl -0x64(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x195ed
movl -0xb4(%rbp), %edi
leaq -0x88(%rbp), %rsi
callq 0x19820
movq -0x90(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0xb0(%rbp), %edx
addl -0x64(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %edi
leaq -0x88(%rbp), %rsi
callq 0x19a30
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
movl $0x0, -0xb4(%rbp)
jmp 0x195fc
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x195fe
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1955a
movl -0xc4(%rbp), %eax
addl -0xac(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xc4(%rbp), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x19732
cmpl $0x2, -0xc0(%rbp)
jne 0x19701
cmpl $0x0, -0xa8(%rbp)
jle 0x196ed
movl -0xa8(%rbp), %eax
movq 0x48(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x196ed
movl -0xb4(%rbp), %edi
leaq -0x88(%rbp), %rsi
callq 0x19820
movl $0x5e, %edi
leaq -0x88(%rbp), %rsi
callq 0x19a30
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x196d1
movq -0x90(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0xb0(%rbp), %edx
addl -0x64(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %edi
leaq -0x88(%rbp), %rsi
callq 0x19a30
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19690
movl $0x0, -0xb4(%rbp)
movl -0xc4(%rbp), %eax
addl -0xbc(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xc4(%rbp), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x19730
cmpl $0x1, -0xc0(%rbp)
jne 0x1972e
movl -0xc4(%rbp), %eax
addl -0xac(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xc4(%rbp), %eax
addl -0xbc(%rbp), %eax
movl %eax, -0xbc(%rbp)
jmp 0x19730
jmp 0x19732
jmp 0x19734
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x194fe
movl -0xb4(%rbp), %edi
leaq -0x88(%rbp), %rsi
callq 0x19820
xorl %edi, %edi
leaq -0x88(%rbp), %rsi
callq 0x19a30
movl -0xb8(%rbp), %ecx
addl -0xbc(%rbp), %ecx
movq 0x50(%rbp), %rax
movl %ecx, (%rax)
movq -0x78(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x30(%rbp), %rax
cmpq 0x10(%rbp), %rax
jl 0x197ee
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl 0x20(%rbp), %ecx
sarl %ecx
cmpl %ecx, %eax
jge 0x197ec
movq 0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x51(%rbp)
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %ecx
subl $0x1, %ecx
subl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x51(%rbp), %dl
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %ecx
subl $0x1, %ecx
subl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19793
jmp 0x197ee
jmp 0x197f0
movq -0x60(%rbp), %rdi
callq 0x3380
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)