// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/cut_target.c
// Function: process_cns
static void process_cns(bam_header_t *h, int tid, int l, uint16_t *cns)
{
	int i, f[2][2], *prev, *curr, *swap_tmp, s;
	uint8_t *b; // backtrack array
	b = calloc(l, 1);
	f[0][0] = f[0][1] = 0;
	prev = f[0]; curr = f[1];
	// fill the backtrack matrix
	for (i = 0; i < l; ++i) {
		int c = (cns[i] == 0)? 0 : (cns[i]>>8 == 0)? 1 : 2;
		int tmp0, tmp1;
		// compute f[0]
		tmp0 = prev[0] + g_param.e[0][c] + g_param.p[0][0]; // (s[i+1],s[i])=(0,0)
		tmp1 = prev[1] + g_param.e[0][c] + g_param.p[1][0]; // (0,1)
		if (tmp0 > tmp1) curr[0] = tmp0, b[i] = 0;
		else curr[0] = tmp1, b[i] = 1;
		// compute f[1]
		tmp0 = prev[0] + g_param.e[1][c] + g_param.p[0][1]; // (s[i+1],s[i])=(1,0)
		tmp1 = prev[1] + g_param.e[1][c] + g_param.p[1][1]; // (1,1)
		if (tmp0 > tmp1) curr[1] = tmp0, b[i] |= 0<<1;
		else curr[1] = tmp1, b[i] |= 1<<1;
		// swap
		swap_tmp = prev; prev = curr; curr = swap_tmp;
	}
	// backtrack
	s = prev[0] > prev[1]? 0 : 1;
	for (i = l - 1; i > 0; --i) {
		b[i] |= s<<2;
		s = b[i]>>s&1;
	}
	// print
	for (i = 0, s = -1; i <= l; ++i) {
		if (i == l || ((b[i]>>2&3) == 0 && s >= 0)) {
			if (s >= 0) {
				int j;
				printf("%s:%d-%d\t0\t%s\t%d\t60\t%dM\t*\t0\t0\t", h->target_name[tid], s+1, i, h->target_name[tid], s+1, i-s);
				for (j = s; j < i; ++j) {
					int c = cns[j]>>8;
					if (c == 0) putchar('N');
					else putchar("ACGT"[c&3]);
				}
				putchar('\t');
				for (j = s; j < i; ++j)
					putchar(33 + (cns[j]>>8>>2));
				putchar('\n');
			}
			//if (s >= 0) printf("%s\t%d\t%d\t%d\n", h->target_name[tid], s, i, i - s);
			s = -1;
		} else if ((b[i]>>2&3) && s < 0) s = i;
	}
	free(b);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r15
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, 0x38(%rsp)
movslq %edx, %r13
movl $0x1, %esi
movq %r13, %rdi
callq 0x3610
movq $0x0, 0x40(%rsp)
movl %r14d, %r11d
testl %r13d, %r13d
jle 0x17db3
leaq 0x48(%rsp), %r10
leaq 0x40(%rsp), %rdi
xorl %ecx, %ecx
leaq 0x39091(%rip), %rsi     # 0x50d88
movq %r10, %rdx
movq %rdi, %r10
movzwl (%r15,%rcx,2), %edi
cmpl $0x100, %edi            # imm = 0x100
movl $0x2, %r8d
sbbl $0x0, %r8d
testl %edi, %edi
cmovel %edi, %r8d
movl (%rsi,%r8,4), %edi
movl (%r10), %r9d
addl %edi, %r9d
addl 0x39077(%rip), %r9d     # 0x50da0
addl 0x4(%r10), %edi
addl 0x39075(%rip), %edi     # 0x50da8
cmpl %edi, %r9d
cmovgl %r9d, %edi
setle (%rax,%rcx)
movl %edi, (%rdx)
movl 0xc(%rsi,%r8,4), %edi
movl (%r10), %r8d
addl %edi, %r8d
addl 0x39052(%rip), %r8d     # 0x50da4
addl 0x4(%r10), %edi
addl 0x39050(%rip), %edi     # 0x50dac
cmpl %edi, %r8d
jle 0x17d67
movl %r8d, 0x4(%rdx)
jmp 0x17d6e
movl %edi, 0x4(%rdx)
orb $0x2, (%rax,%rcx)
incq %rcx
movq %rdx, %rdi
cmpq %rcx, %r11
jne 0x17cf7
cmpl $0x1, %r14d
je 0x17db3
movl (%rdx), %ecx
cmpl 0x4(%rdx), %ecx
setle %cl
leaq 0x1(%r11), %rsi
leal (,%rcx,4), %edx
orb -0x2(%rax,%rsi), %dl
movb %dl, -0x2(%rax,%rsi)
movzbl %dl, %edx
movzbl %cl, %ecx
btl %ecx, %edx
setb %cl
decq %rsi
cmpq $0x2, %rsi
ja 0x17d8f
movq %rax, 0x8(%rsp)
testl %r14d, %r14d
js 0x17f20
movabsq $0x100000000, %r13      # imm = 0x100000000
movslq %ebp, %rax
movq %rax, 0x20(%rsp)
incl %r14d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq %r14, 0x30(%rsp)
movq %r11, 0x28(%rsp)
cmpq %r11, %rbp
jne 0x17dff
testl %ebx, %ebx
jns 0x17e1a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x17f14
movq 0x8(%rsp), %rax
movb (%rax,%rbp), %al
shrb $0x2, %al
testb $0x3, %al
jne 0x17f06
testl %ebx, %ebx
js 0x17f06
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leal 0x1(%rbx), %edx
movl %ebp, %eax
subl %ebx, %eax
movl %eax, (%rsp)
leaq 0x2a087(%rip), %rdi     # 0x41ec4
movl %ebp, %ecx
movq %rsi, %r8
movl %edx, %r9d
xorl %eax, %eax
callq 0x3060
movl %ebx, %r12d
cmpq %r12, %rbp
jbe 0x17ee1
leaq (%r15,%r12,2), %r14
movq %r12, 0x18(%rsp)
shlq $0x20, %r12
addq %r13, %r12
movq %r12, %r15
movq %r14, %rbx
movzwl (%rbx), %eax
movl $0x4e, %edi
cmpq $0x100, %rax            # imm = 0x100
jb 0x17e8f
shrl $0x8, %eax
andl $0x3, %eax
leaq 0x27357(%rip), %rcx     # 0x3f1e2
movsbl (%rax,%rcx), %edi
callq 0x31e0
movq %r15, %rax
sarq $0x20, %rax
addq $0x2, %rbx
addq %r13, %r15
cmpq %rbp, %rax
jl 0x17e6e
movl $0x9, %edi
callq 0x31e0
cmpq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r15
jbe 0x17eeb
movzwl (%r14), %edi
shrl $0xa, %edi
addl $0x21, %edi
callq 0x31e0
movq %r12, %rax
sarq $0x20, %rax
addq $0x2, %r14
addq %r13, %r12
cmpq %rbp, %rax
jl 0x17ebd
jmp 0x17eeb
movl $0x9, %edi
callq 0x31e0
movl $0xa, %edi
callq 0x31e0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r11
jmp 0x17f14
testl %ebx, %ebx
movl %ebp, %ecx
cmovnsl %ebx, %ecx
testb $0x3, %al
cmovel %ebx, %ecx
movl %ecx, %ebx
incq %rbp
cmpq %r14, %rbp
jne 0x17dec
movq 0x8(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3350