// File: /workspace/github_c_makefile_star2_O1/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, 0x58(%rsp)
testq %rcx, %rcx
je 0x11f50
movl $0x0, (%rcx)
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %rbp
movl 0xc0(%rsp), %ebx
testq %rax, %rax
je 0x11f72
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
testl %ebx, %ebx
setle %al
movq %rcx, %r14
subq %rbp, %r14
setle %dil
orb %al, %dil
jne 0x11fa0
movq 0xb0(%rsp), %r13
cmpq %r13, %rbp
setl %al
cmpq %r13, %rcx
setg %dil
testb %dil, %al
je 0x11fa7
xorl %eax, %eax
jmp 0x12739
movl %r9d, 0xc(%rsp)
movq %r8, 0x50(%rsp)
movq %r10, 0x68(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x60(%rsp)
movq 0xb8(%rsp), %rsi
leaq 0x38(%rsp), %r12
movq %r13, %rdi
movq %rbp, %rdx
movq %r12, %r8
callq 0x11344
movq %rbp, %rcx
movq %rax, %rbp
cmpq (%r12), %r14
jne 0x1272c
movl %ebx, %r9d
cmpq %r13, %rcx
movq 0xe8(%rsp), %r13
movl 0xc(%rsp), %r10d
jl 0x12072
cmpl $0x2, %ebx
jb 0x12036
movl %ebx, %eax
shrl %eax
movq 0xc8(%rsp), %rsi
leaq (%r9,%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 0x1201d
movq 0x38(%rsp), %rdx
cmpq $0x2, %rdx
jl 0x12072
leaq -0x1(%rbp), %rax
xorl %ecx, %ecx
movb (%rbp,%rcx), %sil
movb (%rax,%rdx), %dl
movb %dl, (%rbp,%rcx)
movq 0x38(%rsp), %rdx
movb %sil, (%rax,%rdx)
incq %rcx
movq 0x38(%rsp), %rdx
movq %rdx, %rsi
sarq %rsi
decq %rax
cmpq %rcx, %rsi
jg 0x12047
movq 0xe0(%rsp), %rbx
cmpq %r9, %r14
setne %al
testl %r10d, %r10d
setne %cl
orb %al, %cl
movq %rbp, 0x48(%rsp)
movq %r9, 0x70(%rsp)
jne 0x1211c
testq %r13, %r13
movl 0xc0(%rsp), %r14d
je 0x120d7
leaq 0x16429(%rip), %rsi     # 0x284d5
leaq 0x16461(%rip), %rcx     # 0x28514
movl $0x4, %edi
movl $0x90, %edx
callq 0x22d2b
movq %rax, 0x58(%rsp)
movl %r14d, %ecx
shll $0x4, %ecx
movl %ecx, (%rax)
movl $0x1, (%r13)
movl $0x0, (%rbx)
cmpl $0x2, %r14d
movl $0x1, %eax
cmovgel %r14d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0xc8(%rsp), %r12
movzbl (%rbp,%rcx), %esi
leaq (%rsi,%rsi,4), %rsi
movzbl (%r12,%rcx), %edi
addq %r15, %rsi
movsbl (%rdi,%rsi), %esi
addl %esi, %edx
movl %edx, (%rbx)
incq %rcx
cmpq %rcx, %rax
jne 0x120f6
jmp 0x122bb
movl 0xc0(%rsp), %edx
leal 0x1(%rdx), %eax
shrl %eax
movsbl (%r15), %ecx
imull %eax, %ecx
movl %ecx, %eax
subl 0x68(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x50(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x1634d(%rip), %xmm1    # 0x28498
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
subl 0x60(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
cvtsi2sdl 0x28(%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 0x38(%rsp), %ecx
subl %edx, %ecx
movl %ecx, %ebp
negl %ebp
cmovsl %ecx, %ebp
addl %ebp, %eax
incl %eax
shrl %eax
cmpl %r10d, %eax
cmovgel %r10d, %eax
addl $0x3, %ebp
cmpl %ebp, %eax
cmovgl %eax, %ebp
cmpl $0x4, 0x1ec97(%rip)     # 0x30e40
jl 0x12266
leaq 0x1636d(%rip), %rdi     # 0x28523
movl %ebp, %esi
xorl %eax, %eax
callq 0x3060
leaq 0x16375(%rip), %rdi     # 0x2853b
xorl %eax, %eax
callq 0x3060
cmpq $0x0, 0x38(%rsp)
leaq 0x16372(%rip), %r14     # 0x2854c
movq 0x48(%rsp), %r13
jle 0x121fe
xorl %r12d, %r12d
movzbl (%r13,%r12), %eax
movsbl (%rax,%r14), %edi
callq 0x34c0
incq %r12
cmpq %r12, 0x38(%rsp)
jg 0x121e4
movl $0xa, %edi
callq 0x34c0
leaq 0x16343(%rip), %rdi     # 0x28552
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x3060
movl 0xc0(%rsp), %eax
cmpl $0x2, %eax
movl $0x1, %r13d
cmovgel %eax, %r13d
movq 0xc8(%rsp), %rbx
movzbl (%rbx,%r12), %eax
movsbl (%rax,%r14), %edi
callq 0x34c0
incq %r12
cmpq %r12, %r13
jne 0x12235
movl $0xa, %edi
callq 0x34c0
movq 0xe8(%rsp), %r13
movq 0xe0(%rsp), %rbx
movl 0x38(%rsp), %edx
subq $0x8, %rsp
leaq 0x60(%rsp), %rax
movl 0xc8(%rsp), %edi
movq 0xd0(%rsp), %r12
movq %r12, %rsi
movq 0x50(%rsp), %rcx
movl $0x16, %r8d
movq %r15, %r9
pushq %rax
pushq %r13
pushq %rbp
movq %rcx, %rbp
pushq 0x70(%rsp)
pushq 0x90(%rsp)
pushq 0x58(%rsp)
pushq 0x98(%rsp)
callq 0x44e8
addq $0x40, %rsp
movl %eax, (%rbx)
cmpq $0x0, 0xf0(%rsp)
sete %al
testq %r13, %r13
sete %cl
orb %al, %cl
jne 0x126dc
movslq (%r13), %rax
shlq $0x2, %rax
movq 0xb0(%rsp), %rcx
cmpq %rcx, 0xd0(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x16244(%rip), %rax     # 0x2854c
leaq 0x16254(%rip), %r14     # 0x28563
cmovlq %rax, %r14
movl (%r13), %eax
testl %eax, %eax
jle 0x1262e
xorl %edx, %edx
movl $0x0, 0x34(%rsp)
movl $0x0, 0x50(%rsp)
xorl %edi, %edi
movl $0x0, 0xc(%rsp)
movl $0x0, 0x44(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq %rdx, 0x28(%rsp)
movl (%rcx,%rdx,4), %r15d
movl %r15d, %ecx
andl $0xf, %ecx
movl %r15d, %ebx
shrl $0x4, %ebx
cmpl $0x2, %ecx
movq %rbx, 0x60(%rsp)
je 0x124a3
cmpl $0x1, %ecx
je 0x12491
testl %ecx, %ecx
jne 0x12499
cmpl $0x10, %r15d
jb 0x12472
movslq 0x44(%rsp), %rcx
movslq 0xc(%rsp), %rbp
addq 0xc8(%rsp), %rcx
addq 0x48(%rsp), %rbp
xorl %r15d, %r15d
movq %rcx, 0x68(%rsp)
movb (%rcx,%r15), %al
cmpb (%rbp,%r15), %al
jne 0x123bc
incl %edi
jmp 0x12466
leaq 0x10(%rsp), %rsi
callq 0x12748
movzbl (%rbp,%r15), %eax
movq %r14, %r13
movb (%r14,%rax), %bl
movq 0x10(%rsp), %r12
leaq 0x1(%r12), %r14
cmpq 0x18(%rsp), %r14
jb 0x1243f
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, 0x18(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x1631f(%rip), %rdx     # 0x28748
movl $0x37, %ecx
leaq 0x16325(%rip), %r8      # 0x2875a
callq 0x22d64
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %r14, 0x10(%rsp)
movb %bl, (%rax,%r12)
movb $0x0, 0x1(%rax,%r12)
incl 0x50(%rsp)
xorl %edi, %edi
movq %r13, %r14
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %rcx
incq %r15
cmpq %r15, %rbx
jne 0x123aa
addl %ebx, 0x44(%rsp)
addl %ebx, 0xc(%rsp)
movq 0x48(%rsp), %rbp
movq 0xc8(%rsp), %r12
movq 0xe8(%rsp), %r13
jmp 0x12499
addl %ebx, 0x44(%rsp)
addl %ebx, 0x34(%rsp)
movq 0x28(%rsp), %rdx
jmp 0x1261c
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
je 0x12614
decl %eax
cltq
cmpq %rax, %rdx
jge 0x1260f
leaq 0x10(%rsp), %rsi
callq 0x12748
movq 0x10(%rsp), %rbp
leaq 0x1(%rbp), %r12
cmpq 0x18(%rsp), %r12
jb 0x12533
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, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x1622b(%rip), %rdx     # 0x28748
movl $0x37, %ecx
leaq 0x16231(%rip), %r8      # 0x2875a
callq 0x22d64
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %r12, 0x10(%rsp)
movw $0x5e, (%rax,%rbp)
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %rax
cmpl $0x10, %r15d
jb 0x125e5
movslq 0xc(%rsp), %r15
movl %ebx, %r12d
addq 0x48(%rsp), %r15
addq $0x2, %rbp
movzbl (%r15), %ecx
movb (%r14,%rcx), %bl
cmpq %r13, %rbp
jb 0x125c7
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 0x16192(%rip), %rdx     # 0x28748
movl $0x37, %ecx
leaq 0x16198(%rip), %r8      # 0x2875a
callq 0x22d64
movb %bl, -0x1(%rax,%rbp)
movb $0x0, (%rax,%rbp)
incq %r15
incq %rbp
decq %r12
jne 0x12568
decq %rbp
movq %rbp, %r12
movq 0x60(%rsp), %rbx
movq %r12, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movq %rax, 0x20(%rsp)
addl %ebx, 0x34(%rsp)
xorl %edi, %edi
movq 0x48(%rsp), %rbp
movq 0xc8(%rsp), %r12
movq 0xe8(%rsp), %r13
movq 0x28(%rsp), %rdx
addl 0xc(%rsp), %ebx
movl %ebx, 0xc(%rsp)
incq %rdx
movslq (%r13), %rax
cmpq %rax, %rdx
jl 0x12343
jmp 0x12640
xorl %edi, %edi
movl $0x0, 0x50(%rsp)
movl $0x0, 0x34(%rsp)
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x12748
movq (%r15), %rbx
leaq 0x1(%rbx), %r14
cmpq 0x8(%r15), %r14
jb 0x126b5
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, 0x18(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x160a9(%rip), %rdx     # 0x28748
leaq 0x160b4(%rip), %r8      # 0x2875a
movl $0x37, %ecx
callq 0x22d64
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %r14, 0x10(%rsp)
movw $0x0, (%rax,%rbx)
movl 0x34(%rsp), %edx
addl 0x50(%rsp), %edx
movq 0xf0(%rsp), %rcx
movl %edx, (%rcx)
movq %rax, 0x58(%rsp)
movq 0xb0(%rsp), %rax
cmpq %rax, 0xd0(%rsp)
setl %al
movl 0xc0(%rsp), %edi
cmpl $0x2, %edi
setb %cl
orb %al, %cl
jne 0x1272c
shrl %edi
movq 0x70(%rsp), %rax
addq %r12, %rax
decq %rax
negq %rdi
xorl %ecx, %ecx
movb (%r12), %dl
movb (%rax,%rcx), %sil
movb %sil, (%r12)
movb %dl, (%rax,%rcx)
incq %r12
decq %rcx
cmpq %rcx, %rdi
jne 0x12712
movq %rbp, %rdi
callq 0x3390
movq 0x58(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq