// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_pileup.c
// Function: bam_plbuf_push
int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
{
	if (b) { // fill buffer
		if (b->core.tid < 0) return 0;
		if (b->core.flag & buf->flag_mask) return 0;
		bam_copy1(&buf->tail->b, b);
		buf->tail->beg = b->core.pos; buf->tail->end = bam_calend(&b->core, bam1_cigar(b));
		if (b->core.tid < buf->max_tid) {
			fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
			return -1;
		}
		if ((b->core.tid == buf->max_tid) && (buf->tail->beg < buf->max_pos)) {
			fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
			return -1;
		}
		buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg;
		if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) {
			buf->tail->next = mp_alloc(buf->mp);
			buf->tail = buf->tail->next;
		}
	} else buf->is_eof = 1;
	while (buf->is_eof || buf->max_tid > buf->tid || (buf->max_tid == buf->tid && buf->max_pos > buf->pos)) {
		int n_pu = 0;
		lbnode_t *p, *q;
		buf->dummy->next = buf->head;
		for (p = buf->head, q = buf->dummy; p->next; q = p, p = p->next) {
			if (p->b.core.tid < buf->tid || (p->b.core.tid == buf->tid && p->end <= buf->pos)) { // then remove from the list
				q->next = p->next; mp_free(buf->mp, p); p = q;
			} else if (p->b.core.tid == buf->tid && p->beg <= buf->pos) { // here: p->end > pos; then add to pileup
				if (n_pu == buf->max_pu) { // then double the capacity
					buf->max_pu = buf->max_pu? buf->max_pu<<1 : 256;
					buf->pu = (bam_pileup1_t*)realloc(buf->pu, sizeof(bam_pileup1_t) * buf->max_pu);
				}
				buf->pu[n_pu].b = &p->b;
				if (resolve_cigar(buf->pu + n_pu, buf->pos)) ++n_pu; // skip the read if we are looking at BAM_CREF_SKIP
			}
		}
		buf->head = buf->dummy->next; // dummy->next may be changed
		if (n_pu) { // then call user defined function
			buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
		}
		// update tid and pos
		if (buf->head->next) {
			if (buf->tid > buf->head->b.core.tid) {
				fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
				return 1;
			}
		}
		if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
			buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
		} else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
			buf->pos = buf->head->beg; // jump to the next position
		} else ++buf->pos; // scan contiguously
		if (buf->is_eof && buf->head->next == 0) break;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
testq %rdi, %rdi
je 0x1732c
movq %rdi, %r14
xorl %eax, %eax
cmpl $0x0, (%rdi)
js 0x17722
movl 0xc(%r14), %ecx
movl 0x50(%rbx), %edx
testw %cx, %dx
jne 0x17722
movq 0x10(%rbx), %r12
movq 0x30(%r12), %r15
movl 0x28(%r12), %ebp
movl 0x28(%r14), %eax
cmpl %eax, %ebp
jge 0x17274
decl %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ebp
sarl $0x10, %ebp
orl %eax, %ebp
incl %ebp
movslq %ebp, %rsi
movq %r15, %rdi
callq 0x3610
movq %rax, %r15
movq 0x30(%r14), %rsi
movslq 0x24(%r14), %rdx
movq %r15, %rdi
callq 0x3680
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%r12)
movq 0x30(%r14), %rax
movq %rax, 0x30(%r12)
movups %xmm0, (%r12)
movups %xmm1, 0x10(%r12)
movl %ebp, 0x28(%r12)
movq %r15, 0x30(%r12)
movl 0x4(%r14), %eax
movq 0x10(%rbx), %rcx
movl %eax, 0x38(%rcx)
movzbl 0xb(%r14), %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x11c08
movq 0x10(%rbx), %r15
movl %eax, 0x3c(%r15)
movl (%r14), %ecx
cmpl 0x38(%rbx), %ecx
jl 0x17731
jne 0x172f5
movl 0x38(%r15), %edx
cmpl 0x3c(%rbx), %edx
jb 0x17738
movl %ecx, 0x38(%rbx)
movl 0x38(%r15), %ecx
movl %ecx, 0x3c(%rbx)
cmpl 0x34(%rbx), %eax
ja 0x1730c
movl (%r15), %eax
cmpl 0x30(%rbx), %eax
jle 0x1734c
movq (%rbx), %rax
incl (%rax)
movslq 0x4(%rax), %rcx
testq %rcx, %rcx
je 0x17335
movq 0x10(%rax), %rdx
leaq -0x1(%rcx), %rsi
movl %esi, 0x4(%rax)
movq -0x8(%rdx,%rcx,8), %rax
jmp 0x17344
movl $0x1, 0x44(%rbx)
jmp 0x1734c
movl $0x1, %edi
movl $0x48, %esi
callq 0x3530
movq %rax, 0x40(%r15)
movq %rax, 0x10(%rbx)
movl $0x100, %r15d           # imm = 0x100
cmpl $0x0, 0x44(%rbx)
jne 0x17378
movl 0x30(%rbx), %ecx
movl 0x38(%rbx), %edx
cmpl %ecx, %edx
jg 0x17378
xorl %eax, %eax
cmpl %ecx, %edx
jne 0x17722
movl 0x3c(%rbx), %ecx
cmpl 0x34(%rbx), %ecx
jle 0x17722
movq 0x8(%rbx), %r12
movq 0x18(%rbx), %r14
movq %r12, 0x40(%r14)
movq 0x40(%r12), %rax
testq %rax, %rax
je 0x1768d
leaq 0x40(%r12), %rcx
xorl %ebp, %ebp
movl (%r12), %edx
movl 0x30(%rbx), %esi
cmpl %esi, %edx
jge 0x173f9
movq %rax, 0x40(%r14)
movq (%rbx), %r13
decl (%r13)
movq $0x0, (%rcx)
movl 0x8(%r13), %eax
cmpl %eax, 0x4(%r13)
jne 0x173e1
leal (%rax,%rax), %ecx
testl %eax, %eax
cmovel %r15d, %ecx
movl %ecx, 0x8(%r13)
movq 0x10(%r13), %rdi
movslq %ecx, %rsi
shlq $0x3, %rsi
callq 0x3610
movq %rax, 0x10(%r13)
movq 0x10(%r13), %rax
movslq 0x4(%r13), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x4(%r13)
movq %r12, (%rax,%rcx,8)
jmp 0x17674
jne 0x17671
movl 0x3c(%r12), %edi
cmpl 0x34(%rbx), %edi
jbe 0x173a4
cmpl %esi, %edx
jne 0x17671
movl 0x38(%r12), %eax
cmpl 0x34(%rbx), %eax
ja 0x17671
movl 0x40(%rbx), %eax
cmpl %eax, %ebp
jne 0x1744a
leal (%rax,%rax), %ecx
testl %eax, %eax
cmovel %r15d, %ecx
movl %ecx, 0x40(%rbx)
movq 0x48(%rbx), %rdi
movslq %ecx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x3610
movq %rax, 0x48(%rbx)
movq 0x48(%rbx), %rdx
movslq %ebp, %rax
leaq (%rax,%rax,2), %rsi
movq %r12, (%rdx,%rsi,8)
xorl %ecx, %ecx
testb $0x4, 0xc(%r12)
jne 0x1766b
movl 0x34(%rbx), %eax
movl 0x4(%r12), %r11d
cmpl %eax, %r11d
ja 0x17763
leaq (%rdx,%rsi,8), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x8(%rdx)
andb $-0x8, 0x14(%rdx)
movq 0x8(%r12), %r8
movq %r8, %rcx
shrq $0x30, %rcx
movl $0x1, %ecx
jne 0x174a4
movl %r11d, %r9d
jmp 0x17662
xorl %esi, %esi
movb $0x1, %r14b
movl $0x2, %edi
shrl $0x18, %r8d
addq 0x30(%r12), %r8
movl -0x8(%r8,%rdi,4), %r10d
movl %r10d, %r8d
andl $0xf, %r8d
cmpl $0x4, %r8d
ja 0x174e5
shrl $0x4, %r10d
leaq 0xf73c(%rip), %r13      # 0x26c10
movslq (%r13,%r8,4), %r9
addq %r13, %r9
jmpq *%r9
addl %esi, %r10d
movl %r10d, %esi
movl %r11d, %r9d
jmp 0x17611
addl %r11d, %r10d
jmp 0x175bd
leal (%r10,%r11), %r9d
cmpl %eax, %r9d
jbe 0x1760e
movb 0x14(%rdx), %r13b
andb $-0x2, %r13b
movb %r13b, 0x14(%rdx)
movl $0x0, 0xc(%rdx)
leal (%rsi,%rax), %r15d
subl %r11d, %r15d
movl %r15d, 0x8(%rdx)
cmpl %r11d, %eax
setne %r11b
notb %r14b
orb %r11b, %r14b
testb $0x1, %r14b
jne 0x1753b
orb $0x2, %r13b
movb %r13b, 0x14(%rdx)
leal -0x1(%r9), %r11d
cmpl %eax, %r11d
movl $0x100, %r15d           # imm = 0x100
jne 0x1760e
leaq -0x2(%rdi), %r11
movq 0x8(%r12), %r14
movq %r14, %r15
shrq $0x30, %r15
decl %r15d
cmpq %r15, %r11
movl $0x100, %r15d           # imm = 0x100
jae 0x1760a
movq 0x30(%r12), %r11
shrl $0x18, %r14d
addq %r11, %r14
movl -0x4(%r14,%rdi,4), %r13d
movl %r13d, %r14d
andl $0xf, %r14d
cmpl $0x1, %r14d
je 0x175c2
cmpl $0x2, %r14d
jne 0x175ca
shrl $0x4, %r13d
negl %r13d
jmp 0x175c6
addl %r11d, %r10d
cmpl %eax, %r10d
jbe 0x175bd
movl $0x0, 0xc(%rdx)
orb $0x1, 0x14(%rdx)
movl %eax, %r9d
subl %r11d, %r9d
addl %esi, %r9d
movl %r9d, 0x8(%rdx)
movl %r10d, %r9d
jmp 0x17611
shrl $0x4, %r13d
movl %r13d, 0xc(%rdx)
leal -0x1(%r14), %r15d
cmpl $0x1, %r15d
ja 0x175fa
movq 0x8(%r12), %r13
movq %r13, %r15
shrq $0x30, %r15
cmpq %r15, %rdi
jae 0x175f6
shrl $0x18, %r13d
addq %r13, %r11
movl (%r11,%rdi,4), %r14d
andl $0xf, %r14d
jmp 0x175fa
orb $0x4, 0x14(%rdx)
movl $0x100, %r15d           # imm = 0x100
addl $-0x3, %r14d
cmpl $0x2, %r14d
ja 0x1760e
orb $0x4, 0x14(%rdx)
addl %r10d, %esi
cmpl %eax, %r9d
movl %ecx, %r10d
movl $0x0, %r11d
cmoval %r11d, %r10d
cmpl $0x3, %r8d
cmovel %r10d, %ecx
cmpl %eax, %r9d
ja 0x17662
addl $-0x3, %r8d
cmpl $0x3, %r8d
setb %r14b
movq 0x8(%r12), %r8
movq %r8, %r10
shrq $0x30, %r10
leaq 0x1(%rdi), %r15
decq %rdi
movl %r9d, %r11d
cmpq %r10, %rdi
movq %r15, %rdi
movl $0x100, %r15d           # imm = 0x100
jb 0x174ae
cmpl %eax, %r9d
jbe 0x17744
cmpl $0x1, %ecx
sbbl $-0x1, %ebp
movq %r12, %r14
movq 0x40(%r14), %r12
leaq 0x40(%r12), %rcx
movq 0x40(%r12), %rax
testq %rax, %rax
jne 0x17399
jmp 0x1768f
xorl %ebp, %ebp
movq 0x18(%rbx), %rax
movq 0x40(%rax), %rax
movq %rax, 0x8(%rbx)
testl %ebp, %ebp
je 0x176b2
movl 0x30(%rbx), %edi
movl 0x34(%rbx), %esi
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %r8
movl %ebp, %edx
callq *0x20(%rbx)
movq 0x8(%rbx), %rcx
movq 0x40(%rcx), %rax
testq %rax, %rax
je 0x176c6
movl 0x30(%rbx), %edx
cmpl (%rcx), %edx
jg 0x1770a
movl (%rcx), %edx
cmpl %edx, 0x30(%rbx)
jge 0x176d5
movl %edx, 0x30(%rbx)
movl 0x38(%rcx), %ecx
jmp 0x176df
movl 0x34(%rbx), %edx
movl 0x38(%rcx), %ecx
cmpl %ecx, %edx
jae 0x176e4
movl %ecx, 0x34(%rbx)
jmp 0x176e9
incl %edx
movl %edx, 0x34(%rbx)
xorl %ecx, %ecx
cmpl $0x0, 0x44(%rbx)
setne %cl
testq %rax, %rax
leal (%rcx,%rcx,2), %ecx
movl $0x0, %eax
cmovnel %eax, %ecx
testl %ecx, %ecx
je 0x17352
jmp 0x17716
callq 0x46d8
movl $0x1, %ecx
jmp 0x17700
xorl %eax, %eax
cmpl $0x3, %ecx
je 0x17722
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x46bc
jmp 0x1773d
callq 0x46a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x17722
leaq 0xf60a(%rip), %rdi      # 0x26d55
leaq 0xf5c9(%rip), %rsi      # 0x26d1b
leaq 0xf5cf(%rip), %rcx      # 0x26d28
movl $0x69, %edx
callq 0x3140
leaq 0xf5a8(%rip), %rdi      # 0x26d12
leaq 0xf5aa(%rip), %rsi      # 0x26d1b
leaq 0xf5b0(%rip), %rcx      # 0x26d28
movl $0x41, %edx
callq 0x3140