// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %rbx
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, 0x40(%rsp)
movslq %edx, %r13
movl $0x1, %esi
movq %r13, %rdi
callq 0x3610
movq $0x0, 0x10(%rsp)
movl %r14d, %r11d
testl %r13d, %r13d
jle 0x17a95
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
leaq 0x3837a(%rip), %rsi     # 0x4fd88
movq %rcx, %r10
movq %rdi, %rcx
movzwl (%rbx,%rdx,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 (%rcx), %r9d
addl %edi, %r9d
addl 0x38361(%rip), %r9d     # 0x4fda0
addl 0x4(%rcx), %edi
addl 0x38360(%rip), %edi     # 0x4fda8
cmpl %edi, %r9d
cmovgl %r9d, %edi
setle (%rax,%rdx)
movl %edi, (%r10)
movl 0xc(%rsi,%r8,4), %edi
movl (%rcx), %r8d
addl %edi, %r8d
addl 0x3833c(%rip), %r8d     # 0x4fda4
addl 0x4(%rcx), %edi
addl 0x3833b(%rip), %edi     # 0x4fdac
cmpl %edi, %r8d
jle 0x17a7c
movl %r8d, 0x4(%r10)
jmp 0x17a84
movl %edi, 0x4(%r10)
orb $0x2, (%rax,%rdx)
incq %rdx
movq %r10, %rdi
cmpq %rdx, %r11
jne 0x17a0e
jmp 0x17a9a
leaq 0x10(%rsp), %r10
movq %rbx, 0x8(%rsp)
cmpl $0x2, %r14d
jl 0x17ada
movl (%r10), %edx
xorl %ecx, %ecx
cmpl 0x4(%r10), %edx
setle %cl
leaq 0x1(%r11), %rdi
movzbl -0x2(%rax,%rdi), %edx
movzbl %cl, %esi
shll $0x2, %esi
orl %edx, %esi
movb %sil, -0x2(%rax,%rdi)
xorl %edx, %edx
btl %ecx, %esi
setb %dl
decq %rdi
movl %edx, %ecx
cmpq $0x2, %rdi
jg 0x17ab5
testl %r14d, %r14d
js 0x17c3e
movabsq $0x100000000, %r13      # imm = 0x100000000
movslq %ebp, %rcx
movq %rcx, 0x20(%rsp)
incl %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %r14, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq %r11, 0x28(%rsp)
cmpq %r11, %rbp
je 0x17b2c
movb (%rax,%rbp), %cl
shrb $0x2, %cl
testb $0x3, %cl
jne 0x17c1f
testl %r15d, %r15d
js 0x17c1f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %r15d, %r15d
js 0x17c2f
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leal 0x1(%r15), %edx
movl %ebp, %eax
subl %r15d, %eax
movl %eax, (%rsp)
leaq 0x29325(%rip), %rdi     # 0x40e84
movl %ebp, %ecx
movq %rsi, %r8
movl %edx, %r9d
xorl %eax, %eax
callq 0x3060
movl %r15d, %r12d
cmpq %r12, %rbp
jbe 0x17bc3
movq 0x8(%rsp), %rax
leaq (%rax,%r12,2), %r14
movq %r12, %r15
shlq $0x20, %r15
addq %r13, %r15
movzwl (%r14), %eax
movl $0x4e, %edi
cmpq $0x100, %rax            # imm = 0x100
jb 0x17bab
shrl $0x8, %eax
andl $0x3, %eax
leaq 0x2661b(%rip), %rcx     # 0x3e1c2
movsbl (%rax,%rcx), %edi
callq 0x31e0
movq %r15, %rax
sarq $0x20, %rax
addq $0x2, %r14
addq %r13, %r15
cmpq %rbp, %rax
jl 0x17b89
movl $0x9, %edi
callq 0x31e0
cmpq %r12, %rbp
jbe 0x17c04
movq 0x8(%rsp), %rax
leaq (%rax,%r12,2), %r14
shlq $0x20, %r12
addq %r13, %r12
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 0x17be2
movl $0xa, %edi
callq 0x31e0
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %r11
jmp 0x17c2f
testl %r15d, %r15d
movl %ebp, %ebx
cmovnsl %r15d, %ebx
testb $0x3, %cl
cmovel %r15d, %ebx
incq %rbp
movl %ebx, %r15d
cmpq %r14, %rbp
jne 0x17b0f
movq %rax, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3350