// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_tview.c
// Function: tv_pl_func
int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
{
	tview_t *tv = (tview_t*)data;
	int i, j, c, rb, attr, max_ins = 0;
	uint32_t call = 0;
	if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
	// print referece
	rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
	for (i = tv->last_pos + 1; i < pos; ++i) {
		if (i%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", i+1);
		c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
		mvaddch(1, tv->ccol++, c);
	}
	if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
	// print consensus
	call = bam_maqcns_call(n, pl, tv->bmc);
	attr = A_UNDERLINE;
	c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
	i = (call>>8&0xff)/10+1;
	if (i > 4) i = 4;
	attr |= COLOR_PAIR(i);
	if (c == toupper(rb)) c = '.';
	attron(attr);
	mvaddch(2, tv->ccol, c);
	attroff(attr);
	if(tv->ins) {
		// calculate maximum insert
		for (i = 0; i < n; ++i) {
			const bam_pileup1_t *p = pl + i;
			if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
		}
	}
	// core loop
	for (j = 0; j <= max_ins; ++j) {
		for (i = 0; i < n; ++i) {
			const bam_pileup1_t *p = pl + i;
			int row = TV_MIN_ALNROW + p->level - tv->row_shift;
			if (j == 0) {
				if (!p->is_del) {
					if (tv->base_for == TV_BASE_COLOR_SPACE && 
							(c = bam_aux_getCSi(p->b, p->qpos))) {
						c = bam_aux_getCSi(p->b, p->qpos);
						// assume that if we found one color, we will be able to get the color error
						if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
					} else {
						if (tv->show_name) {
							char *name = bam1_qname(p->b);
							c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
						} else {
							c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
							if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
						}
					}
				} else c = '*';
			} else { // padding
				if (j > p->indel) c = '*';
				else { // insertion
					if (tv->base_for ==  TV_BASE_NUCL) {
						if (tv->show_name) {
							char *name = bam1_qname(p->b);
							c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
						} else {
							c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
							if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
						}
					} else {
						c = bam_aux_getCSi(p->b, p->qpos + j);
						if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
					}
				}
			}
			if (row > TV_MIN_ALNROW && row < tv->mrow) {
				int x;
				attr = 0;
				if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
						|| (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
				if (tv->color_for == TV_COLOR_BASEQ) {
					x = bam1_qual(p->b)[p->qpos]/10 + 1;
					if (x > 4) x = 4;
					attr |= COLOR_PAIR(x);
				} else if (tv->color_for == TV_COLOR_MAPQ) {
					x = p->b->core.qual/10 + 1;
					if (x > 4) x = 4;
					attr |= COLOR_PAIR(x);
				} else if (tv->color_for == TV_COLOR_NUCL) {
					x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
					attr |= COLOR_PAIR(x);
				} else if(tv->color_for == TV_COLOR_COL) {
					x = 0;
					switch(bam_aux_getCSi(p->b, p->qpos)) {
						case '0': x = 0; break;
						case '1': x = 1; break;
						case '2': x = 2; break;
						case '3': x = 3; break;
						case '4': x = 4; break;
						default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
					}
					x+=5;
					attr |= COLOR_PAIR(x);
				} else if(tv->color_for == TV_COLOR_COLQ) {
					x = bam_aux_getCQi(p->b, p->qpos);
					if(0 == x) x = bam1_qual(p->b)[p->qpos];
					x = x/10 + 1;
					if (x > 4) x = 4;
					attr |= COLOR_PAIR(x);
				}
				attron(attr);
				mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
				attroff(attr);
			}
		}
		c = j? '*' : rb;
		if (c == '*') {
			attr = COLOR_PAIR(8);
			attron(attr);
			mvaddch(1, tv->ccol++, c);
			attroff(attr);
		} else mvaddch(1, tv->ccol++, c);
	}
	tv->last_pos = pos;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, %rax
movq %rsi, 0x18(%rsp)
subl 0x3c(%r8), %eax
jb 0x5a1a
movq %r8, %rbx
movq %rcx, %r14
movl 0x50(%r8), %ecx
cmpl 0x4(%r8), %ecx
jg 0x5a1a
movq 0x78(%rbx), %rcx
movl $0x4e, 0x8(%rsp)
testq %rcx, %rcx
je 0x52a4
cmpl 0x68(%rbx), %eax
jae 0x52a4
movl %eax, %eax
movsbl (%rcx,%rax), %eax
movl %eax, 0x8(%rsp)
movl 0x54(%rbx), %r12d
incl %r12d
cmpl 0x18(%rsp), %r12d
jae 0x5342
movl $0x19999998, %r13d      # imm = 0x19999998
movq 0x28d15(%rip), %r15     # 0x2dfd8
imull $0xcccccccd, %r12d, %eax # imm = 0xCCCCCCCD
addl %r13d, %eax
rorl %eax
cmpl %r13d, %eax
ja 0x52f6
movl 0x4(%rbx), %eax
movl 0x50(%rbx), %esi
subl %esi, %eax
cmpl $0xa, %eax
jl 0x52f6
leal 0x1(%r12), %ecx
xorl %edi, %edi
leaq 0x1ddd1(%rip), %rdx     # 0x230c0
xorl %eax, %eax
callq 0x3260
movq 0x78(%rbx), %rax
testq %rax, %rax
je 0x530e
movl %r12d, %ecx
subl 0x3c(%rbx), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ebp
jmp 0x5313
movl $0x4e, %ebp
movq (%r15), %rdi
movl 0x50(%rbx), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x50(%rbx)
movl $0x1, %esi
callq 0x3490
cmpl $-0x1, %eax
je 0x5338
movq (%r15), %rdi
movl %ebp, %esi
callq 0x3300
incl %r12d
cmpl %r12d, 0x18(%rsp)
jne 0x52c3
imull $0xcccccccd, 0x18(%rsp), %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0x5378
movl 0x4(%rbx), %eax
movl 0x50(%rbx), %esi
subl %esi, %eax
cmpl $0xa, %eax
jl 0x5378
movq 0x18(%rsp), %rax
leal 0x1(%rax), %ecx
leaq 0x1dd51(%rip), %rdx     # 0x230c0
xorl %edi, %edi
xorl %eax, %eax
callq 0x3260
movq 0x48(%rbx), %rdx
movl 0xc(%rsp), %edi
movq %r14, %rsi
callq 0x7e89
movzbl %ah, %ecx
shrl $0x1c, %eax
leaq 0x1dd2f(%rip), %rdx     # 0x230c4
movsbl (%rax,%rdx), %r12d
imull $0xcd, %ecx, %eax
shrl $0xb, %eax
cmpb $0x3, %al
movl $0x3, %ebp
cmovbl %eax, %ebp
shll $0x8, %ebp
addl $0x20100, %ebp          # imm = 0x20100
movl 0x8(%rsp), %edi
callq 0x3100
movl %eax, %r15d
movq 0x28c0f(%rip), %r13     # 0x2dfd8
movq (%r13), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x35c0
movq (%r13), %rdi
movl 0x50(%rbx), %edx
movl $0x2, %esi
callq 0x3490
cmpl $-0x1, %eax
je 0x5401
cmpl %r12d, %r15d
movl $0x2e, %esi
cmovnel %r12d, %esi
movq (%r13), %rdi
callq 0x3300
movq (%r13), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x30d0
cmpl $0x0, 0x6c(%rbx)
sete %al
movl 0xc(%rsp), %edx
testl %edx, %edx
setle %cl
movl $0x1, %ebp
orb %al, %cl
movl %edx, %eax
jne 0x5457
leaq (,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %ebp, %ebp
movl %ebp, %esi
movl 0xc(%r14,%rdx), %edi
cmpl %edi, %ebp
movl %edi, %ebp
cmovgl %esi, %ebp
testl %edi, %edi
cmovlel %esi, %ebp
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x5439
incl %ebp
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
xorl %r8d, %r8d
movl %ebp, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
movq %r8, 0x10(%rsp)
jle 0x597a
xorl %r12d, %r12d
movl 0x10(%r14,%r12), %r13d
subl 0x58(%rbx), %r13d
movl $0x2a, %r15d
testl %r8d, %r8d
je 0x54ea
cmpl 0xc(%r14,%r12), %r8d
jg 0x56bd
cmpl $0x0, 0x5c(%rbx)
je 0x557f
movq (%r14,%r12), %rdi
movl 0x8(%r14,%r12), %esi
addl %r8d, %esi
movq %r8, %rbp
callq 0xe288
movsbl %al, %r15d
cmpl $0x0, 0x64(%rbx)
je 0x55bb
movq (%r14,%r12), %rdi
movl 0x8(%r14,%r12), %esi
addl %ebp, %esi
callq 0xe39d
cmpb $0x2d, %al
movq %rbp, %r8
je 0x554a
jmp 0x56bd
testb $0x1, 0x14(%r14,%r12)
jne 0x56bd
cmpl $0x1, 0x5c(%rbx)
jne 0x556e
movq (%r14,%r12), %rdi
movl 0x8(%r14,%r12), %esi
callq 0xe288
movq 0x10(%rsp), %r8
testb %al, %al
je 0x556e
movq (%r14,%r12), %rdi
movl 0x8(%r14,%r12), %esi
callq 0xe288
movsbl %al, %r15d
cmpl $0x0, 0x64(%rbx)
je 0x56b8
movq (%r14,%r12), %rdi
movl 0x8(%r14,%r12), %esi
callq 0xe39d
cmpb $0x2d, %al
movq 0x10(%rsp), %r8
jne 0x56bd
movq (%r14,%r12), %rax
xorl %ecx, %ecx
movabsq $0x1000000000, %rdx     # imm = 0x1000000000
testq %rdx, 0x8(%rax)
sete %cl
leal 0x2c(,%rcx,2), %r15d
jmp 0x56bd
cmpl $0x0, 0x74(%rbx)
je 0x55c3
movq (%r14,%r12), %rax
movslq 0x8(%r14,%r12), %rcx
jmp 0x5598
cmpl $0x0, 0x74(%rbx)
je 0x565f
movq (%r14,%r12), %rax
movslq 0x8(%r14,%r12), %rdx
movslq %r8d, %rcx
addq %rdx, %rcx
leal 0x1(%rcx), %edx
movzbl 0xb(%rax), %esi
movl $0x20, %r15d
cmpl %esi, %edx
jge 0x56bd
movq 0x30(%rax), %rax
movsbl (%rax,%rcx), %r15d
jmp 0x56bd
movq %rbp, %r8
jmp 0x56bd
leaq 0x29916(%rip), %rax     # 0x2eee0
movq (%rax), %rax
movq (%r14,%r12), %rcx
movq 0x8(%rcx), %rbp
movq %rbp, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rcx), %rdx
movl %ebp, %ecx
shrl $0x18, %ecx
addq %rdx, %rcx
movl 0x8(%r14,%r12), %edx
movl %edx, %esi
shrl $0x1f, %esi
addl %edx, %esi
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
movzbl (%rdi,%rcx), %edi
andl $0x3e, %esi
subl %esi, %edx
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
shrl %cl, %edi
andl $0xf, %edi
movsbl (%rax,%rdi), %r15d
cmpl $0x0, 0x64(%rbx)
je 0x56bd
movl %r15d, %edi
callq 0x3100
movl %eax, 0x24(%rsp)
movl 0x8(%rsp), %edi
callq 0x3100
movq 0x10(%rsp), %r8
cmpl %eax, 0x24(%rsp)
jne 0x56bd
xorl %eax, %eax
movabsq $0x1000000000, %rcx     # imm = 0x1000000000
testq %rcx, %rbp
sete %al
leal 0x2c(,%rax,2), %r15d
jmp 0x56bd
movq (%r14,%r12), %rax
movq 0x8(%rax), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rax), %rdx
shrl $0x18, %ecx
addq %rdx, %rcx
movl 0x8(%r14,%r12), %eax
addl %r8d, %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
movl %edx, %esi
sarl %esi
movslq %esi, %rsi
movzbl (%rsi,%rcx), %esi
andl $0x3e, %edx
subl %edx, %eax
shll $0x2, %eax
movb $0x4, %cl
subb %al, %cl
shrl %cl, %esi
leaq 0x29835(%rip), %rax     # 0x2eee0
movq (%rax), %rax
andl $0xf, %esi
movsbl (%rax,%rsi), %r15d
jmp 0x56bd
movq 0x10(%rsp), %r8
addl $0x2, %r13d
cmpl $0x3, %r13d
jl 0x596b
cmpl (%rbx), %r13d
jge 0x596b
movq (%r14,%r12), %rdi
movq 0x8(%rdi), %rax
movq %rax, %rcx
movabsq $0x300000000, %rdx      # imm = 0x300000000
andq %rdx, %rcx
movq %rax, %rbp
shrq $0x28, %rbp
andl $0x1, %ebp
shll $0x11, %ebp
movabsq $0x100000000, %rdx      # imm = 0x100000000
cmpq %rdx, %rcx
movl $0x20000, %ecx          # imm = 0x20000
cmovel %ecx, %ebp
movl 0x60(%rbx), %ecx
cmpq $0x4, %rcx
ja 0x58f0
leaq 0x1d8e2(%rip), %rdx     # 0x23004
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x10, %eax
movzbl %al, %eax
imull $0xcd, %eax, %eax
shrl $0xb, %eax
cmpb $0x3, %al
movl $0x3, %ecx
cmovael %ecx, %eax
shll $0x8, %eax
addl %eax, %ebp
addl $0x100, %ebp            # imm = 0x100
jmp 0x58f0
movl 0x8(%r14,%r12), %esi
callq 0xe2d7
testb %al, %al
je 0x5889
movsbl %al, %eax
jmp 0x58c4
movq %rax, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%rdi), %rcx
shrl $0x18, %eax
addq %rcx, %rax
movl 0x8(%r14,%r12), %edx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
movl %ecx, %esi
sarl %esi
movslq %esi, %rsi
movzbl (%rsi,%rax), %eax
andl $0x3e, %ecx
subl %ecx, %edx
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
shrl %cl, %eax
andl $0xf, %eax
leaq 0x28bae(%rip), %rcx     # 0x2e360
movzbl (%rcx,%rax), %eax
jmp 0x587f
movl 0x8(%r14,%r12), %esi
callq 0xe288
addb $-0x30, %al
cmpb $0x5, %al
jae 0x582f
movzbl %al, %eax
shll $0x8, %eax
addl $0x500, %eax            # imm = 0x500
jmp 0x58ec
movq %rax, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%rdi), %rcx
shrl $0x18, %eax
movl 0x10(%rdi), %edx
movl %edx, %esi
incl %esi
shrl $0x1f, %esi
addl %esi, %edx
incl %edx
sarl %edx
movslq %edx, %rdx
addq %rax, %rdx
addq %rcx, %rdx
movslq 0x8(%r14,%r12), %rax
movzbl (%rax,%rdx), %eax
imull $0xcd, %eax, %eax
shrl $0xb, %eax
cmpb $0x3, %al
movl $0x3, %ecx
cmovael %ecx, %eax
incb %al
movzbl %al, %eax
shll $0x8, %eax
jmp 0x58ec
movq (%r14,%r12), %rax
movq 0x8(%rax), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rax), %rdx
shrl $0x18, %ecx
addq %rdx, %rcx
movl 0x8(%r14,%r12), %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
movl %edx, %esi
sarl %esi
movslq %esi, %rsi
movzbl (%rsi,%rcx), %esi
andl $0x3e, %edx
subl %edx, %eax
shll $0x2, %eax
movb $0x4, %cl
subb %al, %cl
shrl %cl, %esi
andl $0xf, %esi
leaq 0x28ae5(%rip), %rax     # 0x2e360
movzbl (%rax,%rsi), %eax
shll $0x8, %eax
addl $0x500, %eax            # imm = 0x500
jmp 0x58e9
movq (%r14,%r12), %rax
movq 0x8(%rax), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rax), %rdx
shrl $0x18, %ecx
movl 0x10(%rax), %eax
movl %eax, %esi
incl %esi
shrl $0x1f, %esi
addl %esi, %eax
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rdx, %rax
movslq 0x8(%r14,%r12), %rcx
movzbl (%rcx,%rax), %eax
cwtl
imull $0x6667, %eax, %eax     # imm = 0x6667
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x12, %eax
addl %ecx, %eax
cmpw $0x3, %ax
movl $0x3, %ecx
cmovgel %ecx, %eax
shll $0x8, %eax
addl $0x100, %eax            # imm = 0x100
movzwl %ax, %eax
orl %ebp, %eax
movl %eax, %ebp
movq 0x286e1(%rip), %rbx     # 0x2dfd8
movq (%rbx), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x35c0
movq (%rbx), %rdi
movq 0x28(%rsp), %rax
movl 0x50(%rax), %edx
movl %r13d, %esi
callq 0x3490
cmpl $-0x1, %eax
je 0x5955
movq (%rbx), %r13
movq (%r14,%r12), %rax
movabsq $0x1000000000, %rcx     # imm = 0x1000000000
testq %rcx, 0x8(%rax)
jne 0x593c
movl %r15d, %edi
callq 0x3100
jmp 0x5944
movl %r15d, %edi
callq 0x34f0
movq %r13, %rdi
movl %eax, %esi
callq 0x3300
movq 0x28683(%rip), %rbx     # 0x2dfd8
movq (%rbx), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x30d0
movq 0x28(%rsp), %rbx
movq 0x10(%rsp), %r8
addq $0x18, %r12
cmpq %r12, 0x30(%rsp)
jne 0x5483
movq 0x28657(%rip), %r15     # 0x2dfd8
movq (%r15), %rdi
testl %r8d, %r8d
jne 0x59ba
cmpl $0x2a, 0x8(%rsp)
je 0x59ba
movl 0x50(%rbx), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x50(%rbx)
movl $0x1, %esi
callq 0x3490
cmpl $-0x1, %eax
movl 0x20(%rsp), %ebp
je 0x5a01
movq (%r15), %rdi
movl 0x8(%rsp), %esi
callq 0x3300
jmp 0x5a01
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x35c0
movq (%r15), %rdi
movl 0x50(%rbx), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x50(%rbx)
movl $0x1, %esi
callq 0x3490
cmpl $-0x1, %eax
movl 0x20(%rsp), %ebp
je 0x59f2
movq (%r15), %rdi
movl $0x2a, %esi
callq 0x3300
movq (%r15), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x30d0
movq 0x10(%rsp), %r8
incl %r8d
cmpl %ebp, %r8d
jne 0x5470
movq 0x18(%rsp), %rax
movl %eax, 0x54(%rbx)
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq